OpenMS
Loading...
Searching...
No Matches
SpectrumHelper.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg$
6// $Authors: Timo Sachsenberg $
7// --------------------------------------------------------------------------
8
9#pragma once
10
15
16namespace OpenMS
17{
26 template <class DataArrayT>
27 typename DataArrayT::iterator getDataArrayByName(DataArrayT& a, const std::string& name)
28 {
29 typename DataArrayT::iterator it = a.begin();
30 for (; it != a.end(); ++it)
31 {
32 if (it->getName() == name) return it;
33 }
34 return it;
35 }
36
37 template <class DataArrayT>
38 typename DataArrayT::const_iterator getDataArrayByName(const DataArrayT& a, const std::string& name)
39 {
40 typename DataArrayT::const_iterator it = a.begin();
41 for (; it != a.end(); ++it)
42 {
43 if (it->getName() == name) return it;
44 }
45 return it;
46 }
47
49 template <typename PeakContainerT>
51 PeakContainerT& p,
52 const double pos_start,
53 const double pos_end,
54 const bool ignore_data_arrays = false
55 )
56 {
57 typename PeakContainerT::iterator it_start = p.PosBegin(pos_start);
58 typename PeakContainerT::iterator it_end = p.PosEnd(pos_end);
59 if (!ignore_data_arrays)
60 {
61 Size hops_left = std::distance(p.begin(), it_start);
62 Size n_elems = std::distance(it_start, it_end);
63
64 typename PeakContainerT::StringDataArrays& SDAs = p.getStringDataArrays();
65 for (DataArrays::StringDataArray& sda : SDAs)
66 {
67 if (sda.size() == p.size())
68 {
69 sda.erase(sda.begin() + hops_left + n_elems, sda.end());
70 sda.erase(sda.begin(), sda.begin() + hops_left);
71 }
72 }
73
74 typename PeakContainerT::FloatDataArrays& FDAs = p.getFloatDataArrays();
75 for (DataArrays::FloatDataArray& fda : FDAs)
76 {
77 if (fda.size() == p.size())
78 {
79 fda.erase(fda.begin() + hops_left + n_elems, fda.end());
80 fda.erase(fda.begin(), fda.begin() + hops_left);
81 }
82 }
83
84 typename PeakContainerT::IntegerDataArrays& IDAs = p.getIntegerDataArrays();
85 for (DataArrays::IntegerDataArray& ida : IDAs)
86 {
87 if (ida.size() == p.size())
88 {
89 ida.erase(ida.begin() + hops_left + n_elems, ida.end());
90 ida.erase(ida.begin(), ida.begin() + hops_left);
91 }
92 }
93 }
94 if (it_start == it_end)
95 { // no elements left
96 p.resize(0);
97 }
98 else
99 { // if it_end != it_start, the second erase operation is safe
100 p.erase(it_end, p.end());
101 p.erase(p.begin(), it_start);
102 }
103 }
104
105 template <typename PeakContainerT>
106 void subtractMinimumIntensity(PeakContainerT& p)
107 {
108 if (p.empty()) return;
109
110 typename PeakContainerT::iterator it = std::min_element(p.begin(), p.end(),
111 [](typename PeakContainerT::PeakType& a, typename PeakContainerT::PeakType& b)
112 {
113 return a.getIntensity() < b.getIntensity();
114 });
115
116 const double rebase = - it->getIntensity();
117 for (typename PeakContainerT::PeakType& peak : p)
118 {
119 peak.setIntensity(peak.getIntensity() + rebase);
120 }
121 // Note: data arrays are not updated
122 }
123
129 enum class IntensityAveragingMethod : int { MEDIAN, MEAN, SUM, MIN, MAX };
130
145 template <typename PeakContainerT>
147 {
148 if (!p.getFloatDataArrays().empty() || !p.getStringDataArrays().empty() || !p.getIntegerDataArrays().empty())
149 {
150 OPENMS_LOG_WARN << "Warning: data arrays are being ignored in the method SpectrumHelper::makePeakPositionUnique().\n";
151 }
152
153 if (p.empty()) return;
154
155 p.sortByPosition();
156
157 double current_position = p.begin()->getPos();
158 PeakContainerT p_new;
159 double intensity_new(0);
160 std::vector<double> intensities_at_same_position;
161 for (typename PeakContainerT::PeakType& peak : p)
162 {
163 if (peak.getPos() > current_position)
164 {
165 // add a peak to the new peak container
166 switch(m)
167 {
168 case IntensityAveragingMethod::MEDIAN: intensity_new = Math::median(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
169 case IntensityAveragingMethod::MEAN: intensity_new = Math::mean(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
170 case IntensityAveragingMethod::SUM: intensity_new = Math::sum(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
171 case IntensityAveragingMethod::MIN: intensity_new = *std::min_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
172 case IntensityAveragingMethod::MAX: intensity_new = *std::max_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
173 }
174 typename PeakContainerT::PeakType peak_new(current_position, intensity_new);
175 p_new.push_back(peak_new);
176
177 current_position = peak.getPos();
178 intensities_at_same_position.clear();
179 }
180
181 intensities_at_same_position.push_back(peak.getIntensity());
182 }
183
184 // add the very last peak to the new peak container
185 switch(m)
186 {
187 case IntensityAveragingMethod::MEDIAN : intensity_new = Math::median(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
188 case IntensityAveragingMethod::MEAN : intensity_new = Math::mean(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
189 case IntensityAveragingMethod::SUM : intensity_new = Math::sum(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
190 case IntensityAveragingMethod::MIN : intensity_new = *std::min_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
191 case IntensityAveragingMethod::MAX : intensity_new = *std::max_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
192 }
193 typename PeakContainerT::PeakType peak_new(current_position, intensity_new);
194 p_new.push_back(peak_new);
195
196 std::swap(p_new, p);
197 }
198
208 OPENMS_DLLAPI void copySpectrumMeta(const MSSpectrum & input, MSSpectrum & output, bool clear_spectrum = true);
209
210} // namespace OpenMS
211
#define OPENMS_LOG_WARN
Macro for warnings.
Definition LogStream.h:581
Float data array class.
Definition DataArrays.h:25
Integer data array class.
Definition DataArrays.h:75
std::string data array class
Definition DataArrays.h:125
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
DataArrayT::iterator getDataArrayByName(DataArrayT &a, const std::string &name)
Helper functions for MSSpectrum and MSChromatogram.
Definition SpectrumHelper.h:27
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition StatisticFunctions.h:116
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition StatisticFunctions.h:103
static double median(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the median of a range of values.
Definition StatisticFunctions.h:134
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
void makePeakPositionUnique(PeakContainerT &p, const IntensityAveragingMethod m=IntensityAveragingMethod::MEDIAN)
Make peak positions unique.
Definition SpectrumHelper.h:146
void copySpectrumMeta(const MSSpectrum &input, MSSpectrum &output, bool clear_spectrum=true)
Copies only the meta data contained in the input spectrum to the output spectrum.
IntensityAveragingMethod
Possible methods for merging peak intensities.
Definition SpectrumHelper.h:129
void subtractMinimumIntensity(PeakContainerT &p)
Definition SpectrumHelper.h:106
void removePeaks(PeakContainerT &p, const double pos_start, const double pos_end, const bool ignore_data_arrays=false)
remove all peaks EXCEPT in the given range
Definition SpectrumHelper.h:50