Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
MSSpectrum.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: Stephan Aiche$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_KERNEL_MSSPECTRUM_H
36 #define OPENMS_KERNEL_MSSPECTRUM_H
37 
43 
44 namespace OpenMS
45 {
46  class Peak1D;
47 
66  template <typename PeakT = Peak1D>
67  class MSSpectrum :
68  public std::vector<PeakT>,
69  public RangeManager<1>,
70  public SpectrumSettings,
71  public PersistentObject
72  {
73 public:
74 
76  class OPENMS_DLLAPI FloatDataArray :
77  public MetaInfoDescription,
78  public std::vector<Real>
79  {};
80 
82  class OPENMS_DLLAPI IntegerDataArray :
83  public MetaInfoDescription,
84  public std::vector<Int>
85  {};
86 
88  class OPENMS_DLLAPI StringDataArray :
89  public MetaInfoDescription,
90  public std::vector<String>
91  {};
92 
94  struct RTLess :
95  public std::binary_function<MSSpectrum, MSSpectrum, bool>
96  {
97  inline bool operator()(const MSSpectrum & a, const MSSpectrum & b) const
98  {
99  return a.getRT() < b.getRT();
100  }
101 
102  };
103 
105 
106  typedef PeakT PeakType;
111  typedef std::vector<PeakType> ContainerType;
113  typedef std::vector<FloatDataArray> FloatDataArrays;
115  typedef std::vector<StringDataArray> StringDataArrays;
117  typedef std::vector<IntegerDataArray> IntegerDataArrays;
119 
121 
122  typedef typename ContainerType::iterator Iterator;
125  typedef typename ContainerType::const_iterator ConstIterator;
127  typedef typename ContainerType::reverse_iterator ReverseIterator;
129  typedef typename ContainerType::const_reverse_iterator ConstReverseIterator;
131 
132 
135  ContainerType(),
136  RangeManager<1>(),
139  retention_time_(-1),
140  ms_level_(1),
141  name_(),
145  {}
146 
148  MSSpectrum(const MSSpectrum & source) :
149  ContainerType(source),
150  RangeManager<1>(source),
151  SpectrumSettings(source),
152  PersistentObject(source),
154  ms_level_(source.ms_level_),
155  name_(source.name_),
159  {}
160 
163  {}
164 
166  MSSpectrum & operator=(const MSSpectrum & source)
167  {
168  if (&source == this) return *this;
169 
170  ContainerType::operator=(source);
174 
176  ms_level_ = source.ms_level_;
177  name_ = source.name_;
181 
182  return *this;
183  }
184 
186  bool operator==(const MSSpectrum & rhs) const
187  {
188  //name_ can differ => it is not checked
189  return std::operator==(*this, rhs) &&
193  ms_level_ == rhs.ms_level_ &&
197  }
198 
200  bool operator!=(const MSSpectrum & rhs) const
201  {
202  return !(operator==(rhs));
203  }
204 
205  // Docu in base class (RangeManager)
206  virtual void updateRanges()
207  {
208  this->clearRanges();
209  updateRanges_(ContainerType::begin(), ContainerType::end());
210  }
211 
215  inline DoubleReal getRT() const
216  {
217  return retention_time_;
218  }
219 
221  inline void setRT(DoubleReal rt)
222  {
223  retention_time_ = rt;
224  }
225 
231  inline UInt getMSLevel() const
232  {
233  return ms_level_;
234  }
235 
237  inline void setMSLevel(UInt ms_level)
238  {
239  ms_level_ = ms_level;
240  }
241 
243  inline const String & getName() const
244  {
245  return name_;
246  }
247 
249  inline void setName(const String & name)
250  {
251  name_ = name;
252  }
253 
255 
269  inline const FloatDataArrays & getFloatDataArrays() const
271  {
272  return float_data_arrays_;
273  }
274 
277  {
278  return float_data_arrays_;
279  }
280 
282  inline const StringDataArrays & getStringDataArrays() const
283  {
284  return string_data_arrays_;
285  }
286 
289  {
290  return string_data_arrays_;
291  }
292 
295  {
296  return integer_data_arrays_;
297  }
298 
301  {
302  return integer_data_arrays_;
303  }
304 
306 
308 
309 
314  void sortByIntensity(bool reverse = false)
315  {
316  if (float_data_arrays_.empty() && string_data_arrays_.empty() && integer_data_arrays_.empty())
317  {
318  if (reverse)
319  {
320  std::sort(ContainerType::begin(), ContainerType::end(), reverseComparator(typename PeakType::IntensityLess()));
321  }
322  else
323  {
324  std::sort(ContainerType::begin(), ContainerType::end(), typename PeakType::IntensityLess());
325  }
326  }
327  else
328  {
329  //sort index list
330  std::vector<std::pair<typename PeakType::IntensityType, Size> > sorted_indices;
331  sorted_indices.reserve(ContainerType::size());
332  for (Size i = 0; i < ContainerType::size(); ++i)
333  {
334  sorted_indices.push_back(std::make_pair(ContainerType::operator[](i).getIntensity(), i));
335  }
336 
337  if (reverse)
338  {
339  std::sort(sorted_indices.begin(), sorted_indices.end(), reverseComparator(PairComparatorFirstElement<std::pair<typename PeakType::IntensityType, Size> >()));
340  }
341  else
342  {
343  std::sort(sorted_indices.begin(), sorted_indices.end(), PairComparatorFirstElement<std::pair<typename PeakType::IntensityType, Size> >());
344  }
345 
346  //apply sorting to ContainerType and to meta data arrays
347  ContainerType tmp;
348  for (Size i = 0; i < sorted_indices.size(); ++i)
349  {
350  tmp.push_back(*(ContainerType::begin() + (sorted_indices[i].second)));
351  }
352  ContainerType::swap(tmp);
353 
354  for (Size i = 0; i < float_data_arrays_.size(); ++i)
355  {
356  std::vector<Real> mda_tmp;
357  for (Size j = 0; j < float_data_arrays_[i].size(); ++j)
358  {
359  mda_tmp.push_back(*(float_data_arrays_[i].begin() + (sorted_indices[j].second)));
360  }
361  float_data_arrays_[i].swap(mda_tmp);
362  }
363 
364  for (Size i = 0; i < string_data_arrays_.size(); ++i)
365  {
366  std::vector<String> mda_tmp;
367  for (Size j = 0; j < string_data_arrays_[i].size(); ++j)
368  {
369  mda_tmp.push_back(*(string_data_arrays_[i].begin() + (sorted_indices[j].second)));
370  }
371  string_data_arrays_[i].swap(mda_tmp);
372  }
373 
374  for (Size i = 0; i < integer_data_arrays_.size(); ++i)
375  {
376  std::vector<Int> mda_tmp;
377  for (Size j = 0; j < integer_data_arrays_[i].size(); ++j)
378  {
379  mda_tmp.push_back(*(integer_data_arrays_[i].begin() + (sorted_indices[j].second)));
380  }
381  integer_data_arrays_[i].swap(mda_tmp);
382  }
383  }
384  }
385 
392  {
393  if (float_data_arrays_.empty())
394  {
395  std::sort(ContainerType::begin(), ContainerType::end(), typename PeakType::PositionLess());
396  }
397  else
398  {
399  //sort index list
400  std::vector<std::pair<typename PeakType::PositionType, Size> > sorted_indices;
401  sorted_indices.reserve(ContainerType::size());
402  for (Size i = 0; i < ContainerType::size(); ++i)
403  {
404  sorted_indices.push_back(std::make_pair(ContainerType::operator[](i).getPosition(), i));
405  }
406  std::sort(sorted_indices.begin(), sorted_indices.end(), PairComparatorFirstElement<std::pair<typename PeakType::PositionType, Size> >());
407 
408  //apply sorting to ContainerType and to metadataarrays
409  ContainerType tmp;
410  tmp.reserve(sorted_indices.size());
411  for (Size i = 0; i < sorted_indices.size(); ++i)
412  {
413  tmp.push_back(*(ContainerType::begin() + (sorted_indices[i].second)));
414  }
415  ContainerType::swap(tmp);
416 
417  for (Size i = 0; i < float_data_arrays_.size(); ++i)
418  {
419  std::vector<Real> mda_tmp;
420  mda_tmp.reserve(float_data_arrays_[i].size());
421  for (Size j = 0; j < float_data_arrays_[i].size(); ++j)
422  {
423  mda_tmp.push_back(*(float_data_arrays_[i].begin() + (sorted_indices[j].second)));
424  }
425  std::swap(float_data_arrays_[i], mda_tmp);
426  }
427 
428  for (Size i = 0; i < string_data_arrays_.size(); ++i)
429  {
430  std::vector<String> mda_tmp;
431  mda_tmp.reserve(string_data_arrays_[i].size());
432  for (Size j = 0; j < string_data_arrays_[i].size(); ++j)
433  {
434  mda_tmp.push_back(*(string_data_arrays_[i].begin() + (sorted_indices[j].second)));
435  }
436  std::swap(string_data_arrays_[i], mda_tmp);
437  }
438 
439  for (Size i = 0; i < integer_data_arrays_.size(); ++i)
440  {
441  std::vector<Int> mda_tmp;
442  mda_tmp.reserve(integer_data_arrays_[i].size());
443  for (Size j = 0; j < integer_data_arrays_[i].size(); ++j)
444  {
445  mda_tmp.push_back(*(integer_data_arrays_[i].begin() + (sorted_indices[j].second)));
446  }
447  std::swap(integer_data_arrays_[i], mda_tmp);
448  }
449  }
450  }
451 
453  bool isSorted() const
454  {
455  for (Size i = 1; i < this->size(); ++i)
456  {
457  if (this->operator[](i - 1).getMZ() > this->operator[](i).getMZ()) return false;
458  }
459  return true;
460  }
461 
463 
466 
477  {
478  // no peak => no search
479  if (ContainerType::size() == 0) throw Exception::Precondition(__FILE__, __LINE__, __PRETTY_FUNCTION__, "There must be at least one peak to determine the nearest peak!");
480 
481  // search for position for inserting
482  ConstIterator it = MZBegin(mz);
483  // border cases
484  if (it == ContainerType::begin()) return 0;
485 
486  if (it == ContainerType::end()) return ContainerType::size() - 1;
487 
488  // the peak before or the current peak are closest
489  ConstIterator it2 = it;
490  --it2;
491  if (std::fabs(it->getMZ() - mz) < std::fabs(it2->getMZ() - mz))
492  {
493  return Size(it - ContainerType::begin());
494  }
495  else
496  {
497  return Size(it2 - ContainerType::begin());
498  }
499  }
500 
507  {
508  PeakType p;
509  p.setPosition(mz);
510  return lower_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
511  }
512 
519  {
520  PeakType p;
521  p.setPosition(mz);
522  return lower_bound(begin, end, p, typename PeakType::PositionLess());
523  }
524 
531  {
532  PeakType p;
533  p.setPosition(mz);
534  return upper_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
535  }
536 
543  {
544  PeakType p;
545  p.setPosition(mz);
546  return upper_bound(begin, end, p, typename PeakType::PositionLess());
547  }
548 
555  {
556  PeakType p;
557  p.setPosition(mz);
558  return lower_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
559  }
560 
567  {
568  PeakType p;
569  p.setPosition(mz);
570  return lower_bound(begin, end, p, typename PeakType::PositionLess());
571  }
572 
579  {
580  PeakType p;
581  p.setPosition(mz);
582  return upper_bound(ContainerType::begin(), ContainerType::end(), p, typename PeakType::PositionLess());
583  }
584 
591  {
592  PeakType p;
593  p.setPosition(mz);
594  return upper_bound(begin, end, p, typename PeakType::PositionLess());
595  }
596 
598 
599 
605  void clear(bool clear_meta_data)
606  {
607  ContainerType::clear();
608 
609  if (clear_meta_data)
610  {
611  clearRanges();
612  clearId();
613  this->SpectrumSettings::operator=(SpectrumSettings()); // no "clear" method
614  retention_time_ = -1.0;
615  ms_level_ = 1;
616  name_.clear();
617  float_data_arrays_.clear();
618  string_data_arrays_.clear();
619  integer_data_arrays_.clear();
620  }
621  }
622 
623 protected:
624  // Docu in base class
625  virtual void clearChildIds_()
626  {}
627 
630 
633 
636 
639 
642 
645  };
646 
648  template <typename PeakT>
649  std::ostream & operator<<(std::ostream & os, const MSSpectrum<PeakT> & spec)
650  {
651  os << "-- MSSPECTRUM BEGIN --" << std::endl;
652 
653  //spectrum settings
654  os << static_cast<const SpectrumSettings &>(spec);
655 
656  //peaklist
657  for (typename MSSpectrum<PeakT>::ConstIterator it = spec.begin(); it != spec.end(); ++it)
658  {
659  os << *it << std::endl;
660  }
661 
662  os << "-- MSSPECTRUM END --" << std::endl;
663  return os;
664  }
665 
666 } // namespace OpenMS
667 
668 #endif // OPENMS_KERNEL_MSSPECTRUM_H
String data array class.
Definition: MSSpectrum.h:88
MSSpectrum & operator=(const MSSpectrum &source)
Assignment operator.
Definition: MSSpectrum.h:166
PeakType::CoordinateType CoordinateType
Coordinate (m/z) type.
Definition: MSSpectrum.h:109
ConstIterator MZEnd(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSSpectrum.h:590
A more convenient string class.
Definition: String.h:56
IntegerDataArrays integer_data_arrays_
Intager data arrays.
Definition: MSSpectrum.h:644
UInt getMSLevel() const
Returns the MS level.
Definition: MSSpectrum.h:231
void sortByPosition()
Lexicographically sorts the peaks by their position.
Definition: MSSpectrum.h:391
Size findNearest(CoordinateType mz) const
Binary search for the peak nearest to a specific m/z.
Definition: MSSpectrum.h:476
Iterator MZEnd(Iterator begin, CoordinateType mz, Iterator end)
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSSpectrum.h:542
StringDataArrays string_data_arrays_
String data arrays.
Definition: MSSpectrum.h:641
bool isSorted() const
Checks if all peaks are sorted with respect to ascending m/z.
Definition: MSSpectrum.h:453
Comparator for the retention time.
Definition: MSSpectrum.h:94
ContainerType::reverse_iterator ReverseIterator
Mutable reverse iterator.
Definition: MSSpectrum.h:127
Iterator MZBegin(Iterator begin, CoordinateType mz, Iterator end)
Binary search for peak range begin.
Definition: MSSpectrum.h:518
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:125
bool operator==(const MSSpectrum &rhs) const
Equality operator.
Definition: MSSpectrum.h:186
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
Iterator MZEnd(CoordinateType mz)
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSSpectrum.h:530
Integer data array class.
Definition: MSSpectrum.h:82
SpectrumSettings()
Constructor.
const String & getName() const
Returns the name.
Definition: MSSpectrum.h:243
Representation of 1D spectrum settings.
Definition: SpectrumSettings.h:64
void setName(const String &name)
Sets the name.
Definition: MSSpectrum.h:249
String name_
Name.
Definition: MSSpectrum.h:635
MSSpectrum(const MSSpectrum &source)
Copy constructor.
Definition: MSSpectrum.h:148
StringDataArrays & getStringDataArrays()
Returns a mutable reference to the string meta data arrays.
Definition: MSSpectrum.h:288
bool operator==(const RangeManager &rhs) const
Equality operator.
Definition: RangeManager.h:88
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
Definition: MSSpectrum.h:294
A 1-dimensional raw data point or peak mith meta information.
Definition: RichPeak1D.h:52
ContainerType::iterator Iterator
Mutable iterator.
Definition: MSSpectrum.h:123
Precondition failed exception.
Definition: Exception.h:167
virtual void clearChildIds_()
Clears the persistence id of all sub-objects.
Definition: MSSpectrum.h:625
IntegerDataArrays & getIntegerDataArrays()
Returns a mutable reference to the integer meta data arrays.
Definition: MSSpectrum.h:300
Description of the meta data arrays of MSSpectrum.
Definition: MetaInfoDescription.h:49
Comparator by position. As this class has dimension 1, this is basically an alias for MZLess...
Definition: Peak1D.h:232
Iterator MZBegin(CoordinateType mz)
Binary search for peak range begin.
Definition: MSSpectrum.h:506
bool operator!=(const MSSpectrum &rhs) const
Equality operator.
Definition: MSSpectrum.h:200
std::vector< FloatDataArray > FloatDataArrays
Float data array vector type.
Definition: MSSpectrum.h:113
virtual void updateRanges()
Updates minimum and maximum position/intensity.
Definition: MSSpectrum.h:206
FloatDataArrays float_data_arrays_
Float data arrays.
Definition: MSSpectrum.h:638
MSSpectrum()
Constructor.
Definition: MSSpectrum.h:134
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
Base class for all persistent objects.
Definition: PersistentObject.h:52
SpectrumSettings & operator=(const SpectrumSettings &source)
RangeManager & operator=(const RangeManager &rhs)
Assignment operator.
Definition: RangeManager.h:77
void sortByIntensity(bool reverse=false)
Lexicographically sorts the peaks by their intensity.
Definition: MSSpectrum.h:314
PersistentObject & operator=(const PersistentObject &rhs)
Assignment operator.
void setPosition(PositionType const &position)
Mutable access to the position.
Definition: Peak1D.h:144
void clearRanges()
Resets the ranges.
Definition: RangeManager.h:140
void setMSLevel(UInt ms_level)
Sets the MS level.
Definition: MSSpectrum.h:237
ConstIterator MZBegin(CoordinateType mz) const
Binary search for peak range begin.
Definition: MSSpectrum.h:554
std::vector< StringDataArray > StringDataArrays
String data array vector type.
Definition: MSSpectrum.h:115
ConstIterator MZBegin(ConstIterator begin, CoordinateType mz, ConstIterator end) const
Binary search for peak range begin.
Definition: MSSpectrum.h:566
FloatDataArrays & getFloatDataArrays()
Returns a mutable reference to the float meta data arrays.
Definition: MSSpectrum.h:276
std::vector< PeakType > ContainerType
Spectrum base type.
Definition: MSSpectrum.h:111
Definition: Peak1D.h:180
void clear(bool clear_meta_data)
Clears all data and meta data.
Definition: MSSpectrum.h:605
PeakT PeakType
Peak type.
Definition: MSSpectrum.h:107
std::vector< IntegerDataArray > IntegerDataArrays
Integer data array vector type.
Definition: MSSpectrum.h:117
Float data array class.
Definition: MSSpectrum.h:76
void clearId(bool deep=true)
Clears the persistence id.
const StringDataArrays & getStringDataArrays() const
Returns a const reference to the string meta data arrays.
Definition: MSSpectrum.h:282
bool operator()(const MSSpectrum &a, const MSSpectrum &b) const
Definition: MSSpectrum.h:97
void setRT(DoubleReal rt)
Sets the absolute retention time (is seconds)
Definition: MSSpectrum.h:221
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
UInt ms_level_
MS level.
Definition: MSSpectrum.h:632
ConstIterator MZEnd(CoordinateType mz) const
Binary search for peak range end (returns the past-the-end iterator)
Definition: MSSpectrum.h:578
DoubleReal getRT() const
Definition: MSSpectrum.h:215
void updateRanges_(const PeakIteratorType &begin, const PeakIteratorType &end)
Updates the range using data points in the iterator range.
Definition: RangeManager.h:155
Handles the managment of a position and intensity range.
Definition: RangeManager.h:48
DoubleReal retention_time_
Retention time.
Definition: MSSpectrum.h:629
Class for comparison of std::pair using first ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:326
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
Definition: MSSpectrum.h:270
bool operator==(const SpectrumSettings &rhs) const
Equality operator.
ContainerType::const_reverse_iterator ConstReverseIterator
Non-mutable reverse iterator.
Definition: MSSpectrum.h:129
~MSSpectrum()
Destructor.
Definition: MSSpectrum.h:162

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