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