OpenMS  2.4.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-2018.
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 
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 & operator=(const MSExperiment & source);
190 
192  MSExperiment & operator=(const ExperimentalSettings & source);
193 
195  bool operator==(const MSExperiment & rhs) const;
196 
198  bool operator!=(const MSExperiment & rhs) const;
199 
201 
202 
208  template <class Container>
209  void get2DData(Container & cont) const
210  {
211  for (typename Base::const_iterator spec = spectra_.begin(); spec != spectra_.end(); ++spec)
212  {
213  if (spec->getMSLevel() != 1)
214  {
215  continue;
216  }
217  typename Container::value_type s; // explicit object here, since instantiation within push_back() fails on VS<12
218  for (typename SpectrumType::const_iterator it = spec->begin(); it != spec->end(); ++it)
219  {
220  cont.push_back(s);
221  cont.back().setRT(spec->getRT());
222  cont.back().setMZ(it->getMZ());
223  cont.back().setIntensity(it->getIntensity());
224  }
225  }
226  }
227 
239  template <class Container>
240  void set2DData(const Container& container)
241  {
242  set2DData<false, Container>(container);
243  }
244 
259  template <class Container>
260  void set2DData(const Container& container, const StringList& store_metadata_names)
261  {
262  // clean up the container first
263  clear(true);
264  SpectrumType* spectrum = nullptr;
265  typename PeakType::CoordinateType current_rt = -std::numeric_limits<typename PeakType::CoordinateType>::max();
266  for (typename Container::const_iterator iter = container.begin(); iter != container.end(); ++iter)
267  {
268  // check if the retention time has changed
269  if (current_rt != iter->getRT() || spectrum == nullptr)
270  {
271  // append new spectrum
272  if (current_rt > iter->getRT())
273  {
274  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Input container is not sorted!");
275  }
276  current_rt = iter->getRT();
277  spectrum = createSpec_(current_rt, store_metadata_names);
278  }
279 
280  // add either data point or mass traces (depending on template argument value)
281  ContainerAdd_<typename Container::value_type, false>::addData_(spectrum, &(*iter), store_metadata_names);
282  }
283  }
284 
302  template <bool add_mass_traces, class Container>
303  void set2DData(const Container& container)
304  {
305  // clean up the container first
306  clear(true);
307  SpectrumType* spectrum = nullptr;
308  typename PeakType::CoordinateType current_rt = -std::numeric_limits<typename PeakType::CoordinateType>::max();
309  for (typename Container::const_iterator iter = container.begin(); iter != container.end(); ++iter)
310  {
311  // check if the retention time has changed
312  if (current_rt != iter->getRT() || spectrum == nullptr)
313  {
314  // append new spectrum
315  if (current_rt > iter->getRT())
316  {
317  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Input container is not sorted!");
318  }
319  current_rt = iter->getRT();
320  spectrum = createSpec_(current_rt);
321  }
322 
323  // add either data point or mass traces (depending on template argument value)
325  }
326  }
327 
329 
330 
332 
333  AreaIterator areaBegin(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz);
335 
337  AreaIterator areaEnd();
338 
340  ConstAreaIterator areaBeginConst(CoordinateType min_rt, CoordinateType max_rt, CoordinateType min_mz, CoordinateType max_mz) const;
341 
343  ConstAreaIterator areaEndConst() const;
344 
352  ConstIterator RTBegin(CoordinateType rt) const;
353 
361  ConstIterator RTEnd(CoordinateType rt) const;
362 
368  Iterator RTBegin(CoordinateType rt);
369 
375  Iterator RTEnd(CoordinateType rt);
376 
378 
384  // Docu in base class
386  void updateRanges() override;
387 
393  void updateRanges(Int ms_level);
394 
396  CoordinateType getMinMZ() const;
397 
399  CoordinateType getMaxMZ() const;
400 
402  CoordinateType getMinRT() const;
403 
405  CoordinateType getMaxRT() const;
406 
412  const AreaType & getDataRange() const;
413 
415  UInt64 getSize() const;
416 
418  const std::vector<UInt> & getMSLevels() const;
419 
421 
424 
429  void sortSpectra(bool sort_mz = true);
430 
436  void sortChromatograms(bool sort_rt = true);
437 
443  bool isSorted(bool check_mz = true) const;
444 
446 
448  void reset();
449 
455  bool clearMetaDataArrays();
456 
458  const ExperimentalSettings & getExperimentalSettings() const;
459 
461  ExperimentalSettings & getExperimentalSettings();
462 
464  void getPrimaryMSRunPath(StringList& toFill) const;
465 
471  ConstIterator getPrecursorSpectrum(ConstIterator iterator) const;
472 
474  void swap(MSExperiment & from);
475 
477  void setSpectra(const std::vector<MSSpectrum> & spectra);
478 
480  void addSpectrum(const MSSpectrum & spectrum);
481 
483  const std::vector<MSSpectrum> & getSpectra() const;
484 
486  std::vector<MSSpectrum> & getSpectra();
487 
489  void setChromatograms(const std::vector<MSChromatogram > & chromatograms);
490 
492  void addChromatogram(const MSChromatogram & chromatogram);
493 
495  const std::vector<MSChromatogram > & getChromatograms() const;
496 
498  std::vector<MSChromatogram > & getChromatograms();
499 
501 
502  MSChromatogram & getChromatogram(Size id);
504 
506  MSSpectrum & getSpectrum(Size id);
507 
509  Size getNrSpectra() const;
510 
512  Size getNrChromatograms() const;
514 
516  const MSChromatogram getTIC() const;
517 
523  void clear(bool clear_meta_data);
524 
525 protected:
526 
527 
529  std::vector<UInt> ms_levels_;
532 
534  std::vector<MSChromatogram > chromatograms_;
535 
537  std::vector<SpectrumType> spectra_;
538 
539 private:
540 
542  template<typename ContainerValueType, bool addMassTraces>
544  {
545  static void addData_(SpectrumType* spectrum, const ContainerValueType* item);
546  static void addData_(SpectrumType* spectrum, const ContainerValueType* item, const StringList& store_metadata_names);
547  };
548 
549  template<typename ContainerValueType>
550  struct ContainerAdd_<ContainerValueType, false>
551  {
553  static void addData_(SpectrumType* spectrum, const ContainerValueType* item)
554  {
555  // create temporary peak and insert it into spectrum
556  spectrum->insert(spectrum->end(), PeakType());
557  spectrum->back().setIntensity(item->getIntensity());
558  spectrum->back().setPosition(item->getMZ());
559  }
561  static void addData_(SpectrumType* spectrum, const ContainerValueType* item, const StringList& store_metadata_names)
562  {
563  addData_(spectrum, item);
564  for (StringList::const_iterator itm = store_metadata_names.begin(); itm != store_metadata_names.end(); ++itm)
565  {
566  float val = std::numeric_limits<float>::quiet_NaN();
567  if (item->metaValueExists(*itm)) val = item->getMetaValue(*itm);
568  spectrum->getFloatDataArrays()[itm - store_metadata_names.begin()].push_back(val);
569  }
570  }
571  };
572 
573  template<typename ContainerValueType>
574  struct ContainerAdd_<ContainerValueType, true>
575  {
577  static void addData_(SpectrumType* spectrum, const ContainerValueType* item)
578  {
579  if (item->metaValueExists("num_of_masstraces"))
580  {
581  Size mts = item->getMetaValue("num_of_masstraces");
582  int charge = (item->getCharge()==0 ? 1 : item->getCharge()); // set to 1 if charge is 0, otherwise div/0 below
583  for (Size i = 0; i < mts; ++i)
584  {
585  String meta_name = String("masstrace_intensity_") + i;
586  if (!item->metaValueExists(meta_name))
587  {
588  throw Exception::Precondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Meta value '") + meta_name + "' expected but not found in container.");
589  }
590  ContainerValueType p;
591  p.setIntensity(item->getMetaValue(meta_name));
592  p.setPosition(item->getMZ() + Constants::C13C12_MASSDIFF_U / charge * i);
594  }
595  }
597  }
598  };
599 
600  /*
601  @brief Append a spectrum to current MSExperiment
602 
603  @param rt RT of new spectrum
604  @return Pointer to newly created spectrum
605  */
606  SpectrumType* createSpec_(PeakType::CoordinateType rt);
607 
608  /*
609  @brief Append a spectrum including floatdata arrays to current MSExperiment
610 
611  @param rt RT of new spectrum
612  @param metadata_names Names of floatdata arrays attached to this spectrum
613  @return Pointer to newly created spectrum
614  */
615  SpectrumType* createSpec_(PeakType::CoordinateType rt, const StringList& metadata_names);
616 
617  };
618 
619 
621  std::ostream & operator<<(std::ostream & os, const MSExperiment & exp);
622 
623 } // namespace OpenMS
624 
626 
627 
const double C13C12_MASSDIFF_U
A more convenient string class.
Definition: String.h:57
void set2DData(const Container &container)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:303
static void addData_(SpectrumType *spectrum, const ContainerValueType *item)
general method for adding data points
Definition: MSExperiment.h:553
std::vector< SpectrumType >::iterator Iterator
Mutable iterator.
Definition: MSExperiment.h:111
Peak1D PeakT
Definition: MSExperiment.h:83
The representation of a chromatogram.
Definition: MSChromatogram.h:54
Base::value_type value_type
Definition: MSExperiment.h:123
Peak2D PeakType
Definition: MassTrace.h:47
ChromatogramPeakT ChromatogramPeakType
Chromatogram peak type.
Definition: MSExperiment.h:91
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:57
ConstIterator end() const
Definition: MSExperiment.h:172
std::vector< MSChromatogram > chromatograms_
chromatograms
Definition: MSExperiment.h:534
Iterator begin()
Definition: MSExperiment.h:157
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806
Base::const_iterator const_iterator
Definition: MSExperiment.h:125
void resize(Size s)
Definition: MSExperiment.h:132
PeakType::IntensityType IntensityType
Intensity type of peaks.
Definition: MSExperiment.h:97
Size size() const
Definition: MSExperiment.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Precondition failed exception.
Definition: Exception.h:166
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
MSSpectrum SpectrumType
Spectrum Type.
Definition: MSExperiment.h:101
RangeManager< 2 > RangeManagerType
RangeManager type.
Definition: MSExperiment.h:99
Iterator end()
Definition: MSExperiment.h:167
Base::iterator iterator
Definition: MSExperiment.h:124
The representation of a 1D spectrum.
Definition: MSSpectrum.h:66
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
void reserve(Size s)
Definition: MSExperiment.h:142
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:260
Helper class to add either general data points in set2DData or use mass traces from meta values...
Definition: MSExperiment.h:543
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:54
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
ChromatogramPeak ChromatogramPeakT
Definition: MSExperiment.h:84
PeakType::CoordinateType CoordinateType
Coordinate type of peak positions.
Definition: MSExperiment.h:95
bool empty() const
Definition: MSExperiment.h:137
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
std::vector< SpectrumType > spectra_
spectra
Definition: MSExperiment.h:537
DRange< 2 > AreaType
Area type.
Definition: MSExperiment.h:93
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
MSChromatogram ChromatogramType
Chromatogram type.
Definition: MSExperiment.h:103
std::vector< SpectrumType >::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSExperiment.h:113
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:577
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
void get2DData(Container &cont) const
Reads out a 2D Spectrum.
Definition: MSExperiment.h:209
std::vector< SpectrumType > Base
STL base class type.
Definition: MSExperiment.h:105
A 1-dimensional raw data point or peak for chromatograms.
Definition: ChromatogramPeak.h:54
Handles the management of a position and intensity range.
Definition: RangeManager.h:47
void set2DData(const Container &container)
Assignment of a data container with RT and MZ to an MSExperiment.
Definition: MSExperiment.h:240
ConstIterator begin() const
Definition: MSExperiment.h:162
UInt64 total_size_
Number of all data points.
Definition: MSExperiment.h:531
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:561
int Int
Signed integer type.
Definition: Types.h:102
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
Description of the experimental settings.
Definition: ExperimentalSettings.h:58
std::vector< UInt > ms_levels_
MS levels of the data.
Definition: MSExperiment.h:529