OpenMS
Loading...
Searching...
No Matches
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
17
18#include <numeric>
19
20namespace OpenMS
21{
22 enum class DriftTimeUnit;
41 class OPENMS_DLLAPI MSSpectrum final :
42 private std::vector<Peak1D>,
43 public RangeManagerContainer<RangeMZ, RangeIntensity, RangeMobility>,
44 public SpectrumSettings
45 {
46public:
47
49 struct OPENMS_DLLAPI RTLess
50 {
51 bool operator()(const MSSpectrum& a, const MSSpectrum& b) const;
52 };
54 struct OPENMS_DLLAPI IMLess {
55 bool operator()(const MSSpectrum& a, const MSSpectrum& b) const;
56 };
57
59 struct Chunk {
62 bool is_sorted;
63 Chunk(Size p_start, Size p_end, bool p_sorted) : start(p_start), end(p_end), is_sorted(p_sorted)
64 {
65 }
66 };
67
78 struct Chunks {
79 public:
80 Chunks(const MSSpectrum& s) : spec_(s) {}
81 void add(bool is_sorted)
82 {
83 chunks_.emplace_back((chunks_.empty() ? 0 : chunks_.back().end), spec_.size(), is_sorted);
84 }
85 std::vector<Chunk>& getChunks()
86 {
87 return chunks_;
88 }
89 private:
90 std::vector<Chunk> chunks_;
92 };
93
95
96
101 typedef std::vector<PeakType> ContainerType;
107 typedef std::vector<FloatDataArray> FloatDataArrays;
110 typedef std::vector<StringDataArray> StringDataArrays;
113 typedef std::vector<IntegerDataArray> IntegerDataArrays;
115
117
118
119 typedef typename ContainerType::iterator Iterator;
121 typedef typename ContainerType::const_iterator ConstIterator;
123 typedef typename ContainerType::reverse_iterator ReverseIterator;
125 typedef typename ContainerType::const_reverse_iterator ConstReverseIterator;
127
129
130 using ContainerType::operator[];
131 using ContainerType::begin;
132 using ContainerType::rbegin;
133 using ContainerType::end;
134 using ContainerType::rend;
135 using ContainerType::cbegin;
136 using ContainerType::cend;
137 using ContainerType::resize;
138 using ContainerType::size;
139 using ContainerType::push_back;
140 using ContainerType::emplace_back;
141 using ContainerType::pop_back;
142 using ContainerType::empty;
143 using ContainerType::front;
144 using ContainerType::back;
145 using ContainerType::reserve;
146 using ContainerType::insert;
147 using ContainerType::erase;
148 using ContainerType::swap;
149 using ContainerType::data;
150 using ContainerType::shrink_to_fit;
151
152 using typename ContainerType::iterator;
153 using typename ContainerType::const_iterator;
154 using typename ContainerType::size_type;
155 using typename ContainerType::value_type;
156 using typename ContainerType::reference;
157 using typename ContainerType::const_reference;
158 using typename ContainerType::pointer;
159 using typename ContainerType::difference_type;
160
162
163
166
168 MSSpectrum(const std::initializer_list<Peak1D>& init);
169
171 MSSpectrum(const MSSpectrum& source);
172
174 MSSpectrum(MSSpectrum&&) = default;
175
177 ~MSSpectrum() = default;
178
181
184
187
189 bool operator==(const MSSpectrum& rhs) const;
190
192 bool operator!=(const MSSpectrum& rhs) const
193 {
194 return !(operator==(rhs));
195 }
196
197 // Docu in base class (RangeManager)
198 void updateRanges() override;
199
203 double getRT() const;
204
206 void setRT(double rt);
207
216 double getDriftTime() const;
217
221 void setDriftTime(double dt);
222
227
229 std::string getDriftTimeUnitAsString() const;
230
235
242
244 void setMSLevel(UInt ms_level);
245
247 const std::string& getName() const;
248
250 void setName(const std::string& name);
251
253
269
272 {
273 return float_data_arrays_;
274 }
275
278
281
284
287
290
293
297
299
300
305 void sortByIntensity(bool reverse = false);
306
313
322
327 void sortByPositionPresorted(const std::vector<Chunk>& chunks);
328
330 bool isSorted() const;
331
336 bool isSortedByIM() const;
337
342 template<class Predicate>
343 bool isSorted(const Predicate& lambda) const
344 {
345 auto value_2_index_wrapper = [this, &lambda](const value_type& value1, const value_type& value2) {
346 // translate values into indices (this relies on no copies being made!)
347 const Size index1 = (&value1) - (&this->front());
348 const Size index2 = (&value2) - (&this->front());
349 // just make sure the pointers above are actually pointing to a Peak inside our container
350 assert(index1 < this->size());
351 assert(index2 < this->size());
352 return lambda(index1, index2);
353 };
354 return std::is_sorted(this->begin(), this->end(), value_2_index_wrapper);
355 }
356
365 template<class Predicate>
366 void sort(const Predicate& lambda)
367 {
368 // Validate up front, before running the (possibly data-array-indexing) predicate, so a
369 // mis-sized array throws instead of being read out of bounds during the sort.
370 checkDataArraySizes_();
371 // The sorted indices are a permutation of [0, size): in range and duplicate-free by
372 // construction, so the unchecked path is safe and skips the redundant bounds re-scan.
373 selectUnchecked(Internal::stableSortedIndices(this->size(), lambda));
374 }
375
377
380
391
404
418 Int findNearest(CoordinateType mz, CoordinateType tolerance_left, CoordinateType tolerance_right) const;
419
431 Int findHighestInWindow(CoordinateType mz, CoordinateType tolerance_left, CoordinateType tolerance_right) const;
432
439
446
453
460
467
474
481
488
497
506
515
524
533
542
551
560
563 bool containsIMData() const;
564
573 std::pair<Size, DriftTimeUnit> getIMData() const;
574
575
581 std::pair<DriftTimeUnit, std::vector<float>> maybeGetIMData() const;
582
584
585
597 void clear(bool clear_meta_data);
598
615 MSSpectrum& select(const std::vector<Size>& indices);
616
639 MSSpectrum& selectUnchecked(const std::vector<Size>& indices);
640
641
652 SpectrumSettings::SpectrumType getType(const bool query_data) const;
653 using SpectrumSettings::getType; // expose base class function
654
658
662
665
670 {
671 SUM,
672 MAX
673 };
674
719 float* output,
720 Size im_bins,
721 Size mz_bins,
722 CoordinateType min_im,
723 CoordinateType max_im,
724 CoordinateType min_mz,
725 CoordinateType max_mz,
726 RasterAggregation aggregation = RasterAggregation::SUM) const;
727
728protected:
739
741 double retention_time_ = -1;
742
744 double drift_time_ = -1;
745
747 DriftTimeUnit drift_time_unit_ = DriftTimeUnit::NONE;
748
750 UInt ms_level_ = 1;
751
753 std::string name_;
754
757
760
763 };
764
765 inline std::ostream& operator<<(std::ostream& os, const MSSpectrum& spec)
766 {
767 os << "-- MSSPECTRUM BEGIN --" << std::endl;
768
769 // spectrum settings
770 os << static_cast<const SpectrumSettings&>(spec);
771
772 // peaklist
773 for (MSSpectrum::ConstIterator it = spec.begin(); it != spec.end(); ++it)
774 {
775 os << *it << std::endl;
776 }
777
778 os << "-- MSSPECTRUM END --" << std::endl;
779 return os;
780 }
781
782} // namespace OpenMS
Float data array class.
Definition DataArrays.h:25
Integer data array class.
Definition DataArrays.h:75
std::string data array class
Definition DataArrays.h:125
The representation of a 1D spectrum.
Definition MSSpectrum.h:45
void setIntegerDataArrays(const IntegerDataArrays &ida)
Sets the integer meta data arrays.
ConstIterator MZBegin(CoordinateType mz) const
Binary search for peak range begin.
void rasterizeIMFrame(float *output, Size im_bins, Size mz_bins, CoordinateType min_im, CoordinateType max_im, CoordinateType min_mz, CoordinateType max_mz, RasterAggregation aggregation=RasterAggregation::SUM) const
Rasterizes an ion mobility frame into a 2D intensity matrix (IM vs m/z).
Iterator getBasePeak()
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:99
RasterAggregation
Aggregation mode for rasterizeIMFrame.
Definition MSSpectrum.h:670
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
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.
MSSpectrum & select(const std::vector< Size > &indices)
Select (a subset of) the spectrum and its data arrays, retaining only the peaks named in indices,...
std::vector< StringDataArray > StringDataArrays
Definition MSSpectrum.h:110
OpenMS::DataArrays::FloatDataArray FloatDataArray
Float data array vector type.
Definition MSSpectrum.h:106
bool operator!=(const MSSpectrum &rhs) const
Equality operator.
Definition MSSpectrum.h:192
bool isSortedByIM() const
const std::string & getName() const
Returns the name.
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.
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition MSSpectrum.h:121
MSSpectrum(const MSSpectrum &source)
Copy constructor.
std::string name_
Name.
Definition MSSpectrum.h:753
bool containsIMData() const
ContainerType::reverse_iterator ReverseIterator
Mutable reverse iterator.
Definition MSSpectrum.h:123
ConstIterator PosBegin(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range begin.
const StringDataArrays & getStringDataArrays() const
Returns a const reference to the string meta data arrays.
MSSpectrum & operator=(const MSSpectrum &source)
Assignment operator.
void setDriftTimeUnit(DriftTimeUnit dt)
Sets the ion mobility drift time unit.
std::vector< PeakType > ContainerType
Spectrum base type.
Definition MSSpectrum.h:101
ContainerType::const_reverse_iterator ConstReverseIterator
Non-mutable reverse iterator.
Definition MSSpectrum.h:125
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.
ContainerType::iterator Iterator
Mutable iterator.
Definition MSSpectrum.h:119
MSSpectrum & operator=(const SpectrumSettings &source)
Assignment operator.
Iterator MZEnd(Iterator begin, CoordinateType mz, Iterator end)
Binary search for peak range end (returns the past-the-end iterator)
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,...
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_
std::string data arrays
Definition MSSpectrum.h:759
std::pair< Size, DriftTimeUnit > getIMData() const
Get the Ion mobility data array's index and its associated unit.
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.
MSSpectrum(MSSpectrum &&)=default
Move constructor.
FloatDataArrays & getFloatDataArrays()
Returns a mutable reference to the float meta data arrays.
Definition MSSpectrum.h:271
IntegerDataArrays integer_data_arrays_
Integer data arrays.
Definition MSSpectrum.h:762
void setName(const std::string &name)
Sets the name.
void sort(const Predicate &lambda)
Definition MSSpectrum.h:366
OpenMS::DataArrays::StringDataArray StringDataArray
std::string data array vector type
Definition MSSpectrum.h:109
~MSSpectrum()=default
Destructor.
bool operator==(const MSSpectrum &rhs) const
Equality operator.
MSSpectrum & operator=(MSSpectrum &&) &=default
Move assignment 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:103
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:343
IntegerDataArrays & getIntegerDataArrays()
Returns a mutable reference to the integer meta data arrays.
FloatDataArrays float_data_arrays_
Float data arrays.
Definition MSSpectrum.h:756
OpenMS::Peak1D PeakType
Peak type.
Definition MSSpectrum.h:97
ConstIterator getBasePeak() const
Iterator PosBegin(CoordinateType mz)
Binary search for peak range begin.
MSSpectrum & selectUnchecked(const std::vector< Size > &indices)
Like select(), but without the per-index range check.
Size findNearest(CoordinateType mz) const
Binary search for the peak nearest to a specific m/z.
void setStringDataArrays(const StringDataArrays &sda)
Sets the string meta data arrays.
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float 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...
void checkDataArraySizes_() const
Throws Exception::Precondition if any non-empty data array's size differs from the peak count.
Iterator MZEnd(CoordinateType mz)
Binary search for peak range end (returns the past-the-end iterator)
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:107
void updateRanges() override
OpenMS::DataArrays::IntegerDataArray IntegerDataArray
Integer data array vector type.
Definition MSSpectrum.h:112
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:113
RangeManager< RangeMZ, RangeIntensity, RangeMobility > RangeManagerType
Definition MSSpectrum.h:104
void setRT(double rt)
Sets the absolute retention time (in seconds)
void setFloatDataArrays(const FloatDataArrays &fda)
Sets the float meta data arrays.
StringDataArrays & getStringDataArrays()
Returns a mutable reference to the string 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)
std::string getDriftTimeUnitAsString() const
returns the ion mobility drift time unit as string
A 1-dimensional raw data point or peak.
Definition Peak1D.h:30
double CoordinateType
Coordinate type.
Definition Peak1D.h:42
float IntensityType
Intensity type.
Definition Peak1D.h:38
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:42
SpectrumType
Spectrum peak type.
Definition SpectrumSettings.h:48
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)
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
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:59
Size end
not inclusive
Definition MSSpectrum.h:61
bool is_sorted
are the Peaks in [start, end) sorted yet?
Definition MSSpectrum.h:62
Size start
inclusive
Definition MSSpectrum.h:60
Chunk(Size p_start, Size p_end, bool p_sorted)
Definition MSSpectrum.h:63
Container for organizing and managing multiple chunks in a spectrum.
Definition MSSpectrum.h:78
std::vector< Chunk > & getChunks()
Definition MSSpectrum.h:85
std::vector< Chunk > chunks_
Definition MSSpectrum.h:90
const MSSpectrum & spec_
Definition MSSpectrum.h:91
void add(bool is_sorted)
Definition MSSpectrum.h:81
Chunks(const MSSpectrum &s)
Definition MSSpectrum.h:80
Comparator for the ion mobility.
Definition MSSpectrum.h:54
bool operator()(const MSSpectrum &a, const MSSpectrum &b) const
Comparator for the retention time.
Definition MSSpectrum.h:50
bool operator()(const MSSpectrum &a, const MSSpectrum &b) const