OpenMS  2.8.0
MSSpectrum.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 $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/KERNEL/Peak1D.h>
43 
44 namespace OpenMS
45 {
46  class Peak1D;
47  enum class DriftTimeUnit;
66  class OPENMS_DLLAPI MSSpectrum final :
67  private std::vector<Peak1D>,
68  public RangeManagerContainer<RangeMZ, RangeIntensity>,
69  public SpectrumSettings
70  {
71 public:
72 
74  struct OPENMS_DLLAPI RTLess
75  {
76  bool operator()(const MSSpectrum& a, const MSSpectrum& b) const;
77  };
78 
80  struct Chunk {
83  bool is_sorted;
84  Chunk(Size start, Size end, bool sorted) : start(start), end(end), is_sorted(sorted) {}
85  };
86 
87  struct Chunks {
88  public:
89  Chunks(const MSSpectrum& s) : spec_(s) {}
90  void add(bool is_sorted)
91  {
92  chunks_.emplace_back((chunks_.empty() ? 0 : chunks_.back().end), spec_.size(), is_sorted);
93  }
94  std::vector<Chunk>& getChunks()
95  {
96  return chunks_;
97  }
98  private:
99  std::vector<Chunk> chunks_;
101  };
102 
104 
105  typedef OpenMS::Peak1D PeakType;
110  typedef std::vector<PeakType> ContainerType;
116  typedef std::vector<FloatDataArray> FloatDataArrays;
119  typedef std::vector<StringDataArray> StringDataArrays;
122  typedef std::vector<IntegerDataArray> IntegerDataArrays;
124 
126 
127  typedef typename ContainerType::iterator Iterator;
130  typedef typename ContainerType::const_iterator ConstIterator;
132  typedef typename ContainerType::reverse_iterator ReverseIterator;
134  typedef typename ContainerType::const_reverse_iterator ConstReverseIterator;
136 
138 
139  using ContainerType::operator[];
140  using ContainerType::begin;
141  using ContainerType::rbegin;
142  using ContainerType::end;
143  using ContainerType::rend;
144  using ContainerType::resize;
145  using ContainerType::size;
146  using ContainerType::push_back;
147  using ContainerType::emplace_back;
148  using ContainerType::pop_back;
149  using ContainerType::empty;
150  using ContainerType::front;
151  using ContainerType::back;
152  using ContainerType::reserve;
153  using ContainerType::insert;
154  using ContainerType::erase;
155  using ContainerType::swap;
156 
157  using typename ContainerType::iterator;
158  using typename ContainerType::const_iterator;
159  using typename ContainerType::size_type;
160  using typename ContainerType::value_type;
161  using typename ContainerType::reference;
162  using typename ContainerType::const_reference;
163  using typename ContainerType::pointer;
164  using typename ContainerType::difference_type;
165 
167 
168 
171 
173  MSSpectrum(const MSSpectrum& source);
174 
176  MSSpectrum(MSSpectrum&&) = default;
177 
179  ~MSSpectrum() = default;
180 
182  MSSpectrum& operator=(const MSSpectrum& source);
183 
185  MSSpectrum& operator=(MSSpectrum&&) & = default;
186 
189 
191  bool operator==(const MSSpectrum& rhs) const;
192 
194  bool operator!=(const MSSpectrum& rhs) const
195  {
196  return !(operator==(rhs));
197  }
198 
199  // Docu in base class (RangeManager)
200  void updateRanges() override;
201 
205  double getRT() const;
206 
208  void setRT(double rt);
209 
218  double getDriftTime() const;
219 
223  void setDriftTime(double dt);
224 
229 
232 
237 
243  UInt getMSLevel() const;
244 
246  void setMSLevel(UInt ms_level);
247 
249  const String& getName() const;
250 
252  void setName(const String& name);
253 
255 
271 
274  {
275  return float_data_arrays_;
276  }
277 
280 
283 
286 
289 
292 
295 
299 
301 
302 
307  void sortByIntensity(bool reverse = false);
308 
315 
320  void sortByPositionPresorted(const std::vector<Chunk>& chunks);
321 
323  bool isSorted() const;
324 
329  template<class Predicate>
330  bool isSorted(const Predicate& lambda) const
331  {
332  auto value_2_index_wrapper = [this, &lambda](const value_type& value1, const value_type& value2) {
333  // translate values into indices (this relies on no copies being made!)
334  const Size index1 = (&value1) - (&this->front());
335  const Size index2 = (&value2) - (&this->front());
336  // just make sure the pointers above are actually pointing to a Peak inside our container
337  assert(index1 < this->size());
338  assert(index2 < this->size());
339  return lambda(index1, index2);
340  };
341  return std::is_sorted(this->begin(), this->end(), value_2_index_wrapper);
342  }
343 
348  template<class Predicate>
349  void sort(const Predicate& lambda)
350  {
351  std::vector<Size> indices(this->size());
352  std::iota(indices.begin(), indices.end(), 0);
353  std::stable_sort(indices.begin(), indices.end(), lambda);
354  select(indices);
355  }
356 
358 
361 
372 
385 
399  Int findNearest(CoordinateType mz, CoordinateType tolerance_left, CoordinateType tolerance_right) const;
400 
412  Int findHighestInWindow(CoordinateType mz, CoordinateType tolerance_left, CoordinateType tolerance_right) const;
413 
420 
427 
434 
441 
448 
455 
462 
469 
478 
487 
496 
505 
514 
523 
532 
541 
544  bool containsIMData() const;
545 
553  std::pair<Size, DriftTimeUnit> getIMData() const;
554 
556 
557 
563  void clear(bool clear_meta_data);
564 
565  /*
566  @brief Select a (subset of) spectrum and its data_arrays, only retaining the indices given in @p indices
567 
568  @param indices Vector of indices to keep
569  @return Reference to this MSSpectrum
570 
571  */
572  MSSpectrum& select(const std::vector<Size>& indices);
573 
574 
585  SpectrumSettings::SpectrumType getType(const bool query_data) const;
586  using SpectrumSettings::getType; // expose base class function
587 
591 
595 
598 
599 protected:
602 
604  double drift_time_;
605 
608 
611 
614 
617 
620 
623  };
624 
625  inline std::ostream& operator<<(std::ostream& os, const MSSpectrum& spec)
626  {
627  os << "-- MSSPECTRUM BEGIN --" << std::endl;
628 
629  // spectrum settings
630  os << static_cast<const SpectrumSettings&>(spec);
631 
632  // peaklist
633  for (MSSpectrum::ConstIterator it = spec.begin(); it != spec.end(); ++it)
634  {
635  os << *it << std::endl;
636  }
637 
638  os << "-- MSSPECTRUM END --" << std::endl;
639  return os;
640  }
641 
642 } // namespace OpenMS
Float data array class.
Definition: DataArrays.h:48
Integer data array class.
Definition: DataArrays.h:55
String data array class.
Definition: DataArrays.h:62
The representation of a 1D spectrum.
Definition: MSSpectrum.h:70
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:273
UInt ms_level_
MS level.
Definition: MSSpectrum.h:610
Iterator MZBegin(Iterator begin, CoordinateType mz, Iterator end)
Binary search for peak range begin.
double getRT() const
RangeManagerContainer< RangeMZ, RangeIntensity > RangeManagerContainerType
RangeManager.
Definition: MSSpectrum.h:112
PeakType::CoordinateType CoordinateType
Coordinate (m/z) type.
Definition: MSSpectrum.h:108
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:119
OpenMS::DataArrays::FloatDataArray FloatDataArray
Float data array vector type.
Definition: MSSpectrum.h:115
bool operator!=(const MSSpectrum &rhs) const
Equality operator.
Definition: MSSpectrum.h:194
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:613
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:130
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:132
ConstIterator PosBegin(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range begin.
double drift_time_
Drift time.
Definition: MSSpectrum.h:604
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:110
ContainerType::const_reverse_iterator ConstReverseIterator
Non-mutable reverse iterator.
Definition: MSSpectrum.h:134
bool isSorted() const
Checks if all peaks are sorted with respect to ascending m/z.
ConstIterator MZBegin(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range begin.
MSSpectrum & select(const std::vector< Size > &indices)
RangeManager< RangeMZ, RangeIntensity > RangeManagerType
Definition: MSSpectrum.h:113
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:619
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:622
void sort(const Predicate &lambda)
Definition: MSSpectrum.h:349
OpenMS::DataArrays::StringDataArray StringDataArray
String data array vector type.
Definition: MSSpectrum.h:118
~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.
double retention_time_
Retention time.
Definition: MSSpectrum.h:601
PeakType::IntensityType calculateTIC() const
compute the total ion count (sum of all peak intensities)
UInt getMSLevel() const
Returns the MS level.
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:330
FloatDataArrays float_data_arrays_
Float data arrays.
Definition: MSSpectrum.h:616
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.
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 (MSSpectrum::DRIFTTIME_NOT_SET means it is not set)
std::vector< FloatDataArray > FloatDataArrays
Definition: MSSpectrum.h:116
void updateRanges() override
OpenMS::DataArrays::IntegerDataArray IntegerDataArray
Integer data array vector type.
Definition: MSSpectrum.h:121
DriftTimeUnit drift_time_unit_
Drift time unit.
Definition: MSSpectrum.h:607
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:122
IntegerDataArrays & getIntegerDataArrays()
Returns a mutable reference to the integer meta data arrays.
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:54
float IntensityType
Intensity type.
Definition: Peak1D.h:62
double CoordinateType
Coordinate type.
Definition: Peak1D.h:66
Definition: RangeManager.h:652
Handles the management of a multidimensional range, e.g. RangeMZ and RangeIntensity for spectra.
Definition: RangeManager.h:455
Representation of 1D spectrum settings.
Definition: SpectrumSettings.h:65
SpectrumType getType() const
returns the spectrum type (centroided (PEAKS) or profile data (RAW))
SpectrumType
Spectrum peak type.
Definition: SpectrumSettings.h:71
A more convenient string class.
Definition: String.h:60
int Int
Signed integer type.
Definition: Types.h:102
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:350
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Peak2D PeakType
Definition: MassTrace.h:47
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
DriftTimeUnit
Drift time unit for ion mobility.
Definition: IMTypes.h:46
Used to remember what subsets in a spectrum are sorted already to allow faster sorting of the spectru...
Definition: MSSpectrum.h:80
Chunk(Size start, Size end, bool sorted)
Definition: MSSpectrum.h:84
Size end
not inclusive
Definition: MSSpectrum.h:82
bool is_sorted
are the Peaks in [start, end) sorted yet?
Definition: MSSpectrum.h:83
Size start
inclusive
Definition: MSSpectrum.h:81
Definition: MSSpectrum.h:87
std::vector< Chunk > chunks_
Definition: MSSpectrum.h:99
const MSSpectrum & spec_
Definition: MSSpectrum.h:100
std::vector< Chunk > & getChunks()
Definition: MSSpectrum.h:94
void add(bool is_sorted)
Definition: MSSpectrum.h:90
Chunks(const MSSpectrum &s)
Definition: MSSpectrum.h:89
Comparator for the retention time.
Definition: MSSpectrum.h:75
bool operator()(const MSSpectrum &a, const MSSpectrum &b) const