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-2015.
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 
43 
44 
45 namespace OpenMS
46 {
51  template <typename PeakT = ChromatogramPeak>
53  private std::vector<PeakT>,
54  public RangeManager<1>,
56  {
57 
58 public:
59 
62  public MetaInfoDescription,
63  public std::vector<float>
64  {};
65 
68  public MetaInfoDescription,
69  public std::vector<String>
70  {};
71 
74  public MetaInfoDescription,
75  public std::vector<Int>
76  {};
77 
79  struct MZLess :
80  public std::binary_function<MSChromatogram, MSChromatogram, bool>
81  {
82  inline bool operator()(const MSChromatogram& a, const MSChromatogram& b) const
83  {
84  return a.getMZ() < b.getMZ();
85  }
86 
87  };
88 
92  typedef PeakT PeakType;
96  typedef std::vector<PeakType> ContainerType;
98  typedef std::vector<FloatDataArray> FloatDataArrays;
100  typedef std::vector<StringDataArray> StringDataArrays;
102  typedef std::vector<IntegerDataArray> IntegerDataArrays;
104 
106 
107  typedef typename ContainerType::iterator Iterator;
110  typedef typename ContainerType::const_iterator ConstIterator;
112  typedef typename ContainerType::reverse_iterator ReverseIterator;
114  typedef typename ContainerType::const_reverse_iterator ConstReverseIterator;
116 
118 
119  using ContainerType::operator[];
120  using ContainerType::begin;
121  using ContainerType::rbegin;
122  using ContainerType::end;
123  using ContainerType::rend;
124  using ContainerType::resize;
125  using ContainerType::size;
126  using ContainerType::push_back;
127  using ContainerType::pop_back;
128  using ContainerType::empty;
129  using ContainerType::front;
130  using ContainerType::back;
131  using ContainerType::reserve;
132  using ContainerType::insert;
133  using ContainerType::erase;
134  using ContainerType::swap;
135 
136  using typename ContainerType::iterator;
137  using typename ContainerType::const_iterator;
138  using typename ContainerType::size_type;
139  using typename ContainerType::value_type;
140  using typename ContainerType::reference;
141  using typename ContainerType::const_reference;
142  using typename ContainerType::pointer;
143  using typename ContainerType::difference_type;
145 
148  ContainerType(),
149  RangeManager<1>(),
151  name_(),
155  {}
156 
159  ContainerType(source),
160  RangeManager<1>(source),
161  ChromatogramSettings(source),
162  name_(source.name_),
166  {}
167 
169  virtual ~MSChromatogram()
170  {}
171 
174  {
175  if (&source == this) return *this;
176 
177  ContainerType::operator=(source);
180 
181  name_ = source.name_;
185 
186  return *this;
187  }
188 
190  bool operator==(const MSChromatogram& rhs) const
191  {
192  //name_ can differ => it is not checked
193  return std::operator==(*this, rhs) &&
199  }
200 
202  bool operator!=(const MSChromatogram& rhs) const
203  {
204  return !(operator==(rhs));
205  }
206 
207  // Docu in base class (RangeManager)
208  virtual void updateRanges()
209  {
210  this->clearRanges();
211  updateRanges_(ContainerType::begin(), ContainerType::end());
212  }
213 
217  inline const String& getName() const
218  {
219  return name_;
220  }
221 
223  inline void setName(const String& name)
224  {
225  name_ = name;
226  }
227 
229 
231  inline double getMZ() const
232  {
233  return getProduct().getMZ();
234  }
235 
248  inline const FloatDataArrays& getFloatDataArrays() const
251  {
252  return float_data_arrays_;
253  }
254 
256  inline FloatDataArrays& getFloatDataArrays()
257  {
258  return float_data_arrays_;
259  }
260 
262  inline const StringDataArrays& getStringDataArrays() const
263  {
264  return string_data_arrays_;
265  }
266 
268  inline StringDataArrays& getStringDataArrays()
269  {
270  return string_data_arrays_;
271  }
272 
274  inline const IntegerDataArrays& getIntegerDataArrays() const
275  {
276  return integer_data_arrays_;
277  }
278 
280  inline IntegerDataArrays& getIntegerDataArrays()
281  {
282  return integer_data_arrays_;
283  }
284 
286 
289 
294  void sortByIntensity(bool reverse = false)
295  {
296  if (float_data_arrays_.empty() && string_data_arrays_.size() && integer_data_arrays_.size())
297  {
298  if (reverse)
299  {
300  std::sort(ContainerType::begin(), ContainerType::end(), reverseComparator(typename PeakType::IntensityLess()));
301  }
302  else
303  {
304  std::sort(ContainerType::begin(), ContainerType::end(), typename PeakType::IntensityLess());
305  }
306  }
307  else
308  {
309  //sort index list
310  std::vector<std::pair<typename PeakType::IntensityType, Size> > sorted_indices;
311  sorted_indices.reserve(ContainerType::size());
312  for (Size i = 0; i < ContainerType::size(); ++i)
313  {
314  sorted_indices.push_back(std::make_pair(ContainerType::operator[](i).getIntensity(), i));
315  }
316 
317  if (reverse)
318  {
319  std::sort(sorted_indices.begin(), sorted_indices.end(), reverseComparator(PairComparatorFirstElement<std::pair<typename PeakType::IntensityType, Size> >()));
320  }
321  else
322  {
323  std::sort(sorted_indices.begin(), sorted_indices.end(), PairComparatorFirstElement<std::pair<typename PeakType::IntensityType, Size> >());
324  }
325 
326  //apply sorting to ContainerType and to meta data arrays
327  ContainerType tmp;
328  for (Size i = 0; i < sorted_indices.size(); ++i)
329  {
330  tmp.push_back(*(ContainerType::begin() + (sorted_indices[i].second)));
331  }
332  ContainerType::swap(tmp);
333 
334  for (Size i = 0; i < float_data_arrays_.size(); ++i)
335  {
336  std::vector<float> mda_tmp;
337  for (Size j = 0; j < float_data_arrays_[i].size(); ++j)
338  {
339  mda_tmp.push_back(*(float_data_arrays_[i].begin() + (sorted_indices[j].second)));
340  }
341  float_data_arrays_[i].swap(mda_tmp);
342  }
343 
344  for (Size i = 0; i < string_data_arrays_.size(); ++i)
345  {
346  std::vector<String> mda_tmp;
347  for (Size j = 0; j < string_data_arrays_[i].size(); ++j)
348  {
349  mda_tmp.push_back(*(string_data_arrays_[i].begin() + (sorted_indices[j].second)));
350  }
351  string_data_arrays_[i].swap(mda_tmp);
352  }
353 
354  for (Size i = 0; i < integer_data_arrays_.size(); ++i)
355  {
356  std::vector<Int> mda_tmp;
357  for (Size j = 0; j < integer_data_arrays_[i].size(); ++j)
358  {
359  mda_tmp.push_back(*(integer_data_arrays_[i].begin() + (sorted_indices[j].second)));
360  }
361  integer_data_arrays_[i].swap(mda_tmp);
362  }
363  }
364  }
365 
373  {
374  if (float_data_arrays_.empty())
375  {
376  std::sort(ContainerType::begin(), ContainerType::end(), typename PeakType::PositionLess());
377  }
378  else
379  {
380  //sort index list
381  std::vector<std::pair<typename PeakType::PositionType, Size> > sorted_indices;
382  sorted_indices.reserve(ContainerType::size());
383  for (Size i = 0; i < ContainerType::size(); ++i)
384  {
385  sorted_indices.push_back(std::make_pair(ContainerType::operator[](i).getPosition(), i));
386  }
387  std::sort(sorted_indices.begin(), sorted_indices.end(), PairComparatorFirstElement<std::pair<typename PeakType::PositionType, Size> >());
388 
389  //apply sorting to ContainerType and to metadataarrays
390  ContainerType tmp;
391  for (Size i = 0; i < sorted_indices.size(); ++i)
392  {
393  tmp.push_back(*(ContainerType::begin() + (sorted_indices[i].second)));
394  }
395  ContainerType::swap(tmp);
396 
397  for (Size i = 0; i < float_data_arrays_.size(); ++i)
398  {
399  std::vector<float> mda_tmp;
400  for (Size j = 0; j < float_data_arrays_[i].size(); ++j)
401  {
402  mda_tmp.push_back(*(float_data_arrays_[i].begin() + (sorted_indices[j].second)));
403  }
404  std::swap(float_data_arrays_[i], mda_tmp);
405  }
406 
407  for (Size i = 0; i < string_data_arrays_.size(); ++i)
408  {
409  std::vector<String> mda_tmp;
410  for (Size j = 0; j < string_data_arrays_[i].size(); ++j)
411  {
412  mda_tmp.push_back(*(string_data_arrays_[i].begin() + (sorted_indices[j].second)));
413  }
414  std::swap(string_data_arrays_[i], mda_tmp);
415  }
416 
417  for (Size i = 0; i < integer_data_arrays_.size(); ++i)
418  {
419  std::vector<Int> mda_tmp;
420  for (Size j = 0; j < integer_data_arrays_[i].size(); ++j)
421  {
422  mda_tmp.push_back(*(integer_data_arrays_[i].begin() + (sorted_indices[j].second)));
423  }
424  std::swap(integer_data_arrays_[i], mda_tmp);
425  }
426  }
427  }
428 
430  bool isSorted() const
431  {
432  for (Size i = 1; i < this->size(); ++i)
433  {
434  if (this->operator[](i - 1).getRT() > this->operator[](i).getRT()) return false;
435  }
436  return true;
437  }
438 
440 
443 
453  Size findNearest(CoordinateType rt) const
454  {
455  //no peak => no search
456  if (ContainerType::size() == 0) throw Exception::Precondition(__FILE__, __LINE__, __PRETTY_FUNCTION__, "There must be at least one peak to determine the nearest peak!");
457 
458  //search for position for inserting
459  ConstIterator it = RTBegin(rt);
460  //border cases
461  if (it == ContainerType::begin()) return 0;
462 
463  if (it == ContainerType::end()) return ContainerType::size() - 1;
464 
465  //the peak before or the current peak are closest
466  ConstIterator it2 = it;
467  --it2;
468  if (std::fabs(it->getRT() - rt) < std::fabs(it2->getRT() - rt))
469  {
470  return Size(it - ContainerType::begin());
471  }
472  else
473  {
474  return Size(it2 - ContainerType::begin());
475  }
476  }
477 
484  Iterator RTBegin(CoordinateType rt)
485  {
486  PeakType p;
487  p.setPosition(rt);
488  return lower_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
489  }
490 
497  Iterator RTBegin(Iterator begin, CoordinateType rt, Iterator end)
498  {
499  PeakType p;
500  p.setPosition(rt);
501  return lower_bound(begin, end, p, typename PeakType::PositionLess());
502  }
503 
510  Iterator RTEnd(CoordinateType rt)
511  {
512  PeakType p;
513  p.setPosition(rt);
514  return upper_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
515  }
516 
523  Iterator RTEnd(Iterator begin, CoordinateType rt, Iterator end)
524  {
525  PeakType p;
526  p.setPosition(rt);
527  return upper_bound(begin, end, p, typename PeakType::PositionLess());
528  }
529 
536  ConstIterator RTBegin(CoordinateType rt) const
537  {
538  PeakType p;
539  p.setPosition(rt);
540  return lower_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
541  }
542 
549  ConstIterator RTBegin(ConstIterator begin, CoordinateType rt, ConstIterator end) const
550  {
551  PeakType p;
552  p.setPosition(rt);
553  return lower_bound(begin, end, p, typename PeakType::PositionLess());
554  }
555 
562  ConstIterator RTEnd(CoordinateType rt) const
563  {
564  PeakType p;
565  p.setPosition(rt);
566  return upper_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
567  }
568 
575  ConstIterator RTEnd(ConstIterator begin, CoordinateType rt, ConstIterator end) const
576  {
577  PeakType p;
578  p.setPosition(rt);
579  return upper_bound(begin, end, p, typename PeakType::PositionLess());
580  }
581 
587  void clear(bool clear_meta_data)
588  {
589  ContainerType::clear();
590 
591  if (clear_meta_data)
592  {
593  clearRanges();
594  this->ChromatogramSettings::operator=(ChromatogramSettings()); // no "clear" method
595  name_.clear();
596  float_data_arrays_.clear();
597  string_data_arrays_.clear();
598  integer_data_arrays_.clear();
599  }
600  }
601 
603 
604 protected:
605 
608 
610  FloatDataArrays float_data_arrays_;
611 
613  StringDataArrays string_data_arrays_;
614 
616  IntegerDataArrays integer_data_arrays_;
617  };
618 
620  template <typename PeakT>
621  std::ostream& operator<<(std::ostream& os, const MSChromatogram<PeakT>& chrom)
622  {
623  os << "-- MSCHROMATOGRAM BEGIN --" << std::endl;
624 
625  //chromatogram settings
626  os << static_cast<const ChromatogramSettings&>(chrom);
627 
628  //data list
629  for (typename MSChromatogram<PeakT>::ConstIterator it = chrom.begin(); it != chrom.end(); ++it)
630  {
631  os << *it << std::endl;
632  }
633 
634  os << "-- MSCHROMATOGRAM END --" << std::endl;
635 
636  return os;
637  }
638 
639 } // namespace OpenMS
640 
641 #endif // OPENMS_KERNEL_MSCHROMATOGRAM_H
Iterator RTBegin(CoordinateType rt)
Binary search for peak range begin.
Definition: MSChromatogram.h:484
Iterator RTEnd(Iterator begin, CoordinateType rt, Iterator end)
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSChromatogram.h:523
FloatDataArrays & getFloatDataArrays()
Returns a mutable reference to the float meta data arrays.
Definition: MSChromatogram.h:256
FloatDataArrays float_data_arrays_
Float data arrays.
Definition: MSChromatogram.h:610
A more convenient string class.
Definition: String.h:57
const String & getName() const
Definition: MSChromatogram.h:217
bool operator==(const MSChromatogram &rhs) const
Equality operator.
Definition: MSChromatogram.h:190
Size findNearest(CoordinateType rt) const
Binary search for the peak nearest to a specific RT.
Definition: MSChromatogram.h:453
std::vector< IntegerDataArray > IntegerDataArrays
Integer data array vector type.
Definition: MSChromatogram.h:102
The representation of a chromatogram.
Definition: MSChromatogram.h:52
ContainerType::iterator Iterator
Mutable iterator.
Definition: MSChromatogram.h:108
std::vector< PeakType > ContainerType
Chromatogram base type.
Definition: MSChromatogram.h:96
ConstIterator RTEnd(CoordinateType rt) const
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSChromatogram.h:562
Iterator RTBegin(Iterator begin, CoordinateType rt, Iterator end)
Binary search for peak range begin.
Definition: MSChromatogram.h:497
bool operator()(const MSChromatogram &a, const MSChromatogram &b) const
Definition: MSChromatogram.h:82
Float data array class.
Definition: MSChromatogram.h:73
const StringDataArrays & getStringDataArrays() const
Returns a const reference to the string meta data arrays.
Definition: MSChromatogram.h:262
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:536
PeakType::CoordinateType CoordinateType
Coordinate (RT) type.
Definition: MSChromatogram.h:94
Definition: Peak2D.h:249
bool operator==(const RangeManager &rhs) const
Equality operator.
Definition: RangeManager.h:88
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
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:110
void setName(const String &name)
Sets the name.
Definition: MSChromatogram.h:223
Iterator RTEnd(CoordinateType rt)
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSChromatogram.h:510
virtual void updateRanges()
Updates minimum and maximum position/intensity.
Definition: MSChromatogram.h:208
StringDataArrays & getStringDataArrays()
Returns a mutable reference to the string meta data arrays.
Definition: MSChromatogram.h:268
MSChromatogram()
Constructor.
Definition: MSChromatogram.h:147
Comparator by position. Lexicographical comparison (first RT then m/z) is done.
Definition: Peak2D.h:327
double getMZ() const
returns the target m/z
ConstIterator RTBegin(ConstIterator begin, CoordinateType rt, ConstIterator end) const
Binary search for peak range begin.
Definition: MSChromatogram.h:549
void sortByIntensity(bool reverse=false)
Lexicographically sorts the peaks by their intensity.
Definition: MSChromatogram.h:294
MSChromatogram(const MSChromatogram &source)
Copy constructor.
Definition: MSChromatogram.h:158
virtual ~MSChromatogram()
Destructor.
Definition: MSChromatogram.h:169
std::vector< StringDataArray > StringDataArrays
String data array vector type.
Definition: MSChromatogram.h:100
Float data array class.
Definition: MSChromatogram.h:61
MSChromatogram & operator=(const MSChromatogram &source)
Assignment operator.
Definition: MSChromatogram.h:173
Comparator for the retention time.
Definition: MSChromatogram.h:79
RangeManager & operator=(const RangeManager &rhs)
Assignment operator.
Definition: RangeManager.h:77
void clearRanges()
Resets the ranges.
Definition: RangeManager.h:140
std::vector< FloatDataArray > FloatDataArrays
Float data array vector type.
Definition: MSChromatogram.h:98
const Product & getProduct() const
returns a const reference to the products
PeakT PeakType
Definition: MSChromatogram.h:92
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:274
StringDataArrays string_data_arrays_
String data arrays.
Definition: MSChromatogram.h:613
ContainerType::reverse_iterator ReverseIterator
Mutable reverse iterator.
Definition: MSChromatogram.h:112
bool operator!=(const MSChromatogram &rhs) const
Equality operator.
Definition: MSChromatogram.h:202
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:121
void sortByPosition()
Lexicographically sorts the peaks by their position.
Definition: MSChromatogram.h:372
IntegerDataArrays integer_data_arrays_
Integer data arrays.
Definition: MSChromatogram.h:616
ConstIterator RTEnd(ConstIterator begin, CoordinateType rt, ConstIterator end) const
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSChromatogram.h:575
ContainerType::const_reverse_iterator ConstReverseIterator
Non-mutable reverse iterator.
Definition: MSChromatogram.h:114
String name_
Name.
Definition: MSChromatogram.h:607
bool isSorted() const
Checks if all peaks are sorted with respect to ascending RT.
Definition: MSChromatogram.h:430
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:250
Handles the management 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:587
ChromatogramSettings & operator=(const ChromatogramSettings &source)
double getMZ() const
returns the mz of the product entry, makes sense especially for MRM scans
Definition: MSChromatogram.h:231
IntegerDataArrays & getIntegerDataArrays()
Returns a mutable reference to the integer meta data arrays.
Definition: MSChromatogram.h:280
String data array class.
Definition: MSChromatogram.h:67
bool operator==(const ChromatogramSettings &rhs) const
Equality operator.
ChromatogramSettings()
Constructor.

OpenMS / TOPP release 2.0.0 Documentation generated on Tue Aug 25 2015 05:53:49 using doxygen 1.8.9.1