OpenMS  3.0.0
MSExperiment.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-2022.
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, Tom Waschischeck $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
43 
44 #include <vector>
45 
46 
47 namespace OpenMS
48 {
49  class Peak1D;
50  class ChromatogramPeak;
51 
70  class OPENMS_DLLAPI MSExperiment final : public RangeManagerContainer<RangeRT, RangeMZ, RangeIntensity, RangeMobility>,
72  {
73 
74 public:
75  typedef Peak1D PeakT;
77 
79 
80  typedef PeakT PeakType;
97  typedef std::vector<SpectrumType> Base;
99 
101 
102  typedef std::vector<SpectrumType>::iterator Iterator;
105  typedef std::vector<SpectrumType>::const_iterator ConstIterator;
111 
113  // Attention: these refer to the spectra vector only!
115  typedef Base::value_type value_type;
116  typedef Base::iterator iterator;
117  typedef Base::const_iterator const_iterator;
118 
120  MSExperiment();
121 
123  MSExperiment(const MSExperiment & source);
124 
126  MSExperiment(MSExperiment&&) = default;
127 
129  MSExperiment & operator=(const MSExperiment & source);
130 
132  MSExperiment& operator=(MSExperiment&&) & = default;
133 
135  MSExperiment & operator=(const ExperimentalSettings & source);
136 
138  ~MSExperiment() override;
139 
141  bool operator==(const MSExperiment & rhs) const;
142 
144  bool operator!=(const MSExperiment & rhs) const;
145 
147  inline Size size() const
148  {
149  return spectra_.size();
150  }
151 
153  inline void resize(Size n)
154  {
155  spectra_.resize(n);
156  }
157 
159  inline bool empty() const
160  {
161  return spectra_.empty();
162  }
163 
165  inline void reserve(Size n)
166  {
167  spectra_.reserve(n);
168  }
169 
172  {
173  return spectra_[n];
174  }
175 
177  inline const SpectrumType& operator[](Size n) const
178  {
179  return spectra_[n];
180  }
181 
182  inline Iterator begin()
183  {
184  return spectra_.begin();
185  }
186 
187  inline ConstIterator begin() const
188  {
189  return spectra_.begin();
190  }
191 
192  inline Iterator end()
193  {
194  return spectra_.end();
195  }
196 
197  inline ConstIterator end() const
198  {
199  return spectra_.end();
200  }
202 
203  // Aliases / chromatograms
204  void reserveSpaceSpectra(Size s);
205  void reserveSpaceChromatograms(Size s);
206 
208 
209 
215  template <class Container>
216  void get2DData(Container& cont) const
217  {
218  for (typename Base::const_iterator spec = spectra_.begin(); spec != spectra_.end(); ++spec)
219  {
220  if (spec->getMSLevel() != 1)
221  {
222  continue;
223  }
224  typename Container::value_type s; // explicit object here, since instantiation within push_back() fails on VS<12
225  for (typename SpectrumType::const_iterator it = spec->begin(); it != spec->end(); ++it)
226  {
227  cont.push_back(s);
228  cont.back().setRT(spec->getRT());
229  cont.back().setMZ(it->getMZ());
230  cont.back().setIntensity(it->getIntensity());
231  }
232  }
233  }
234 
246  template <class Container>
247  void set2DData(const Container& container)
248  {
249  set2DData<false, Container>(container);
250  }
251 
266  template <class Container>
267  void set2DData(const Container& container, const StringList& store_metadata_names)
268  {
269  // clean up the container first
270  clear(true);
271  SpectrumType* spectrum = nullptr;
272  typename PeakType::CoordinateType current_rt = -std::numeric_limits<typename PeakType::CoordinateType>::max();
273  for (typename Container::const_iterator iter = container.begin(); iter != container.end(); ++iter)
274  {
275  // check if the retention time has changed
276  if (current_rt != iter->getRT() || spectrum == nullptr)
277  {
278  // append new spectrum
279  if (current_rt > iter->getRT())
280  {
281  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Input container is not sorted!");
282  }
283  current_rt = iter->getRT();
284  spectrum = createSpec_(current_rt, store_metadata_names);
285  }
286 
287  // add either data point or mass traces (depending on template argument value)
288  ContainerAdd_<typename Container::value_type, false>::addData_(spectrum, &(*iter), store_metadata_names);
289  }
290  }
291 
309  template <bool add_mass_traces, class Container>
310  void set2DData(const Container& container)
311  {
312  // clean up the container first
313  clear(true);
314  SpectrumType* spectrum = nullptr;
315  typename PeakType::CoordinateType current_rt = -std::numeric_limits<typename PeakType::CoordinateType>::max();
316  for (typename Container::const_iterator iter = container.begin(); iter != container.end(); ++iter)
317  {
318  // check if the retention time has changed
319  if (current_rt != iter->getRT() || spectrum == nullptr)
320  {
321  // append new spectrum
322  if (current_rt > iter->getRT())
323  {
324  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Input container is not sorted!");
325  }
326  current_rt = iter->getRT();
327  spectrum = createSpec_(current_rt);
328  }
329 
330  // add either data point or mass traces (depending on template argument value)
332  }
333  }
334 
336 
337 
339 
340  AreaIterator areaBegin(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz, UInt ms_level = 1);
342 
344  AreaIterator areaBegin(const RangeManagerType& range, UInt ms_level = 1);
345 
347  AreaIterator areaEnd();
348 
350  ConstAreaIterator areaBeginConst(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz, UInt ms_level = 1) const;
351 
353  ConstAreaIterator areaBeginConst(const RangeManagerType& range, UInt ms_level = 1) const;
354 
356  ConstAreaIterator areaEndConst() const;
357 
358  // for fast pyOpenMS access to MS1 peak data in format: [rt, [mz, intensity]]
360  std::vector<float>& rt,
361  std::vector<std::vector<float>>& mz,
362  std::vector<std::vector<float>>& intensity) const
363  {
364  float t = -1.0;
365  for (auto it = areaBeginConst(min_rt, max_rt, min_mz, max_mz); it != areaEndConst(); ++it)
366  {
367  if (it.getRT() != t)
368  {
369  t = (float)it.getRT();
370  rt.emplace_back(t);
371  mz.resize(mz.size() + 1);
372  rt.resize(rt.size() + 1);
373  intensity.resize(intensity.size() + 1);
374  }
375  mz.back().push_back((float)it->getMZ());
376  intensity.back().emplace_back(it->getIntensity());
377  }
378  }
379 
380  // for fast pyOpenMS access to MS1 peak data in format: [rt, mz, intensity]
382  std::vector<float>& rt,
383  std::vector<float>& mz,
384  std::vector<float>& intensity) const
385  {
386  for (auto it = areaBeginConst(min_rt, max_rt, min_mz, max_mz); it != areaEndConst(); ++it)
387  {
388  rt.push_back((float)it.getRT());
389  mz.push_back((float)it->getMZ());
390  intensity.push_back(it->getIntensity());
391  }
392  }
393 
394 
402  ConstIterator RTBegin(CoordinateType rt) const;
403 
411  ConstIterator RTEnd(CoordinateType rt) const;
412 
418  Iterator RTBegin(CoordinateType rt);
419 
425  Iterator RTEnd(CoordinateType rt);
426 
427 
435  ConstIterator IMBegin(CoordinateType im) const;
436 
444  ConstIterator IMEnd(CoordinateType im) const;
446 
452  // Docu in base class
454  void updateRanges() override;
455 
461  void updateRanges(Int ms_level);
462 
464  CoordinateType getMinMZ() const;
465 
467  CoordinateType getMaxMZ() const;
468 
470  CoordinateType getMinRT() const;
471 
473  CoordinateType getMaxRT() const;
474 
476  UInt64 getSize() const;
477 
479  const std::vector<UInt>& getMSLevels() const;
480 
482 
485  UInt64 getSqlRunID() const;
486 
488  void setSqlRunID(UInt64 id);
489 
492 
497  void sortSpectra(bool sort_mz = true);
498 
504  void sortChromatograms(bool sort_rt = true);
505 
511  bool isSorted(bool check_mz = true) const;
512 
514 
516  void reset();
517 
523  bool clearMetaDataArrays();
524 
526  const ExperimentalSettings& getExperimentalSettings() const;
527 
529  ExperimentalSettings& getExperimentalSettings();
530 
532  void getPrimaryMSRunPath(StringList& toFill) const;
533 
539  ConstIterator getPrecursorSpectrum(ConstIterator iterator) const;
540 
546  int getPrecursorSpectrum(int zero_based_index) const;
547 
549  void swap(MSExperiment& from);
550 
552  void setSpectra(const std::vector<MSSpectrum>& spectra);
553  void setSpectra(std::vector<MSSpectrum>&& spectra);
554 
556  void addSpectrum(const MSSpectrum& spectrum);
557  void addSpectrum(MSSpectrum&& spectrum);
558 
560  const std::vector<MSSpectrum>& getSpectra() const;
561 
563  std::vector<MSSpectrum>& getSpectra();
564 
566  void setChromatograms(const std::vector<MSChromatogram>& chromatograms);
567  void setChromatograms(std::vector<MSChromatogram>&& chromatograms);
568 
570  void addChromatogram(const MSChromatogram& chromatogram);
571  void addChromatogram(MSChromatogram&& chrom);
572 
574  const std::vector<MSChromatogram>& getChromatograms() const;
575 
577  std::vector<MSChromatogram>& getChromatograms();
578 
580 
581  MSChromatogram& getChromatogram(Size id);
583 
585  MSSpectrum& getSpectrum(Size id);
586 
588  Size getNrSpectra() const;
589 
591  Size getNrChromatograms() const;
593 
604  const MSChromatogram calculateTIC(float rt_bin_size = 0, UInt ms_level = 1) const;
605 
611  void clear(bool clear_meta_data);
612 
614  bool containsScanOfLevel(size_t ms_level) const;
615 
617  bool hasZeroIntensities(size_t ms_level) const;
618 
620  bool hasPeptideIdentifications() const;
621 
623  bool isIMFrame() const;
624 
625  protected:
627  std::vector<UInt> ms_levels_;
631  std::vector<MSChromatogram > chromatograms_;
633  std::vector<SpectrumType> spectra_;
634 
635 private:
636 
638  template<typename ContainerValueType, bool addMassTraces>
640  {
641  static void addData_(SpectrumType* spectrum, const ContainerValueType* item);
642  static void addData_(SpectrumType* spectrum, const ContainerValueType* item, const StringList& store_metadata_names);
643  };
644 
645  template<typename ContainerValueType>
646  struct ContainerAdd_<ContainerValueType, false>
647  {
649  static void addData_(SpectrumType* spectrum, const ContainerValueType* item)
650  {
651  // create temporary peak and insert it into spectrum
652  spectrum->insert(spectrum->end(), PeakType());
653  spectrum->back().setIntensity(item->getIntensity());
654  spectrum->back().setPosition(item->getMZ());
655  }
657  static void addData_(SpectrumType* spectrum, const ContainerValueType* item, const StringList& store_metadata_names)
658  {
659  addData_(spectrum, item);
660  for (StringList::const_iterator itm = store_metadata_names.begin(); itm != store_metadata_names.end(); ++itm)
661  {
662  float val = std::numeric_limits<float>::quiet_NaN();
663  if (item->metaValueExists(*itm)) val = item->getMetaValue(*itm);
664  spectrum->getFloatDataArrays()[itm - store_metadata_names.begin()].push_back(val);
665  }
666  }
667  };
668 
669  template<typename ContainerValueType>
670  struct ContainerAdd_<ContainerValueType, true>
671  {
673  static void addData_(SpectrumType* spectrum, const ContainerValueType* item)
674  {
675  if (item->metaValueExists("num_of_masstraces"))
676  {
677  Size mts = item->getMetaValue("num_of_masstraces");
678  int charge = (item->getCharge()==0 ? 1 : item->getCharge()); // set to 1 if charge is 0, otherwise div/0 below
679  for (Size i = 0; i < mts; ++i)
680  {
681  String meta_name = String("masstrace_intensity_") + i;
682  if (!item->metaValueExists(meta_name))
683  {
684  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Meta value '") + meta_name + "' expected but not found in container.");
685  }
686  ContainerValueType p;
687  p.setIntensity(item->getMetaValue(meta_name));
688  p.setPosition(item->getMZ() + Constants::C13C12_MASSDIFF_U / charge * i);
690  }
691  }
693  }
694  };
695 
696  /*
697  @brief Append a spectrum to current MSExperiment
698 
699  @param rt RT of new spectrum
700  @return Pointer to newly created spectrum
701  */
702  SpectrumType* createSpec_(PeakType::CoordinateType rt);
703 
704  /*
705  @brief Append a spectrum including floatdata arrays to current MSExperiment
706 
707  @param rt RT of new spectrum
708  @param metadata_names Names of floatdata arrays attached to this spectrum
709  @return Pointer to newly created spectrum
710  */
711  SpectrumType* createSpec_(PeakType::CoordinateType rt, const StringList& metadata_names);
712 
713  };
714 
716  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const MSExperiment& exp);
717 
718 } // namespace OpenMS
719 
721 
722 
const double C13C12_MASSDIFF_U
Definition: Constants.h:121
A more convenient string class.
Definition: String.h:58
void set2DData(const Container &container)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:310
static void addData_(SpectrumType *spectrum, const ContainerValueType *item)
general method for adding data points
Definition: MSExperiment.h:649
Internal::AreaIterator< const PeakT, const PeakT &, const PeakT *, ConstIterator, SpectrumType::ConstIterator > ConstAreaIterator
Immutable area iterator type (for traversal of a rectangular subset of the peaks) ...
Definition: MSExperiment.h:109
std::vector< SpectrumType >::iterator Iterator
Mutable iterator.
Definition: MSExperiment.h:103
Peak1D PeakT
Definition: MSExperiment.h:75
The representation of a chromatogram.
Definition: MSChromatogram.h:53
void get2DPeakData(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz, std::vector< float > &rt, std::vector< float > &mz, std::vector< float > &intensity) const
Definition: MSExperiment.h:381
Base::value_type value_type
Definition: MSExperiment.h:115
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
Internal::AreaIterator< PeakT, PeakT &, PeakT *, Iterator, SpectrumType::Iterator > AreaIterator
Mutable area iterator type (for traversal of a rectangular subset of the peaks)
Definition: MSExperiment.h:107
Peak2D PeakType
Definition: MassTrace.h:47
ChromatogramPeakT ChromatogramPeakType
Chromatogram peak type.
Definition: MSExperiment.h:83
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:61
ConstIterator end() const
Definition: MSExperiment.h:197
std::vector< MSChromatogram > chromatograms_
chromatograms
Definition: MSExperiment.h:631
Iterator begin()
Definition: MSExperiment.h:182
Base::const_iterator const_iterator
Definition: MSExperiment.h:117
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
PeakType::IntensityType IntensityType
Intensity type of peaks.
Definition: MSExperiment.h:87
RangeManagerContainer< RangeRT, RangeMZ, RangeIntensity, RangeMobility > RangeManagerContainerType
RangeManager type.
Definition: MSExperiment.h:91
Size size() const
The number of spectra.
Definition: MSExperiment.h:147
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Precondition failed exception.
Definition: Exception.h:157
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
MSSpectrum SpectrumType
Spectrum Type.
Definition: MSExperiment.h:93
SpectrumType & operator[](Size n)
Random access to n&#39;th spectrum.
Definition: MSExperiment.h:171
void reserve(Size n)
Reserve space for n spectra.
Definition: MSExperiment.h:165
Iterator end()
Definition: MSExperiment.h:192
void resize(Size n)
Resize to n spectra.
Definition: MSExperiment.h:153
Base::iterator iterator
Definition: MSExperiment.h:116
The representation of a 1D spectrum.
Definition: MSSpectrum.h:66
void set2DData(const Container &container, const StringList &store_metadata_names)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:267
Helper class to add either general data points in set2DData or use mass traces from meta values...
Definition: MSExperiment.h:639
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:53
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
ChromatogramPeak ChromatogramPeakT
Definition: MSExperiment.h:76
PeakType::CoordinateType CoordinateType
Coordinate type of peak positions.
Definition: MSExperiment.h:85
bool empty() const
Are there any spectra (does not consider chromatograms)
Definition: MSExperiment.h:159
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
std::vector< SpectrumType > spectra_
spectra
Definition: MSExperiment.h:633
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
void get2DPeakData(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz, std::vector< float > &rt, std::vector< std::vector< float >> &mz, std::vector< std::vector< float >> &intensity) const
Definition: MSExperiment.h:359
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:70
MSChromatogram ChromatogramType
Chromatogram type.
Definition: MSExperiment.h:95
std::vector< SpectrumType >::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSExperiment.h:105
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
static void addData_(SpectrumType *spectrum, const ContainerValueType *item)
specialization for adding feature mass traces (does not support metadata_names currently) ...
Definition: MSExperiment.h:673
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
RangeManager< RangeRT, RangeMZ, RangeIntensity, RangeMobility > RangeManagerType
RangeManager type.
Definition: MSExperiment.h:89
void get2DData(Container &cont) const
Reads out a 2D Spectrum.
Definition: MSExperiment.h:216
std::vector< SpectrumType > Base
STL base class type.
Definition: MSExperiment.h:97
A 1-dimensional raw data point or peak for chromatograms.
Definition: ChromatogramPeak.h:53
void set2DData(const Container &container)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:247
Definition: RangeManager.h:896
ConstIterator begin() const
Definition: MSExperiment.h:187
UInt64 total_size_
Number of all data points.
Definition: MSExperiment.h:629
static void addData_(SpectrumType *spectrum, const ContainerValueType *item, const StringList &store_metadata_names)
general method for adding data points, including metadata arrays (populated from metainfointerface) ...
Definition: MSExperiment.h:657
int Int
Signed integer type.
Definition: Types.h:102
const SpectrumType & operator[](Size n) const
Random access to n&#39;th spectrum.
Definition: MSExperiment.h:177
Description of the experimental settings.
Definition: ExperimentalSettings.h:59
std::vector< UInt > ms_levels_
MS levels of the data.
Definition: MSExperiment.h:627