OpenMS  2.4.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-2018.
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 
37 
38 #pragma once
39 
40 namespace OpenMS
41 {
42  class String;
49  template <class DataArrayT>
52  typename DataArrayT::iterator getDataArrayByName(DataArrayT& a, const String& name)
53  {
54  typename DataArrayT::iterator it = a.begin();
55  for (; it != a.end(); ++it)
56  {
57  if (it->getName() == name) return it;
58  }
59  return it;
60  }
61 
62  template <class DataArrayT>
63  typename DataArrayT::const_iterator getDataArrayByName(const DataArrayT& a, const String& name)
64  {
65  typename DataArrayT::const_iterator it = a.begin();
66  for (; it != a.end(); ++it)
67  {
68  if (it->getName() == name) return it;
69  }
70  return it;
71  }
72 
73  template <typename PeakContainerT>
75  PeakContainerT& p,
76  const double pos_start,
77  const double pos_end,
78  const bool ignoreDataArrays = false
79  )
80  {
81  typename PeakContainerT::iterator it_start = p.PosBegin(pos_start);
82  typename PeakContainerT::iterator it_end = p.PosEnd(pos_end);
83  if (!ignoreDataArrays)
84  {
85  Size hops_left = std::distance(p.begin(), it_start);
86  Size n_elems = std::distance(it_start, it_end);
87 
88  typename PeakContainerT::StringDataArrays& SDAs = p.getStringDataArrays();
89  for (DataArrays::StringDataArray& sda : SDAs)
90  {
91  if (sda.size() == p.size())
92  {
93  sda.erase(sda.begin() + hops_left + n_elems, sda.end());
94  sda.erase(sda.begin(), sda.begin() + hops_left);
95  }
96  }
97 
98  typename PeakContainerT::FloatDataArrays& FDAs = p.getFloatDataArrays();
99  for (DataArrays::FloatDataArray& fda : FDAs)
100  {
101  if (fda.size() == p.size())
102  {
103  fda.erase(fda.begin() + hops_left + n_elems, fda.end());
104  fda.erase(fda.begin(), fda.begin() + hops_left);
105  }
106  }
107 
108  typename PeakContainerT::IntegerDataArrays& IDAs = p.getIntegerDataArrays();
109  for (DataArrays::IntegerDataArray& ida : IDAs)
110  {
111  if (ida.size() == p.size())
112  {
113  ida.erase(ida.begin() + hops_left + n_elems, ida.end());
114  ida.erase(ida.begin(), ida.begin() + hops_left);
115  }
116  }
117  }
118  p.erase(it_end, p.end());
119  p.erase(p.begin(), it_start);
120  }
121 
122  template <typename PeakContainerT>
123  void subtractMinimumIntensity(PeakContainerT& p)
124  {
125  if (p.empty()) return;
126 
127  typename PeakContainerT::iterator it = std::min_element(p.begin(), p.end(),
128  [](typename PeakContainerT::PeakType& a, typename PeakContainerT::PeakType& b)
129  {
130  return a.getIntensity() < b.getIntensity();
131  });
132 
133  const double rebase = - it->getIntensity();
134  for (typename PeakContainerT::PeakType& peak : p)
135  {
136  peak.setIntensity(peak.getIntensity() + rebase);
137  }
138  // Note: data arrays are not updated
139  }
140 
146  enum class IntensityAveragingMethod : int { MEDIAN, MEAN, SUM, MIN, MAX };
147 
162  template <typename PeakContainerT>
164  {
165  if (!p.getFloatDataArrays().empty() || !p.getStringDataArrays().empty() || !p.getIntegerDataArrays().empty())
166  {
167  LOG_WARN << "Warning: data arrays are being ignored in the method SpectrumHelper::makePeakPositionUnique().\n";
168  }
169 
170  if (p.empty()) return;
171 
172  p.sortByPosition();
173 
174  double current_position = p.begin()->getPos();
175  PeakContainerT p_new;
176  double intensity_new(0);
177  std::vector<double> intensities_at_same_position;
178  for (typename PeakContainerT::PeakType& peak : p)
179  {
180  if (peak.getPos() > current_position)
181  {
182  // add a peak to the new peak container
183  switch(m)
184  {
185  case IntensityAveragingMethod::MEDIAN: intensity_new = Math::median(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
186  case IntensityAveragingMethod::MEAN: intensity_new = Math::mean(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
187  case IntensityAveragingMethod::SUM: intensity_new = Math::sum(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
188  case IntensityAveragingMethod::MIN: intensity_new = *std::min_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
189  case IntensityAveragingMethod::MAX: intensity_new = *std::max_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
190  }
191  typename PeakContainerT::PeakType peak_new(current_position, intensity_new);
192  p_new.push_back(peak_new);
193 
194  current_position = peak.getPos();
195  intensities_at_same_position.clear();
196  }
197 
198  intensities_at_same_position.push_back(peak.getIntensity());
199  }
200 
201  // add the very last peak to the new peak container
202  switch(m)
203  {
204  case IntensityAveragingMethod::MEDIAN : intensity_new = Math::median(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
205  case IntensityAveragingMethod::MEAN : intensity_new = Math::mean(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
206  case IntensityAveragingMethod::SUM : intensity_new = Math::sum(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
207  case IntensityAveragingMethod::MIN : intensity_new = *std::min_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
208  case IntensityAveragingMethod::MAX : intensity_new = *std::max_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
209  }
210  typename PeakContainerT::PeakType peak_new(current_position, intensity_new);
211  p_new.push_back(peak_new);
212 
213  std::swap(p_new, p);
214  }
215 
216 } // namespace OpenMS
217 
218 
A more convenient string class.
Definition: String.h:58
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:120
void subtractMinimumIntensity(PeakContainerT &p)
Definition: SpectrumHelper.h:123
Peak2D PeakType
Definition: MassTrace.h:47
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:133
Float data array class.
Definition: DataArrays.h:45
#define LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged...
Definition: LogStream.h:452
Integer data array class.
Definition: DataArrays.h:52
void makePeakPositionUnique(PeakContainerT &p, const IntensityAveragingMethod m=IntensityAveragingMethod::MEDIAN)
Make peak positions unique.
Definition: SpectrumHelper.h:163
IntensityAveragingMethod
Possible methods for merging peak intensities.
Definition: SpectrumHelper.h:146
static double median(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the median of a range of values.
Definition: StatisticFunctions.h:151
void removePeaks(PeakContainerT &p, const double pos_start, const double pos_end, const bool ignoreDataArrays=false)
Definition: SpectrumHelper.h:74
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
String data array class.
Definition: DataArrays.h:59
DataArrayT::iterator getDataArrayByName(DataArrayT &a, const String &name)
Helper functions for MSSpectrum and MSChromatogram.
Definition: SpectrumHelper.h:52