OpenMS
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MSSpectrum.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 $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/KERNEL/Peak1D.h>
16 
17 #include <numeric>
18 
19 namespace OpenMS
20 {
21  enum class DriftTimeUnit;
40  class OPENMS_DLLAPI MSSpectrum final :
41  private std::vector<Peak1D>,
42  public RangeManagerContainer<RangeMZ, RangeIntensity, RangeMobility>,
43  public SpectrumSettings
44  {
45 public:
46 
48  struct OPENMS_DLLAPI RTLess
49  {
50  bool operator()(const MSSpectrum& a, const MSSpectrum& b) const;
51  };
53  struct OPENMS_DLLAPI IMLess {
54  bool operator()(const MSSpectrum& a, const MSSpectrum& b) const;
55  };
56 
58  struct Chunk {
61  bool is_sorted;
62  Chunk(Size p_start, Size p_end, bool p_sorted) : start(p_start), end(p_end), is_sorted(p_sorted)
63  {
64  }
65  };
66 
77  struct Chunks {
78  public:
79  Chunks(const MSSpectrum& s) : spec_(s) {}
80  void add(bool is_sorted)
81  {
82  chunks_.emplace_back((chunks_.empty() ? 0 : chunks_.back().end), spec_.size(), is_sorted);
83  }
84  std::vector<Chunk>& getChunks()
85  {
86  return chunks_;
87  }
88  private:
89  std::vector<Chunk> chunks_;
90  const MSSpectrum& spec_;
91  };
92 
94 
95  typedef OpenMS::Peak1D PeakType;
100  typedef std::vector<PeakType> ContainerType;
106  typedef std::vector<FloatDataArray> FloatDataArrays;
109  typedef std::vector<StringDataArray> StringDataArrays;
112  typedef std::vector<IntegerDataArray> IntegerDataArrays;
114 
116 
117  typedef typename ContainerType::iterator Iterator;
120  typedef typename ContainerType::const_iterator ConstIterator;
122  typedef typename ContainerType::reverse_iterator ReverseIterator;
124  typedef typename ContainerType::const_reverse_iterator ConstReverseIterator;
126 
128 
129  using ContainerType::operator[];
130  using ContainerType::begin;
131  using ContainerType::rbegin;
132  using ContainerType::end;
133  using ContainerType::rend;
134  using ContainerType::cbegin;
135  using ContainerType::cend;
136  using ContainerType::resize;
137  using ContainerType::size;
138  using ContainerType::push_back;
139  using ContainerType::emplace_back;
140  using ContainerType::pop_back;
141  using ContainerType::empty;
142  using ContainerType::front;
143  using ContainerType::back;
144  using ContainerType::reserve;
145  using ContainerType::insert;
146  using ContainerType::erase;
147  using ContainerType::swap;
148  using ContainerType::data;
149  using ContainerType::shrink_to_fit;
150 
151  using typename ContainerType::iterator;
152  using typename ContainerType::const_iterator;
153  using typename ContainerType::size_type;
154  using typename ContainerType::value_type;
155  using typename ContainerType::reference;
156  using typename ContainerType::const_reference;
157  using typename ContainerType::pointer;
158  using typename ContainerType::difference_type;
159 
161 
162 
165 
167  MSSpectrum(const std::initializer_list<Peak1D>& init);
168 
170  MSSpectrum(const MSSpectrum& source);
171 
173  MSSpectrum(MSSpectrum&&) = default;
174 
176  ~MSSpectrum() = default;
177 
179  MSSpectrum& operator=(const MSSpectrum& source);
180 
182  MSSpectrum& operator=(MSSpectrum&&) & = default;
183 
186 
188  bool operator==(const MSSpectrum& rhs) const;
189 
191  bool operator!=(const MSSpectrum& rhs) const
192  {
193  return !(operator==(rhs));
194  }
195 
196  // Docu in base class (RangeManager)
197  void updateRanges() override;
198 
202  double getRT() const;
203 
205  void setRT(double rt);
206 
215  double getDriftTime() const;
216 
220  void setDriftTime(double dt);
221 
226 
229 
234 
240  UInt getMSLevel() const;
241 
243  void setMSLevel(UInt ms_level);
244 
246  const String& getName() const;
247 
249  void setName(const String& name);
250 
252 
268 
271  {
272  return float_data_arrays_;
273  }
274 
277 
280 
283 
286 
289 
292 
296 
298 
299 
304  void sortByIntensity(bool reverse = false);
305 
312 
321 
326  void sortByPositionPresorted(const std::vector<Chunk>& chunks);
327 
329  bool isSorted() const;
330 
335  bool isSortedByIM() const;
336 
341  template<class Predicate>
342  bool isSorted(const Predicate& lambda) const
343  {
344  auto value_2_index_wrapper = [this, &lambda](const value_type& value1, const value_type& value2) {
345  // translate values into indices (this relies on no copies being made!)
346  const Size index1 = (&value1) - (&this->front());
347  const Size index2 = (&value2) - (&this->front());
348  // just make sure the pointers above are actually pointing to a Peak inside our container
349  assert(index1 < this->size());
350  assert(index2 < this->size());
351  return lambda(index1, index2);
352  };
353  return std::is_sorted(this->begin(), this->end(), value_2_index_wrapper);
354  }
355 
360  template<class Predicate>
361  void sort(const Predicate& lambda)
362  {
363  std::vector<Size> indices(this->size());
364  std::iota(indices.begin(), indices.end(), 0);
365  std::stable_sort(indices.begin(), indices.end(), lambda);
366  select(indices);
367  }
368 
370 
373 
384 
397 
411  Int findNearest(CoordinateType mz, CoordinateType tolerance_left, CoordinateType tolerance_right) const;
412 
424  Int findHighestInWindow(CoordinateType mz, CoordinateType tolerance_left, CoordinateType tolerance_right) const;
425 
432 
439 
446 
453 
460 
467 
474 
481 
490 
499 
508 
517 
526 
535 
544 
553 
556  bool containsIMData() const;
557 
566  std::pair<Size, DriftTimeUnit> getIMData() const;
567 
568 
574  std::pair<DriftTimeUnit, std::vector<float>> maybeGetIMData() const;
575 
577 
578 
590  void clear(bool clear_meta_data);
591 
592  /*
593  @brief Select a (subset of) spectrum and its data_arrays, only retaining the indices given in @p indices
594 
595  @param indices Vector of indices to keep
596  @return Reference to this MSSpectrum
597 
598  */
599  MSSpectrum& select(const std::vector<Size>& indices);
600 
601 
612  SpectrumSettings::SpectrumType getType(const bool query_data) const;
613  using SpectrumSettings::getType; // expose base class function
614 
618 
622 
625 
626 protected:
628  double retention_time_ = -1;
629 
631  double drift_time_ = -1;
632 
634  DriftTimeUnit drift_time_unit_ = DriftTimeUnit::NONE;
635 
637  UInt ms_level_ = 1;
638 
641 
644 
647 
650  };
651 
652  inline std::ostream& operator<<(std::ostream& os, const MSSpectrum& spec)
653  {
654  os << "-- MSSPECTRUM BEGIN --" << std::endl;
655 
656  // spectrum settings
657  os << static_cast<const SpectrumSettings&>(spec);
658 
659  // peaklist
660  for (MSSpectrum::ConstIterator it = spec.begin(); it != spec.end(); ++it)
661  {
662  os << *it << std::endl;
663  }
664 
665  os << "-- MSSPECTRUM END --" << std::endl;
666  return os;
667  }
668 
669 } // namespace OpenMS
Float data array class.
Definition: DataArrays.h:22
Integer data array class.
Definition: DataArrays.h:30
String data array class.
Definition: DataArrays.h:38
The representation of a 1D spectrum.
Definition: MSSpectrum.h:44
void setIntegerDataArrays(const IntegerDataArrays &ida)
Sets the integer meta data arrays.
ConstIterator MZBegin(CoordinateType mz) const
Binary search for peak range begin.
Iterator getBasePeak()
FloatDataArrays & getFloatDataArrays()
Returns a mutable reference to the float meta data arrays.
Definition: MSSpectrum.h:270
Iterator MZBegin(Iterator begin, CoordinateType mz, Iterator end)
Binary search for peak range begin.
double getRT() const
PeakType::CoordinateType CoordinateType
Coordinate (m/z) type.
Definition: MSSpectrum.h:98
Iterator PosEnd(Iterator begin, CoordinateType mz, Iterator end)
Binary search for peak range end (returns the past-the-end iterator)
void setMSLevel(UInt ms_level)
Sets the MS level.
Int findNearest(CoordinateType mz, CoordinateType tolerance) const
Binary search for the peak nearest to a specific m/z given a +/- tolerance windows in Th.
ConstIterator PosBegin(CoordinateType mz) const
Binary search for peak range begin.
MSSpectrum()
Constructor.
std::vector< StringDataArray > StringDataArrays
Definition: MSSpectrum.h:109
OpenMS::DataArrays::FloatDataArray FloatDataArray
Float data array vector type.
Definition: MSSpectrum.h:105
bool operator!=(const MSSpectrum &rhs) const
Equality operator.
Definition: MSSpectrum.h:191
bool isSortedByIM() const
Iterator PosBegin(Iterator begin, CoordinateType mz, Iterator end)
Binary search for peak range begin.
SpectrumSettings::SpectrumType getType(const bool query_data) const
Determine if spectrum is profile or centroided using up to three layers of information.
String name_
Name.
Definition: MSSpectrum.h:640
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:120
MSSpectrum(const MSSpectrum &source)
Copy constructor.
String getDriftTimeUnitAsString() const
returns the ion mobility drift time unit as string
bool containsIMData() const
StringDataArrays & getStringDataArrays()
Returns a mutable reference to the string meta data arrays.
ContainerType::reverse_iterator ReverseIterator
Mutable reverse iterator.
Definition: MSSpectrum.h:122
ConstIterator PosBegin(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range begin.
MSSpectrum & operator=(const SpectrumSettings &source)
Assignment operator.
void setDriftTimeUnit(DriftTimeUnit dt)
Sets the ion mobility drift time unit.
std::vector< PeakType > ContainerType
Spectrum base type.
Definition: MSSpectrum.h:100
ContainerType::const_reverse_iterator ConstReverseIterator
Non-mutable reverse iterator.
Definition: MSSpectrum.h:124
bool isSorted() const
Checks if all peaks are sorted with respect to ascending m/z.
std::pair< DriftTimeUnit, std::vector< float > > maybeGetIMData() const
Get the spectrum's ion mobility data (if exists) and its associated unit as a pair of {unit,...
ConstIterator MZBegin(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range begin.
MSSpectrum & select(const std::vector< Size > &indices)
Iterator MZEnd(Iterator begin, CoordinateType mz, Iterator end)
Binary search for peak range end (returns the past-the-end iterator)
void setName(const String &name)
Sets the name.
Iterator MZBegin(CoordinateType mz)
Binary search for peak range begin.
ConstIterator PosEnd(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range end (returns the past-the-end iterator)
void sortByPosition()
Lexicographically sorts the peaks by their position.
ConstIterator MZEnd(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range end (returns the past-the-end iterator)
StringDataArrays string_data_arrays_
String data arrays.
Definition: MSSpectrum.h:646
ConstIterator MZEnd(CoordinateType mz) const
Binary search for peak range end (returns the past-the-end iterator)
void sortByIntensity(bool reverse=false)
Lexicographically sorts the peaks by their intensity.
void sortByPositionPresorted(const std::vector< Chunk > &chunks)
Sort the spectrum, but uses the fact, that certain chunks are presorted.
DriftTimeUnit getDriftTimeUnit() const
Returns the ion mobility drift time unit.
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
MSSpectrum(MSSpectrum &&)=default
Move constructor.
IntegerDataArrays integer_data_arrays_
Integer data arrays.
Definition: MSSpectrum.h:649
void sort(const Predicate &lambda)
Definition: MSSpectrum.h:361
OpenMS::DataArrays::StringDataArray StringDataArray
String data array vector type.
Definition: MSSpectrum.h:108
~MSSpectrum()=default
Destructor.
std::pair< Size, DriftTimeUnit > getIMData() const
Get the Ion mobility data array's index and its associated unit.
bool operator==(const MSSpectrum &rhs) const
Equality operator.
PeakType::IntensityType calculateTIC() const
compute the total ion count (sum of all peak intensities)
UInt getMSLevel() const
Returns the MS level.
MSSpectrum(const std::initializer_list< Peak1D > &init)
Constructor from a list of Peak1D, e.g. MSSpectrum spec{ {mz1, int1}, {mz2, int2},...
RangeManagerContainer< RangeMZ, RangeIntensity, RangeMobility > RangeManagerContainerType
RangeManager.
Definition: MSSpectrum.h:102
const String & getName() const
Returns the name.
Iterator PosEnd(CoordinateType mz)
Binary search for peak range end (returns the past-the-end iterator)
bool isSorted(const Predicate &lambda) const
Definition: MSSpectrum.h:342
FloatDataArrays float_data_arrays_
Float data arrays.
Definition: MSSpectrum.h:643
ConstIterator getBasePeak() const
Iterator PosBegin(CoordinateType mz)
Binary search for peak range begin.
Size findNearest(CoordinateType mz) const
Binary search for the peak nearest to a specific m/z.
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
void setStringDataArrays(const StringDataArrays &sda)
Sets the string meta data arrays.
void sortByIonMobility()
Sorts the m/z peaks by their ion mobility value (and the accociated IM data arrays accordingly).
Int findHighestInWindow(CoordinateType mz, CoordinateType tolerance_left, CoordinateType tolerance_right) const
Search for the peak with highest intensity among the peaks near to a specific m/z given two +/- toler...
Iterator MZEnd(CoordinateType mz)
Binary search for peak range end (returns the past-the-end iterator)
const StringDataArrays & getStringDataArrays() const
Returns a const reference to the string meta data arrays.
MSSpectrum & operator=(const MSSpectrum &source)
Assignment operator.
double getDriftTime() const
Returns the ion mobility drift time (IMTypes::DRIFTTIME_NOT_SET means it is not set)
std::vector< FloatDataArray > FloatDataArrays
Definition: MSSpectrum.h:106
void updateRanges() override
OpenMS::DataArrays::IntegerDataArray IntegerDataArray
Integer data array vector type.
Definition: MSSpectrum.h:111
MSSpectrum & operator=(MSSpectrum &&) &=default
Move assignment operator.
void clear(bool clear_meta_data)
Clears all data and meta data.
void setDriftTime(double dt)
Sets the ion mobility drift time.
std::vector< IntegerDataArray > IntegerDataArrays
Definition: MSSpectrum.h:112
IntegerDataArrays & getIntegerDataArrays()
Returns a mutable reference to the integer meta data arrays.
RangeManager< RangeMZ, RangeIntensity, RangeMobility > RangeManagerType
Definition: MSSpectrum.h:103
void setRT(double rt)
Sets the absolute retention time (in seconds)
void setFloatDataArrays(const FloatDataArrays &fda)
Sets the float meta data arrays.
Int findNearest(CoordinateType mz, CoordinateType tolerance_left, CoordinateType tolerance_right) const
Search for the peak nearest to a specific m/z given two +/- tolerance windows in Th.
ConstIterator PosEnd(CoordinateType mz) const
Binary search for peak range end (returns the past-the-end iterator)
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:28
double CoordinateType
Coordinate type.
Definition: Peak1D.h:40
float IntensityType
Intensity type.
Definition: Peak1D.h:36
Definition: RangeManager.h:889
Handles the management of a multidimensional range, e.g. RangeMZ and RangeIntensity for spectra.
Definition: RangeManager.h:568
Representation of 1D spectrum settings.
Definition: SpectrumSettings.h:39
SpectrumType getType() const
returns the spectrum type (centroided (PEAKS) or profile data (RAW))
SpectrumType
Spectrum peak type.
Definition: SpectrumSettings.h:45
A more convenient string class.
Definition: String.h:34
int Int
Signed integer type.
Definition: Types.h:72
unsigned int UInt
Unsigned integer type.
Definition: Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:330
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Peak2D PeakType
Definition: MassTrace.h:21
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
DriftTimeUnit
Drift time unit for ion mobility.
Definition: IMTypes.h:23
Used to remember what subsets in a spectrum are sorted already to allow faster sorting of the spectru...
Definition: MSSpectrum.h:58
Size end
not inclusive
Definition: MSSpectrum.h:60
bool is_sorted
are the Peaks in [start, end) sorted yet?
Definition: MSSpectrum.h:61
Size start
inclusive
Definition: MSSpectrum.h:59
Chunk(Size p_start, Size p_end, bool p_sorted)
Definition: MSSpectrum.h:62
Container for organizing and managing multiple chunks in a spectrum.
Definition: MSSpectrum.h:77
std::vector< Chunk > chunks_
Definition: MSSpectrum.h:89
const MSSpectrum & spec_
Definition: MSSpectrum.h:90
std::vector< Chunk > & getChunks()
Definition: MSSpectrum.h:84
void add(bool is_sorted)
Definition: MSSpectrum.h:80
Chunks(const MSSpectrum &s)
Definition: MSSpectrum.h:79
Comparator for the ion mobility.
Definition: MSSpectrum.h:53
bool operator()(const MSSpectrum &a, const MSSpectrum &b) const
Comparator for the retention time.
Definition: MSSpectrum.h:49
bool operator()(const MSSpectrum &a, const MSSpectrum &b) const