OpenMS
RangeUtils.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg$
6 // $Authors: Marc Sturm, Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <functional>
12 #include <algorithm>
13 #include <vector>
14 #include <OpenMS/CONCEPT/Types.h>
18 
19 namespace OpenMS
20 {
69  template <class MetaContainer>
71  {
72 public:
79  HasMetaValue(String metavalue, bool reverse = false) :
80  metavalue_key_(metavalue),
82  {}
83 
84  inline bool operator()(const MetaContainer& s) const
85  {
86  bool has_meta_value = s.metaValueExists(metavalue_key_);
87  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
88  return reverse_ ^ has_meta_value;
89  }
90 
91 protected:
93  bool reverse_;
94  };
95 
96 
104  template <class SpectrumType>
105  class InRTRange
106  {
107 public:
116  InRTRange(double min, double max, bool reverse = false) :
117  min_(min),
118  max_(max),
120  {}
121 
122  inline bool operator()(const SpectrumType& s) const
123  {
124  double tmp = s.getRT();
125  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
126  return reverse_ ^ (min_ <= tmp && tmp <= max_);
127  }
128 
129 protected:
130  double min_, max_;
131  bool reverse_;
132  };
133 
141  template <class SpectrumType>
143  {
144 public:
152  InMSLevelRange(const IntList& levels, bool reverse = false) :
153  levels_(levels),
155  {}
156 
157  inline bool operator()(const SpectrumType& s) const
158  {
159  Int tmp = s.getMSLevel();
160  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
161  return reverse_ ^ (std::find(levels_.begin(), levels_.end(), tmp) != levels_.end());
162  }
163 
164 protected:
166  bool reverse_;
167  };
168 
176  template <class SpectrumType>
178  {
179 public:
187  HasScanMode(Int mode, bool reverse = false) :
188  mode_(mode),
190  {}
191 
192  inline bool operator()(const SpectrumType& s) const
193  {
194  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
195  return reverse_ ^ (s.getInstrumentSettings().getScanMode() == mode_);
196  }
197 
198 protected:
200  bool reverse_;
201  };
202 
210  template <class SpectrumType>
212  {
213 public:
221  HasScanPolarity(Int polarity, bool reverse = false) :
222  polarity_(polarity),
224  {}
225 
226  inline bool operator()(const SpectrumType& s) const
227  {
228  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
230  }
231 
232 protected:
234  bool reverse_;
235  };
236 
237 
245  template <class SpectrumType>
247  {
248 public:
254  explicit IsEmptySpectrum(bool reverse = false) :
256  {}
257 
258  inline bool operator()(const SpectrumType& s) const
259  {
260  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
261  return reverse_ ^ s.empty();
262  }
263 
264 protected:
265  bool reverse_;
266  };
267 
275  template <class SpectrumType>
277  {
278 public:
285  explicit IsZoomSpectrum(bool reverse = false) :
287  {}
288 
289  inline bool operator()(const SpectrumType& s) const
290  {
291  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
293  }
294 
295 protected:
296  bool reverse_;
297  };
298 
299 
308  template <class SpectrumType>
310  {
311 public:
319  HasActivationMethod(const StringList& methods, bool reverse = false) :
320  methods_(methods),
322  {}
323 
324  inline bool operator()(const SpectrumType& s) const
325  {
326  for (const Precursor& p : s.getPrecursors())
327  {
328  for (const Precursor::ActivationMethod am : p.getActivationMethods())
329  {
331  {
332  // found matching activation method
333  if (reverse_) return false;
334  else return true;
335  }
336  }
337  }
338 
339  return reverse_;
340  }
341 
342 protected:
344  bool reverse_;
345  };
346 
356  template <class SpectrumType>
358  {
359 public:
367  InPrecursorMZRange(const double& mz_left, const double& mz_right, bool reverse = false) :
368  mz_left_(mz_left),
369  mz_right_(mz_right),
371  {}
372 
373  inline bool operator()(const SpectrumType& s) const
374  {
375  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
376  {
377  //std::cerr << mz_left_ << " " << mz_right_ << " " << it->getMZ() << "\n";
378  if (!(mz_left_ <= it->getMZ() && it->getMZ() <= mz_right_))
379  { // found PC outside of allowed window
380  if (reverse_) return true;
381  else return false;
382  }
383  }
384 
385  if (reverse_) return false;
386  else return true;
387  }
388 
389 protected:
390  double mz_left_;
391  double mz_right_;
392  bool reverse_;
393  };
394 
395 
404  template <class SpectrumType>
406  {
407 public:
415  HasPrecursorCharge(const IntList& charges, bool reverse = false) :
416  charges_(charges),
418  {}
419 
420  inline bool operator()(const SpectrumType& s) const
421  {
422  bool match = false;
423  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
424  {
425  Int tmp = it->getCharge();
426  match = match || (std::find(charges_.begin(), charges_.end(), tmp) != charges_.end());
427  }
428 
429  if (reverse_) return !match;
430  else return match;
431  }
432 
433 protected:
435  bool reverse_;
436  };
437 
438 
448  template <class PeakType>
449  class InMzRange
450  {
451 public:
460  InMzRange(double min, double max, bool reverse = false) :
461  min_(min),
462  max_(max),
464  {}
465 
466  inline bool operator()(const PeakType& p) const
467  {
468  double tmp = p.getPosition()[0];
469  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
470  return reverse_ ^ (min_ <= tmp && tmp <= max_);
471  }
472 
473 protected:
474  double min_, max_;
475  bool reverse_;
476  };
477 
485  template <class PeakType>
487  {
488 public:
496  InIntensityRange(double min, double max, bool reverse = false) :
497  min_(min),
498  max_(max),
500  {}
501 
502  inline bool operator()(const PeakType& p) const
503  {
504  double tmp = p.getIntensity();
505  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
506  return reverse_ ^ (min_ <= tmp && tmp <= max_);
507  }
508 
509 protected:
510  double min_, max_;
511  bool reverse_;
512  };
513 
521  template <class SpectrumType>
523  {
524 public:
532  IsInCollisionEnergyRange(double min, double max, bool reverse = false) :
533  min_energy_(min),
534  max_energy_(max),
536  {}
537 
538  inline bool operator()(const SpectrumType& s) const
539  {
540  // leave non-fragmentation spectra untouched
541  if (s.getMSLevel() == 1) return false;
542 
543  bool isIn = false;
544  bool hasCollisionEnergy = false;
545  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
546  {
547  if (it->metaValueExists("collision energy"))
548  {
549  hasCollisionEnergy = true;
550  double cE = it->getMetaValue("collision energy");
551  isIn |= !(cE > max_energy_ || cE < min_energy_);
552  }
553  }
554 
555  // we accept all spectra that have no collision energy value
556  if (!hasCollisionEnergy) return false;
557 
558  if (reverse_) return !isIn;
559  else return isIn;
560  }
561 
562 private:
564  bool reverse_;
565  };
566 
573  template <class SpectrumType>
575  {
576 
577 public:
585  IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse = false) :
586  min_size_(min_size),
587  max_size_(max_size),
589  {}
590 
591  inline bool operator()(const SpectrumType& s) const
592  {
593  // leave non-fragmentation spectra untouched
594  if (s.getMSLevel() == 1) return false;
595 
596  bool isIn = false;
597  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
598  {
599  const double isolationWindowSize = it->getIsolationWindowUpperOffset() + it->getIsolationWindowLowerOffset();
600  isIn |= !(isolationWindowSize > max_size_ || isolationWindowSize < min_size_);
601  }
602 
603  if (reverse_) return !isIn;
604  else return isIn;
605  }
606 
607 private:
609  bool reverse_;
610  };
611 
618  template <class SpectrumType>
620  {
621 
622 public:
629  IsInIsolationWindow(std::vector<double> vec_mz, bool reverse = false) :
630  vec_mz_(vec_mz),
632  {
633  std::sort(vec_mz_.begin(), vec_mz_.end());
634  }
635 
636  inline bool operator()(const SpectrumType& s) const
637  {
638  // leave non-fragmentation spectra untouched
639  if (s.getMSLevel() == 1) return false;
640 
641  bool isIn = false;
642  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
643  {
644  if (it->getIsolationWindowLowerOffset() == 0 || it->getIsolationWindowUpperOffset() == 0)
645  {
646  OPENMS_LOG_WARN << "IsInIsolationWindow(): Lower/Upper Offset for Precursor Isolation Window is Zero! " <<
647  "Filtering will probably be too strict (unless you hit the exact precursor m/z)!" << std::endl;
648  }
649  const double lower_mz = it->getMZ() - it->getIsolationWindowLowerOffset();
650  std::vector<double>::const_iterator it_mz = std::lower_bound(vec_mz_.begin(), vec_mz_.end(), lower_mz);
651  if (it_mz != vec_mz_.end()) // left side ok
652  { // right side?
653  const double upper_mz = it->getMZ() + it->getIsolationWindowUpperOffset();
654  isIn |= (*it_mz <= upper_mz);
655  }
656  }
657 
658  if (reverse_) return !isIn;
659  else return isIn;
660  }
661 
662 private:
663  std::vector<double> vec_mz_;
664  bool reverse_;
665  };
666 
667 } // namespace OpenMS
668 
#define OPENMS_LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged.
Definition: LogStream.h:444
Predicate that determines if a spectrum was generated using any activation method given in the constr...
Definition: RangeUtils.h:310
HasActivationMethod(const StringList &methods, bool reverse=false)
Constructor.
Definition: RangeUtils.h:319
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:324
StringList methods_
Definition: RangeUtils.h:343
bool reverse_
Definition: RangeUtils.h:344
Predicate that determines if a class has a certain metavalue.
Definition: RangeUtils.h:71
bool operator()(const MetaContainer &s) const
Definition: RangeUtils.h:84
HasMetaValue(String metavalue, bool reverse=false)
Constructor.
Definition: RangeUtils.h:79
bool reverse_
Definition: RangeUtils.h:93
String metavalue_key_
Definition: RangeUtils.h:92
Predicate that determines if a spectrum has a certain precursor charge as given in the constructor li...
Definition: RangeUtils.h:406
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:420
HasPrecursorCharge(const IntList &charges, bool reverse=false)
Constructor.
Definition: RangeUtils.h:415
IntList charges_
Definition: RangeUtils.h:434
bool reverse_
Definition: RangeUtils.h:435
Predicate that determines if a spectrum has a certain scan mode.
Definition: RangeUtils.h:178
Int mode_
Definition: RangeUtils.h:199
HasScanMode(Int mode, bool reverse=false)
Constructor.
Definition: RangeUtils.h:187
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:192
bool reverse_
Definition: RangeUtils.h:200
Predicate that determines if a spectrum has a certain scan polarity.
Definition: RangeUtils.h:212
HasScanPolarity(Int polarity, bool reverse=false)
Constructor.
Definition: RangeUtils.h:221
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:226
Int polarity_
Definition: RangeUtils.h:233
bool reverse_
Definition: RangeUtils.h:234
Predicate that determines if a peak lies inside/outside a specific intensity range.
Definition: RangeUtils.h:487
double max_
Definition: RangeUtils.h:510
InIntensityRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:496
double min_
Definition: RangeUtils.h:510
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:502
bool reverse_
Definition: RangeUtils.h:511
Predicate that determines if a spectrum lies inside/outside a specific MS level set.
Definition: RangeUtils.h:143
InMSLevelRange(const IntList &levels, bool reverse=false)
Constructor.
Definition: RangeUtils.h:152
IntList levels_
Definition: RangeUtils.h:165
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:157
bool reverse_
Definition: RangeUtils.h:166
Predicate that determines if a peak lies inside/outside a specific m/z range.
Definition: RangeUtils.h:450
double max_
Definition: RangeUtils.h:474
InMzRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:460
double min_
Definition: RangeUtils.h:474
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:466
bool reverse_
Definition: RangeUtils.h:475
Predicate that determines if a spectrum's precursor is within a certain m/z range.
Definition: RangeUtils.h:358
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:373
double mz_right_
Definition: RangeUtils.h:391
InPrecursorMZRange(const double &mz_left, const double &mz_right, bool reverse=false)
Constructor.
Definition: RangeUtils.h:367
double mz_left_
Definition: RangeUtils.h:390
bool reverse_
Definition: RangeUtils.h:392
Predicate that determines if a spectrum lies inside/outside a specific retention time range.
Definition: RangeUtils.h:106
double max_
Definition: RangeUtils.h:130
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:122
InRTRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:116
double min_
Definition: RangeUtils.h:130
bool reverse_
Definition: RangeUtils.h:131
ScanMode getScanMode() const
returns the scan mode
IonSource::Polarity getPolarity() const
returns the polarity
bool getZoomScan() const
return if this scan is a zoom (enhanced resolution) scan
Predicate that determines if a spectrum is empty.
Definition: RangeUtils.h:247
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:258
IsEmptySpectrum(bool reverse=false)
Constructor.
Definition: RangeUtils.h:254
bool reverse_
Definition: RangeUtils.h:265
Predicate that determines if an MSn spectrum was generated with a collision energy in the given range...
Definition: RangeUtils.h:523
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:538
IsInCollisionEnergyRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:532
double min_energy_
Definition: RangeUtils.h:563
double max_energy_
Definition: RangeUtils.h:563
bool reverse_
Definition: RangeUtils.h:564
Predicate that determines if the width of the isolation window of an MSn spectrum is in the given ran...
Definition: RangeUtils.h:575
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:591
double max_size_
Definition: RangeUtils.h:608
double min_size_
Definition: RangeUtils.h:608
IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse=false)
Constructor.
Definition: RangeUtils.h:585
bool reverse_
Definition: RangeUtils.h:609
Predicate that determines if the isolation window covers ANY of the given m/z values.
Definition: RangeUtils.h:620
IsInIsolationWindow(std::vector< double > vec_mz, bool reverse=false)
Constructor.
Definition: RangeUtils.h:629
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:636
std::vector< double > vec_mz_
Definition: RangeUtils.h:663
bool reverse_
Definition: RangeUtils.h:664
Predicate that determines if a spectrum is a zoom (enhanced resolution) spectrum.
Definition: RangeUtils.h:277
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:289
IsZoomSpectrum(bool reverse=false)
Constructor.
Definition: RangeUtils.h:285
bool reverse_
Definition: RangeUtils.h:296
static bool contains(const std::vector< T > &container, const E &elem)
Checks whether the element elem is contained in the given container.
Definition: ListUtils.h:137
The representation of a 1D spectrum.
Definition: MSSpectrum.h:44
double getRT() const
UInt getMSLevel() const
Returns the MS level.
A 2-dimensional raw data point or peak.
Definition: Peak2D.h:29
PositionType const & getPosition() const
Non-mutable access to the position.
Definition: Peak2D.h:154
IntensityType getIntensity() const
Definition: Peak2D.h:142
Precursor meta information.
Definition: Precursor.h:35
static const std::string NamesOfActivationMethod[SIZE_OF_ACTIVATIONMETHOD]
Names of activation methods.
Definition: Precursor.h:82
ActivationMethod
Method of activation.
Definition: Precursor.h:59
const InstrumentSettings & getInstrumentSettings() const
returns a const reference to the instrument settings of the current spectrum
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
A more convenient string class.
Definition: String.h:34
int Int
Signed integer type.
Definition: Types.h:72
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:29
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:44
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:330
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19