OpenMS  3.0.0
Histogram.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-2022.
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 
37 //OpenMS
38 #include <OpenMS/CONCEPT/Types.h>
40 
41 //STL
42 #include <vector>
43 #include <cmath>
44 #include <limits>
45 #include <iostream>
46 #include <algorithm>
47 
48 namespace OpenMS
49 {
50  namespace Math
51  {
52 
62  template <typename ValueType = UInt, typename BinSizeType = double>
63  class Histogram
64  {
65 public:
66 
68  typedef typename std::vector<ValueType>::const_iterator ConstIterator;
69 
73  Histogram() :
75  min_(0),
76  max_(0),
77  bin_size_(0)
78  {
79  }
80 
82  Histogram(const Histogram & histogram) :
83  min_(histogram.min_),
84  max_(histogram.max_),
85  bin_size_(histogram.bin_size_),
86  bins_(histogram.bins_)
87  {
88  }
89 
95  Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size) :
96  min_(min),
97  max_(max),
98  bin_size_(bin_size)
99  {
100  this->initBins_();
101  }
102 
103 
109  template <typename DataIterator>
110  Histogram(DataIterator begin, DataIterator end, BinSizeType min, BinSizeType max, BinSizeType bin_size) :
111  min_(min),
112  max_(max),
113  bin_size_(bin_size)
114  {
115  this->initBins_();
116  for (DataIterator it = begin; it != end; ++it)
117  {
118  this->inc((BinSizeType) *it);
119  }
120  }
121 
123  virtual ~Histogram()
124  {
125  }
126 
128 
130  BinSizeType minBound() const
131  {
132  return min_;
133  }
134 
136  BinSizeType maxBound() const
137  {
138  return max_;
139  }
140 
142  ValueType maxValue() const
143  {
144  return *(std::max_element(bins_.begin(), bins_.end()));
145  }
146 
148  ValueType minValue() const
149  {
150  return *(std::min_element(bins_.begin(), bins_.end()));
151  }
152 
154  BinSizeType binSize() const
155  {
156  return bin_size_;
157  }
158 
160  Size size() const
161  {
162  return bins_.size();
163  }
164 
170  ValueType operator[](Size index) const
171  {
172  if (index >= bins_.size())
173  {
174  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
175  }
176  return bins_[index];
177  }
178 
184  BinSizeType centerOfBin(Size bin_index) const
185  {
186  if (bin_index >= bins_.size())
187  {
188  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
189  }
190 
191  return (BinSizeType)(min_ + ((BinSizeType)bin_index + 0.5) * bin_size_);
192  }
193 
199  BinSizeType rightBorderOfBin(Size bin_index) const
200  {
201  if (bin_index >= bins_.size())
202  {
203  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
204  }
205  if (bin_index + 1 == bins_.size())
206  { // rightmost bin is special. It contains the maximum value - see valueToBin().
207  return std::nextafter(maxBound(), maxBound() + 1);
208  }
209  return (BinSizeType)(min_ + ((BinSizeType) bin_index + 1) * bin_size_);
210  }
211 
217  BinSizeType leftBorderOfBin(Size bin_index) const
218  {
219  if (bin_index >= bins_.size())
220  {
221  throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
222  }
223 
224  return (BinSizeType)(min_ + (BinSizeType)bin_index * bin_size_);
225  }
226 
232  ValueType binValue(BinSizeType val) const
233  {
234  return bins_[valueToBin(val)];
235  }
236 
244  Size inc(BinSizeType val, ValueType increment = 1)
245  {
246  Size bin_index = this->valueToBin(val);
247  this->bins_[bin_index] += increment;
248  return bin_index;
249  }
250 
258  Size incUntil(BinSizeType val, bool inclusive, ValueType increment = 1)
259  {
260  Size bin_index = this->valueToBin(val);
261  for (Size i = 0; i < bin_index; ++i)
262  {
263  this->bins_[i] += increment;
264  }
265  if (inclusive)
266  {
267  this->bins_[bin_index] += increment;
268  }
269  return bin_index;
270  }
271 
279  Size incFrom(BinSizeType val, bool inclusive, ValueType increment = 1)
280  {
281  Size bin_index = this->valueToBin(val);
282  for (Size i = bin_index + 1; i < this->bins_.size(); ++i)
283  {
284  this->bins_[i] += increment;
285  }
286  if (inclusive)
287  {
288  this->bins_[bin_index] += increment;
289  }
290  return bin_index;
291  }
292 
293  template< typename DataIterator >
294  static void getCumulativeHistogram(DataIterator begin, DataIterator end,
295  bool complement,
296  bool inclusive,
298  {
299  for (DataIterator it = begin; it != end; ++it)
300  {
301  if (complement)
302  {
303  histogram.incUntil(*it, inclusive);
304  }
305  else
306  {
307  histogram.incFrom(*it, inclusive);
308  }
309  }
310  }
311 
312 
318  void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
319  {
320  min_ = min;
321  max_ = max;
322  bin_size_ = bin_size;
323  bins_.clear();
324 
325  if (bin_size_ <= 0)
326  {
327  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
328  }
329  else
330  {
331  // if max_ == min_ there is only one bin
332  if (max_ != min_)
333  {
334  bins_.resize(Size(ceil((max_ - min_) / bin_size_)), 0);
335  }
336  else
337  {
338  bins_.resize(1, 0);
339  }
340  }
341  }
342 
346  bool operator==(const Histogram & histogram) const
348  {
349  return min_ == histogram.min_ &&
350  max_ == histogram.max_ &&
351  bin_size_ == histogram.bin_size_ &&
352  bins_ == histogram.bins_;
353  }
354 
356  bool operator!=(const Histogram & histogram) const
357  {
358  return !operator==(histogram);
359  }
360 
362  Histogram & operator=(const Histogram & histogram)
363  {
364  if (&histogram == this) return *this;
365 
366  min_ = histogram.min_;
367  max_ = histogram.max_;
368  bin_size_ = histogram.bin_size_;
369  bins_ = histogram.bins_;
370 
371  return *this;
372  }
373 
375 
379  inline ConstIterator begin() const { return bins_.begin(); }
381 
383  inline ConstIterator end() const { return bins_.end(); }
385 
387  void applyLogTransformation(BinSizeType multiplier)
388  {
389  for (typename std::vector<ValueType>::iterator it = bins_.begin(); it != bins_.end(); ++it)
390  {
391  *it = (ValueType)(multiplier * log((BinSizeType)(*it + 1.0f)));
392  }
393  }
394 
400  Size valueToBin(BinSizeType val) const
401  {
402  //std::cout << "val: " << val << " (min: " << min_ << " max: " << max_ << ")" << std::endl;
403  if (val < min_ || val > max_)
404  {
405  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
406  }
407  if (val == max_)
408  {
409  return Size(bins_.size() - 1);
410  }
411  else
412  {
413  return (Size) floor((val - min_) / bin_size_);
414  }
415  }
416 
417 protected:
419  BinSizeType min_;
421  BinSizeType max_;
423  BinSizeType bin_size_;
425  std::vector<ValueType> bins_;
426 
427 
429  void initBins_()
430  {
431  if (this->bin_size_ <= 0)
432  {
433  throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
434  }
435  else
436  {
437  if (this->max_ != this->min_)
438  {
439  this->bins_ = std::vector<ValueType>(Size(ceil((max_ - min_) / bin_size_)), 0);
440  }
441  else
442  { // if max_ == min_ there is only one bin
443  this->bins_ = std::vector<ValueType>(1, 0);
444  }
445  }
446  }
447  };
448 
450  template <typename ValueType, typename BinSizeType>
451  std::ostream & operator<<(std::ostream & os, const Histogram<ValueType, BinSizeType> & hist)
452  {
453  for (Size i = 0; i < hist.size(); ++i)
454  {
455  os << hist.centerOfBin(i) << "\t"<< hist[i] << std::endl;
456  }
457  return os;
458  }
459 
460  } // namespace Math
461 
462 } // namespace OpenMS
463 
LogStream.h
OpenMS::PrecursorCorrection::writeHist
static void writeHist(const String &out_csv, const std::vector< double > &delta_mzs, const std::vector< double > &mzs, const std::vector< double > &rts)
Writer can be used in association with correctToNearestMS1Peak or correctToHighestIntensityMS1Peak.
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:147
OpenMS::Math::Histogram::incUntil
Size incUntil(BinSizeType val, bool inclusive, ValueType increment=1)
Increment all bins from to lowest(=first) bin up to (and including?) the bin for val by a certain num...
Definition: Histogram.h:258
OpenMS::Math::Histogram::Histogram
Histogram()
default constructor
Definition: Histogram.h:74
OpenMS::Math::Histogram::centerOfBin
BinSizeType centerOfBin(Size bin_index) const
returns the center position of the bin with the index bin_index
Definition: Histogram.h:184
Types.h
OpenMS::Math::Histogram::incFrom
Size incFrom(BinSizeType val, bool inclusive, ValueType increment=1)
Increment all bins from the bin of val to the highest(=last) bin by a certain number of counts.
Definition: Histogram.h:279
OpenMS::PrecursorCorrection::correctToNearestMS1Peak
static std::set< Size > correctToNearestMS1Peak(MSExperiment &exp, double mz_tolerance, bool ppm, std::vector< double > &delta_mzs, std::vector< double > &mzs, std::vector< double > &rts)
Selection of the peak in closest proximity as corrected precursor mass in a given mass range (e....
OpenMS::Math::Histogram::~Histogram
virtual ~Histogram()
destructor
Definition: Histogram.h:123
OpenMS::MzMLFile::store
void store(const String &filename, const PeakMap &map) const
Stores a map in an MzML file.
OpenMS::Math::operator<<
std::ostream & operator<<(std::ostream &os, const Histogram< ValueType, BinSizeType > &hist)
Print the contents to a stream.
Definition: Histogram.h:451
OpenMS::Math::Histogram::Histogram
Histogram(BinSizeType min, BinSizeType max, BinSizeType bin_size)
constructor with min, max (inclusive) and bin width
Definition: Histogram.h:95
OpenMS::MzMLFile
File adapter for MzML files.
Definition: MzMLFile.h:57
MzMLFile.h
OpenMS::MSExperiment
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:70
OpenMS::Math::Histogram::rightBorderOfBin
BinSizeType rightBorderOfBin(Size bin_index) const
returns the first value to the right side of the bin with the index bin_index, which is not part of t...
Definition: Histogram.h:199
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
FeatureXMLFile.h
OpenMS::Math::Histogram::valueToBin
Size valueToBin(BinSizeType val) const
Returns the bin index a given value belongs to.
Definition: Histogram.h:400
OpenMS::PrecursorCorrection::correctToHighestIntensityMS1Peak
static std::set< Size > correctToHighestIntensityMS1Peak(MSExperiment &exp, double mz_tolerance, bool ppm, std::vector< double > &delta_mzs, std::vector< double > &mzs, std::vector< double > &rts)
Selection of the peak with the highest intensity as corrected precursor mass in a given mass range (e...
OPENMS_LOG_WARN
#define OPENMS_LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged.
Definition: LogStream.h:460
Constants.h
OpenMS::Math::Histogram::size
Size size() const
returns the number of bins
Definition: Histogram.h:160
OpenMS::PrecursorCorrection::correctToNearestFeature
static std::set< Size > correctToNearestFeature(const FeatureMap &features, MSExperiment &exp, double rt_tolerance_s=0.0, double mz_tolerance=0.0, bool ppm=true, bool believe_charge=false, bool keep_original=false, bool all_matching_features=false, int max_trace=2, int debug_level=0)
Reassigns a precursor to the nearest feature in a given rt and mass range. Wrong assignment of the mo...
OpenMS::Math::Histogram::end
ConstIterator end() const
Non-mutable iterator pointing after the last bin.
Definition: Histogram.h:383
OpenMS::Math::Histogram::max_
BinSizeType max_
Upper bound.
Definition: Histogram.h:421
OpenMS::ListUtils::concatenate
static String concatenate(const std::vector< T > &container, const String &glue="")
Concatenates all elements of the container and puts the glue string between elements.
Definition: ListUtils.h:209
OpenMS::Exception::OutOfRange
Out of range exception.
Definition: Exception.h:311
OpenMS::Math::Histogram::bins_
std::vector< ValueType > bins_
Vector of bins.
Definition: Histogram.h:425
OpenMS::Math::Histogram::Histogram
Histogram(DataIterator begin, DataIterator end, BinSizeType min, BinSizeType max, BinSizeType bin_size)
constructor with data iterator and min, max, bin_size parameters
Definition: Histogram.h:110
OpenMS::Math::Histogram::operator=
Histogram & operator=(const Histogram &histogram)
Assignment.
Definition: Histogram.h:362
OpenMS::Math::Histogram::inc
Size inc(BinSizeType val, ValueType increment=1)
increases the bin corresponding to value val by increment
Definition: Histogram.h:244
OpenMS::Math::Histogram::ConstIterator
std::vector< ValueType >::const_iterator ConstIterator
Non-mutable iterator of the bins.
Definition: Histogram.h:68
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::Math::Histogram::min_
BinSizeType min_
Lower bound.
Definition: Histogram.h:419
OpenMS::Math::Histogram
Representation of a histogram.
Definition: Histogram.h:63
OpenMS::Math::Histogram::operator[]
ValueType operator[](Size index) const
returns the value of bin index
Definition: Histogram.h:170
Exception.h
OpenMS::MzMLFile::load
void load(const String &filename, PeakMap &map)
Loads a map from a MzML file. Spectra and chromatograms are sorted by default (this can be disabled u...
OpenMS::Math::Histogram::minBound
BinSizeType minBound() const
returns the lower bound (inclusive)
Definition: Histogram.h:130
OpenMS::FeatureXMLFile::load
void load(const String &filename, FeatureMap &feature_map)
loads the file with name filename into map and calls updateRanges().
OpenMS::Math::Histogram::operator!=
bool operator!=(const Histogram &histogram) const
Inequality operator.
Definition: Histogram.h:356
OpenMS::Math::Histogram::binSize
BinSizeType binSize() const
returns the bin size
Definition: Histogram.h:154
MathFunctions.h
PrecursorCorrection.h
OpenMS::Math::Histogram::operator==
bool operator==(const Histogram &histogram) const
Equality operator.
Definition: Histogram.h:347
OpenMS::Math::Histogram::Histogram
Histogram(const Histogram &histogram)
copy constructor
Definition: Histogram.h:82
OpenMS::Math::Histogram::initBins_
void initBins_()
initialize the bins
Definition: Histogram.h:429
OpenMS::Math::Histogram::maxBound
BinSizeType maxBound() const
returns the upper bound (inclusive)
Definition: Histogram.h:136
OpenMS::Math::Histogram::getCumulativeHistogram
static void getCumulativeHistogram(DataIterator begin, DataIterator end, bool complement, bool inclusive, Histogram< ValueType, BinSizeType > &histogram)
Definition: Histogram.h:294
OpenMS::Exception::IndexOverflow
Int overflow exception.
Definition: Exception.h:245
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:71
OpenMS::Math::Histogram::minValue
ValueType minValue() const
returns the bin with lowest count
Definition: Histogram.h:148
OPENMS_LOG_ERROR
#define OPENMS_LOG_ERROR
Macro to be used if non-fatal error are reported (processing continues)
Definition: LogStream.h:455
OpenMS::Math::Histogram::binValue
ValueType binValue(BinSizeType val) const
returns the value of bin corresponding to the value val
Definition: Histogram.h:232
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:98
OpenMS::Math::Histogram::bin_size_
BinSizeType bin_size_
Bin size.
Definition: Histogram.h:423
OpenMS::Math::Histogram::leftBorderOfBin
BinSizeType leftBorderOfBin(Size bin_index) const
returns the leftmost value of the bin with the index bin_index, which is part of this bin,...
Definition: Histogram.h:217
OpenMS::FeatureXMLFile
This class provides Input/Output functionality for feature maps.
Definition: FeatureXMLFile.h:62
DataValue.h
OPENMS_LOG_INFO
#define OPENMS_LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:465
OpenMS::Math::Histogram::begin
ConstIterator begin() const
Non-mutable iterator pointing to the first bin.
Definition: Histogram.h:380
PeakPickerHiRes.h
StandardTypes.h
TOPPBase.h
OpenMS::Math::Histogram::applyLogTransformation
void applyLogTransformation(BinSizeType multiplier)
Transforms the bin values with f(x)=multiplier*log(x+1)
Definition: Histogram.h:387
OpenMS::Math::Histogram::reset
void reset(BinSizeType min, BinSizeType max, BinSizeType bin_size)
resets the histogram with the given range and bin size
Definition: Histogram.h:318
OpenMS::PrecursorCorrection::csv_header
static const std::string csv_header
Definition: PrecursorCorrection.h:67
OpenMS::Math::Histogram::maxValue
ValueType maxValue() const
returns the bin with the highest count
Definition: Histogram.h:142