OpenMS
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-2023.
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>
62  {
63  public:
64  typedef double CoordinateType;
65  typedef ValueT PeakType;
66  typedef SpectrumIteratorT SpectrumIteratorType;
67  typedef PeakIteratorT PeakIteratorType;
68  using SpectrumT = typename std::iterator_traits<SpectrumIteratorType>::value_type;
69 
72  class Param
73  {
74  friend AreaIterator; // allow access to private members (avoids writing get-accessors)
75  public:
83  Param(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end, uint8_t ms_level) : first_(first), current_scan_(begin), end_scan_(end), ms_level_(ms_level)
84  {
85  }
86 
88  static Param end()
89  {
90  static Param p;
91  p.is_end_ = true;
92  return p;
93  }
94 
96  Param& operator=(const Param& rhs) = default;
97 
103  {
104  low_mz_ = low_mz;
105  return *this;
106  }
109  {
110  high_mz_ = high_mz;
111  return *this;
112  }
115  {
116  low_im_ = low_im;
117  return *this;
118  }
121  {
122  high_im_ = high_im;
123  return *this;
124  }
126  Param& msLevel(int8_t ms_level)
127  {
128  ms_level_ = ms_level;
129  return *this;
130  }
132 
133  protected:
144  /* optional parameters */
146  CoordinateType low_mz_ = std::numeric_limits<CoordinateType>::lowest();
148  CoordinateType high_mz_ = std::numeric_limits<CoordinateType>::max();
150  CoordinateType low_im_ = std::numeric_limits<CoordinateType>::lowest();
152  CoordinateType high_im_ = std::numeric_limits<CoordinateType>::max();
154  int8_t ms_level_ {};
156  bool is_end_ = false;
157 
158  private:
160  Param() = default;
161  };
162 
163 
168  typedef std::forward_iterator_tag iterator_category;
170  typedef ValueT value_type;
172  typedef ReferenceT reference;
174  typedef PointerT pointer;
176  typedef unsigned int difference_type;
178 
180  explicit AreaIterator(const Param& p) : p_(p)
181  {
182  nextScan_();
183  }
184 
186  AreaIterator() : p_(Param::end())
187  {
188  }
189 
191  ~AreaIterator() = default;
192 
194  AreaIterator(const AreaIterator& rhs) = default;
195 
198  {
199  p_.is_end_ = rhs.p_.is_end_;
200  // only copy iterators, if the assigned iterator is not the end iterator
201  if (!p_.is_end_)
202  {
203  p_ = rhs.p_;
204  }
205 
206  return *this;
207  }
208 
210  bool operator==(const AreaIterator& rhs) const
211  {
212  // Both end iterators => equal
213  if (p_.is_end_ && rhs.p_.is_end_)
214  return true;
215 
216  // Normal and end iterator => not equal
217  if (p_.is_end_ ^ rhs.p_.is_end_)
218  return false;
219 
220  // Equality of pointed to peak addresses
221  return &(*(p_.current_peak_)) == &(*(rhs.p_.current_peak_));
222  }
223 
225  bool operator!=(const AreaIterator& rhs) const
226  {
227  return !(*this == rhs);
228  }
229 
232  {
233  // no increment if this is the end iterator
234  if (p_.is_end_)
235  return *this;
236 
237  ++p_.current_peak_;
238  // test whether we arrived at the end of the current scan
239  if (p_.current_peak_ == p_.end_peak_)
240  {
241  ++p_.current_scan_;
242  nextScan_();
243  }
244  return *this;
245  }
246 
249  {
250  AreaIterator tmp(*this);
251  ++(*this);
252  return tmp;
253  }
254 
257  {
258  return p_.current_peak_.operator*();
259  }
260 
263  {
264  return p_.current_peak_.operator->();
265  }
266 
269  {
270  return p_.current_scan_->getRT();
271  }
272 
275  {
276  return p_.current_scan_->getDriftTime();
277  }
278 
280  const SpectrumT& getSpectrum() const
281  {
282  return *p_.current_scan_;
283  }
284 
286  inline PeakIndex getPeakIndex() const
287  {
288  if (p_.is_end_)
289  {
290  return {};
291  }
292  else
293  {
295  }
296  }
297 
298  private:
300  void nextScan_()
301  {
302  using MSLevelType = decltype(p_.current_scan_->getMSLevel());
304  while (true)
305  {
306  // skip over invalid MS levels and Mobility
307  while (p_.current_scan_ != p_.end_scan_ && (p_.current_scan_->getMSLevel() != (MSLevelType)p_.ms_level_ || !mb.containsMobility(p_.current_scan_->getDriftTime())))
308  {
309  ++p_.current_scan_;
310  }
311  if (p_.current_scan_ == p_.end_scan_)
312  {
313  p_.is_end_ = true;
314  return;
315  }
318  if (p_.current_peak_ != p_.end_peak_)
319  {
320  return;
321  }
322  ++p_.current_scan_;
323  }
324  }
325 
328  };
329 
330  } // namespace Internal
331 } // namespace OpenMS
Definition: AreaIterator.h:73
Param & highIM(CoordinateType high_im)
high ion mobility boundary
Definition: AreaIterator.h:120
Param & lowIM(CoordinateType low_im)
low ion mobility boundary
Definition: AreaIterator.h:114
SpectrumIteratorType current_scan_
Iterator to the current spectrum.
Definition: AreaIterator.h:137
CoordinateType low_mz_
low m/z boundary
Definition: AreaIterator.h:146
CoordinateType high_mz_
high m/z boundary
Definition: AreaIterator.h:148
PeakIteratorType end_peak_
Past-the-end iterator of peaks in the current spectrum.
Definition: AreaIterator.h:143
Param & lowMZ(CoordinateType low_mz)
low m/z boundary
Definition: AreaIterator.h:102
int8_t ms_level_
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:154
Param & operator=(const Param &rhs)=default
Assignment operator.
bool is_end_
Flag that indicates that this iterator is the end iterator.
Definition: AreaIterator.h:156
PeakIteratorType current_peak_
Iterator to the current peak.
Definition: AreaIterator.h:141
friend AreaIterator
Definition: AreaIterator.h:74
SpectrumIteratorType first_
Iterator to the first scan of the map (needed to calculate the index)
Definition: AreaIterator.h:135
Param & msLevel(int8_t ms_level)
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:126
Param()=default
only used internally for end()
CoordinateType high_im_
high mobility boundary
Definition: AreaIterator.h:152
SpectrumIteratorType end_scan_
Past-the-end iterator of spectra.
Definition: AreaIterator.h:139
static Param end()
return the end-iterator
Definition: AreaIterator.h:88
Param & highMZ(CoordinateType high_mz)
high m/z boundary
Definition: AreaIterator.h:108
Param(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end, uint8_t ms_level)
C'tor with mandatory parameters.
Definition: AreaIterator.h:83
CoordinateType low_im_
low mobility boundary
Definition: AreaIterator.h:150
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:62
AreaIterator(const AreaIterator &rhs)=default
Copy constructor.
bool operator!=(const AreaIterator &rhs) const
Test for inequality.
Definition: AreaIterator.h:225
std::forward_iterator_tag iterator_category
The iterator's category type.
Definition: AreaIterator.h:168
PointerT pointer
The pointer type as returned by operator->()
Definition: AreaIterator.h:174
double CoordinateType
Definition: AreaIterator.h:64
ValueT value_type
The iterator's value type.
Definition: AreaIterator.h:170
AreaIterator(const Param &p)
Constructor for the begin iterator.
Definition: AreaIterator.h:180
AreaIterator & operator++()
Step forward by one (prefix operator)
Definition: AreaIterator.h:231
AreaIterator()
Default constructor (for the end iterator)
Definition: AreaIterator.h:186
unsigned int difference_type
The difference type.
Definition: AreaIterator.h:176
Param p_
holds spectra iterators and area limits
Definition: AreaIterator.h:327
PeakIteratorT PeakIteratorType
Definition: AreaIterator.h:67
CoordinateType getDriftTime() const
returns the ion mobility time of the current scan
Definition: AreaIterator.h:274
AreaIterator operator++(int)
Step forward by one (postfix operator)
Definition: AreaIterator.h:248
bool operator==(const AreaIterator &rhs) const
Test for equality.
Definition: AreaIterator.h:210
reference operator*() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:256
ReferenceT reference
The reference type as returned by operator*()
Definition: AreaIterator.h:172
void nextScan_()
advances the iterator to the next valid peak in the next valid spectrum
Definition: AreaIterator.h:300
~AreaIterator()=default
Destructor.
ValueT PeakType
Definition: AreaIterator.h:65
typename std::iterator_traits< SpectrumIteratorType >::value_type SpectrumT
Definition: AreaIterator.h:68
SpectrumIteratorT SpectrumIteratorType
Definition: AreaIterator.h:66
AreaIterator & operator=(const AreaIterator &rhs)
Assignment operator.
Definition: AreaIterator.h:197
PeakIndex getPeakIndex() const
returns the PeakIndex corresponding to the current iterator position
Definition: AreaIterator.h:286
const SpectrumT & getSpectrum() const
returns the current scan into which the iterator points
Definition: AreaIterator.h:280
pointer operator->() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:262
CoordinateType getRT() const
returns the retention time of the current scan
Definition: AreaIterator.h:268
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
Index of a peak or feature.
Definition: PeakIndex.h:51
Definition: RangeManager.h:483