OpenMS
StatsHelpers.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-2023.
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: Witold Wolski $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/OPENSWATHALGO/OpenSwathAlgoConfig.h>
38 #include <algorithm>
39 #include <cmath>
40 #include <complex>
41 #include <numeric>
42 #include <vector>
43 #include <cstddef>
44 
45 namespace OpenSwath
46 {
47 
51  OPENSWATHALGO_DLLAPI void normalize(const std::vector<double>& intensities, double normalization_factor, std::vector<double>& normalized_intensities);
52 
56  template <typename T>
57  double norm(T beg, T end)
58  {
59  double res = 0.0;
60  for (; beg != end; ++beg)
61  {
62  double tmp = *beg;
63  res += tmp * tmp;
64  }
65  return sqrt(res);
66  }
67 
71  template <typename Texp, typename Ttheo>
72  double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo)
73  {
74  std::vector<double> res(std::distance(intExpBeg, intExpEnd));
75  std::transform(intExpBeg, intExpEnd, intTheo, res.begin(), std::multiplies<double>());
76  double sum = std::accumulate(res.begin(), res.end(), 0.);
77  return sum;
78  }
79 
87  OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector<double> intExp, std::vector<double> theorint);
88 
92  template <typename Texp, typename Ttheo>
93  double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
94  {
95  double sum = 0.0;
96  for (std::size_t i = 0; itExpBeg < itExpEnd; ++itExpBeg, ++itTheo, ++i)
97  {
98  double x = *itExpBeg - *itTheo;
99  x = fabs(x);
100  sum += x;
101  }
102  return sum;
103  }
104 
112  OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector<double> intExp, std::vector<double> theorint);
113 
114 
118  template <typename TInputIterator, typename TInputIteratorY>
119  typename std::iterator_traits<TInputIterator>::value_type cor_pearson(
120  TInputIterator xBeg,
121  TInputIterator xEnd,
122  TInputIteratorY yBeg
123  )
124  {
125  typedef typename std::iterator_traits<TInputIterator>::value_type value_type;
126  value_type m1, m2;
127  value_type s1, s2;
128  value_type corr;
129  m1 = m2 = s1 = s2 = 0.0;
130  corr = 0.0;
131  ptrdiff_t n = std::distance(xBeg, xEnd);
132  value_type nd = static_cast<value_type>(n);
133  for (; xBeg != xEnd; ++xBeg, ++yBeg)
134  {
135  corr += *xBeg * *yBeg;
136  m1 += *xBeg;
137  m2 += *yBeg;
138  s1 += *xBeg * *xBeg;
139  s2 += *yBeg * *yBeg;
140  }
141  m1 /= nd;
142  m2 /= nd;
143  s1 -= m1 * m1 * nd;
144  s2 -= m2 * m2 * nd;
145 
146  if (s1 < 1.0e-12 || s2 < 1.0e-12)
147  return 0.0;
148  else
149  {
150  corr -= m1 * m2 * (double)n;
151  corr /= sqrt(s1 * s2);
152  return corr;
153  }
154  }
155 
159  class OPENSWATHALGO_DLLAPI mean_and_stddev
160  {
161  double m_, q_;
162  unsigned long c_;
163 public:
164  typedef double argument_type, result_type;
166  m_(0.0), q_(0.0), c_(0u)
167  {
168  }
169 
170  void operator()(double sample)
171  {
172  double const delta = sample - m_;
173  m_ += delta / ++c_;
174  q_ += delta * (sample - m_);
175  }
176 
177  double sample_variance() const
178  {
179  return (c_ > 1u) ? (q_ / (c_ - 1)) : 0;
180  }
181 
182  double standard_variance() const
183  {
184  return (c_ > 1u) ? (q_ / c_) : 0;
185  }
186 
187  double sample_stddev() const
188  {
189  return std::sqrt(sample_variance());
190  }
191 
192  double standard_stddev() const
193  {
194  return std::sqrt(standard_variance());
195  }
196 
197  double mean() const
198  {
199  return m_;
200  }
201 
202  unsigned long count() const
203  {
204  return c_;
205  }
206 
207  double variance() const
208  {
209  return sample_variance();
210  }
211 
212  double stddev() const
213  {
214  return sample_stddev();
215  }
216 
217  double operator()() const
218  {
219  return stddev();
220  }
221 
222  };
223 
224 } //end namespace OpenSwath
225 
functor to compute the mean and stddev of sequence using the std::foreach algorithm
Definition: StatsHelpers.h:160
double result_type
Definition: StatsHelpers.h:164
mean_and_stddev()
Definition: StatsHelpers.h:165
double standard_stddev() const
Definition: StatsHelpers.h:192
unsigned long c_
Definition: StatsHelpers.h:162
double sample_stddev() const
Definition: StatsHelpers.h:187
double mean() const
Definition: StatsHelpers.h:197
double stddev() const
Definition: StatsHelpers.h:212
double argument_type
Definition: StatsHelpers.h:164
double variance() const
Definition: StatsHelpers.h:207
unsigned long count() const
Definition: StatsHelpers.h:202
double standard_variance() const
Definition: StatsHelpers.h:182
void operator()(double sample)
Definition: StatsHelpers.h:170
double m_
Definition: StatsHelpers.h:161
double operator()() const
Definition: StatsHelpers.h:217
double sample_variance() const
Definition: StatsHelpers.h:177
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:107
Definition: Scoring.h:44
double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
compute manhattan distance between Exp and Theo
Definition: StatsHelpers.h:93
OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector< double > intExp, std::vector< double > theorint)
the dot product scoring
OPENSWATHALGO_DLLAPI void normalize(const std::vector< double > &intensities, double normalization_factor, std::vector< double > &normalized_intensities)
Normalize intensities in vector by normalization_factor.
double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo)
compute dotprod of vectors
Definition: StatsHelpers.h:72
OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector< double > intExp, std::vector< double > theorint)
manhattan scoring
double norm(T beg, T end)
compute the Euclidean norm of the vector
Definition: StatsHelpers.h:57
std::iterator_traits< TInputIterator >::value_type cor_pearson(TInputIterator xBeg, TInputIterator xEnd, TInputIteratorY yBeg)
compute pearson correlation of vector x and y
Definition: StatsHelpers.h:119