OpenMS  3.0.0
SpectrumHelper.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2022.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg$
32 // $Authors: Timo Sachsenberg $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
41 
42 namespace OpenMS
43 {
44  class String;
51  template <class DataArrayT>
54  typename DataArrayT::iterator getDataArrayByName(DataArrayT& a, const String& name)
55  {
56  typename DataArrayT::iterator it = a.begin();
57  for (; it != a.end(); ++it)
58  {
59  if (it->getName() == name) return it;
60  }
61  return it;
62  }
63 
64  template <class DataArrayT>
65  typename DataArrayT::const_iterator getDataArrayByName(const DataArrayT& a, const String& name)
66  {
67  typename DataArrayT::const_iterator it = a.begin();
68  for (; it != a.end(); ++it)
69  {
70  if (it->getName() == name) return it;
71  }
72  return it;
73  }
74 
76  template <typename PeakContainerT>
78  PeakContainerT& p,
79  const double pos_start,
80  const double pos_end,
81  const bool ignore_data_arrays = false
82  )
83  {
84  typename PeakContainerT::iterator it_start = p.PosBegin(pos_start);
85  typename PeakContainerT::iterator it_end = p.PosEnd(pos_end);
86  if (!ignore_data_arrays)
87  {
88  Size hops_left = std::distance(p.begin(), it_start);
89  Size n_elems = std::distance(it_start, it_end);
90 
91  typename PeakContainerT::StringDataArrays& SDAs = p.getStringDataArrays();
92  for (DataArrays::StringDataArray& sda : SDAs)
93  {
94  if (sda.size() == p.size())
95  {
96  sda.erase(sda.begin() + hops_left + n_elems, sda.end());
97  sda.erase(sda.begin(), sda.begin() + hops_left);
98  }
99  }
100 
101  typename PeakContainerT::FloatDataArrays& FDAs = p.getFloatDataArrays();
102  for (DataArrays::FloatDataArray& fda : FDAs)
103  {
104  if (fda.size() == p.size())
105  {
106  fda.erase(fda.begin() + hops_left + n_elems, fda.end());
107  fda.erase(fda.begin(), fda.begin() + hops_left);
108  }
109  }
110 
111  typename PeakContainerT::IntegerDataArrays& IDAs = p.getIntegerDataArrays();
112  for (DataArrays::IntegerDataArray& ida : IDAs)
113  {
114  if (ida.size() == p.size())
115  {
116  ida.erase(ida.begin() + hops_left + n_elems, ida.end());
117  ida.erase(ida.begin(), ida.begin() + hops_left);
118  }
119  }
120  }
121  if (it_start == it_end)
122  { // no elements left
123  p.resize(0);
124  }
125  else
126  { // if it_end != it_start, the second erase operation is safe
127  p.erase(it_end, p.end());
128  p.erase(p.begin(), it_start);
129  }
130  }
131 
132  template <typename PeakContainerT>
133  void subtractMinimumIntensity(PeakContainerT& p)
134  {
135  if (p.empty()) return;
136 
137  typename PeakContainerT::iterator it = std::min_element(p.begin(), p.end(),
138  [](typename PeakContainerT::PeakType& a, typename PeakContainerT::PeakType& b)
139  {
140  return a.getIntensity() < b.getIntensity();
141  });
142 
143  const double rebase = - it->getIntensity();
144  for (typename PeakContainerT::PeakType& peak : p)
145  {
146  peak.setIntensity(peak.getIntensity() + rebase);
147  }
148  // Note: data arrays are not updated
149  }
150 
156  enum class IntensityAveragingMethod : int { MEDIAN, MEAN, SUM, MIN, MAX };
157 
172  template <typename PeakContainerT>
174  {
175  if (!p.getFloatDataArrays().empty() || !p.getStringDataArrays().empty() || !p.getIntegerDataArrays().empty())
176  {
177  OPENMS_LOG_WARN << "Warning: data arrays are being ignored in the method SpectrumHelper::makePeakPositionUnique().\n";
178  }
179 
180  if (p.empty()) return;
181 
182  p.sortByPosition();
183 
184  double current_position = p.begin()->getPos();
185  PeakContainerT p_new;
186  double intensity_new(0);
187  std::vector<double> intensities_at_same_position;
188  for (typename PeakContainerT::PeakType& peak : p)
189  {
190  if (peak.getPos() > current_position)
191  {
192  // add a peak to the new peak container
193  switch(m)
194  {
195  case IntensityAveragingMethod::MEDIAN: intensity_new = Math::median(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
196  case IntensityAveragingMethod::MEAN: intensity_new = Math::mean(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
197  case IntensityAveragingMethod::SUM: intensity_new = Math::sum(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
198  case IntensityAveragingMethod::MIN: intensity_new = *std::min_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
199  case IntensityAveragingMethod::MAX: intensity_new = *std::max_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
200  }
201  typename PeakContainerT::PeakType peak_new(current_position, intensity_new);
202  p_new.push_back(peak_new);
203 
204  current_position = peak.getPos();
205  intensities_at_same_position.clear();
206  }
207 
208  intensities_at_same_position.push_back(peak.getIntensity());
209  }
210 
211  // add the very last peak to the new peak container
212  switch(m)
213  {
214  case IntensityAveragingMethod::MEDIAN : intensity_new = Math::median(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
215  case IntensityAveragingMethod::MEAN : intensity_new = Math::mean(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
216  case IntensityAveragingMethod::SUM : intensity_new = Math::sum(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
217  case IntensityAveragingMethod::MIN : intensity_new = *std::min_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
218  case IntensityAveragingMethod::MAX : intensity_new = *std::max_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
219  }
220  typename PeakContainerT::PeakType peak_new(current_position, intensity_new);
221  p_new.push_back(peak_new);
222 
223  std::swap(p_new, p);
224  }
225 
235  OPENMS_DLLAPI void copySpectrumMeta(const MSSpectrum & input, MSSpectrum & output, bool clear_spectrum = true);
236 
237 } // namespace OpenMS
238 
OpenMS::PeakType
Peak2D PeakType
Definition: MassTrace.h:47
LogStream.h
OpenMS::DataArrays::IntegerDataArray
Integer data array class.
Definition: DataArrays.h:52
OpenMS::subtractMinimumIntensity
void subtractMinimumIntensity(PeakContainerT &p)
Definition: SpectrumHelper.h:133
DataArrays.h
OpenMS::Math::sum
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:107
OpenMS::makePeakPositionUnique
void makePeakPositionUnique(PeakContainerT &p, const IntensityAveragingMethod m=IntensityAveragingMethod::MEDIAN)
Make peak positions unique.
Definition: SpectrumHelper.h:173
OpenMS::Math::median
static double median(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the median of a range of values.
Definition: StatisticFunctions.h:138
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
StatisticFunctions.h
OPENMS_LOG_WARN
#define OPENMS_LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged.
Definition: LogStream.h:460
OpenMS::IntensityAveragingMethod::MEDIAN
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::IntensityAveragingMethod::MIN
OpenMS::DataArrays::StringDataArray
String data array class.
Definition: DataArrays.h:59
OpenMS::IntensityAveragingMethod
IntensityAveragingMethod
Possible methods for merging peak intensities.
Definition: SpectrumHelper.h:156
OpenMS::Math::mean
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:120
OpenMS::getDataArrayByName
DataArrayT::iterator getDataArrayByName(DataArrayT &a, const String &name)
Helper functions for MSSpectrum and MSChromatogram.
Definition: SpectrumHelper.h:54
OpenMS::IntensityAveragingMethod::MAX
OpenMS::copySpectrumMeta
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.
OpenMS::removePeaks
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:77
OpenMS::IntensityAveragingMethod::MEAN
OpenMS::DataArrays::FloatDataArray
Float data array class.
Definition: DataArrays.h:45
OpenMS::IntensityAveragingMethod::SUM
MSSpectrum.h