OpenMS  3.0.0
AreaIterator.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: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 // OpenMS includes
38 #include <OpenMS/CONCEPT/Types.h>
41 
42 // STL includes
43 #include <iterator>
44 
45 namespace OpenMS
46 {
47  namespace Internal
48  {
60  template <class ValueT, class ReferenceT, class PointerT, class SpectrumIteratorT, class PeakIteratorT>
61  class AreaIterator :
62  public std::iterator<std::forward_iterator_tag, ValueT>
63  {
64 public:
65  typedef double CoordinateType;
66  typedef ValueT PeakType;
67  typedef SpectrumIteratorT SpectrumIteratorType;
68  typedef PeakIteratorT PeakIteratorType;
69 
72  class Param
73  {
74  friend AreaIterator; // allow access to private members (avoids writing get-accessors)
75  public:
83  : first_(first), current_scan_(begin), end_scan_(end)
84  {
85  // if begin is dereferencable ...
86  if (begin != end)
87  {
88  ms_level_ = begin->getMSLevel();
89  }
90  else
91  {
92  ms_level_ = 1;
93  }
94  }
95 
97  static Param end()
98  {
99  static Param p;
100  p.is_end_ = true;
101  return p;
102  }
103 
105  Param& operator=(const Param& rhs) = default;
106 
110  Param& lowMZ(CoordinateType low_mz) { low_mz_ = low_mz; return *this;}
113  Param& highMZ(CoordinateType high_mz) { high_mz_ = high_mz; return *this;}
115  Param& lowIM(CoordinateType low_im) { low_im_ = low_im; return *this;}
117  Param& highIM(CoordinateType high_im) { high_im_ = high_im; return *this;}
119  Param& msLevel(int8_t ms_level) { ms_level_ = ms_level; return *this;}
121 
122  protected:
133  /* optional parameters */
135  CoordinateType low_mz_ = std::numeric_limits<CoordinateType>::lowest();
137  CoordinateType high_mz_ = std::numeric_limits<CoordinateType>::max();
139  CoordinateType low_im_ = std::numeric_limits<CoordinateType>::lowest();
141  CoordinateType high_im_ = std::numeric_limits<CoordinateType>::max();
143  int8_t ms_level_{};
145  bool is_end_ = false;
146 
147  private:
149  Param() = default;
150  };
151 
152 
153 
157  typedef ValueT value_type;
160  typedef ReferenceT reference;
162  typedef PointerT pointer;
164  typedef unsigned int difference_type;
166 
168  explicit AreaIterator(const Param& p) :
169  p_(p)
170  {
171  nextScan_();
172  }
173 
175  AreaIterator() : p_(Param::end()) {}
176 
178  ~AreaIterator() = default;
179 
181  AreaIterator(const AreaIterator& rhs) = default;
182 
185  {
186  p_.is_end_ = rhs.p_.is_end_;
187  // only copy iterators, if the assigned iterator is not the end iterator
188  if (!p_.is_end_)
189  {
190  p_ = rhs.p_;
191  }
192 
193  return *this;
194  }
195 
197  bool operator==(const AreaIterator & rhs) const
198  {
199  // Both end iterators => equal
200  if (p_.is_end_ && rhs.p_.is_end_) return true;
201 
202  // Normal and end iterator => not equal
203  if (p_.is_end_ ^ rhs.p_.is_end_)
204  return false;
205 
206  // Equality of pointed to peak addresses
207  return &(*(p_.current_peak_)) == &(*(rhs.p_.current_peak_));
208  }
209 
211  bool operator!=(const AreaIterator & rhs) const
212  {
213  return !(*this == rhs);
214  }
215 
218  {
219  //no increment if this is the end iterator
220  if (p_.is_end_) return *this;
221 
222  ++p_.current_peak_;
223  // test whether we arrived at the end of the current scan
224  if (p_.current_peak_ == p_.end_peak_)
225  {
226  ++p_.current_scan_;
227  nextScan_();
228  }
229  return *this;
230  }
231 
234  {
235  AreaIterator tmp(*this);
236  ++(*this);
237  return tmp;
238  }
239 
242  {
243  return p_.current_peak_.operator*();
244  }
245 
248  {
249  return p_.current_peak_.operator->();
250  }
251 
254  {
255  return p_.current_scan_->getRT();
256  }
257 
260  {
261  return p_.current_scan_->getDriftTime();
262  }
263 
265  inline PeakIndex getPeakIndex() const
266  {
267  if (p_.is_end_)
268  {
269  return {};
270  }
271  else
272  {
274  }
275  }
276 
277 private:
279  void nextScan_()
280  {
281  using MSLevelType = decltype(p_.current_scan_->getMSLevel());
283  while (true)
284  {
285  // skip over invalid MS levels and Mobility
286  while (p_.current_scan_ != p_.end_scan_ &&
287  (p_.current_scan_->getMSLevel() != (MSLevelType)p_.ms_level_ ||
288  !mb.containsMobility(p_.current_scan_->getDriftTime())))
289  {
290  ++p_.current_scan_;
291  }
292  if (p_.current_scan_ == p_.end_scan_)
293  {
294  p_.is_end_ = true;
295  return;
296  }
299  if (p_.current_peak_ != p_.end_peak_)
300  {
301  return;
302  }
303  ++p_.current_scan_;
304  }
305  }
306 
309  };
310 
311  }
312 }
313 
CoordinateType high_mz_
high m/z boundary
Definition: AreaIterator.h:137
SpectrumIteratorType current_scan_
Iterator to the current spectrum.
Definition: AreaIterator.h:126
CoordinateType getDriftTime() const
returns the ion mobility time of the current scan
Definition: AreaIterator.h:259
PeakIteratorT PeakIteratorType
Definition: AreaIterator.h:68
ValueT PeakType
Definition: AreaIterator.h:66
SpectrumIteratorType first_
Iterator to the first scan of the map (needed to calculate the index)
Definition: AreaIterator.h:124
AreaIterator & operator=(const AreaIterator &rhs)
Assignment operator.
Definition: AreaIterator.h:184
CoordinateType high_im_
high mobility boundary
Definition: AreaIterator.h:141
double CoordinateType
Definition: AreaIterator.h:65
void nextScan_()
advances the iterator to the next valid peak in the next valid spectrum
Definition: AreaIterator.h:279
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:61
Param & lowIM(CoordinateType low_im)
low ion mobility boundary
Definition: AreaIterator.h:115
unsigned int difference_type
The difference type.
Definition: AreaIterator.h:164
Param & lowMZ(CoordinateType low_mz)
low m/z boundary
Definition: AreaIterator.h:111
Param & highMZ(CoordinateType high_mz)
high m/z boundary
Definition: AreaIterator.h:113
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Param p_
holds spectra iterators and area limits
Definition: AreaIterator.h:308
Param(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end)
C&#39;tor with mandatory parameters.
Definition: AreaIterator.h:82
SpectrumIteratorType end_scan_
Past-the-end iterator of spectra.
Definition: AreaIterator.h:128
Param & operator=(const Param &rhs)=default
Assignment operator.
CoordinateType low_im_
low mobility boundary
Definition: AreaIterator.h:139
AreaIterator operator++(int)
Step forward by one (postfix operator)
Definition: AreaIterator.h:233
AreaIterator()
Default constructor (for the end iterator)
Definition: AreaIterator.h:175
AreaIterator(const Param &p)
Constructor for the begin iterator.
Definition: AreaIterator.h:168
Param()=default
only used internally for end()
PeakIteratorType end_peak_
Past-the-end iterator of peaks in the current spectrum.
Definition: AreaIterator.h:132
SpectrumIteratorT SpectrumIteratorType
Definition: AreaIterator.h:67
~AreaIterator()=default
Destructor.
ReferenceT reference
The reference type as returned by operator*()
Definition: AreaIterator.h:160
pointer operator->() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:247
PeakIteratorType current_peak_
Iterator to the current peak.
Definition: AreaIterator.h:130
static Param end()
return the end-iterator
Definition: AreaIterator.h:97
friend AreaIterator
Definition: AreaIterator.h:74
CoordinateType getRT() const
returns the retention time of the current scan
Definition: AreaIterator.h:253
int8_t ms_level_
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:143
reference operator*() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:241
CoordinateType low_mz_
low m/z boundary
Definition: AreaIterator.h:135
bool operator==(const AreaIterator &rhs) const
Test for equality.
Definition: AreaIterator.h:197
PointerT pointer
The pointer type as returned by operator->()
Definition: AreaIterator.h:162
Definition: AreaIterator.h:72
ValueT value_type
The iterator&#39;s value type.
Definition: AreaIterator.h:158
Definition: RangeManager.h:482
Param & highIM(CoordinateType high_im)
high ion mobility boundary
Definition: AreaIterator.h:117
bool operator!=(const AreaIterator &rhs) const
Test for inequality.
Definition: AreaIterator.h:211
Param & msLevel(int8_t ms_level)
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:119
bool is_end_
Flag that indicates that this iterator is the end iterator.
Definition: AreaIterator.h:145
PeakIndex getPeakIndex() const
returns the PeakIndex corresponding to the current iterator position
Definition: AreaIterator.h:265
AreaIterator & operator++()
Step forward by one (prefix operator)
Definition: AreaIterator.h:217
Index of a peak or feature.
Definition: PeakIndex.h:50