OpenMS  2.7.0
RangeUtils.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-2021.
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, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <functional>
38 #include <algorithm>
39 #include <vector>
40 #include <OpenMS/CONCEPT/Types.h>
44 
45 namespace OpenMS
46 {
95  template <class MetaContainer>
97  {
98 public:
105  HasMetaValue(String metavalue, bool reverse = false) :
106  metavalue_key_(metavalue),
107  reverse_(reverse)
108  {}
109 
110  inline bool operator()(const MetaContainer& s) const
111  {
112  bool has_meta_value = s.metaValueExists(metavalue_key_);
113  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
114  return reverse_ ^ has_meta_value;
115  }
116 
117 protected:
119  bool reverse_;
120  };
121 
122 
130  template <class SpectrumType>
131  class InRTRange
132  {
133 public:
142  InRTRange(double min, double max, bool reverse = false) :
143  min_(min),
144  max_(max),
145  reverse_(reverse)
146  {}
147 
148  inline bool operator()(const SpectrumType& s) const
149  {
150  double tmp = s.getRT();
151  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
152  return reverse_ ^ (min_ <= tmp && tmp <= max_);
153  }
154 
155 protected:
156  double min_, max_;
157  bool reverse_;
158  };
159 
167  template <class SpectrumType>
169  {
170 public:
178  InMSLevelRange(const IntList& levels, bool reverse = false) :
179  levels_(levels),
180  reverse_(reverse)
181  {}
182 
183  inline bool operator()(const SpectrumType& s) const
184  {
185  Int tmp = s.getMSLevel();
186  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
187  return reverse_ ^ std::find(levels_.begin(), levels_.end(), tmp) != levels_.end();
188  }
189 
190 protected:
192  bool reverse_;
193  };
194 
202  template <class SpectrumType>
204  {
205 public:
213  HasScanMode(Int mode, bool reverse = false) :
214  mode_(mode),
215  reverse_(reverse)
216  {}
217 
218  inline bool operator()(const SpectrumType& s) const
219  {
220  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
221  return reverse_ ^ (s.getInstrumentSettings().getScanMode() == mode_);
222  }
223 
224 protected:
226  bool reverse_;
227  };
228 
236  template <class SpectrumType>
238  {
239 public:
247  HasScanPolarity(Int polarity, bool reverse = false) :
248  polarity_(polarity),
249  reverse_(reverse)
250  {}
251 
252  inline bool operator()(const SpectrumType& s) const
253  {
254  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
256  }
257 
258 protected:
260  bool reverse_;
261  };
262 
263 
271  template <class SpectrumType>
273  {
274 public:
280  explicit IsEmptySpectrum(bool reverse = false) :
281  reverse_(reverse)
282  {}
283 
284  inline bool operator()(const SpectrumType& s) const
285  {
286  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
287  return reverse_ ^ s.empty();
288  }
289 
290 protected:
291  bool reverse_;
292  };
293 
301  template <class SpectrumType>
303  {
304 public:
311  explicit IsZoomSpectrum(bool reverse = false) :
312  reverse_(reverse)
313  {}
314 
315  inline bool operator()(const SpectrumType& s) const
316  {
317  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
319  }
320 
321 protected:
322  bool reverse_;
323  };
324 
325 
334  template <class SpectrumType>
336  {
337 public:
345  HasActivationMethod(const StringList& methods, bool reverse = false) :
346  methods_(methods),
347  reverse_(reverse)
348  {}
349 
350  inline bool operator()(const SpectrumType& s) const
351  {
352  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
353  {
354  for (std::set<Precursor::ActivationMethod>::const_iterator it_a = it->getActivationMethods().begin();
355  it_a != it->getActivationMethods().end();
356  ++it_a)
357  {
359  {
360  // found matching activation method
361  if (reverse_) return false;
362  else return true;
363  }
364  }
365  }
366 
367  if (reverse_) return true;
368  else return false;
369  }
370 
371 protected:
373  bool reverse_;
374  };
375 
385  template <class SpectrumType>
387  {
388 public:
396  InPrecursorMZRange(const double& mz_left, const double& mz_right, bool reverse = false) :
397  mz_left_(mz_left),
398  mz_right_(mz_right),
399  reverse_(reverse)
400  {}
401 
402  inline bool operator()(const SpectrumType& s) const
403  {
404  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
405  {
406  //std::cerr << mz_left_ << " " << mz_right_ << " " << it->getMZ() << "\n";
407  if (!(mz_left_ <= it->getMZ() && it->getMZ() <= mz_right_))
408  { // found PC outside of allowed window
409  if (reverse_) return true;
410  else return false;
411  }
412  }
413 
414  if (reverse_) return false;
415  else return true;
416  }
417 
418 protected:
419  double mz_left_;
420  double mz_right_;
421  bool reverse_;
422  };
423 
424 
433  template <class SpectrumType>
435  {
436 public:
444  HasPrecursorCharge(const IntList& charges, bool reverse = false) :
445  charges_(charges),
446  reverse_(reverse)
447  {}
448 
449  inline bool operator()(const SpectrumType& s) const
450  {
451  bool match = false;
452  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
453  {
454  Int tmp = it->getCharge();
455  match = match || (std::find(charges_.begin(), charges_.end(), tmp) != charges_.end());
456  }
457 
458  if (reverse_) return !match;
459  else return match;
460  }
461 
462 protected:
464  bool reverse_;
465  };
466 
467 
477  template <class PeakType>
478  class InMzRange
479  {
480 public:
489  InMzRange(double min, double max, bool reverse = false) :
490  min_(min),
491  max_(max),
492  reverse_(reverse)
493  {}
494 
495  inline bool operator()(const PeakType& p) const
496  {
497  double tmp = p.getPosition()[0];
498  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
499  return reverse_ ^ (min_ <= tmp && tmp <= max_);
500  }
501 
502 protected:
503  double min_, max_;
504  bool reverse_;
505  };
506 
514  template <class PeakType>
516  {
517 public:
525  InIntensityRange(double min, double max, bool reverse = false) :
526  min_(min),
527  max_(max),
528  reverse_(reverse)
529  {}
530 
531  inline bool operator()(const PeakType& p) const
532  {
533  double tmp = p.getIntensity();
534  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
535  return reverse_ ^ (min_ <= tmp && tmp <= max_);
536  }
537 
538 protected:
539  double min_, max_;
540  bool reverse_;
541  };
542 
550  template <class SpectrumType>
552  {
553 public:
561  IsInCollisionEnergyRange(double min, double max, bool reverse = false) :
562  min_energy_(min),
563  max_energy_(max),
564  reverse_(reverse)
565  {}
566 
567  inline bool operator()(const SpectrumType& s) const
568  {
569  // leave non-fragmentation spectra untouched
570  if (s.getMSLevel() == 1) return false;
571 
572  bool isIn = false;
573  bool hasCollisionEnergy = false;
574  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
575  {
576  if (it->metaValueExists("collision energy"))
577  {
578  hasCollisionEnergy = true;
579  double cE = it->getMetaValue("collision energy");
580  isIn |= !(cE > max_energy_ || cE < min_energy_);
581  }
582  }
583 
584  // we accept all spectra that have no collision energy value
585  if (!hasCollisionEnergy) return false;
586 
587  if (reverse_) return !isIn;
588  else return isIn;
589  }
590 
591 private:
593  bool reverse_;
594  };
595 
602  template <class SpectrumType>
604  {
605 
606 public:
614  IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse = false) :
615  min_size_(min_size),
616  max_size_(max_size),
617  reverse_(reverse)
618  {}
619 
620  inline bool operator()(const SpectrumType& s) const
621  {
622  // leave non-fragmentation spectra untouched
623  if (s.getMSLevel() == 1) return false;
624 
625  bool isIn = false;
626  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
627  {
628  const double isolationWindowSize = it->getIsolationWindowUpperOffset() + it->getIsolationWindowLowerOffset();
629  isIn |= !(isolationWindowSize > max_size_ || isolationWindowSize < min_size_);
630  }
631 
632  if (reverse_) return !isIn;
633  else return isIn;
634  }
635 
636 private:
638  bool reverse_;
639  };
640 
647  template <class SpectrumType>
649  {
650 
651 public:
658  IsInIsolationWindow(std::vector<double> vec_mz, bool reverse = false) :
659  vec_mz_(vec_mz),
660  reverse_(reverse)
661  {
662  std::sort(vec_mz_.begin(), vec_mz_.end());
663  }
664 
665  inline bool operator()(const SpectrumType& s) const
666  {
667  // leave non-fragmentation spectra untouched
668  if (s.getMSLevel() == 1) return false;
669 
670  bool isIn = false;
671  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
672  {
673  if (it->getIsolationWindowLowerOffset() == 0 || it->getIsolationWindowUpperOffset() == 0)
674  {
675  OPENMS_LOG_WARN << "IsInIsolationWindow(): Lower/Upper Offset for Precursor Isolation Window is Zero! " <<
676  "Filtering will probably be too strict (unless you hit the exact precursor m/z)!" << std::endl;
677  }
678  const double lower_mz = it->getMZ() - it->getIsolationWindowLowerOffset();
679  std::vector<double>::const_iterator it_mz = std::lower_bound(vec_mz_.begin(), vec_mz_.end(), lower_mz);
680  if (it_mz != vec_mz_.end()) // left side ok
681  { // right side?
682  const double upper_mz = it->getMZ() + it->getIsolationWindowUpperOffset();
683  isIn |= (*it_mz <= upper_mz);
684  }
685  }
686 
687  if (reverse_) return !isIn;
688  else return isIn;
689  }
690 
691 private:
692  std::vector<double> vec_mz_;
693  bool reverse_;
694  };
695 
696 } // namespace OpenMS
697 
#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:460
Predicate that determines if a spectrum was generated using any activation method given in the constr...
Definition: RangeUtils.h:336
HasActivationMethod(const StringList &methods, bool reverse=false)
Constructor.
Definition: RangeUtils.h:345
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:350
StringList methods_
Definition: RangeUtils.h:372
bool reverse_
Definition: RangeUtils.h:373
Predicate that determines if a class has a certain metavalue.
Definition: RangeUtils.h:97
bool operator()(const MetaContainer &s) const
Definition: RangeUtils.h:110
HasMetaValue(String metavalue, bool reverse=false)
Constructor.
Definition: RangeUtils.h:105
bool reverse_
Definition: RangeUtils.h:119
String metavalue_key_
Definition: RangeUtils.h:118
Predicate that determines if a spectrum has a certain precursor charge as given in the constructor li...
Definition: RangeUtils.h:435
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:449
HasPrecursorCharge(const IntList &charges, bool reverse=false)
Constructor.
Definition: RangeUtils.h:444
IntList charges_
Definition: RangeUtils.h:463
bool reverse_
Definition: RangeUtils.h:464
Predicate that determines if a spectrum has a certain scan mode.
Definition: RangeUtils.h:204
Int mode_
Definition: RangeUtils.h:225
HasScanMode(Int mode, bool reverse=false)
Constructor.
Definition: RangeUtils.h:213
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:218
bool reverse_
Definition: RangeUtils.h:226
Predicate that determines if a spectrum has a certain scan polarity.
Definition: RangeUtils.h:238
HasScanPolarity(Int polarity, bool reverse=false)
Constructor.
Definition: RangeUtils.h:247
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:252
Int polarity_
Definition: RangeUtils.h:259
bool reverse_
Definition: RangeUtils.h:260
Predicate that determines if a peak lies inside/outside a specific intensity range.
Definition: RangeUtils.h:516
double max_
Definition: RangeUtils.h:539
InIntensityRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:525
double min_
Definition: RangeUtils.h:539
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:531
bool reverse_
Definition: RangeUtils.h:540
Predicate that determines if a spectrum lies inside/outside a specific MS level set.
Definition: RangeUtils.h:169
InMSLevelRange(const IntList &levels, bool reverse=false)
Constructor.
Definition: RangeUtils.h:178
IntList levels_
Definition: RangeUtils.h:191
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:183
bool reverse_
Definition: RangeUtils.h:192
Predicate that determines if a peak lies inside/outside a specific m/z range.
Definition: RangeUtils.h:479
double max_
Definition: RangeUtils.h:503
InMzRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:489
double min_
Definition: RangeUtils.h:503
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:495
bool reverse_
Definition: RangeUtils.h:504
Predicate that determines if a spectrum's precursor is within a certain m/z range.
Definition: RangeUtils.h:387
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:402
double mz_right_
Definition: RangeUtils.h:420
InPrecursorMZRange(const double &mz_left, const double &mz_right, bool reverse=false)
Constructor.
Definition: RangeUtils.h:396
double mz_left_
Definition: RangeUtils.h:419
bool reverse_
Definition: RangeUtils.h:421
Predicate that determines if a spectrum lies inside/outside a specific retention time range.
Definition: RangeUtils.h:132
double max_
Definition: RangeUtils.h:156
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:148
InRTRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:142
double min_
Definition: RangeUtils.h:156
bool reverse_
Definition: RangeUtils.h:157
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:273
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:284
IsEmptySpectrum(bool reverse=false)
Constructor.
Definition: RangeUtils.h:280
bool reverse_
Definition: RangeUtils.h:291
Predicate that determines if an MSn spectrum was generated with a collision energy in the given range...
Definition: RangeUtils.h:552
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:567
IsInCollisionEnergyRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:561
double min_energy_
Definition: RangeUtils.h:592
double max_energy_
Definition: RangeUtils.h:592
bool reverse_
Definition: RangeUtils.h:593
Predicate that determines if the width of the isolation window of an MSn spectrum is in the given ran...
Definition: RangeUtils.h:604
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:620
double max_size_
Definition: RangeUtils.h:637
double min_size_
Definition: RangeUtils.h:637
IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse=false)
Constructor.
Definition: RangeUtils.h:614
bool reverse_
Definition: RangeUtils.h:638
Predicate that determines if the isolation window covers ANY of the given m/z values.
Definition: RangeUtils.h:649
IsInIsolationWindow(std::vector< double > vec_mz, bool reverse=false)
Constructor.
Definition: RangeUtils.h:658
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:665
std::vector< double > vec_mz_
Definition: RangeUtils.h:692
bool reverse_
Definition: RangeUtils.h:693
Predicate that determines if a spectrum is a zoom (enhanced resolution) spectrum.
Definition: RangeUtils.h:303
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:315
IsZoomSpectrum(bool reverse=false)
Constructor.
Definition: RangeUtils.h:311
bool reverse_
Definition: RangeUtils.h:322
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:162
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
double getRT() const
UInt getMSLevel() const
Returns the MS level.
A 2-dimensional raw data point or peak.
Definition: Peak2D.h:55
PositionType const & getPosition() const
Non-mutable access to the position.
Definition: Peak2D.h:178
IntensityType getIntensity() const
Definition: Peak2D.h:166
static const std::string NamesOfActivationMethod[SIZE_OF_ACTIVATIONMETHOD]
Names of activation methods.
Definition: Precursor.h:103
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:61
int Int
Signed integer type.
Definition: Types.h:102
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:55
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
bool find(TFinder &finder, const Pattern< TNeedle, FuzzyAC > &me, PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:886