OpenMS  2.4.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-2018.
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>
96  class HasMetaValue :
97  std::unary_function<MetaContainer, bool>
98  {
99 public:
106  HasMetaValue(String metavalue, bool reverse = false) :
107  metavalue_key_(metavalue),
108  reverse_(reverse)
109  {}
110 
111  inline bool operator()(const MetaContainer& s) const
112  {
113  bool has_meta_value = s.metaValueExists(metavalue_key_);
114  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
115  return reverse_ ^ has_meta_value;
116  }
117 
118 protected:
120  bool reverse_;
121  };
122 
123 
131  template <class SpectrumType>
132  class InRTRange :
133  std::unary_function<SpectrumType, bool>
134  {
135 public:
144  InRTRange(double min, double max, bool reverse = false) :
145  min_(min),
146  max_(max),
147  reverse_(reverse)
148  {}
149 
150  inline bool operator()(const SpectrumType& s) const
151  {
152  double tmp = s.getRT();
153  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
154  return reverse_ ^ (min_ <= tmp && tmp <= max_);
155  }
156 
157 protected:
158  double min_, max_;
159  bool reverse_;
160  };
161 
169  template <class SpectrumType>
171  std::unary_function<SpectrumType, bool>
172  {
173 public:
181  InMSLevelRange(const IntList& levels, bool reverse = false) :
182  levels_(levels),
183  reverse_(reverse)
184  {}
185 
186  inline bool operator()(const SpectrumType& s) const
187  {
188  Int tmp = s.getMSLevel();
189  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
190  return reverse_ ^ std::find(levels_.begin(), levels_.end(), tmp) != levels_.end();
191  }
192 
193 protected:
195  bool reverse_;
196  };
197 
205  template <class SpectrumType>
206  class HasScanMode :
207  std::unary_function<SpectrumType, bool>
208  {
209 public:
217  HasScanMode(Int mode, bool reverse = false) :
218  mode_(mode),
219  reverse_(reverse)
220  {}
221 
222  inline bool operator()(const SpectrumType& s) const
223  {
224  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
225  return reverse_ ^ (s.getInstrumentSettings().getScanMode() == mode_);
226  }
227 
228 protected:
230  bool reverse_;
231  };
232 
240  template <class SpectrumType>
242  std::unary_function<SpectrumType, bool>
243  {
244 public:
252  HasScanPolarity(Int polarity, bool reverse = false) :
253  polarity_(polarity),
254  reverse_(reverse)
255  {}
256 
257  inline bool operator()(const SpectrumType& s) const
258  {
259  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
261  }
262 
263 protected:
265  bool reverse_;
266  };
267 
268 
276  template <class SpectrumType>
278  std::unary_function<SpectrumType, bool>
279  {
280 public:
286  explicit IsEmptySpectrum(bool reverse = false) :
287  reverse_(reverse)
288  {}
289 
290  inline bool operator()(const SpectrumType& s) const
291  {
292  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
293  return reverse_ ^ s.empty();
294  }
295 
296 protected:
297  bool reverse_;
298  };
299 
307  template <class SpectrumType>
309  std::unary_function<SpectrumType, bool>
310  {
311 public:
318  explicit IsZoomSpectrum(bool reverse = false) :
319  reverse_(reverse)
320  {}
321 
322  inline bool operator()(const SpectrumType& s) const
323  {
324  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
326  }
327 
328 protected:
329  bool reverse_;
330  };
331 
332 
341  template <class SpectrumType>
343  std::unary_function<SpectrumType, bool>
344  {
345 public:
353  HasActivationMethod(const StringList& methods, bool reverse = false) :
354  methods_(methods),
355  reverse_(reverse)
356  {}
357 
358  inline bool operator()(const SpectrumType& s) const
359  {
360  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
361  {
362  for (std::set<Precursor::ActivationMethod>::const_iterator it_a = it->getActivationMethods().begin();
363  it_a != it->getActivationMethods().end();
364  ++it_a)
365  {
367  {
368  // found matching activation method
369  if (reverse_) return false;
370  else return true;
371  }
372  }
373  }
374 
375  if (reverse_) return true;
376  else return false;
377  }
378 
379 protected:
381  bool reverse_;
382  };
383 
393  template <class SpectrumType>
395  std::unary_function<SpectrumType, bool>
396  {
397 public:
405  InPrecursorMZRange(const double& mz_left, const double& mz_right, bool reverse = false) :
406  mz_left_(mz_left),
407  mz_right_(mz_right),
408  reverse_(reverse)
409  {}
410 
411  inline bool operator()(const SpectrumType& s) const
412  {
413  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
414  {
415  //std::cerr << mz_left_ << " " << mz_right_ << " " << it->getMZ() << "\n";
416  if (!(mz_left_ <= it->getMZ() && it->getMZ() <= mz_right_))
417  { // found PC outside of allowed window
418  if (reverse_) return true;
419  else return false;
420  }
421  }
422 
423  if (reverse_) return false;
424  else return true;
425  }
426 
427 protected:
428  double mz_left_;
429  double mz_right_;
430  bool reverse_;
431  };
432 
433 
442  template <class SpectrumType>
444  std::unary_function<SpectrumType, bool>
445  {
446 public:
454  HasPrecursorCharge(const IntList& charges, bool reverse = false) :
455  charges_(charges),
456  reverse_(reverse)
457  {}
458 
459  inline bool operator()(const SpectrumType& s) const
460  {
461  bool match = false;
462  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
463  {
464  Int tmp = it->getCharge();
465  match = match || (std::find(charges_.begin(), charges_.end(), tmp) != charges_.end());
466  }
467 
468  if (reverse_) return !match;
469  else return match;
470  }
471 
472 protected:
474  bool reverse_;
475  };
476 
477 
487  template <class PeakType>
488  class InMzRange :
489  std::unary_function<PeakType, bool>
490  {
491 public:
500  InMzRange(double min, double max, bool reverse = false) :
501  min_(min),
502  max_(max),
503  reverse_(reverse)
504  {}
505 
506  inline bool operator()(const PeakType& p) const
507  {
508  double tmp = p.getPosition()[0];
509  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
510  return reverse_ ^ (min_ <= tmp && tmp <= max_);
511  }
512 
513 protected:
514  double min_, max_;
515  bool reverse_;
516  };
517 
525  template <class PeakType>
527  std::unary_function<PeakType, bool>
528  {
529 public:
537  InIntensityRange(double min, double max, bool reverse = false) :
538  min_(min),
539  max_(max),
540  reverse_(reverse)
541  {}
542 
543  inline bool operator()(const PeakType& p) const
544  {
545  double tmp = p.getIntensity();
546  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
547  return reverse_ ^ (min_ <= tmp && tmp <= max_);
548  }
549 
550 protected:
551  double min_, max_;
552  bool reverse_;
553  };
554 
562  template <class SpectrumType>
564  std::unary_function<SpectrumType, bool>
565  {
566 public:
574  IsInCollisionEnergyRange(double min, double max, bool reverse = false) :
575  min_energy_(min),
576  max_energy_(max),
577  reverse_(reverse)
578  {}
579 
580  inline bool operator()(const SpectrumType& s) const
581  {
582  // leave non-fragmentation spectra untouched
583  if (s.getMSLevel() == 1) return false;
584 
585  bool isIn = false;
586  bool hasCollisionEnergy = false;
587  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
588  {
589  if (it->metaValueExists("collision energy"))
590  {
591  hasCollisionEnergy = true;
592  double cE = it->getMetaValue("collision energy");
593  isIn |= !(cE > max_energy_ || cE < min_energy_);
594  }
595  }
596 
597  // we accept all spectra that have no collision energy value
598  if (!hasCollisionEnergy) return false;
599 
600  if (reverse_) return !isIn;
601  else return isIn;
602  }
603 
604 private:
606  bool reverse_;
607  };
608 
615  template <class SpectrumType>
617  std::unary_function<SpectrumType, bool>
618  {
619 
620 public:
628  IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse = false) :
629  min_size_(min_size),
630  max_size_(max_size),
631  reverse_(reverse)
632  {}
633 
634  inline bool operator()(const SpectrumType& s) const
635  {
636  // leave non-fragmentation spectra untouched
637  if (s.getMSLevel() == 1) return false;
638 
639  bool isIn = false;
640  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
641  {
642  const double isolationWindowSize = it->getIsolationWindowUpperOffset() + it->getIsolationWindowLowerOffset();
643  isIn |= !(isolationWindowSize > max_size_ || isolationWindowSize < min_size_);
644  }
645 
646  if (reverse_) return !isIn;
647  else return isIn;
648  }
649 
650 private:
652  bool reverse_;
653  };
654 
661  template <class SpectrumType>
663  std::unary_function<SpectrumType, bool>
664  {
665 
666 public:
673  IsInIsolationWindow(std::vector<double> vec_mz, bool reverse = false) :
674  vec_mz_(vec_mz),
675  reverse_(reverse)
676  {
677  std::sort(vec_mz_.begin(), vec_mz_.end());
678  }
679 
680  inline bool operator()(const SpectrumType& s) const
681  {
682  // leave non-fragmentation spectra untouched
683  if (s.getMSLevel() == 1) return false;
684 
685  bool isIn = false;
686  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
687  {
688  if (it->getIsolationWindowLowerOffset() == 0 || it->getIsolationWindowUpperOffset() == 0)
689  {
690  LOG_WARN << "IsInIsolationWindow(): Lower/Upper Offset for Precursor Isolation Window is Zero! " <<
691  "Filtering will probably be too strict (unless you hit the exact precursor m/z)!" << std::endl;
692  }
693  const double lower_mz = it->getMZ() - it->getIsolationWindowLowerOffset();
694  std::vector<double>::const_iterator it_mz = std::lower_bound(vec_mz_.begin(), vec_mz_.end(), lower_mz);
695  if (it_mz != vec_mz_.end()) // left side ok
696  { // right side?
697  const double upper_mz = it->getMZ() + it->getIsolationWindowUpperOffset();
698  isIn |= (*it_mz <= upper_mz);
699  }
700  }
701 
702  if (reverse_) return !isIn;
703  else return isIn;
704  }
705 
706 private:
707  std::vector<double> vec_mz_;
708  bool reverse_;
709  };
710 
711 } // namespace OpenMS
712 
IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse=false)
Constructor.
Definition: RangeUtils.h:628
IntList levels_
Definition: RangeUtils.h:194
Predicate that determines if a spectrum lies inside/outside a specific retention time range...
Definition: RangeUtils.h:132
double min_
Definition: RangeUtils.h:514
bool reverse_
Definition: RangeUtils.h:606
InMSLevelRange(const IntList &levels, bool reverse=false)
Constructor.
Definition: RangeUtils.h:181
bool reverse_
Definition: RangeUtils.h:297
Predicate that determines if a class has a certain metavalue.
Definition: RangeUtils.h:96
A more convenient string class.
Definition: String.h:57
A 2-dimensional raw data point or peak.
Definition: Peak2D.h:54
HasScanPolarity(Int polarity, bool reverse=false)
Constructor.
Definition: RangeUtils.h:252
double max_size_
Definition: RangeUtils.h:651
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:322
String metavalue_key_
Definition: RangeUtils.h:119
IsInCollisionEnergyRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:574
std::vector< double > vec_mz_
Definition: RangeUtils.h:707
Predicate that determines if a spectrum has a certain scan mode.
Definition: RangeUtils.h:206
bool reverse_
Definition: RangeUtils.h:381
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:150
ScanMode getScanMode() const
returns the scan mode
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:358
bool reverse_
Definition: RangeUtils.h:230
const InstrumentSettings & getInstrumentSettings() const
returns a const reference to the instrument settings of the current spectrum
bool reverse_
Definition: RangeUtils.h:652
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:543
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:58
bool getZoomScan() const
return if this scan is a zoom (enhanced resolution) scan
double min_energy_
Definition: RangeUtils.h:605
double max_energy_
Definition: RangeUtils.h:605
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
IntensityType getIntensity() const
Definition: Peak2D.h:166
static const std::string NamesOfActivationMethod[SIZE_OF_ACTIVATIONMETHOD]
Names of activation methods.
Definition: Precursor.h:84
Predicate that determines if the isolation window covers ANY of the given m/z values.
Definition: RangeUtils.h:662
InMzRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:500
bool find(TFinder &finder, const Pattern< TNeedle, FuzzyAC > &me, PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:884
#define LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged...
Definition: LogStream.h:450
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:459
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:290
bool reverse_
Definition: RangeUtils.h:430
InRTRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:144
HasScanMode(Int mode, bool reverse=false)
Constructor.
Definition: RangeUtils.h:217
IsInIsolationWindow(std::vector< double > vec_mz, bool reverse=false)
Constructor.
Definition: RangeUtils.h:673
IsZoomSpectrum(bool reverse=false)
Constructor.
Definition: RangeUtils.h:318
Predicate that determines if a spectrum lies inside/outside a specific MS level set.
Definition: RangeUtils.h:170
The representation of a 1D spectrum.
Definition: MSSpectrum.h:66
double min_size_
Definition: RangeUtils.h:651
IsEmptySpectrum(bool reverse=false)
Constructor.
Definition: RangeUtils.h:286
bool reverse_
Definition: RangeUtils.h:159
double min_
Definition: RangeUtils.h:551
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:506
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:634
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:411
bool reverse_
Definition: RangeUtils.h:120
bool reverse_
Definition: RangeUtils.h:708
double max_
Definition: RangeUtils.h:514
HasMetaValue(String metavalue, bool reverse=false)
Constructor.
Definition: RangeUtils.h:106
double max_
Definition: RangeUtils.h:551
Predicate that determines if a spectrum has a certain precursor charge as given in the constructor li...
Definition: RangeUtils.h:443
bool reverse_
Definition: RangeUtils.h:265
bool reverse_
Definition: RangeUtils.h:329
Predicate that determines if a peak lies inside/outside a specific intensity range.
Definition: RangeUtils.h:526
InPrecursorMZRange(const double &mz_left, const double &mz_right, bool reverse=false)
Constructor.
Definition: RangeUtils.h:405
bool reverse_
Definition: RangeUtils.h:195
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:580
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
Predicate that determines if a spectrum has a certain scan polarity.
Definition: RangeUtils.h:241
Int mode_
Definition: RangeUtils.h:229
PositionType const & getPosition() const
Non-mutable access to the position.
Definition: Peak2D.h:178
bool reverse_
Definition: RangeUtils.h:552
IonSource::Polarity getPolarity() const
returns the polarity
double mz_right_
Definition: RangeUtils.h:429
IntList charges_
Definition: RangeUtils.h:473
StringList methods_
Definition: RangeUtils.h:380
double max_
Definition: RangeUtils.h:158
InIntensityRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:537
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:222
HasActivationMethod(const StringList &methods, bool reverse=false)
Constructor.
Definition: RangeUtils.h:353
UInt getMSLevel() const
Returns the MS level.
Predicate that determines if the width of the isolation window of an MSn spectrum is in the given ran...
Definition: RangeUtils.h:616
bool reverse_
Definition: RangeUtils.h:474
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:186
HasPrecursorCharge(const IntList &charges, bool reverse=false)
Constructor.
Definition: RangeUtils.h:454
Predicate that determines if a peak lies inside/outside a specific m/z range.
Definition: RangeUtils.h:488
bool operator()(const MetaContainer &s) const
Definition: RangeUtils.h:111
Predicate that determines if a spectrum&#39;s precursor is within a certain m/z range.
Definition: RangeUtils.h:394
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:257
double getRT() const
bool reverse_
Definition: RangeUtils.h:515
Predicate that determines if a spectrum is a zoom (enhanced resolution) spectrum. ...
Definition: RangeUtils.h:308
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:149
double mz_left_
Definition: RangeUtils.h:428
int Int
Signed integer type.
Definition: Types.h:102
double min_
Definition: RangeUtils.h:158
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:680
Predicate that determines if a spectrum was generated using any activation method given in the constr...
Definition: RangeUtils.h:342
Predicate that determines if an MSn spectrum was generated with a collision energy in the given range...
Definition: RangeUtils.h:563
Predicate that determines if a spectrum is empty.
Definition: RangeUtils.h:277
Int polarity_
Definition: RangeUtils.h:264