OpenMS
AreaIterator.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: Marc Sturm $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 // OpenMS includes
12 #include <OpenMS/CONCEPT/Types.h>
15 
16 // STL includes
17 #include <iterator>
18 
19 namespace OpenMS
20 {
21  namespace Internal
22  {
34  template<class ValueT, class ReferenceT, class PointerT, class SpectrumIteratorT, class PeakIteratorT>
36  {
37  public:
38  typedef double CoordinateType;
39  typedef ValueT PeakType;
40  typedef SpectrumIteratorT SpectrumIteratorType;
41  typedef PeakIteratorT PeakIteratorType;
42  using SpectrumT = typename std::iterator_traits<SpectrumIteratorType>::value_type;
43 
46  class Param
47  {
48  friend AreaIterator; // allow access to private members (avoids writing get-accessors)
49  public:
57  Param(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end, uint8_t ms_level) : first_(first), current_scan_(begin), end_scan_(end), ms_level_(ms_level)
58  {
59  }
60 
62  static Param end()
63  {
64  static Param p;
65  p.is_end_ = true;
66  return p;
67  }
68 
70  Param& operator=(const Param& rhs) = default;
71 
77  {
78  low_mz_ = low_mz;
79  return *this;
80  }
83  {
84  high_mz_ = high_mz;
85  return *this;
86  }
89  {
90  low_im_ = low_im;
91  return *this;
92  }
95  {
96  high_im_ = high_im;
97  return *this;
98  }
100  Param& msLevel(int8_t ms_level)
101  {
102  ms_level_ = ms_level;
103  return *this;
104  }
106 
107  protected:
118  /* optional parameters */
120  CoordinateType low_mz_ = std::numeric_limits<CoordinateType>::lowest();
122  CoordinateType high_mz_ = std::numeric_limits<CoordinateType>::max();
124  CoordinateType low_im_ = std::numeric_limits<CoordinateType>::lowest();
126  CoordinateType high_im_ = std::numeric_limits<CoordinateType>::max();
128  int8_t ms_level_ {};
130  bool is_end_ = false;
131 
132  private:
134  Param() = default;
135  };
136 
137 
142  typedef std::forward_iterator_tag iterator_category;
144  typedef ValueT value_type;
146  typedef ReferenceT reference;
148  typedef PointerT pointer;
150  typedef unsigned int difference_type;
152 
154  explicit AreaIterator(const Param& p) : p_(p)
155  {
156  nextScan_();
157  }
158 
160  AreaIterator() : p_(Param::end())
161  {
162  }
163 
165  ~AreaIterator() = default;
166 
168  AreaIterator(const AreaIterator& rhs) = default;
169 
172  {
173  p_.is_end_ = rhs.p_.is_end_;
174  // only copy iterators, if the assigned iterator is not the end iterator
175  if (!p_.is_end_)
176  {
177  p_ = rhs.p_;
178  }
179 
180  return *this;
181  }
182 
184  bool operator==(const AreaIterator& rhs) const
185  {
186  // Both end iterators => equal
187  if (p_.is_end_ && rhs.p_.is_end_)
188  return true;
189 
190  // Normal and end iterator => not equal
191  if (p_.is_end_ ^ rhs.p_.is_end_)
192  return false;
193 
194  // Equality of pointed to peak addresses
195  return &(*(p_.current_peak_)) == &(*(rhs.p_.current_peak_));
196  }
197 
199  bool operator!=(const AreaIterator& rhs) const
200  {
201  return !(*this == rhs);
202  }
203 
206  {
207  // no increment if this is the end iterator
208  if (p_.is_end_)
209  return *this;
210 
211  ++p_.current_peak_;
212  // test whether we arrived at the end of the current scan
213  if (p_.current_peak_ == p_.end_peak_)
214  {
215  ++p_.current_scan_;
216  nextScan_();
217  }
218  return *this;
219  }
220 
223  {
224  AreaIterator tmp(*this);
225  ++(*this);
226  return tmp;
227  }
228 
231  {
232  return p_.current_peak_.operator*();
233  }
234 
237  {
238  return p_.current_peak_.operator->();
239  }
240 
243  {
244  return p_.current_scan_->getRT();
245  }
246 
249  {
250  return p_.current_scan_->getDriftTime();
251  }
252 
254  const SpectrumT& getSpectrum() const
255  {
256  return *p_.current_scan_;
257  }
258 
260  inline PeakIndex getPeakIndex() const
261  {
262  if (p_.is_end_)
263  {
264  return {};
265  }
266  else
267  {
269  }
270  }
271 
272  private:
274  void nextScan_()
275  {
276  using MSLevelType = decltype(p_.current_scan_->getMSLevel());
278  while (true)
279  {
280  // skip over invalid MS levels and Mobility
281  while (p_.current_scan_ != p_.end_scan_ && (p_.current_scan_->getMSLevel() != (MSLevelType)p_.ms_level_ || !mb.containsMobility(p_.current_scan_->getDriftTime())))
282  {
283  ++p_.current_scan_;
284  }
285  if (p_.current_scan_ == p_.end_scan_)
286  {
287  p_.is_end_ = true;
288  return;
289  }
292  if (p_.current_peak_ != p_.end_peak_)
293  {
294  return;
295  }
296  ++p_.current_scan_;
297  }
298  }
299 
302  };
303 
304  } // namespace Internal
305 } // namespace OpenMS
Definition: AreaIterator.h:47
Param & highIM(CoordinateType high_im)
high ion mobility boundary
Definition: AreaIterator.h:94
Param & lowIM(CoordinateType low_im)
low ion mobility boundary
Definition: AreaIterator.h:88
SpectrumIteratorType current_scan_
Iterator to the current spectrum.
Definition: AreaIterator.h:111
CoordinateType low_mz_
low m/z boundary
Definition: AreaIterator.h:120
CoordinateType high_mz_
high m/z boundary
Definition: AreaIterator.h:122
PeakIteratorType end_peak_
Past-the-end iterator of peaks in the current spectrum.
Definition: AreaIterator.h:117
Param & lowMZ(CoordinateType low_mz)
low m/z boundary
Definition: AreaIterator.h:76
int8_t ms_level_
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:128
Param & operator=(const Param &rhs)=default
Assignment operator.
bool is_end_
Flag that indicates that this iterator is the end iterator.
Definition: AreaIterator.h:130
PeakIteratorType current_peak_
Iterator to the current peak.
Definition: AreaIterator.h:115
friend AreaIterator
Definition: AreaIterator.h:48
SpectrumIteratorType first_
Iterator to the first scan of the map (needed to calculate the index)
Definition: AreaIterator.h:109
Param & msLevel(int8_t ms_level)
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:100
Param()=default
only used internally for end()
CoordinateType high_im_
high mobility boundary
Definition: AreaIterator.h:126
SpectrumIteratorType end_scan_
Past-the-end iterator of spectra.
Definition: AreaIterator.h:113
static Param end()
return the end-iterator
Definition: AreaIterator.h:62
Param & highMZ(CoordinateType high_mz)
high m/z boundary
Definition: AreaIterator.h:82
Param(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end, uint8_t ms_level)
C'tor with mandatory parameters.
Definition: AreaIterator.h:57
CoordinateType low_im_
low mobility boundary
Definition: AreaIterator.h:124
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:36
AreaIterator(const AreaIterator &rhs)=default
Copy constructor.
bool operator!=(const AreaIterator &rhs) const
Test for inequality.
Definition: AreaIterator.h:199
std::forward_iterator_tag iterator_category
The iterator's category type.
Definition: AreaIterator.h:142
PointerT pointer
The pointer type as returned by operator->()
Definition: AreaIterator.h:148
double CoordinateType
Definition: AreaIterator.h:38
ValueT value_type
The iterator's value type.
Definition: AreaIterator.h:144
AreaIterator(const Param &p)
Constructor for the begin iterator.
Definition: AreaIterator.h:154
AreaIterator & operator++()
Step forward by one (prefix operator)
Definition: AreaIterator.h:205
AreaIterator()
Default constructor (for the end iterator)
Definition: AreaIterator.h:160
unsigned int difference_type
The difference type.
Definition: AreaIterator.h:150
Param p_
holds spectra iterators and area limits
Definition: AreaIterator.h:301
PeakIteratorT PeakIteratorType
Definition: AreaIterator.h:41
CoordinateType getDriftTime() const
returns the ion mobility time of the current scan
Definition: AreaIterator.h:248
AreaIterator operator++(int)
Step forward by one (postfix operator)
Definition: AreaIterator.h:222
bool operator==(const AreaIterator &rhs) const
Test for equality.
Definition: AreaIterator.h:184
reference operator*() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:230
ReferenceT reference
The reference type as returned by operator*()
Definition: AreaIterator.h:146
void nextScan_()
advances the iterator to the next valid peak in the next valid spectrum
Definition: AreaIterator.h:274
~AreaIterator()=default
Destructor.
ValueT PeakType
Definition: AreaIterator.h:39
typename std::iterator_traits< SpectrumIteratorType >::value_type SpectrumT
Definition: AreaIterator.h:42
SpectrumIteratorT SpectrumIteratorType
Definition: AreaIterator.h:40
AreaIterator & operator=(const AreaIterator &rhs)
Assignment operator.
Definition: AreaIterator.h:171
PeakIndex getPeakIndex() const
returns the PeakIndex corresponding to the current iterator position
Definition: AreaIterator.h:260
const SpectrumT & getSpectrum() const
returns the current scan into which the iterator points
Definition: AreaIterator.h:254
pointer operator->() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:236
CoordinateType getRT() const
returns the retention time of the current scan
Definition: AreaIterator.h:242
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Index of a peak or feature.
Definition: PeakIndex.h:25
Definition: RangeManager.h:457