OpenMS  2.8.0
SignalToNoiseEstimator.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-2021.
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: Chris Bielow $
32 // $Authors: Chris Bielow $
33 // --------------------------------------------------------------------------
34 //
35 
36 #pragma once
37 
41 
42 #include <vector>
43 #include <cmath>
44 
45 namespace OpenMS
46 {
47  class MSExperiment;
48 
56  template <typename Container = MSSpectrum>
58  public DefaultParamHandler, public ProgressLogger
59  {
60 public:
61 
65  typedef typename Container::const_iterator PeakIterator;
66  typedef typename PeakIterator::value_type PeakType;
67 
68 
70 
73  DefaultParamHandler("SignalToNoiseEstimator"),
75  {
76  }
77 
80  DefaultParamHandler(source),
81  ProgressLogger(source),
83  {}
84 
87  {
88  if (&source == this) return *this;
89 
93  return *this;
94  }
95 
98  {}
99 
101  virtual void init(const Container& c)
102  {
103  computeSTN_(c);
104  }
105 
109  virtual double getSignalToNoise(const Size index) const
110  {
111  OPENMS_POSTCONDITION(index < stn_estimates_.size(),"SignalToNoiseEstimator estimates beyond container size was requested.");
112  return stn_estimates_[index];
113  }
114 
115 protected:
116 
122  virtual void computeSTN_(const Container& c) = 0;
123 
124 
125 
132  {
133  double mean;
134  double variance;
135  };
136 
137 
139  inline GaussianEstimate estimate_(const PeakIterator & scan_first_, const PeakIterator & scan_last_) const
140  {
141  int size = 0;
142  // add up
143  double v = 0;
144  double m = 0;
145  PeakIterator run = scan_first_;
146  while (run != scan_last_)
147  {
148  m += (*run).getIntensity();
149  ++size;
150  ++run;
151  }
152  //average
153  m = m / size;
154 
155  //determine variance
156  run = scan_first_;
157  while (run != scan_last_)
158  {
159  double tmp(m - (*run).getIntensity());
160  v += tmp * tmp;
161  ++run;
162  }
163  v = v / ((double)size); // divide by n
164 
165  GaussianEstimate value = {m, v};
166  return value;
167  }
168 
169  //MEMBERS:
170 
172  std::vector<double> stn_estimates_;
173  };
174 
177  OPENMS_DLLAPI float estimateNoiseFromRandomScans(const MSExperiment& exp, const UInt ms_level, const UInt n_scans = 10, const double percentile = 80);
178 
179 } // namespace OpenMS
180 
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:93
virtual DefaultParamHandler & operator=(const DefaultParamHandler &rhs)
Assignment operator.
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:73
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:53
ProgressLogger & operator=(const ProgressLogger &other)
Assignment Operator.
This class represents the abstract base class of a signal to noise estimator.
Definition: SignalToNoiseEstimator.h:59
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:101
double variance
variance of estimated Gaussian
Definition: SignalToNoiseEstimator.h:134
SignalToNoiseEstimator & operator=(const SignalToNoiseEstimator &source)
Assignment operator.
Definition: SignalToNoiseEstimator.h:86
PeakIterator::value_type PeakType
Definition: SignalToNoiseEstimator.h:66
SignalToNoiseEstimator(const SignalToNoiseEstimator &source)
Copy constructor.
Definition: SignalToNoiseEstimator.h:79
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:139
~SignalToNoiseEstimator() override
Destructor.
Definition: SignalToNoiseEstimator.h:97
virtual double getSignalToNoise(const Size index) const
Definition: SignalToNoiseEstimator.h:109
double mean
mean of estimated Gaussian
Definition: SignalToNoiseEstimator.h:133
std::vector< double > stn_estimates_
stores the noise estimate for each peak
Definition: SignalToNoiseEstimator.h:172
Container::const_iterator PeakIterator
Definition: SignalToNoiseEstimator.h:65
SignalToNoiseEstimator()
Constructor.
Definition: SignalToNoiseEstimator.h:72
protected struct to store parameters my, sigma for a Gaussian distribution
Definition: SignalToNoiseEstimator.h:132
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
#define OPENMS_POSTCONDITION(condition, message)
Postcondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:127
const double c
Definition: Constants.h:209
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
float estimateNoiseFromRandomScans(const MSExperiment &exp, const UInt ms_level, const UInt n_scans=10, const double percentile=80)