Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
MSChromatogram.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-2013.
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: Andreas Bertsch $
32 // $Authors: Andreas Bertsch $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_KERNEL_MSCHROMATOGRAM_H
36 #define OPENMS_KERNEL_MSCHROMATOGRAM_H
37 
44 
45 
46 namespace OpenMS
47 {
52  template <typename PeakT = ChromatogramPeak>
54  public std::vector<PeakT>,
55  public RangeManager<1>,
56  public ChromatogramSettings,
57  public PersistentObject
58  {
59 
60 public:
61 
63  class OPENMS_DLLAPI FloatDataArray :
64  public MetaInfoDescription,
65  public std::vector<Real>
66  {};
67 
69  class OPENMS_DLLAPI StringDataArray :
70  public MetaInfoDescription,
71  public std::vector<String>
72  {};
73 
75  class OPENMS_DLLAPI IntegerDataArray :
76  public MetaInfoDescription,
77  public std::vector<Int>
78  {};
79 
81  struct MZLess :
82  public std::binary_function<MSChromatogram, MSChromatogram, bool>
83  {
84  inline bool operator()(const MSChromatogram & a, const MSChromatogram & b) const
85  {
86  return a.getMZ() < b.getMZ();
87  }
88 
89  };
90 
94  typedef PeakT PeakType;
98  typedef std::vector<PeakType> ContainerType;
100  typedef std::vector<FloatDataArray> FloatDataArrays;
102  typedef std::vector<StringDataArray> StringDataArrays;
104  typedef std::vector<IntegerDataArray> IntegerDataArrays;
106 
108 
109  typedef typename ContainerType::iterator Iterator;
112  typedef typename ContainerType::const_iterator ConstIterator;
114  typedef typename ContainerType::reverse_iterator ReverseIterator;
116  typedef typename ContainerType::const_reverse_iterator ConstReverseIterator;
118 
119 
122  ContainerType(),
123  RangeManager<1>(),
126  name_(),
130  {}
131 
133  MSChromatogram(const MSChromatogram & source) :
134  ContainerType(source),
135  RangeManager<1>(source),
136  ChromatogramSettings(source),
137  PersistentObject(source),
138  name_(source.name_),
142  {}
143 
145  virtual ~MSChromatogram()
146  {}
147 
150  {
151  if (&source == this) return *this;
152 
153  ContainerType::operator=(source);
157 
158  name_ = source.name_;
162 
163  return *this;
164  }
165 
167  bool operator==(const MSChromatogram & rhs) const
168  {
169  //name_ can differ => it is not checked
170  return std::operator==(*this, rhs) &&
176  }
177 
179  bool operator!=(const MSChromatogram & rhs) const
180  {
181  return !(operator==(rhs));
182  }
183 
184  // Docu in base class (RangeManager)
185  virtual void updateRanges()
186  {
187  this->clearRanges();
188  updateRanges_(ContainerType::begin(), ContainerType::end());
189  }
190 
194  inline const String & getName() const
195  {
196  return name_;
197  }
198 
200  inline void setName(const String & name)
201  {
202  name_ = name;
203  }
204 
206 
208  inline DoubleReal getMZ() const
209  {
210  return getProduct().getMZ();
211  }
212 
225  inline const FloatDataArrays & getFloatDataArrays() const
228  {
229  return float_data_arrays_;
230  }
231 
234  {
235  return float_data_arrays_;
236  }
237 
239  inline const StringDataArrays & getStringDataArrays() const
240  {
241  return string_data_arrays_;
242  }
243 
246  {
247  return string_data_arrays_;
248  }
249 
252  {
253  return integer_data_arrays_;
254  }
255 
258  {
259  return integer_data_arrays_;
260  }
261 
263 
266 
271  void sortByIntensity(bool reverse = false)
272  {
273  if (float_data_arrays_.empty() && string_data_arrays_.size() && integer_data_arrays_.size())
274  {
275  if (reverse)
276  {
277  std::sort(ContainerType::begin(), ContainerType::end(), reverseComparator(typename PeakType::IntensityLess()));
278  }
279  else
280  {
281  std::sort(ContainerType::begin(), ContainerType::end(), typename PeakType::IntensityLess());
282  }
283  }
284  else
285  {
286  //sort index list
287  std::vector<std::pair<typename PeakType::IntensityType, Size> > sorted_indices;
288  sorted_indices.reserve(ContainerType::size());
289  for (Size i = 0; i < ContainerType::size(); ++i)
290  {
291  sorted_indices.push_back(std::make_pair(ContainerType::operator[](i).getIntensity(), i));
292  }
293 
294  if (reverse)
295  {
296  std::sort(sorted_indices.begin(), sorted_indices.end(), reverseComparator(PairComparatorFirstElement<std::pair<typename PeakType::IntensityType, Size> >()));
297  }
298  else
299  {
300  std::sort(sorted_indices.begin(), sorted_indices.end(), PairComparatorFirstElement<std::pair<typename PeakType::IntensityType, Size> >());
301  }
302 
303  //apply sorting to ContainerType and to meta data arrays
304  ContainerType tmp;
305  for (Size i = 0; i < sorted_indices.size(); ++i)
306  {
307  tmp.push_back(*(ContainerType::begin() + (sorted_indices[i].second)));
308  }
309  ContainerType::swap(tmp);
310 
311  for (Size i = 0; i < float_data_arrays_.size(); ++i)
312  {
313  std::vector<Real> mda_tmp;
314  for (Size j = 0; j < float_data_arrays_[i].size(); ++j)
315  {
316  mda_tmp.push_back(*(float_data_arrays_[i].begin() + (sorted_indices[j].second)));
317  }
318  float_data_arrays_[i].swap(mda_tmp);
319  }
320 
321  for (Size i = 0; i < string_data_arrays_.size(); ++i)
322  {
323  std::vector<String> mda_tmp;
324  for (Size j = 0; j < string_data_arrays_[i].size(); ++j)
325  {
326  mda_tmp.push_back(*(string_data_arrays_[i].begin() + (sorted_indices[j].second)));
327  }
328  string_data_arrays_[i].swap(mda_tmp);
329  }
330 
331  for (Size i = 0; i < integer_data_arrays_.size(); ++i)
332  {
333  std::vector<Int> mda_tmp;
334  for (Size j = 0; j < integer_data_arrays_[i].size(); ++j)
335  {
336  mda_tmp.push_back(*(integer_data_arrays_[i].begin() + (sorted_indices[j].second)));
337  }
338  integer_data_arrays_[i].swap(mda_tmp);
339  }
340  }
341  }
342 
350  {
351  if (float_data_arrays_.empty())
352  {
353  std::sort(ContainerType::begin(), ContainerType::end(), typename PeakType::PositionLess());
354  }
355  else
356  {
357  //sort index list
358  std::vector<std::pair<typename PeakType::PositionType, Size> > sorted_indices;
359  sorted_indices.reserve(ContainerType::size());
360  for (Size i = 0; i < ContainerType::size(); ++i)
361  {
362  sorted_indices.push_back(std::make_pair(ContainerType::operator[](i).getPosition(), i));
363  }
364  std::sort(sorted_indices.begin(), sorted_indices.end(), PairComparatorFirstElement<std::pair<typename PeakType::PositionType, Size> >());
365 
366  //apply sorting to ContainerType and to metadataarrays
367  ContainerType tmp;
368  for (Size i = 0; i < sorted_indices.size(); ++i)
369  {
370  tmp.push_back(*(ContainerType::begin() + (sorted_indices[i].second)));
371  }
372  ContainerType::swap(tmp);
373 
374  for (Size i = 0; i < float_data_arrays_.size(); ++i)
375  {
376  std::vector<Real> mda_tmp;
377  for (Size j = 0; j < float_data_arrays_[i].size(); ++j)
378  {
379  mda_tmp.push_back(*(float_data_arrays_[i].begin() + (sorted_indices[j].second)));
380  }
381  std::swap(float_data_arrays_[i], mda_tmp);
382  }
383 
384  for (Size i = 0; i < string_data_arrays_.size(); ++i)
385  {
386  std::vector<String> mda_tmp;
387  for (Size j = 0; j < string_data_arrays_[i].size(); ++j)
388  {
389  mda_tmp.push_back(*(string_data_arrays_[i].begin() + (sorted_indices[j].second)));
390  }
391  std::swap(string_data_arrays_[i], mda_tmp);
392  }
393 
394  for (Size i = 0; i < integer_data_arrays_.size(); ++i)
395  {
396  std::vector<Int> mda_tmp;
397  for (Size j = 0; j < integer_data_arrays_[i].size(); ++j)
398  {
399  mda_tmp.push_back(*(integer_data_arrays_[i].begin() + (sorted_indices[j].second)));
400  }
401  std::swap(integer_data_arrays_[i], mda_tmp);
402  }
403  }
404  }
405 
407  bool isSorted() const
408  {
409  for (Size i = 1; i < this->size(); ++i)
410  {
411  if (this->operator[](i - 1).getRT() > this->operator[](i).getRT()) return false;
412  }
413  return true;
414  }
415 
417 
420 
431  {
432  //no peak => no search
433  if (ContainerType::size() == 0) throw Exception::Precondition(__FILE__, __LINE__, __PRETTY_FUNCTION__, "There must be at least one peak to determine the nearest peak!");
434 
435  //search for position for inserting
436  ConstIterator it = RTBegin(rt);
437  //border cases
438  if (it == ContainerType::begin()) return 0;
439 
440  if (it == ContainerType::end()) return ContainerType::size() - 1;
441 
442  //the peak before or the current peak are closest
443  ConstIterator it2 = it;
444  --it2;
445  if (std::fabs(it->getRT() - rt) < std::fabs(it2->getRT() - rt))
446  {
447  return Size(it - ContainerType::begin());
448  }
449  else
450  {
451  return Size(it2 - ContainerType::begin());
452  }
453  }
454 
462  {
463  PeakType p;
464  p.setPosition(rt);
465  return lower_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
466  }
467 
475  {
476  PeakType p;
477  p.setPosition(rt);
478  return lower_bound(begin, end, p, typename PeakType::PositionLess());
479  }
480 
488  {
489  PeakType p;
490  p.setPosition(rt);
491  return upper_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
492  }
493 
501  {
502  PeakType p;
503  p.setPosition(rt);
504  return upper_bound(begin, end, p, typename PeakType::PositionLess());
505  }
506 
514  {
515  PeakType p;
516  p.setPosition(rt);
517  return lower_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
518  }
519 
527  {
528  PeakType p;
529  p.setPosition(rt);
530  return lower_bound(begin, end, p, typename PeakType::PositionLess());
531  }
532 
540  {
541  PeakType p;
542  p.setPosition(rt);
543  return upper_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
544  }
545 
553  {
554  PeakType p;
555  p.setPosition(rt);
556  return upper_bound(begin, end, p, typename PeakType::PositionLess());
557  }
558 
564  void clear(bool clear_meta_data)
565  {
566  ContainerType::clear();
567 
568  if (clear_meta_data)
569  {
570  clearRanges();
571  clearId();
572  this->ChromatogramSettings::operator=(ChromatogramSettings()); // no "clear" method
573  name_.clear();
574  float_data_arrays_.clear();
575  string_data_arrays_.clear();
576  integer_data_arrays_.clear();
577  }
578  }
579 
581 
582 protected:
583  // Docu in base class
584  virtual void clearChildIds_()
585  {}
586 
589 
592 
595 
598  };
599 
601  template <typename PeakT>
602  std::ostream & operator<<(std::ostream & os, const MSChromatogram<PeakT> & spec)
603  {
604  os << "-- MSSPECTRUM BEGIN --" << std::endl;
605 
606  //chromatogram settings
607  os << static_cast<const ChromatogramSettings &>(spec);
608 
609  //peaklist
610  os << static_cast<const typename MSChromatogram<PeakT>::ContainerType &>(spec);
611 
612  os << "-- MSSPECTRUM END --" << std::endl;
613 
614  return os;
615  }
616 
617 } // namespace OpenMS
618 
619 #endif // OPENMS_KERNEL_MSCHROMATOGRAM_H
Iterator RTBegin(CoordinateType rt)
Binary search for peak range begin.
Definition: MSChromatogram.h:461
Iterator RTEnd(Iterator begin, CoordinateType rt, Iterator end)
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSChromatogram.h:500
FloatDataArrays & getFloatDataArrays()
Returns a mutable reference to the float meta data arrays.
Definition: MSChromatogram.h:233
FloatDataArrays float_data_arrays_
Float data arrays.
Definition: MSChromatogram.h:591
A more convenient string class.
Definition: String.h:56
const String & getName() const
Definition: MSChromatogram.h:194
bool operator==(const MSChromatogram &rhs) const
Equality operator.
Definition: MSChromatogram.h:167
Size findNearest(CoordinateType rt) const
Binary search for the peak nearest to a specific RT.
Definition: MSChromatogram.h:430
std::vector< IntegerDataArray > IntegerDataArrays
Integer data array vector type.
Definition: MSChromatogram.h:104
The representation of a chromatogram.
Definition: MSChromatogram.h:53
ContainerType::iterator Iterator
Mutable iterator.
Definition: MSChromatogram.h:110
std::vector< PeakType > ContainerType
Chromatogram base type.
Definition: MSChromatogram.h:98
ConstIterator RTEnd(CoordinateType rt) const
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSChromatogram.h:539
Iterator RTBegin(Iterator begin, CoordinateType rt, Iterator end)
Binary search for peak range begin.
Definition: MSChromatogram.h:474
bool operator()(const MSChromatogram &a, const MSChromatogram &b) const
Definition: MSChromatogram.h:84
Float data array class.
Definition: MSChromatogram.h:75
const StringDataArrays & getStringDataArrays() const
Returns a const reference to the string meta data arrays.
Definition: MSChromatogram.h:239
ReverseComparator< Cmp > reverseComparator(Cmp const &cmp)
Make-function to create a ReverseComparator from another comparator without the need to specify the t...
Definition: ComparatorUtils.h:261
ConstIterator RTBegin(CoordinateType rt) const
Binary search for peak range begin.
Definition: MSChromatogram.h:513
PeakType::CoordinateType CoordinateType
Coordinate (RT) type.
Definition: MSChromatogram.h:96
Definition: Peak2D.h:246
bool operator==(const RangeManager &rhs) const
Equality operator.
Definition: RangeManager.h:88
Precondition failed exception.
Definition: Exception.h:167
Description of the meta data arrays of MSSpectrum.
Definition: MetaInfoDescription.h:49
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSChromatogram.h:112
void setName(const String &name)
Sets the name.
Definition: MSChromatogram.h:200
Iterator RTEnd(CoordinateType rt)
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSChromatogram.h:487
virtual void updateRanges()
Updates minimum and maximum position/intensity.
Definition: MSChromatogram.h:185
StringDataArrays & getStringDataArrays()
Returns a mutable reference to the string meta data arrays.
Definition: MSChromatogram.h:245
MSChromatogram()
Constructor.
Definition: MSChromatogram.h:121
Comparator by position. Lexicographical comparison (first RT then m/z) is done.
Definition: Peak2D.h:324
ConstIterator RTBegin(ConstIterator begin, CoordinateType rt, ConstIterator end) const
Binary search for peak range begin.
Definition: MSChromatogram.h:526
void sortByIntensity(bool reverse=false)
Lexicographically sorts the peaks by their intensity.
Definition: MSChromatogram.h:271
MSChromatogram(const MSChromatogram &source)
Copy constructor.
Definition: MSChromatogram.h:133
virtual ~MSChromatogram()
Destructor.
Definition: MSChromatogram.h:145
std::vector< StringDataArray > StringDataArrays
String data array vector type.
Definition: MSChromatogram.h:102
Float data array class.
Definition: MSChromatogram.h:63
MSChromatogram & operator=(const MSChromatogram &source)
Assignment operator.
Definition: MSChromatogram.h:149
DoubleReal getMZ() const
returns the target m/z
Comparator for the retention time.
Definition: MSChromatogram.h:81
Base class for all persistent objects.
Definition: PersistentObject.h:52
RangeManager & operator=(const RangeManager &rhs)
Assignment operator.
Definition: RangeManager.h:77
PersistentObject & operator=(const PersistentObject &rhs)
Assignment operator.
void clearRanges()
Resets the ranges.
Definition: RangeManager.h:140
DoubleReal getMZ() const
returns the mz of the product entry, makes sense especially for MRM scans
Definition: MSChromatogram.h:208
std::vector< FloatDataArray > FloatDataArrays
Float data array vector type.
Definition: MSChromatogram.h:100
const Product & getProduct() const
returns a const reference to the products
PeakT PeakType
Definition: MSChromatogram.h:94
void clearId(bool deep=true)
Clears the persistence id.
Representation of chromatogram settings, e.g. SRM/MRM chromatograms.
Definition: ChromatogramSettings.h:59
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
Definition: MSChromatogram.h:251
StringDataArrays string_data_arrays_
String data arrays.
Definition: MSChromatogram.h:594
ContainerType::reverse_iterator ReverseIterator
Mutable reverse iterator.
Definition: MSChromatogram.h:114
bool operator!=(const MSChromatogram &rhs) const
Equality operator.
Definition: MSChromatogram.h:179
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
void sortByPosition()
Lexicographically sorts the peaks by their position.
Definition: MSChromatogram.h:349
IntegerDataArrays integer_data_arrays_
Intager data arrays.
Definition: MSChromatogram.h:597
ConstIterator RTEnd(ConstIterator begin, CoordinateType rt, ConstIterator end) const
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSChromatogram.h:552
ContainerType::const_reverse_iterator ConstReverseIterator
Non-mutable reverse iterator.
Definition: MSChromatogram.h:116
String name_
Name.
Definition: MSChromatogram.h:588
bool isSorted() const
Checks if all peaks are sorted with respect to ascending RT.
Definition: MSChromatogram.h:407
void updateRanges_(const PeakIteratorType &begin, const PeakIteratorType &end)
Updates the range using data points in the iterator range.
Definition: RangeManager.h:155
const FloatDataArrays & getFloatDataArrays() const
Definition: MSChromatogram.h:227
virtual void clearChildIds_()
Clears the persistence id of all sub-objects.
Definition: MSChromatogram.h:584
Handles the managment of a position and intensity range.
Definition: RangeManager.h:48
Class for comparison of std::pair using first ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:326
void clear(bool clear_meta_data)
Clears all data and meta data.
Definition: MSChromatogram.h:564
ChromatogramSettings & operator=(const ChromatogramSettings &source)
IntegerDataArrays & getIntegerDataArrays()
Returns a mutable reference to the integer meta data arrays.
Definition: MSChromatogram.h:257
String data array class.
Definition: MSChromatogram.h:69
bool operator==(const ChromatogramSettings &rhs) const
Equality operator.
ChromatogramSettings()
Constructor.

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:17 using doxygen 1.8.5