OpenMS
SignalToNoiseEstimator.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Chris Bielow $
7 // --------------------------------------------------------------------------
8 //
9 
10 #pragma once
11 
15 
16 #include <vector>
17 #include <cmath>
18 
19 namespace OpenMS
20 {
21  class MSExperiment;
22 
30  template <typename Container = MSSpectrum>
32  public DefaultParamHandler, public ProgressLogger
33  {
34 public:
35 
39  typedef typename Container::const_iterator PeakIterator;
40  typedef typename PeakIterator::value_type PeakType;
41 
42 
44 
47  DefaultParamHandler("SignalToNoiseEstimator"),
49  {
50  }
51 
54  DefaultParamHandler(source),
55  ProgressLogger(source),
57  {}
58 
61  {
62  if (&source == this) return *this;
63 
67  return *this;
68  }
69 
72  {}
73 
75  virtual void init(const Container& c)
76  {
77  computeSTN_(c);
78  }
79 
83  virtual double getSignalToNoise(const Size index) const
84  {
85  OPENMS_POSTCONDITION(index < stn_estimates_.size(),"SignalToNoiseEstimator estimates beyond container size was requested.");
86  return stn_estimates_[index];
87  }
88 
89 protected:
90 
96  virtual void computeSTN_(const Container& c) = 0;
97 
98 
99 
106  {
107  double mean;
108  double variance;
109  };
110 
111 
113  inline GaussianEstimate estimate_(const PeakIterator & scan_first_, const PeakIterator & scan_last_) const
114  {
115  int size = 0;
116  // add up
117  double v = 0;
118  double m = 0;
119  PeakIterator run = scan_first_;
120  while (run != scan_last_)
121  {
122  m += (*run).getIntensity();
123  ++size;
124  ++run;
125  }
126  //average
127  m = m / size;
128 
129  //determine variance
130  run = scan_first_;
131  while (run != scan_last_)
132  {
133  double tmp(m - (*run).getIntensity());
134  v += tmp * tmp;
135  ++run;
136  }
137  v = v / ((double)size); // divide by n
138 
139  GaussianEstimate value = {m, v};
140  return value;
141  }
142 
143  //MEMBERS:
144 
146  std::vector<double> stn_estimates_;
147  };
148 
151  OPENMS_DLLAPI float estimateNoiseFromRandomScans(const MSExperiment& exp, const UInt ms_level, const UInt n_scans = 10, const double percentile = 80);
152 
153 } // namespace OpenMS
154 
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:66
DefaultParamHandler & operator=(const DefaultParamHandler &rhs)
Assignment operator.
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:46
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:27
ProgressLogger & operator=(const ProgressLogger &other)
Assignment Operator.
This class represents the abstract base class of a signal to noise estimator.
Definition: SignalToNoiseEstimator.h:33
virtual void init(const Container &c)
Set the start and endpoint of the raw data interval, for which signal to noise ratios will be estimat...
Definition: SignalToNoiseEstimator.h:75
double variance
variance of estimated Gaussian
Definition: SignalToNoiseEstimator.h:108
SignalToNoiseEstimator & operator=(const SignalToNoiseEstimator &source)
Assignment operator.
Definition: SignalToNoiseEstimator.h:60
PeakIterator::value_type PeakType
Definition: SignalToNoiseEstimator.h:40
SignalToNoiseEstimator(const SignalToNoiseEstimator &source)
Copy constructor.
Definition: SignalToNoiseEstimator.h:53
virtual void computeSTN_(const Container &c)=0
computes the S/N values when init() is called
GaussianEstimate estimate_(const PeakIterator &scan_first_, const PeakIterator &scan_last_) const
calculate mean & stdev of intensities of a spectrum
Definition: SignalToNoiseEstimator.h:113
~SignalToNoiseEstimator() override
Destructor.
Definition: SignalToNoiseEstimator.h:71
virtual double getSignalToNoise(const Size index) const
Definition: SignalToNoiseEstimator.h:83
double mean
mean of estimated Gaussian
Definition: SignalToNoiseEstimator.h:107
std::vector< double > stn_estimates_
stores the noise estimate for each peak
Definition: SignalToNoiseEstimator.h:146
Container::const_iterator PeakIterator
Definition: SignalToNoiseEstimator.h:39
SignalToNoiseEstimator()
Constructor.
Definition: SignalToNoiseEstimator.h:46
protected struct to store parameters my, sigma for a Gaussian distribution
Definition: SignalToNoiseEstimator.h:106
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
#define OPENMS_POSTCONDITION(condition, message)
Postcondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:101
const double c
Definition: Constants.h:188
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
float estimateNoiseFromRandomScans(const MSExperiment &exp, const UInt ms_level, const UInt n_scans=10, const double percentile=80)