OpenMS
Loading...
Searching...
No Matches
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>
19
20namespace OpenMS
21{
70 template <class MetaContainer>
72 {
73public:
80 HasMetaValue(String metavalue, bool reverse = false) :
81 metavalue_key_(metavalue),
82 reverse_(reverse)
83 {}
84
85 inline bool operator()(const MetaContainer& s) const
86 {
87 bool has_meta_value = s.metaValueExists(metavalue_key_);
88 // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
89 return reverse_ ^ has_meta_value;
90 }
91
92protected:
95 };
96
97
105 template <class SpectrumType>
107 {
108public:
117 InRTRange(double min, double max, bool reverse = false) :
118 min_(min),
119 max_(max),
120 reverse_(reverse)
121 {}
122
123 inline bool operator()(const SpectrumType& s) const
124 {
125 double tmp = s.getRT();
126 // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
127 return reverse_ ^ (min_ <= tmp && tmp <= max_);
128 }
129
130protected:
131 double min_, max_;
133 };
134
142 template <class SpectrumType>
144 {
145public:
153 InMSLevelRange(const IntList& levels, bool reverse = false) :
154 levels_(levels),
155 reverse_(reverse)
156 {}
157
158 inline bool operator()(const SpectrumType& s) const
159 {
160 Int tmp = s.getMSLevel();
161 // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
162 return reverse_ ^ (std::find(levels_.begin(), levels_.end(), tmp) != levels_.end());
163 }
164
165protected:
168 };
169
177 template <class SpectrumType>
179 {
180public:
188 HasScanMode(Int mode, bool reverse = false) :
189 mode_(mode),
190 reverse_(reverse)
191 {}
192
193 inline bool operator()(const SpectrumType& s) const
194 {
195 // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
197 }
198
199protected:
202 };
203
211 template <class SpectrumType>
213 {
214public:
222 HasScanPolarity(IonSource::Polarity polarity, bool reverse = false) :
223 polarity_(polarity),
224 reverse_(reverse)
225 {}
226
227 inline bool operator()(const SpectrumType& s) const
228 {
229 // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
231 }
232
233protected:
236 };
237
238
246 template <class SpectrumType>
248 {
249public:
255 explicit IsEmptySpectrum(bool reverse = false) :
256 reverse_(reverse)
257 {}
258
259 inline bool operator()(const SpectrumType& s) const
260 {
261 // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
262 return reverse_ ^ s.empty();
263 }
264
265protected:
267 };
268
276 template <class SpectrumType>
278 {
279public:
286 explicit IsZoomSpectrum(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
294 }
295
296protected:
298 };
299
300
309 template <class SpectrumType>
311 {
312public:
320 HasActivationMethod(const StringList& methods, bool reverse = false) :
321 methods_(methods),
322 reverse_(reverse)
323 {}
324
325 inline bool operator()(const SpectrumType& s) const
326 {
327 for (const Precursor& p : s.getPrecursors())
328 {
329 for (const Precursor::ActivationMethod am : p.getActivationMethods())
330 {
332 {
333 // found matching activation method
334 if (reverse_) return false;
335 else return true;
336 }
337 }
338 }
339
340 return reverse_;
341 }
342
343protected:
346 };
347
357 template <class SpectrumType>
359 {
360public:
368 InPrecursorMZRange(const double& mz_left, const double& mz_right, bool reverse = false) :
369 mz_left_(mz_left),
370 mz_right_(mz_right),
371 reverse_(reverse)
372 {}
373
374 inline bool operator()(const SpectrumType& s) const
375 {
376 for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
377 {
378 //std::cerr << mz_left_ << " " << mz_right_ << " " << it->getMZ() << "\n";
379 if (!(mz_left_ <= it->getMZ() && it->getMZ() <= mz_right_))
380 { // found PC outside of allowed window
381 if (reverse_) return true;
382 else return false;
383 }
384 }
385
386 if (reverse_) return false;
387 else return true;
388 }
389
390protected:
391 double mz_left_;
392 double mz_right_;
394 };
395
396
405 template <class SpectrumType>
407 {
408public:
416 HasPrecursorCharge(const IntList& charges, bool reverse = false) :
417 charges_(charges),
418 reverse_(reverse)
419 {}
420
421 inline bool operator()(const SpectrumType& s) const
422 {
423 bool match = false;
424 for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
425 {
426 Int tmp = it->getCharge();
427 match = match || (std::find(charges_.begin(), charges_.end(), tmp) != charges_.end());
428 }
429
430 if (reverse_) return !match;
431 else return match;
432 }
433
434protected:
437 };
438
439
449 template <class PeakType>
451 {
452public:
461 InMzRange(double min, double max, bool reverse = false) :
462 min_(min),
463 max_(max),
464 reverse_(reverse)
465 {}
466
467 inline bool operator()(const PeakType& p) const
468 {
469 double tmp = p.getPosition()[0];
470 // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
471 return reverse_ ^ (min_ <= tmp && tmp <= max_);
472 }
473
474protected:
475 double min_, max_;
477 };
478
486 template <class PeakType>
488 {
489public:
497 InIntensityRange(double min, double max, bool reverse = false) :
498 min_(min),
499 max_(max),
500 reverse_(reverse)
501 {}
502
503 inline bool operator()(const PeakType& p) const
504 {
505 double tmp = p.getIntensity();
506 // XOR(^): same as 'if (rev_) return !(test) else return test;' where (test) is the condition; Speed: XOR is about 25% faster in VS10
507 return reverse_ ^ (min_ <= tmp && tmp <= max_);
508 }
509
510protected:
511 double min_, max_;
513 };
514
522 template <class SpectrumType>
524 {
525public:
533 IsInCollisionEnergyRange(double min, double max, bool reverse = false) :
534 min_energy_(min),
535 max_energy_(max),
536 reverse_(reverse)
537 {}
538
539 inline bool operator()(const SpectrumType& s) const
540 {
541 // leave non-fragmentation spectra untouched
542 if (s.getMSLevel() == 1) return false;
543
544 bool isIn = false;
545 bool hasCollisionEnergy = false;
546 for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
547 {
548 if (it->metaValueExists("collision energy"))
549 {
550 hasCollisionEnergy = true;
551 double cE = it->getMetaValue("collision energy");
552 isIn |= !(cE > max_energy_ || cE < min_energy_);
553 }
554 }
555
556 // we accept all spectra that have no collision energy value
557 if (!hasCollisionEnergy) return false;
558
559 if (reverse_) return !isIn;
560 else return isIn;
561 }
562
563private:
566 };
567
574 template <class SpectrumType>
576 {
577
578public:
586 IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse = false) :
587 min_size_(min_size),
588 max_size_(max_size),
589 reverse_(reverse)
590 {}
591
592 inline bool operator()(const SpectrumType& s) const
593 {
594 // leave non-fragmentation spectra untouched
595 if (s.getMSLevel() == 1) return false;
596
597 bool isIn = false;
598 for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
599 {
600 const double isolationWindowSize = it->getIsolationWindowUpperOffset() + it->getIsolationWindowLowerOffset();
601 isIn |= !(isolationWindowSize > max_size_ || isolationWindowSize < min_size_);
602 }
603
604 if (reverse_) return !isIn;
605 else return isIn;
606 }
607
608private:
611 };
612
619 template <class SpectrumType>
621 {
622
623public:
630 IsInIsolationWindow(std::vector<double> vec_mz, bool reverse = false) :
631 vec_mz_(vec_mz),
632 reverse_(reverse)
633 {
634 std::sort(vec_mz_.begin(), vec_mz_.end());
635 }
636
637 inline bool operator()(const SpectrumType& s) const
638 {
639 // leave non-fragmentation spectra untouched
640 if (s.getMSLevel() == 1) return false;
641
642 bool isIn = false;
643 for (std::vector<Precursor>::const_iterator it = s.getPrecursors().begin(); it != s.getPrecursors().end(); ++it)
644 {
645 if (it->getIsolationWindowLowerOffset() == 0 || it->getIsolationWindowUpperOffset() == 0)
646 {
647 OPENMS_LOG_WARN << "IsInIsolationWindow(): Lower/Upper Offset for Precursor Isolation Window is Zero! " <<
648 "Filtering will probably be too strict (unless you hit the exact precursor m/z)!" << std::endl;
649 }
650 const double lower_mz = it->getMZ() - it->getIsolationWindowLowerOffset();
651 std::vector<double>::const_iterator it_mz = std::lower_bound(vec_mz_.begin(), vec_mz_.end(), lower_mz);
652 if (it_mz != vec_mz_.end()) // left side ok
653 { // right side?
654 const double upper_mz = it->getMZ() + it->getIsolationWindowUpperOffset();
655 isIn |= (*it_mz <= upper_mz);
656 }
657 }
658
659 if (reverse_) return !isIn;
660 else return isIn;
661 }
662
663private:
664 std::vector<double> vec_mz_;
666 };
667
668} // namespace OpenMS
669
#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:447
Predicate that determines if a spectrum was generated using any activation method given in the constr...
Definition RangeUtils.h:311
HasActivationMethod(const StringList &methods, bool reverse=false)
Constructor.
Definition RangeUtils.h:320
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:325
StringList methods_
Definition RangeUtils.h:344
bool reverse_
Definition RangeUtils.h:345
Predicate that determines if a class has a certain metavalue.
Definition RangeUtils.h:72
bool operator()(const MetaContainer &s) const
Definition RangeUtils.h:85
HasMetaValue(String metavalue, bool reverse=false)
Constructor.
Definition RangeUtils.h:80
bool reverse_
Definition RangeUtils.h:94
String metavalue_key_
Definition RangeUtils.h:93
Predicate that determines if a spectrum has a certain precursor charge as given in the constructor li...
Definition RangeUtils.h:407
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:421
HasPrecursorCharge(const IntList &charges, bool reverse=false)
Constructor.
Definition RangeUtils.h:416
IntList charges_
Definition RangeUtils.h:435
bool reverse_
Definition RangeUtils.h:436
Predicate that determines if a spectrum has a certain scan mode.
Definition RangeUtils.h:179
Int mode_
Definition RangeUtils.h:200
HasScanMode(Int mode, bool reverse=false)
Constructor.
Definition RangeUtils.h:188
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:193
bool reverse_
Definition RangeUtils.h:201
Predicate that determines if a spectrum has a certain scan polarity.
Definition RangeUtils.h:213
IonSource::Polarity polarity_
Definition RangeUtils.h:234
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:227
HasScanPolarity(IonSource::Polarity polarity, bool reverse=false)
Constructor.
Definition RangeUtils.h:222
bool reverse_
Definition RangeUtils.h:235
Predicate that determines if a peak lies inside/outside a specific intensity range.
Definition RangeUtils.h:488
double max_
Definition RangeUtils.h:511
InIntensityRange(double min, double max, bool reverse=false)
Constructor.
Definition RangeUtils.h:497
double min_
Definition RangeUtils.h:511
bool operator()(const PeakType &p) const
Definition RangeUtils.h:503
bool reverse_
Definition RangeUtils.h:512
Predicate that determines if a spectrum lies inside/outside a specific MS level set.
Definition RangeUtils.h:144
InMSLevelRange(const IntList &levels, bool reverse=false)
Constructor.
Definition RangeUtils.h:153
IntList levels_
Definition RangeUtils.h:166
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:158
bool reverse_
Definition RangeUtils.h:167
Predicate that determines if a peak lies inside/outside a specific m/z range.
Definition RangeUtils.h:451
double max_
Definition RangeUtils.h:475
InMzRange(double min, double max, bool reverse=false)
Constructor.
Definition RangeUtils.h:461
double min_
Definition RangeUtils.h:475
bool operator()(const PeakType &p) const
Definition RangeUtils.h:467
bool reverse_
Definition RangeUtils.h:476
Predicate that determines if a spectrum's precursor is within a certain m/z range.
Definition RangeUtils.h:359
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:374
double mz_right_
Definition RangeUtils.h:392
InPrecursorMZRange(const double &mz_left, const double &mz_right, bool reverse=false)
Constructor.
Definition RangeUtils.h:368
double mz_left_
Definition RangeUtils.h:391
bool reverse_
Definition RangeUtils.h:393
Predicate that determines if a spectrum lies inside/outside a specific retention time range.
Definition RangeUtils.h:107
double max_
Definition RangeUtils.h:131
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:123
InRTRange(double min, double max, bool reverse=false)
Constructor.
Definition RangeUtils.h:117
double min_
Definition RangeUtils.h:131
bool reverse_
Definition RangeUtils.h:132
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
Polarity
Polarity of the ion source.
Definition IonSource.h:119
Predicate that determines if a spectrum is empty.
Definition RangeUtils.h:248
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:259
IsEmptySpectrum(bool reverse=false)
Constructor.
Definition RangeUtils.h:255
bool reverse_
Definition RangeUtils.h:266
Predicate that determines if an MSn spectrum was generated with a collision energy in the given range...
Definition RangeUtils.h:524
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:539
IsInCollisionEnergyRange(double min, double max, bool reverse=false)
Constructor.
Definition RangeUtils.h:533
double min_energy_
Definition RangeUtils.h:564
double max_energy_
Definition RangeUtils.h:564
bool reverse_
Definition RangeUtils.h:565
Predicate that determines if the width of the isolation window of an MSn spectrum is in the given ran...
Definition RangeUtils.h:576
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:592
double max_size_
Definition RangeUtils.h:609
double min_size_
Definition RangeUtils.h:609
IsInIsolationWindowSizeRange(double min_size, double max_size, bool reverse=false)
Constructor.
Definition RangeUtils.h:586
bool reverse_
Definition RangeUtils.h:610
Predicate that determines if the isolation window covers ANY of the given m/z values.
Definition RangeUtils.h:621
IsInIsolationWindow(std::vector< double > vec_mz, bool reverse=false)
Constructor.
Definition RangeUtils.h:630
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:637
std::vector< double > vec_mz_
Definition RangeUtils.h:664
bool reverse_
Definition RangeUtils.h:665
Predicate that determines if a spectrum is a zoom (enhanced resolution) spectrum.
Definition RangeUtils.h:278
bool operator()(const SpectrumType &s) const
Definition RangeUtils.h:290
IsZoomSpectrum(bool reverse=false)
Constructor.
Definition RangeUtils.h:286
bool reverse_
Definition RangeUtils.h:297
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:30
PositionType const & getPosition() const
Non-mutable access to the position.
Definition Peak2D.h:155
IntensityType getIntensity() const
Definition Peak2D.h:143
Precursor meta information.
Definition Precursor.h:37
static const std::string NamesOfActivationMethod[static_cast< size_t >(ActivationMethod::SIZE_OF_ACTIVATIONMETHOD)]
Names of activation methods.
Definition Precursor.h:84
ActivationMethod
Method of activation.
Definition Precursor.h:61
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
const InstrumentSettings & getInstrumentSettings() const
returns a const reference to the instrument settings of the current spectrum
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
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19