OpenMS  2.5.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-2020.
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 
74  template <typename PeakContainerT>
76  PeakContainerT& p,
77  const double pos_start,
78  const double pos_end,
79  const bool ignore_data_arrays = false
80  )
81  {
82  typename PeakContainerT::iterator it_start = p.PosBegin(pos_start);
83  typename PeakContainerT::iterator it_end = p.PosEnd(pos_end);
84  if (!ignore_data_arrays)
85  {
86  Size hops_left = std::distance(p.begin(), it_start);
87  Size n_elems = std::distance(it_start, it_end);
88 
89  typename PeakContainerT::StringDataArrays& SDAs = p.getStringDataArrays();
90  for (DataArrays::StringDataArray& sda : SDAs)
91  {
92  if (sda.size() == p.size())
93  {
94  sda.erase(sda.begin() + hops_left + n_elems, sda.end());
95  sda.erase(sda.begin(), sda.begin() + hops_left);
96  }
97  }
98 
99  typename PeakContainerT::FloatDataArrays& FDAs = p.getFloatDataArrays();
100  for (DataArrays::FloatDataArray& fda : FDAs)
101  {
102  if (fda.size() == p.size())
103  {
104  fda.erase(fda.begin() + hops_left + n_elems, fda.end());
105  fda.erase(fda.begin(), fda.begin() + hops_left);
106  }
107  }
108 
109  typename PeakContainerT::IntegerDataArrays& IDAs = p.getIntegerDataArrays();
110  for (DataArrays::IntegerDataArray& ida : IDAs)
111  {
112  if (ida.size() == p.size())
113  {
114  ida.erase(ida.begin() + hops_left + n_elems, ida.end());
115  ida.erase(ida.begin(), ida.begin() + hops_left);
116  }
117  }
118  }
119  if (it_start == it_end)
120  { // no elements left
121  p.resize(0);
122  }
123  else
124  { // if it_end != it_start, the second erase operation is safe
125  p.erase(it_end, p.end());
126  p.erase(p.begin(), it_start);
127  }
128  }
129 
130  template <typename PeakContainerT>
131  void subtractMinimumIntensity(PeakContainerT& p)
132  {
133  if (p.empty()) return;
134 
135  typename PeakContainerT::iterator it = std::min_element(p.begin(), p.end(),
136  [](typename PeakContainerT::PeakType& a, typename PeakContainerT::PeakType& b)
137  {
138  return a.getIntensity() < b.getIntensity();
139  });
140 
141  const double rebase = - it->getIntensity();
142  for (typename PeakContainerT::PeakType& peak : p)
143  {
144  peak.setIntensity(peak.getIntensity() + rebase);
145  }
146  // Note: data arrays are not updated
147  }
148 
155 
170  template <typename PeakContainerT>
172  {
173  if (!p.getFloatDataArrays().empty() || !p.getStringDataArrays().empty() || !p.getIntegerDataArrays().empty())
174  {
175  OPENMS_LOG_WARN << "Warning: data arrays are being ignored in the method SpectrumHelper::makePeakPositionUnique().\n";
176  }
177 
178  if (p.empty()) return;
179 
180  p.sortByPosition();
181 
182  double current_position = p.begin()->getPos();
183  PeakContainerT p_new;
184  double intensity_new(0);
185  std::vector<double> intensities_at_same_position;
186  for (typename PeakContainerT::PeakType& peak : p)
187  {
188  if (peak.getPos() > current_position)
189  {
190  // add a peak to the new peak container
191  switch(m)
192  {
193  case IntensityAveragingMethod::MEDIAN: intensity_new = Math::median(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
194  case IntensityAveragingMethod::MEAN: intensity_new = Math::mean(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
195  case IntensityAveragingMethod::SUM: intensity_new = Math::sum(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
196  case IntensityAveragingMethod::MIN: intensity_new = *std::min_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
197  case IntensityAveragingMethod::MAX: intensity_new = *std::max_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
198  }
199  typename PeakContainerT::PeakType peak_new(current_position, intensity_new);
200  p_new.push_back(peak_new);
201 
202  current_position = peak.getPos();
203  intensities_at_same_position.clear();
204  }
205 
206  intensities_at_same_position.push_back(peak.getIntensity());
207  }
208 
209  // add the very last peak to the new peak container
210  switch(m)
211  {
212  case IntensityAveragingMethod::MEDIAN : intensity_new = Math::median(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
213  case IntensityAveragingMethod::MEAN : intensity_new = Math::mean(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
214  case IntensityAveragingMethod::SUM : intensity_new = Math::sum(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
215  case IntensityAveragingMethod::MIN : intensity_new = *std::min_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
216  case IntensityAveragingMethod::MAX : intensity_new = *std::max_element(intensities_at_same_position.begin(), intensities_at_same_position.end()); break;
217  }
218  typename PeakContainerT::PeakType peak_new(current_position, intensity_new);
219  p_new.push_back(peak_new);
220 
221  std::swap(p_new, p);
222  }
223 
224 } // namespace OpenMS
225 
226 
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:131
OpenMS::Math::sum
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:120
OpenMS::makePeakPositionUnique
void makePeakPositionUnique(PeakContainerT &p, const IntensityAveragingMethod m=IntensityAveragingMethod::MEDIAN)
Make peak positions unique.
Definition: SpectrumHelper.h:171
OpenMS::Math::median
static double median(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the median of a range of values.
Definition: StatisticFunctions.h:151
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:46
int
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:154
OpenMS::Math::mean
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:133
OpenMS::getDataArrayByName
DataArrayT::iterator getDataArrayByName(DataArrayT &a, const String &name)
Helper functions for MSSpectrum and MSChromatogram.
Definition: SpectrumHelper.h:52
OpenMS::IntensityAveragingMethod::MAX
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:75
OpenMS::IntensityAveragingMethod::MEAN
OpenMS::DataArrays::FloatDataArray
Float data array class.
Definition: DataArrays.h:45
OpenMS::IntensityAveragingMethod::SUM