OpenMS  2.8.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),
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),
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),
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),
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),
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) :
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) :
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),
348  {}
349 
350  inline bool operator()(const SpectrumType& s) const
351  {
352  for (const Precursor& p : s.getPrecursors())
353  {
354  for (const Precursor::ActivationMethod am : p.getActivationMethods())
355  {
357  {
358  // found matching activation method
359  if (reverse_) return false;
360  else return true;
361  }
362  }
363  }
364 
365  return reverse_;
366  }
367 
368 protected:
370  bool reverse_;
371  };
372 
382  template <class SpectrumType>
384  {
385 public:
393  InPrecursorMZRange(const double& mz_left, const double& mz_right, bool reverse = false) :
394  mz_left_(mz_left),
395  mz_right_(mz_right),
397  {}
398 
399  inline bool operator()(const SpectrumType& s) const
400  {
401  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
402  {
403  //std::cerr << mz_left_ << " " << mz_right_ << " " << it->getMZ() << "\n";
404  if (!(mz_left_ <= it->getMZ() && it->getMZ() <= mz_right_))
405  { // found PC outside of allowed window
406  if (reverse_) return true;
407  else return false;
408  }
409  }
410 
411  if (reverse_) return false;
412  else return true;
413  }
414 
415 protected:
416  double mz_left_;
417  double mz_right_;
418  bool reverse_;
419  };
420 
421 
430  template <class SpectrumType>
432  {
433 public:
441  HasPrecursorCharge(const IntList& charges, bool reverse = false) :
442  charges_(charges),
444  {}
445 
446  inline bool operator()(const SpectrumType& s) const
447  {
448  bool match = false;
449  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
450  {
451  Int tmp = it->getCharge();
452  match = match || (std::find(charges_.begin(), charges_.end(), tmp) != charges_.end());
453  }
454 
455  if (reverse_) return !match;
456  else return match;
457  }
458 
459 protected:
461  bool reverse_;
462  };
463 
464 
474  template <class PeakType>
475  class InMzRange
476  {
477 public:
486  InMzRange(double min, double max, bool reverse = false) :
487  min_(min),
488  max_(max),
490  {}
491 
492  inline bool operator()(const PeakType& p) const
493  {
494  double tmp = p.getPosition()[0];
495  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
496  return reverse_ ^ (min_ <= tmp && tmp <= max_);
497  }
498 
499 protected:
500  double min_, max_;
501  bool reverse_;
502  };
503 
511  template <class PeakType>
513  {
514 public:
522  InIntensityRange(double min, double max, bool reverse = false) :
523  min_(min),
524  max_(max),
526  {}
527 
528  inline bool operator()(const PeakType& p) const
529  {
530  double tmp = p.getIntensity();
531  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
532  return reverse_ ^ (min_ <= tmp && tmp <= max_);
533  }
534 
535 protected:
536  double min_, max_;
537  bool reverse_;
538  };
539 
547  template <class SpectrumType>
549  {
550 public:
558  IsInCollisionEnergyRange(double min, double max, bool reverse = false) :
559  min_energy_(min),
560  max_energy_(max),
562  {}
563 
564  inline bool operator()(const SpectrumType& s) const
565  {
566  // leave non-fragmentation spectra untouched
567  if (s.getMSLevel() == 1) return false;
568 
569  bool isIn = false;
570  bool hasCollisionEnergy = false;
571  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
572  {
573  if (it->metaValueExists("collision energy"))
574  {
575  hasCollisionEnergy = true;
576  double cE = it->getMetaValue("collision energy");
577  isIn |= !(cE > max_energy_ || cE < min_energy_);
578  }
579  }
580 
581  // we accept all spectra that have no collision energy value
582  if (!hasCollisionEnergy) return false;
583 
584  if (reverse_) return !isIn;
585  else return isIn;
586  }
587 
588 private:
590  bool reverse_;
591  };
592 
599  template <class SpectrumType>
601  {
602 
603 public:
611  IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse = false) :
612  min_size_(min_size),
613  max_size_(max_size),
615  {}
616 
617  inline bool operator()(const SpectrumType& s) const
618  {
619  // leave non-fragmentation spectra untouched
620  if (s.getMSLevel() == 1) return false;
621 
622  bool isIn = false;
623  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
624  {
625  const double isolationWindowSize = it->getIsolationWindowUpperOffset() + it->getIsolationWindowLowerOffset();
626  isIn |= !(isolationWindowSize > max_size_ || isolationWindowSize < min_size_);
627  }
628 
629  if (reverse_) return !isIn;
630  else return isIn;
631  }
632 
633 private:
635  bool reverse_;
636  };
637 
644  template <class SpectrumType>
646  {
647 
648 public:
655  IsInIsolationWindow(std::vector<double> vec_mz, bool reverse = false) :
656  vec_mz_(vec_mz),
658  {
659  std::sort(vec_mz_.begin(), vec_mz_.end());
660  }
661 
662  inline bool operator()(const SpectrumType& s) const
663  {
664  // leave non-fragmentation spectra untouched
665  if (s.getMSLevel() == 1) return false;
666 
667  bool isIn = false;
668  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
669  {
670  if (it->getIsolationWindowLowerOffset() == 0 || it->getIsolationWindowUpperOffset() == 0)
671  {
672  OPENMS_LOG_WARN << "IsInIsolationWindow(): Lower/Upper Offset for Precursor Isolation Window is Zero! " <<
673  "Filtering will probably be too strict (unless you hit the exact precursor m/z)!" << std::endl;
674  }
675  const double lower_mz = it->getMZ() - it->getIsolationWindowLowerOffset();
676  std::vector<double>::const_iterator it_mz = std::lower_bound(vec_mz_.begin(), vec_mz_.end(), lower_mz);
677  if (it_mz != vec_mz_.end()) // left side ok
678  { // right side?
679  const double upper_mz = it->getMZ() + it->getIsolationWindowUpperOffset();
680  isIn |= (*it_mz <= upper_mz);
681  }
682  }
683 
684  if (reverse_) return !isIn;
685  else return isIn;
686  }
687 
688 private:
689  std::vector<double> vec_mz_;
690  bool reverse_;
691  };
692 
693 } // namespace OpenMS
694 
#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:369
bool reverse_
Definition: RangeUtils.h:370
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:432
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:446
HasPrecursorCharge(const IntList &charges, bool reverse=false)
Constructor.
Definition: RangeUtils.h:441
IntList charges_
Definition: RangeUtils.h:460
bool reverse_
Definition: RangeUtils.h:461
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:513
double max_
Definition: RangeUtils.h:536
InIntensityRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:522
double min_
Definition: RangeUtils.h:536
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:528
bool reverse_
Definition: RangeUtils.h:537
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:476
double max_
Definition: RangeUtils.h:500
InMzRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:486
double min_
Definition: RangeUtils.h:500
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:492
bool reverse_
Definition: RangeUtils.h:501
Predicate that determines if a spectrum's precursor is within a certain m/z range.
Definition: RangeUtils.h:384
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:399
double mz_right_
Definition: RangeUtils.h:417
InPrecursorMZRange(const double &mz_left, const double &mz_right, bool reverse=false)
Constructor.
Definition: RangeUtils.h:393
double mz_left_
Definition: RangeUtils.h:416
bool reverse_
Definition: RangeUtils.h:418
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:549
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:564
IsInCollisionEnergyRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:558
double min_energy_
Definition: RangeUtils.h:589
double max_energy_
Definition: RangeUtils.h:589
bool reverse_
Definition: RangeUtils.h:590
Predicate that determines if the width of the isolation window of an MSn spectrum is in the given ran...
Definition: RangeUtils.h:601
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:617
double max_size_
Definition: RangeUtils.h:634
double min_size_
Definition: RangeUtils.h:634
IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse=false)
Constructor.
Definition: RangeUtils.h:611
bool reverse_
Definition: RangeUtils.h:635
Predicate that determines if the isolation window covers ANY of the given m/z values.
Definition: RangeUtils.h:646
IsInIsolationWindow(std::vector< double > vec_mz, bool reverse=false)
Constructor.
Definition: RangeUtils.h:655
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:662
std::vector< double > vec_mz_
Definition: RangeUtils.h:689
bool reverse_
Definition: RangeUtils.h:690
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:70
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
Precursor meta information.
Definition: Precursor.h:61
static const std::string NamesOfActivationMethod[SIZE_OF_ACTIVATIONMETHOD]
Names of activation methods.
Definition: Precursor.h:106
ActivationMethod
Method of activation.
Definition: Precursor.h:85
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:60
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
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:350
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47