OpenMS  2.7.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-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, Tom Waschischeck $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
44 
45 #include <vector>
46 
47 
48 namespace OpenMS
49 {
50  class Peak1D;
51  class ChromatogramPeak;
52 
77  class OPENMS_DLLAPI MSExperiment :
78  public RangeManager<2>,
80  {
81 
82 public:
83  typedef Peak1D PeakT;
85 
87 
88  typedef PeakT PeakType;
105  typedef std::vector<SpectrumType> Base;
107 
109 
110  typedef std::vector<SpectrumType>::iterator Iterator;
113  typedef std::vector<SpectrumType>::const_iterator ConstIterator;
119 
121  // Attention: these refer to the spectra vector only!
123  typedef Base::value_type value_type;
124  typedef Base::iterator iterator;
125  typedef Base::const_iterator const_iterator;
126 
127  inline Size size() const
128  {
129  return spectra_.size();
130  }
131 
132  inline void resize(Size s)
133  {
134  spectra_.resize(s);
135  }
136 
137  inline bool empty() const
138  {
139  return spectra_.empty();
140  }
141 
142  inline void reserve(Size s)
143  {
144  spectra_.reserve(s);
145  }
146 
147  inline SpectrumType& operator[] (Size n)
148  {
149  return spectra_[n];
150  }
151 
152  inline const SpectrumType& operator[] (Size n) const
153  {
154  return spectra_[n];
155  }
156 
157  inline Iterator begin()
158  {
159  return spectra_.begin();
160  }
161 
162  inline ConstIterator begin() const
163  {
164  return spectra_.begin();
165  }
166 
167  inline Iterator end()
168  {
169  return spectra_.end();
170  }
171 
172  inline ConstIterator end() const
173  {
174  return spectra_.end();
175  }
177 
178  // Aliases / chromatograms
181 
184 
186  MSExperiment(const MSExperiment & source);
187 
190 
193 
196 
199 
201  bool operator==(const MSExperiment & rhs) const;
202 
204  bool operator!=(const MSExperiment & rhs) const;
205 
207 
208 
214  template <class Container>
215  void get2DData(Container& cont) const
216  {
217  for (typename Base::const_iterator spec = spectra_.begin(); spec != spectra_.end(); ++spec)
218  {
219  if (spec->getMSLevel() != 1)
220  {
221  continue;
222  }
223  typename Container::value_type s; // explicit object here, since instantiation within push_back() fails on VS<12
224  for (typename SpectrumType::const_iterator it = spec->begin(); it != spec->end(); ++it)
225  {
226  cont.push_back(s);
227  cont.back().setRT(spec->getRT());
228  cont.back().setMZ(it->getMZ());
229  cont.back().setIntensity(it->getIntensity());
230  }
231  }
232  }
233 
245  template <class Container>
246  void set2DData(const Container& container)
247  {
248  set2DData<false, Container>(container);
249  }
250 
265  template <class Container>
266  void set2DData(const Container& container, const StringList& store_metadata_names)
267  {
268  // clean up the container first
269  clear(true);
270  SpectrumType* spectrum = nullptr;
271  typename PeakType::CoordinateType current_rt = -std::numeric_limits<typename PeakType::CoordinateType>::max();
272  for (typename Container::const_iterator iter = container.begin(); iter != container.end(); ++iter)
273  {
274  // check if the retention time has changed
275  if (current_rt != iter->getRT() || spectrum == nullptr)
276  {
277  // append new spectrum
278  if (current_rt > iter->getRT())
279  {
280  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Input container is not sorted!");
281  }
282  current_rt = iter->getRT();
283  spectrum = createSpec_(current_rt, store_metadata_names);
284  }
285 
286  // add either data point or mass traces (depending on template argument value)
287  ContainerAdd_<typename Container::value_type, false>::addData_(spectrum, &(*iter), store_metadata_names);
288  }
289  }
290 
308  template <bool add_mass_traces, class Container>
309  void set2DData(const Container& container)
310  {
311  // clean up the container first
312  clear(true);
313  SpectrumType* spectrum = nullptr;
314  typename PeakType::CoordinateType current_rt = -std::numeric_limits<typename PeakType::CoordinateType>::max();
315  for (typename Container::const_iterator iter = container.begin(); iter != container.end(); ++iter)
316  {
317  // check if the retention time has changed
318  if (current_rt != iter->getRT() || spectrum == nullptr)
319  {
320  // append new spectrum
321  if (current_rt > iter->getRT())
322  {
323  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Input container is not sorted!");
324  }
325  current_rt = iter->getRT();
326  spectrum = createSpec_(current_rt);
327  }
328 
329  // add either data point or mass traces (depending on template argument value)
331  }
332  }
333 
335 
336 
338 
339  AreaIterator areaBegin(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz);
341 
344 
347 
350 
359 
368 
375 
382 
384 
391  // Docu in base class
392  void updateRanges() override;
393 
399  void updateRanges(Int ms_level);
400 
403 
406 
409 
412 
418  const AreaType& getDataRange() const;
419 
421  UInt64 getSize() const;
422 
424  const std::vector<UInt>& getMSLevels() const;
425 
427 
431 
433  void setSqlRunID(UInt64 id);
434 
437 
442  void sortSpectra(bool sort_mz = true);
443 
449  void sortChromatograms(bool sort_rt = true);
450 
456  bool isSorted(bool check_mz = true) const;
457 
459 
461  void reset();
462 
469 
472 
475 
477  void getPrimaryMSRunPath(StringList& toFill) const;
478 
485 
487  void swap(MSExperiment& from);
488 
490  void setSpectra(const std::vector<MSSpectrum>& spectra);
491 
493  void addSpectrum(const MSSpectrum& spectrum);
494 
495 
496  void addSpectrum(MSSpectrum&& spectrum)
497  {
498  spectra_.push_back(std::forward<MSSpectrum>(spectrum));
499  }
500 
502  const std::vector<MSSpectrum>& getSpectra() const;
503 
505  std::vector<MSSpectrum>& getSpectra();
506 
508  void setChromatograms(const std::vector<MSChromatogram>& chromatograms);
509 
511  void addChromatogram(const MSChromatogram& chromatogram);
512 
514  {
515  chromatograms_.push_back(std::forward<MSChromatogram>(chrom));
516  }
517 
519  const std::vector<MSChromatogram>& getChromatograms() const;
520 
522  std::vector<MSChromatogram>& getChromatograms();
523 
525 
526  MSChromatogram& getChromatogram(Size id);
528 
531 
534 
538 
549  const MSChromatogram calculateTIC(float rt_bin_size = 0, UInt ms_level = 1) const;
550 
556  void clear(bool clear_meta_data);
557 
559  bool containsScanOfLevel(size_t ms_level) const;
560 
562  bool hasZeroIntensities(size_t ms_level) const;
563 
566 
567  protected:
569  std::vector<UInt> ms_levels_;
573  std::vector<MSChromatogram > chromatograms_;
575  std::vector<SpectrumType> spectra_;
576 
577 private:
578 
580  template<typename ContainerValueType, bool addMassTraces>
582  {
583  static void addData_(SpectrumType* spectrum, const ContainerValueType* item);
584  static void addData_(SpectrumType* spectrum, const ContainerValueType* item, const StringList& store_metadata_names);
585  };
586 
587  template<typename ContainerValueType>
588  struct ContainerAdd_<ContainerValueType, false>
589  {
591  static void addData_(SpectrumType* spectrum, const ContainerValueType* item)
592  {
593  // create temporary peak and insert it into spectrum
594  spectrum->insert(spectrum->end(), PeakType());
595  spectrum->back().setIntensity(item->getIntensity());
596  spectrum->back().setPosition(item->getMZ());
597  }
599  static void addData_(SpectrumType* spectrum, const ContainerValueType* item, const StringList& store_metadata_names)
600  {
601  addData_(spectrum, item);
602  for (StringList::const_iterator itm = store_metadata_names.begin(); itm != store_metadata_names.end(); ++itm)
603  {
604  float val = std::numeric_limits<float>::quiet_NaN();
605  if (item->metaValueExists(*itm)) val = item->getMetaValue(*itm);
606  spectrum->getFloatDataArrays()[itm - store_metadata_names.begin()].push_back(val);
607  }
608  }
609  };
610 
611  template<typename ContainerValueType>
612  struct ContainerAdd_<ContainerValueType, true>
613  {
615  static void addData_(SpectrumType* spectrum, const ContainerValueType* item)
616  {
617  if (item->metaValueExists("num_of_masstraces"))
618  {
619  Size mts = item->getMetaValue("num_of_masstraces");
620  int charge = (item->getCharge()==0 ? 1 : item->getCharge()); // set to 1 if charge is 0, otherwise div/0 below
621  for (Size i = 0; i < mts; ++i)
622  {
623  String meta_name = String("masstrace_intensity_") + i;
624  if (!item->metaValueExists(meta_name))
625  {
626  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Meta value '") + meta_name + "' expected but not found in container.");
627  }
628  ContainerValueType p;
629  p.setIntensity(item->getMetaValue(meta_name));
630  p.setPosition(item->getMZ() + Constants::C13C12_MASSDIFF_U / charge * i);
632  }
633  }
635  }
636  };
637 
638  /*
639  @brief Append a spectrum to current MSExperiment
640 
641  @param rt RT of new spectrum
642  @return Pointer to newly created spectrum
643  */
645 
646  /*
647  @brief Append a spectrum including floatdata arrays to current MSExperiment
648 
649  @param rt RT of new spectrum
650  @param metadata_names Names of floatdata arrays attached to this spectrum
651  @return Pointer to newly created spectrum
652  */
654 
655  };
656 
658  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const MSExperiment& exp);
659 
660 } // namespace OpenMS
661 
663 
664 
A 1-dimensional raw data point or peak for chromatograms.
Definition: ChromatogramPeak.h:54
Precondition failed exception.
Definition: Exception.h:160
Description of the experimental settings.
Definition: ExperimentalSettings.h:62
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:59
The representation of a chromatogram.
Definition: MSChromatogram.h:58
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:80
const std::vector< UInt > & getMSLevels() const
returns an array of MS levels
const ExperimentalSettings & getExperimentalSettings() const
returns the meta information of this experiment (const access)
MSExperiment(MSExperiment &&)=default
Move constructor.
RangeManager< 2 > RangeManagerType
RangeManager type.
Definition: MSExperiment.h:99
std::vector< SpectrumType > spectra_
spectra
Definition: MSExperiment.h:575
const std::vector< MSChromatogram > & getChromatograms() const
returns the chromatogram list
std::vector< SpectrumType > Base
STL base class type.
Definition: MSExperiment.h:105
Base::iterator iterator
Definition: MSExperiment.h:124
bool containsScanOfLevel(size_t ms_level) const
returns true if at least one of the spectra has the specified level
DRange< 2 > AreaType
Area type.
Definition: MSExperiment.h:93
PeakType::CoordinateType CoordinateType
Coordinate type of peak positions.
Definition: MSExperiment.h:95
void get2DData(Container &cont) const
Reads out a 2D Spectrum.
Definition: MSExperiment.h:215
void swap(MSExperiment &from)
Swaps the content of this map with the content of from.
MSChromatogram ChromatogramType
Chromatogram type.
Definition: MSExperiment.h:103
void addSpectrum(const MSSpectrum &spectrum)
adds a spectrum to the list
PeakType::IntensityType IntensityType
Intensity type of peaks.
Definition: MSExperiment.h:97
Iterator begin()
Definition: MSExperiment.h:157
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:266
std::vector< SpectrumType >::iterator Iterator
Mutable iterator.
Definition: MSExperiment.h:111
bool clearMetaDataArrays()
Clears the meta data arrays of all contained spectra (float, integer and string arrays)
SpectrumType * createSpec_(PeakType::CoordinateType rt)
ConstIterator RTBegin(CoordinateType rt) const
Fast search for spectrum range begin.
Size getNrSpectra() const
get the total number of spectra available
UInt64 getSize() const
returns the total number of peaks
MSExperiment & operator=(MSExperiment &&) &=default
Move assignment operator.
ConstIterator RTEnd(CoordinateType rt) const
Fast search for spectrum range end (returns the past-the-end iterator)
void addChromatogram(MSChromatogram &&chrom)
Definition: MSExperiment.h:513
ExperimentalSettings & getExperimentalSettings()
returns the meta information of this experiment (mutable access)
void reserveSpaceChromatograms(Size s)
SpectrumType * createSpec_(PeakType::CoordinateType rt, const StringList &metadata_names)
ChromatogramPeakT ChromatogramPeakType
Chromatogram peak type.
Definition: MSExperiment.h:91
MSSpectrum & getSpectrum(Size id)
returns a single spectrum
const AreaType & getDataRange() const
Returns RT and m/z range the data lies in.
CoordinateType getMinMZ() const
returns the minimal m/z value
Iterator RTEnd(CoordinateType rt)
Fast search for spectrum range end (returns the past-the-end iterator)
MSExperiment & operator=(const MSExperiment &source)
Assignment operator.
MSSpectrum SpectrumType
Spectrum Type.
Definition: MSExperiment.h:101
bool empty() const
Definition: MSExperiment.h:137
Size getNrChromatograms() const
get the total number of chromatograms available
ConstIterator end() const
Definition: MSExperiment.h:172
CoordinateType getMaxRT() const
returns the maximal retention time value
Base::value_type value_type
Definition: MSExperiment.h:123
MSExperiment()
Constructor.
bool operator!=(const MSExperiment &rhs) const
Equality operator.
CoordinateType getMaxMZ() const
returns the maximal m/z value
Peak1D PeakT
Definition: MSExperiment.h:83
ConstAreaIterator areaEndConst() const
Returns an non-mutable invalid area iterator marking the end of an area.
void set2DData(const Container &container)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:246
void getPrimaryMSRunPath(StringList &toFill) const
get the file path to the first MS run
std::vector< UInt > ms_levels_
MS levels of the data.
Definition: MSExperiment.h:569
ConstAreaIterator areaBeginConst(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz) const
Returns a non-mutable area iterator for area.
std::vector< MSChromatogram > & getChromatograms()
returns the chromatogram list (mutable)
void setSpectra(const std::vector< MSSpectrum > &spectra)
sets the spectrum list
void updateRanges(Int ms_level)
Updates the m/z, intensity, retention time and MS level ranges of all spectra with a certain ms level...
void sortChromatograms(bool sort_rt=true)
Sorts the data points of the chromatograms by m/z.
Iterator RTBegin(CoordinateType rt)
Fast search for spectrum range begin.
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:117
void setChromatograms(const std::vector< MSChromatogram > &chromatograms)
sets the chromatogram list
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:115
CoordinateType getMinRT() const
returns the minimal retention time value
const MSChromatogram calculateTIC(float rt_bin_size=0, UInt ms_level=1) const
Computes the total ion chromatogram (TIC) for a given MS level (use ms_level = 0 for all levels).
const std::vector< MSSpectrum > & getSpectra() const
returns the spectrum list
Iterator end()
Definition: MSExperiment.h:167
UInt64 total_size_
Number of all data points.
Definition: MSExperiment.h:571
bool isSorted(bool check_mz=true) const
Checks if all spectra are sorted with respect to ascending RT.
MSExperiment(const MSExperiment &source)
Copy constructor.
std::vector< MSSpectrum > & getSpectra()
returns the spectrum list (mutable)
void resize(Size s)
Definition: MSExperiment.h:132
void set2DData(const Container &container)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:309
ChromatogramPeak ChromatogramPeakT
Definition: MSExperiment.h:84
void sortSpectra(bool sort_mz=true)
Sorts the data points by retention time.
Size size() const
Definition: MSExperiment.h:127
void reset()
Resets all internal values.
ConstIterator begin() const
Definition: MSExperiment.h:162
void addSpectrum(MSSpectrum &&spectrum)
Definition: MSExperiment.h:496
void reserveSpaceSpectra(Size s)
bool hasZeroIntensities(size_t ms_level) const
returns true if any MS spectra of the specified level contain at least one peak with intensity of 0....
bool operator==(const MSExperiment &rhs) const
Equality operator.
ConstIterator getPrecursorSpectrum(ConstIterator iterator) const
Returns the precursor spectrum of the scan pointed to by iterator.
Base::const_iterator const_iterator
Definition: MSExperiment.h:125
void updateRanges() override
Updates minimum and maximum position/intensity.
void setSqlRunID(UInt64 id)
sets the run-ID which is used when storing an sqMass file
MSExperiment & operator=(const ExperimentalSettings &source)
Assignment operator.
UInt64 getSqlRunID() const
void addChromatogram(const MSChromatogram &chromatogram)
adds a chromatogram to the list
std::vector< SpectrumType >::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSExperiment.h:113
bool hasPeptideIdentifications() const
do any of the spectra have a PeptideID?
void clear(bool clear_meta_data)
Clears all data and meta data.
void reserve(Size s)
Definition: MSExperiment.h:142
std::vector< MSChromatogram > chromatograms_
chromatograms
Definition: MSExperiment.h:573
AreaIterator areaEnd()
Returns an invalid area iterator marking the end of an area.
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
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
Handles the management of a position and intensity range.
Definition: RangeManager.h:48
A more convenient string class.
Definition: String.h:61
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
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
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
const double C13C12_MASSDIFF_U
Definition: Constants.h:121
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Peak2D PeakType
Definition: MassTrace.h:47
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
static void addData_(SpectrumType *spectrum, const ContainerValueType *item)
general method for adding data points
Definition: MSExperiment.h:591
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:599
static void addData_(SpectrumType *spectrum, const ContainerValueType *item)
specialization for adding feature mass traces (does not support metadata_names currently)
Definition: MSExperiment.h:615
Helper class to add either general data points in set2DData or use mass traces from meta values.
Definition: MSExperiment.h:582
static void addData_(SpectrumType *spectrum, const ContainerValueType *item)
static void addData_(SpectrumType *spectrum, const ContainerValueType *item, const StringList &store_metadata_names)