OpenMS  2.6.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-2020.
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
179  void reserveSpaceSpectra(Size s);
180  void reserveSpaceChromatograms(Size s);
181 
183  MSExperiment();
184 
186  MSExperiment(const MSExperiment & source);
187 
189  MSExperiment(MSExperiment&&) = default;
190 
192  MSExperiment & operator=(const MSExperiment & source);
193 
195  MSExperiment& operator=(MSExperiment&&) & = default;
196 
198  MSExperiment & operator=(const ExperimentalSettings & source);
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 
343  AreaIterator areaEnd();
344 
346  ConstAreaIterator areaBeginConst(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz) const;
347 
349  ConstAreaIterator areaEndConst() const;
350 
358  ConstIterator RTBegin(CoordinateType rt) const;
359 
367  ConstIterator RTEnd(CoordinateType rt) const;
368 
374  Iterator RTBegin(CoordinateType rt);
375 
381  Iterator RTEnd(CoordinateType rt);
382 
384 
390  // Docu in base class
392  void updateRanges() override;
393 
399  void updateRanges(Int ms_level);
400 
402  CoordinateType getMinMZ() const;
403 
405  CoordinateType getMaxMZ() const;
406 
408  CoordinateType getMinRT() const;
409 
411  CoordinateType getMaxRT() const;
412 
418  const AreaType& getDataRange() const;
419 
421  UInt64 getSize() const;
422 
424  const std::vector<UInt>& getMSLevels() const;
425 
427 
430 
435  void sortSpectra(bool sort_mz = true);
436 
442  void sortChromatograms(bool sort_rt = true);
443 
449  bool isSorted(bool check_mz = true) const;
450 
452 
454  void reset();
455 
461  bool clearMetaDataArrays();
462 
464  const ExperimentalSettings& getExperimentalSettings() const;
465 
467  ExperimentalSettings& getExperimentalSettings();
468 
470  void getPrimaryMSRunPath(StringList& toFill) const;
471 
477  ConstIterator getPrecursorSpectrum(ConstIterator iterator) const;
478 
480  void swap(MSExperiment& from);
481 
483  void setSpectra(const std::vector<MSSpectrum>& spectra);
484 
486  void addSpectrum(const MSSpectrum& spectrum);
487 
488 
489  void addSpectrum(MSSpectrum&& spectrum)
490  {
491  spectra_.push_back(std::forward<MSSpectrum>(spectrum));
492  }
493 
495  const std::vector<MSSpectrum>& getSpectra() const;
496 
498  std::vector<MSSpectrum>& getSpectra();
499 
501  void setChromatograms(const std::vector<MSChromatogram>& chromatograms);
502 
504  void addChromatogram(const MSChromatogram& chromatogram);
505 
507  {
508  chromatograms_.push_back(std::forward<MSChromatogram>(chrom));
509  }
510 
512  const std::vector<MSChromatogram>& getChromatograms() const;
513 
515  std::vector<MSChromatogram>& getChromatograms();
516 
518 
519  MSChromatogram& getChromatogram(Size id);
521 
523  MSSpectrum& getSpectrum(Size id);
524 
526  Size getNrSpectra() const;
527 
529  Size getNrChromatograms() const;
531 
540  const MSChromatogram getTIC(float rt_bin_size = 0) const;
541 
547  void clear(bool clear_meta_data);
548 
550  bool containsScanOfLevel(size_t ms_level) const;
551 
553  bool hasZeroIntensities(size_t ms_level) const;
554 
556  bool hasPeptideIdentifications() const;
557 
558 protected:
559 
561  std::vector<UInt> ms_levels_;
564 
566  std::vector<MSChromatogram > chromatograms_;
567 
569  std::vector<SpectrumType> spectra_;
570 
571 private:
572 
574  template<typename ContainerValueType, bool addMassTraces>
576  {
577  static void addData_(SpectrumType* spectrum, const ContainerValueType* item);
578  static void addData_(SpectrumType* spectrum, const ContainerValueType* item, const StringList& store_metadata_names);
579  };
580 
581  template<typename ContainerValueType>
582  struct ContainerAdd_<ContainerValueType, false>
583  {
585  static void addData_(SpectrumType* spectrum, const ContainerValueType* item)
586  {
587  // create temporary peak and insert it into spectrum
588  spectrum->insert(spectrum->end(), PeakType());
589  spectrum->back().setIntensity(item->getIntensity());
590  spectrum->back().setPosition(item->getMZ());
591  }
593  static void addData_(SpectrumType* spectrum, const ContainerValueType* item, const StringList& store_metadata_names)
594  {
595  addData_(spectrum, item);
596  for (StringList::const_iterator itm = store_metadata_names.begin(); itm != store_metadata_names.end(); ++itm)
597  {
598  float val = std::numeric_limits<float>::quiet_NaN();
599  if (item->metaValueExists(*itm)) val = item->getMetaValue(*itm);
600  spectrum->getFloatDataArrays()[itm - store_metadata_names.begin()].push_back(val);
601  }
602  }
603  };
604 
605  template<typename ContainerValueType>
606  struct ContainerAdd_<ContainerValueType, true>
607  {
609  static void addData_(SpectrumType* spectrum, const ContainerValueType* item)
610  {
611  if (item->metaValueExists("num_of_masstraces"))
612  {
613  Size mts = item->getMetaValue("num_of_masstraces");
614  int charge = (item->getCharge()==0 ? 1 : item->getCharge()); // set to 1 if charge is 0, otherwise div/0 below
615  for (Size i = 0; i < mts; ++i)
616  {
617  String meta_name = String("masstrace_intensity_") + i;
618  if (!item->metaValueExists(meta_name))
619  {
620  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Meta value '") + meta_name + "' expected but not found in container.");
621  }
622  ContainerValueType p;
623  p.setIntensity(item->getMetaValue(meta_name));
624  p.setPosition(item->getMZ() + Constants::C13C12_MASSDIFF_U / charge * i);
626  }
627  }
629  }
630  };
631 
632  /*
633  @brief Append a spectrum to current MSExperiment
634 
635  @param rt RT of new spectrum
636  @return Pointer to newly created spectrum
637  */
638  SpectrumType* createSpec_(PeakType::CoordinateType rt);
639 
640  /*
641  @brief Append a spectrum including floatdata arrays to current MSExperiment
642 
643  @param rt RT of new spectrum
644  @param metadata_names Names of floatdata arrays attached to this spectrum
645  @return Pointer to newly created spectrum
646  */
647  SpectrumType* createSpec_(PeakType::CoordinateType rt, const StringList& metadata_names);
648 
649  };
650 
652  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const MSExperiment& exp);
653 
654 } // namespace OpenMS
655 
657 
658 
OpenMS::PeakType
Peak2D PeakType
Definition: MassTrace.h:47
OpenMS::MSExperiment::set2DData
void set2DData(const Container &container)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:246
OpenMS::UInt64
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
DRange.h
AreaIterator.h
double
OpenMS::Internal::AreaIterator
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:57
OpenMS::MSExperiment::Base
std::vector< SpectrumType > Base
STL base class type.
Definition: MSExperiment.h:105
OpenMS::MSExperiment::set2DData
void set2DData(const Container &container)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:309
OpenMS::MSExperiment::value_type
Base::value_type value_type
Definition: MSExperiment.h:123
OpenMS::ExperimentalSettings
Description of the experimental settings.
Definition: ExperimentalSettings.h:59
OpenMS::String
A more convenient string class.
Definition: String.h:59
OpenMS::MSExperiment::begin
Iterator begin()
Definition: MSExperiment.h:157
OpenMS::Exception::Precondition
Precondition failed exception.
Definition: Exception.h:166
OpenMS::DRange< 2 >
KDTree::operator!=
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
OpenMS::MSExperiment
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::MSExperiment::ChromatogramPeakType
ChromatogramPeakT ChromatogramPeakType
Chromatogram peak type.
Definition: MSExperiment.h:91
OpenMS::MSExperiment::IntensityType
PeakType::IntensityType IntensityType
Intensity type of peaks.
Definition: MSExperiment.h:97
OpenMS::MSExperiment::set2DData
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
OpenMS::MSExperiment::CoordinateType
PeakType::CoordinateType CoordinateType
Coordinate type of peak positions.
Definition: MSExperiment.h:95
OpenMS::MSExperiment::ContainerAdd_< ContainerValueType, false >::addData_
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:593
OpenMS::MSExperiment::const_iterator
Base::const_iterator const_iterator
Definition: MSExperiment.h:125
OpenMS::MSExperiment::RangeManagerType
RangeManager< 2 > RangeManagerType
RangeManager type.
Definition: MSExperiment.h:99
OpenMS::MSExperiment::size
Size size() const
Definition: MSExperiment.h:127
OpenMS::MSExperiment::SpectrumType
MSSpectrum SpectrumType
Spectrum Type.
Definition: MSExperiment.h:101
OpenMS::MSExperiment::reserve
void reserve(Size s)
Definition: MSExperiment.h:142
OpenMS::MSExperiment::ChromatogramType
MSChromatogram ChromatogramType
Chromatogram type.
Definition: MSExperiment.h:103
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::MSExperiment::Iterator
std::vector< SpectrumType >::iterator Iterator
Mutable iterator.
Definition: MSExperiment.h:111
OpenMS::RangeManager
Handles the management of a position and intensity range.
Definition: RangeManager.h:47
Exception.h
OpenMS::MSExperiment::ContainerAdd_
Helper class to add either general data points in set2DData or use mass traces from meta values.
Definition: MSExperiment.h:575
OpenMS::MSExperiment::iterator
Base::iterator iterator
Definition: MSExperiment.h:124
OpenMS::MSExperiment::end
ConstIterator end() const
Definition: MSExperiment.h:172
int
OpenMS::ChromatogramPeak
A 1-dimensional raw data point or peak for chromatograms.
Definition: ChromatogramPeak.h:53
OpenMS::MSExperiment::ChromatogramPeakT
ChromatogramPeak ChromatogramPeakT
Definition: MSExperiment.h:84
OpenMS::MSExperiment::begin
ConstIterator begin() const
Definition: MSExperiment.h:162
OpenMS::MSExperiment::empty
bool empty() const
Definition: MSExperiment.h:137
OpenMS::MSExperiment::AreaType
DRange< 2 > AreaType
Area type.
Definition: MSExperiment.h:93
OpenMS::Peak1D
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:54
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
OpenMS::MSExperiment::addChromatogram
void addChromatogram(MSChromatogram &&chrom)
Definition: MSExperiment.h:506
OpenMS::MSExperiment::resize
void resize(Size s)
Definition: MSExperiment.h:132
OpenMS::MSSpectrum::getFloatDataArrays
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
Iterator
ExperimentalSettings.h
OpenMS::MSExperiment::addSpectrum
void addSpectrum(MSSpectrum &&spectrum)
Definition: MSExperiment.h:489
OpenMS::Internal::operator==
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
OpenMS::MSExperiment::end
Iterator end()
Definition: MSExperiment.h:167
MSChromatogram.h
float
OpenMS::MSExperiment::PeakT
Peak1D PeakT
Definition: MSExperiment.h:83
OpenMS::MSChromatogram
The representation of a chromatogram.
Definition: MSChromatogram.h:54
StandardDeclarations.h
OpenMS::MSExperiment::ContainerAdd_< ContainerValueType, true >::addData_
static void addData_(SpectrumType *spectrum, const ContainerValueType *item)
specialization for adding feature mass traces (does not support metadata_names currently)
Definition: MSExperiment.h:609
OpenMS::MSExperiment::ms_levels_
std::vector< UInt > ms_levels_
MS levels of the data.
Definition: MSExperiment.h:561
OpenMS::MSExperiment::chromatograms_
std::vector< MSChromatogram > chromatograms_
chromatograms
Definition: MSExperiment.h:566
OpenMS::MSExperiment::get2DData
void get2DData(Container &cont) const
Reads out a 2D Spectrum.
Definition: MSExperiment.h:215
OpenMS::MSExperiment::ConstIterator
std::vector< SpectrumType >::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSExperiment.h:113
OpenMS::MSExperiment::AreaIterator
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
OpenMS::MSExperiment::ConstAreaIterator
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
OpenMS::Constants::C13C12_MASSDIFF_U
const double C13C12_MASSDIFF_U
OpenMS::MSExperiment::ContainerAdd_< ContainerValueType, false >::addData_
static void addData_(SpectrumType *spectrum, const ContainerValueType *item)
general method for adding data points
Definition: MSExperiment.h:585
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
StandardTypes.h
OpenMS::MSExperiment::total_size_
UInt64 total_size_
Number of all data points.
Definition: MSExperiment.h:563
MSSpectrum.h
OpenMS::MSExperiment::spectra_
std::vector< SpectrumType > spectra_
spectra
Definition: MSExperiment.h:569