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) ms_level_ = begin->getMSLevel();
87  }
88 
90  static Param end()
91  {
92  static Param p;
93  p.is_end_ = true;
94  return p;
95  }
96 
98  Param& operator=(const Param& rhs) = default;
99 
104  Param& lowMZ(CoordinateType low_mz) { low_mz_ = low_mz; return *this;}
106  Param& highMZ(CoordinateType high_mz) { high_mz_ = high_mz; return *this;}
108  Param& lowIM(CoordinateType low_im) { low_im_ = low_im; return *this;}
110  Param& highIM(CoordinateType high_im) { high_im_ = high_im; return *this;}
112  Param& msLevel(int8_t ms_level) { ms_level_ = ms_level; return *this;}
114 
115  protected:
126  /* optional parameters */
128  CoordinateType low_mz_ = std::numeric_limits<CoordinateType>::lowest();
130  CoordinateType high_mz_ = std::numeric_limits<CoordinateType>::max();
132  CoordinateType low_im_ = std::numeric_limits<CoordinateType>::lowest();
134  CoordinateType high_im_ = std::numeric_limits<CoordinateType>::max();
136  int8_t ms_level_;
138  bool is_end_ = false;
139 
140  private:
142  Param() = default;
143  };
144 
145 
146 
151  typedef ValueT value_type;
153  typedef ReferenceT reference;
155  typedef PointerT pointer;
157  typedef unsigned int difference_type;
159 
161  AreaIterator(const Param& p) :
162  p_(p)
163  {
164  nextScan_();
165  }
166 
168  AreaIterator() : p_(Param::end()) {}
169 
171  ~AreaIterator() = default;
172 
174  AreaIterator(const AreaIterator& rhs) = default;
175 
178  {
179  p_.is_end_ = rhs.p_.is_end_;
180  // only copy iterators, if the assigned iterator is not the end iterator
181  if (!p_.is_end_)
182  {
183  p_ = rhs.p_;
184  }
185 
186  return *this;
187  }
188 
190  bool operator==(const AreaIterator & rhs) const
191  {
192  // Both end iterators => equal
193  if (p_.is_end_ && rhs.p_.is_end_) return true;
194 
195  // Normal and end iterator => not equal
196  if (p_.is_end_ ^ rhs.p_.is_end_)
197  return false;
198 
199  // Equality of pointed to peak addresses
200  return &(*(p_.current_peak_)) == &(*(rhs.p_.current_peak_));
201  }
202 
204  bool operator!=(const AreaIterator & rhs) const
205  {
206  return !(*this == rhs);
207  }
208 
211  {
212  //no increment if this is the end iterator
213  if (p_.is_end_) return *this;
214 
215  ++p_.current_peak_;
216  // test whether we arrived at the end of the current scan
217  if (p_.current_peak_ == p_.end_peak_)
218  {
219  ++p_.current_scan_;
220  nextScan_();
221  }
222  return *this;
223  }
224 
227  {
228  AreaIterator tmp(*this);
229  ++(*this);
230  return tmp;
231  }
232 
235  {
236  return p_.current_peak_.operator*();
237  }
238 
241  {
242  return p_.current_peak_.operator->();
243  }
244 
247  {
248  return p_.current_scan_->getRT();
249  }
250 
253  {
254  return p_.current_scan_->getDriftTime();
255  }
256 
258  inline PeakIndex getPeakIndex() const
259  {
260  if (p_.is_end_)
261  {
262  return PeakIndex();
263  }
264  else
265  {
267  }
268  }
269 
270 private:
272  void nextScan_()
273  {
274  using MSLevelType = decltype(p_.current_scan_->getMSLevel());
276  while (true)
277  {
278  // skip over invalid MS levels and Mobility
279  while (p_.current_scan_ != p_.end_scan_ &&
280  (p_.current_scan_->getMSLevel() != (MSLevelType)p_.ms_level_ ||
281  !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  }
305 }
306 
Definition: AreaIterator.h:73
Param & highIM(CoordinateType high_im)
high ion mobility boundary
Definition: AreaIterator.h:110
Param & lowIM(CoordinateType low_im)
low ion mobility boundary
Definition: AreaIterator.h:108
SpectrumIteratorType current_scan_
Iterator to the current spectrum.
Definition: AreaIterator.h:119
CoordinateType low_mz_
low m/z boundary
Definition: AreaIterator.h:128
CoordinateType high_mz_
high m/z boundary
Definition: AreaIterator.h:130
PeakIteratorType end_peak_
Past-the-end iterator of peaks in the current spectrum.
Definition: AreaIterator.h:125
Param & lowMZ(CoordinateType low_mz)
low m/z boundary
Definition: AreaIterator.h:104
int8_t ms_level_
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:136
Param & operator=(const Param &rhs)=default
Assignment operator.
bool is_end_
Flag that indicates that this iterator is the end iterator.
Definition: AreaIterator.h:138
PeakIteratorType current_peak_
Iterator to the current peak.
Definition: AreaIterator.h:123
friend AreaIterator
Definition: AreaIterator.h:74
SpectrumIteratorType first_
Iterator to the first scan of the map (needed to calculate the index)
Definition: AreaIterator.h:117
Param & msLevel(int8_t ms_level)
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:112
Param()=default
only used internally for end()
CoordinateType high_im_
high mobility boundary
Definition: AreaIterator.h:134
SpectrumIteratorType end_scan_
Past-the-end iterator of spectra.
Definition: AreaIterator.h:121
static Param end()
return the end-iterator
Definition: AreaIterator.h:90
Param & highMZ(CoordinateType high_mz)
high m/z boundary
Definition: AreaIterator.h:106
Param(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end)
C'tor with mandatory parameters.
Definition: AreaIterator.h:82
CoordinateType low_im_
low mobility boundary
Definition: AreaIterator.h:132
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:63
AreaIterator(const AreaIterator &rhs)=default
Copy constructor.
bool operator!=(const AreaIterator &rhs) const
Test for inequality.
Definition: AreaIterator.h:204
PointerT pointer
The pointer type as returned by operator->()
Definition: AreaIterator.h:155
double CoordinateType
Definition: AreaIterator.h:65
ValueT value_type
The iterator's value type.
Definition: AreaIterator.h:151
AreaIterator(const Param &p)
Constructor for the begin iterator.
Definition: AreaIterator.h:161
AreaIterator & operator++()
Step forward by one (prefix operator)
Definition: AreaIterator.h:210
AreaIterator()
Default constructor (for the end iterator)
Definition: AreaIterator.h:168
unsigned int difference_type
The difference type.
Definition: AreaIterator.h:157
Param p_
holds spectra iterators and area limits
Definition: AreaIterator.h:301
PeakIteratorT PeakIteratorType
Definition: AreaIterator.h:68
CoordinateType getDriftTime() const
returns the ion mobility time of the current scan
Definition: AreaIterator.h:252
AreaIterator operator++(int)
Step forward by one (postfix operator)
Definition: AreaIterator.h:226
bool operator==(const AreaIterator &rhs) const
Test for equality.
Definition: AreaIterator.h:190
reference operator*() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:234
ReferenceT reference
The reference type as returned by operator*()
Definition: AreaIterator.h:153
void nextScan_()
advances the iterator to the next valid peak in the next valid spectrum
Definition: AreaIterator.h:272
~AreaIterator()=default
Destructor.
ValueT PeakType
Definition: AreaIterator.h:66
SpectrumIteratorT SpectrumIteratorType
Definition: AreaIterator.h:67
AreaIterator & operator=(const AreaIterator &rhs)
Assignment operator.
Definition: AreaIterator.h:177
PeakIndex getPeakIndex() const
returns the PeakIndex corresponding to the current iterator position
Definition: AreaIterator.h:258
pointer operator->() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:240
CoordinateType getRT() const
returns the retention time of the current scan
Definition: AreaIterator.h:246
FLASHIda C++ to C# (or vice versa) bridge functions The functions here are called in C# to invoke fun...
Definition: FeatureDeconvolution.h:48
Index of a peak or feature.
Definition: PeakIndex.h:51
Definition: RangeManager.h:497