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 
35 #pragma once
36 
37 namespace OpenMS
38 {
39  class String;
46  template <class DataArrayT>
49  typename DataArrayT::iterator getDataArrayByName(DataArrayT& a, const String& name)
50  {
51  typename DataArrayT::iterator it = a.begin();
52  for (; it != a.end(); ++it)
53  {
54  if (it->getName() == name) return it;
55  }
56  return it;
57  }
58  template <class DataArrayT>
59  typename DataArrayT::const_iterator getDataArrayByName(const DataArrayT& a, const String& name)
60  {
61  typename DataArrayT::const_iterator it = a.begin();
62  for (; it != a.end(); ++it)
63  {
64  if (it->getName() == name) return it;
65  }
66  return it;
67  }
68 
69  template <typename PeakContainerT>
71  PeakContainerT& p,
72  const double pos_start,
73  const double pos_end,
74  const bool ignoreDataArrays = false
75  )
76  {
77  typename PeakContainerT::iterator it_start = p.PosBegin(pos_start);
78  typename PeakContainerT::iterator it_end = p.PosEnd(pos_end);
79  if (!ignoreDataArrays)
80  {
81  Size hops_left = std::distance(p.begin(), it_start);
82  Size n_elems = std::distance(it_start, it_end);
83 
84  typename PeakContainerT::StringDataArrays& SDAs = p.getStringDataArrays();
85  for (DataArrays::StringDataArray& sda : SDAs)
86  {
87  if (sda.size() == p.size())
88  {
89  sda.erase(sda.begin() + hops_left + n_elems, sda.end());
90  sda.erase(sda.begin(), sda.begin() + hops_left);
91  }
92  }
93 
94  typename PeakContainerT::FloatDataArrays& FDAs = p.getFloatDataArrays();
95  for (DataArrays::FloatDataArray& fda : FDAs)
96  {
97  if (fda.size() == p.size())
98  {
99  fda.erase(fda.begin() + hops_left + n_elems, fda.end());
100  fda.erase(fda.begin(), fda.begin() + hops_left);
101  }
102  }
103 
104  typename PeakContainerT::IntegerDataArrays& IDAs = p.getIntegerDataArrays();
105  for (DataArrays::IntegerDataArray& ida : IDAs)
106  {
107  if (ida.size() == p.size())
108  {
109  ida.erase(ida.begin() + hops_left + n_elems, ida.end());
110  ida.erase(ida.begin(), ida.begin() + hops_left);
111  }
112  }
113  }
114  p.erase(it_end, p.end());
115  p.erase(p.begin(), it_start);
116  }
117 
118  template <typename PeakContainerT>
119  void subtractMinimumIntensity(PeakContainerT& p)
120  {
121  if (p.empty()) return;
122 
123  typename PeakContainerT::iterator it = std::min_element(p.begin(), p.end(),
124  [](typename PeakContainerT::PeakType& a, typename PeakContainerT::PeakType& b)
125  {
126  return a.getIntensity() < b.getIntensity();
127  });
128 
129  const double rebase = - it->getIntensity();
130  for (typename PeakContainerT::PeakType& peak : p)
131  {
132  peak.setIntensity(peak.getIntensity() + rebase);
133  }
134  // Note: data arrays are not updated
135  }
136 } // namespace OpenMS
137 
138 
A more convenient string class.
Definition: String.h:57
void subtractMinimumIntensity(PeakContainerT &p)
Definition: SpectrumHelper.h:119
Peak2D PeakType
Definition: MassTrace.h:47
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Float data array class.
Definition: DataArrays.h:45
Integer data array class.
Definition: DataArrays.h:51
void removePeaks(PeakContainerT &p, const double pos_start, const double pos_end, const bool ignoreDataArrays=false)
Definition: SpectrumHelper.h:70
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:57
DataArrayT::iterator getDataArrayByName(DataArrayT &a, const String &name)
Helper functions for MSSpectrum and MSChromatogram.
Definition: SpectrumHelper.h:49