Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-2017.
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 #ifndef OPENMS_KERNEL_RANGEUTILS_H
36 #define OPENMS_KERNEL_RANGEUTILS_H
37 
38 #include <functional>
39 #include <algorithm>
40 #include <vector>
41 #include <OpenMS/CONCEPT/Types.h>
45 
46 namespace OpenMS
47 {
96  template <class MetaContainer>
97  class HasMetaValue :
98  std::unary_function<MetaContainer, bool>
99  {
100 public:
107  HasMetaValue(String metavalue, bool reverse = false) :
108  metavalue_key_(metavalue),
109  reverse_(reverse)
110  {}
111 
112  inline bool operator()(const MetaContainer& s) const
113  {
114  bool has_meta_value = s.metaValueExists(metavalue_key_);
115  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
116  return reverse_ ^ has_meta_value;
117  }
118 
119 protected:
121  bool reverse_;
122  };
123 
124 
132  template <class SpectrumType>
133  class InRTRange :
134  std::unary_function<SpectrumType, bool>
135  {
136 public:
145  InRTRange(double min, double max, bool reverse = false) :
146  min_(min),
147  max_(max),
148  reverse_(reverse)
149  {}
150 
151  inline bool operator()(const SpectrumType& s) const
152  {
153  double tmp = s.getRT();
154  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
155  return reverse_ ^ (min_ <= tmp && tmp <= max_);
156  }
157 
158 protected:
159  double min_, max_;
160  bool reverse_;
161  };
162 
170  template <class SpectrumType>
172  std::unary_function<SpectrumType, bool>
173  {
174 public:
182  InMSLevelRange(const IntList& levels, bool reverse = false) :
183  levels_(levels),
184  reverse_(reverse)
185  {}
186 
187  inline bool operator()(const SpectrumType& s) const
188  {
189  Int tmp = s.getMSLevel();
190  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
191  return reverse_ ^ std::find(levels_.begin(), levels_.end(), tmp) != levels_.end();
192  }
193 
194 protected:
196  bool reverse_;
197  };
198 
206  template <class SpectrumType>
207  class HasScanMode :
208  std::unary_function<SpectrumType, bool>
209  {
210 public:
218  HasScanMode(Int mode, bool reverse = false) :
219  mode_(mode),
220  reverse_(reverse)
221  {}
222 
223  inline bool operator()(const SpectrumType& s) const
224  {
225  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
226  return reverse_ ^ (s.getInstrumentSettings().getScanMode() == mode_);
227  }
228 
229 protected:
231  bool reverse_;
232  };
233 
241  template <class SpectrumType>
243  std::unary_function<SpectrumType, bool>
244  {
245 public:
253  HasScanPolarity(Int polarity, bool reverse = false) :
254  polarity_(polarity),
255  reverse_(reverse)
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.getInstrumentSettings().getPolarity() == polarity_);
262  }
263 
264 protected:
266  bool reverse_;
267  };
268 
269 
277  template <class SpectrumType>
279  std::unary_function<SpectrumType, bool>
280  {
281 public:
287  explicit IsEmptySpectrum(bool reverse = false) :
288  reverse_(reverse)
289  {}
290 
291  inline bool operator()(const SpectrumType& s) const
292  {
293  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
294  return reverse_ ^ s.empty();
295  }
296 
297 protected:
298  bool reverse_;
299  };
300 
308  template <class SpectrumType>
310  std::unary_function<SpectrumType, bool>
311  {
312 public:
319  explicit IsZoomSpectrum(bool reverse = false) :
320  reverse_(reverse)
321  {}
322 
323  inline bool operator()(const SpectrumType& s) const
324  {
325  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
327  }
328 
329 protected:
330  bool reverse_;
331  };
332 
333 
342  template <class SpectrumType>
344  std::unary_function<SpectrumType, bool>
345  {
346 public:
354  HasActivationMethod(const StringList& methods, bool reverse = false) :
355  methods_(methods),
356  reverse_(reverse)
357  {}
358 
359  inline bool operator()(const SpectrumType& s) const
360  {
361  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
362  {
363  for (std::set<Precursor::ActivationMethod>::const_iterator it_a = it->getActivationMethods().begin();
364  it_a != it->getActivationMethods().end();
365  ++it_a)
366  {
368  {
369  // found matching activation method
370  if (reverse_) return false;
371  else return true;
372  }
373  }
374  }
375 
376  if (reverse_) return true;
377  else return false;
378  }
379 
380 protected:
382  bool reverse_;
383  };
384 
394  template <class SpectrumType>
396  std::unary_function<SpectrumType, bool>
397  {
398 public:
406  InPrecursorMZRange(const double& mz_left, const double& mz_right, bool reverse = false) :
407  mz_left_(mz_left),
408  mz_right_(mz_right),
409  reverse_(reverse)
410  {}
411 
412  inline bool operator()(const SpectrumType& s) const
413  {
414  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
415  {
416  //std::cerr << mz_left_ << " " << mz_right_ << " " << it->getMZ() << "\n";
417  if (!(mz_left_ <= it->getMZ() && it->getMZ() <= mz_right_))
418  { // found PC outside of allowed window
419  if (reverse_) return true;
420  else return false;
421  }
422  }
423 
424  if (reverse_) return false;
425  else return true;
426  }
427 
428 protected:
429  double mz_left_;
430  double mz_right_;
431  bool reverse_;
432  };
433 
434 
443  template <class SpectrumType>
445  std::unary_function<SpectrumType, bool>
446  {
447 public:
455  HasPrecursorCharge(const IntList& charges, bool reverse = false) :
456  charges_(charges),
457  reverse_(reverse)
458  {}
459 
460  inline bool operator()(const SpectrumType& s) const
461  {
462  bool match = false;
463  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
464  {
465  Int tmp = it->getCharge();
466  match = match || (std::find(charges_.begin(), charges_.end(), tmp) != charges_.end());
467  }
468 
469  if (reverse_) return !match;
470  else return match;
471  }
472 
473 protected:
475  bool reverse_;
476  };
477 
478 
488  template <class PeakType>
489  class InMzRange :
490  std::unary_function<PeakType, bool>
491  {
492 public:
501  InMzRange(double min, double max, bool reverse = false) :
502  min_(min),
503  max_(max),
504  reverse_(reverse)
505  {}
506 
507  inline bool operator()(const PeakType& p) const
508  {
509  double tmp = p.getPosition()[0];
510  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
511  return reverse_ ^ (min_ <= tmp && tmp <= max_);
512  }
513 
514 protected:
515  double min_, max_;
516  bool reverse_;
517  };
518 
526  template <class PeakType>
528  std::unary_function<PeakType, bool>
529  {
530 public:
538  InIntensityRange(double min, double max, bool reverse = false) :
539  min_(min),
540  max_(max),
541  reverse_(reverse)
542  {}
543 
544  inline bool operator()(const PeakType& p) const
545  {
546  double tmp = p.getIntensity();
547  // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
548  return reverse_ ^ (min_ <= tmp && tmp <= max_);
549  }
550 
551 protected:
552  double min_, max_;
553  bool reverse_;
554  };
555 
563  template <class SpectrumType>
565  std::unary_function<SpectrumType, bool>
566  {
567 public:
575  IsInCollisionEnergyRange(double min, double max, bool reverse = false) :
576  min_energy_(min),
577  max_energy_(max),
578  reverse_(reverse)
579  {}
580 
581  inline bool operator()(const SpectrumType& s) const
582  {
583  // leave non-fragmentation spectra untouched
584  if (s.getMSLevel() == 1) return false;
585 
586  bool isIn = false;
587  bool hasCollisionEnergy = false;
588  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
589  {
590  if (it->metaValueExists("collision energy"))
591  {
592  hasCollisionEnergy = true;
593  double cE = it->getMetaValue("collision energy");
594  isIn |= !(cE > max_energy_ || cE < min_energy_);
595  }
596  }
597 
598  // we accept all spectra that have no collision energy value
599  if (!hasCollisionEnergy) return false;
600 
601  if (reverse_) return !isIn;
602  else return isIn;
603  }
604 
605 private:
606  double min_energy_, max_energy_;
607  bool reverse_;
608  };
609 
616  template <class SpectrumType>
618  std::unary_function<SpectrumType, bool>
619  {
620 
621 public:
629  IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse = false) :
630  min_size_(min_size),
631  max_size_(max_size),
632  reverse_(reverse)
633  {}
634 
635  inline bool operator()(const SpectrumType& s) const
636  {
637  // leave non-fragmentation spectra untouched
638  if (s.getMSLevel() == 1) return false;
639 
640  bool isIn = false;
641  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
642  {
643  const double isolationWindowSize = it->getIsolationWindowUpperOffset() + it->getIsolationWindowLowerOffset();
644  isIn |= !(isolationWindowSize > max_size_ || isolationWindowSize < min_size_);
645  }
646 
647  if (reverse_) return !isIn;
648  else return isIn;
649  }
650 
651 private:
652  double min_size_, max_size_;
653  bool reverse_;
654  };
655 
662  template <class SpectrumType>
664  std::unary_function<SpectrumType, bool>
665  {
666 
667 public:
674  IsInIsolationWindow(std::vector<double> vec_mz, bool reverse = false) :
675  vec_mz_(vec_mz),
676  reverse_(reverse)
677  {
678  std::sort(vec_mz_.begin(), vec_mz_.end());
679  }
680 
681  inline bool operator()(const SpectrumType& s) const
682  {
683  // leave non-fragmentation spectra untouched
684  if (s.getMSLevel() == 1) return false;
685 
686  bool isIn = false;
687  for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
688  {
689  if (it->getIsolationWindowLowerOffset() == 0 || it->getIsolationWindowLowerOffset() == 0)
690  {
691  LOG_WARN << "IsInIsolationWindow(): Lower/Upper Offset for Precursor Isolation Window is Zero! 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 
713 #endif // OPENMS_KERNEL_RANGEUTILS_H
IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse=false)
Constructor.
Definition: RangeUtils.h:629
IntList levels_
Definition: RangeUtils.h:195
Predicate that determines if a spectrum lies inside/outside a specific retention time range...
Definition: RangeUtils.h:133
double min_
Definition: RangeUtils.h:515
bool reverse_
Definition: RangeUtils.h:607
InMSLevelRange(const IntList &levels, bool reverse=false)
Constructor.
Definition: RangeUtils.h:182
bool reverse_
Definition: RangeUtils.h:298
Predicate that determines if a class has a certain metavalue.
Definition: RangeUtils.h:97
A more convenient string class.
Definition: String.h:57
A 2-dimensional raw data point or peak.
Definition: Peak2D.h:55
HasScanPolarity(Int polarity, bool reverse=false)
Constructor.
Definition: RangeUtils.h:253
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:323
String metavalue_key_
Definition: RangeUtils.h:120
IsInCollisionEnergyRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:575
std::vector< double > vec_mz_
Definition: RangeUtils.h:707
Predicate that determines if a spectrum has a certain scan mode.
Definition: RangeUtils.h:207
bool reverse_
Definition: RangeUtils.h:382
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:151
ScanMode getScanMode() const
returns the scan mode
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:359
bool reverse_
Definition: RangeUtils.h:231
const InstrumentSettings & getInstrumentSettings() const
returns a const reference to the instrument settings of the current spectrum
bool reverse_
Definition: RangeUtils.h:653
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:544
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:59
bool getZoomScan() const
return if this scan is a zoom (enhanced resolution) scan
double min_energy_
Definition: RangeUtils.h:606
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
IntensityType getIntensity() const
Definition: Peak2D.h:167
static const std::string NamesOfActivationMethod[SIZE_OF_ACTIVATIONMETHOD]
Names of activation methods.
Definition: Precursor.h:85
Predicate that determines if the isolation window covers ANY of the given m/z values.
Definition: RangeUtils.h:663
InMzRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:501
#define LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged...
Definition: LogStream.h:451
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:460
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:291
bool reverse_
Definition: RangeUtils.h:431
InRTRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:145
HasScanMode(Int mode, bool reverse=false)
Constructor.
Definition: RangeUtils.h:218
IsInIsolationWindow(std::vector< double > vec_mz, bool reverse=false)
Constructor.
Definition: RangeUtils.h:674
IsZoomSpectrum(bool reverse=false)
Constructor.
Definition: RangeUtils.h:319
Predicate that determines if a spectrum lies inside/outside a specific MS level set.
Definition: RangeUtils.h:171
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
double min_size_
Definition: RangeUtils.h:652
IsEmptySpectrum(bool reverse=false)
Constructor.
Definition: RangeUtils.h:287
bool reverse_
Definition: RangeUtils.h:160
double min_
Definition: RangeUtils.h:552
bool operator()(const PeakType &p) const
Definition: RangeUtils.h:507
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:635
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:412
bool reverse_
Definition: RangeUtils.h:121
bool reverse_
Definition: RangeUtils.h:708
HasMetaValue(String metavalue, bool reverse=false)
Constructor.
Definition: RangeUtils.h:107
Predicate that determines if a spectrum has a certain precursor charge as given in the constructor li...
Definition: RangeUtils.h:444
bool reverse_
Definition: RangeUtils.h:266
bool reverse_
Definition: RangeUtils.h:330
Predicate that determines if a peak lies inside/outside a specific intensity range.
Definition: RangeUtils.h:527
InPrecursorMZRange(const double &mz_left, const double &mz_right, bool reverse=false)
Constructor.
Definition: RangeUtils.h:406
bool reverse_
Definition: RangeUtils.h:196
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:74
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:581
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:242
Int mode_
Definition: RangeUtils.h:230
PositionType const & getPosition() const
Non-mutable access to the position.
Definition: Peak2D.h:179
bool reverse_
Definition: RangeUtils.h:553
IonSource::Polarity getPolarity() const
returns the polarity
double mz_right_
Definition: RangeUtils.h:430
IntList charges_
Definition: RangeUtils.h:474
StringList methods_
Definition: RangeUtils.h:381
InIntensityRange(double min, double max, bool reverse=false)
Constructor.
Definition: RangeUtils.h:538
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:223
HasActivationMethod(const StringList &methods, bool reverse=false)
Constructor.
Definition: RangeUtils.h:354
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:617
bool reverse_
Definition: RangeUtils.h:475
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:187
HasPrecursorCharge(const IntList &charges, bool reverse=false)
Constructor.
Definition: RangeUtils.h:455
Predicate that determines if a peak lies inside/outside a specific m/z range.
Definition: RangeUtils.h:489
bool operator()(const MetaContainer &s) const
Definition: RangeUtils.h:112
Predicate that determines if a spectrum&#39;s precursor is within a certain m/z range.
Definition: RangeUtils.h:395
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:258
double getRT() const
bool reverse_
Definition: RangeUtils.h:516
Predicate that determines if a spectrum is a zoom (enhanced resolution) spectrum. ...
Definition: RangeUtils.h:309
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:150
double mz_left_
Definition: RangeUtils.h:429
int Int
Signed integer type.
Definition: Types.h:103
double min_
Definition: RangeUtils.h:159
bool operator()(const SpectrumType &s) const
Definition: RangeUtils.h:681
Predicate that determines if a spectrum was generated using any activation method given in the constr...
Definition: RangeUtils.h:343
Predicate that determines if an MSn spectrum was generated with a collision energy in the given range...
Definition: RangeUtils.h:564
Predicate that determines if a spectrum is empty.
Definition: RangeUtils.h:278
Int polarity_
Definition: RangeUtils.h:265

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:03 using doxygen 1.8.13