Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef OPENMS_ANALYSIS_OPENSWATH_OPENSWATHALGO_ALGO_STATSHELPERS_H
00036 #define OPENMS_ANALYSIS_OPENSWATH_OPENSWATHALGO_ALGO_STATSHELPERS_H
00037
00038 #include <cmath>
00039 #include <vector>
00040 #include <numeric>
00041 #include <boost/bind.hpp>
00042 #include <complex>
00043 #include <algorithm>
00044 #include <cmath>
00045
00046 #include <OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/OpenSwathAlgoConfig.h>
00047
00048 #include "OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h"
00049
00050 namespace OpenSwath
00051 {
00052
00056 OPENSWATHALGO_DLLAPI void normalize(const std::vector<double>& intensities, double normalization_factor, std::vector<double>& normalized_intensities);
00057
00061 template <typename T>
00062 double norm(T beg, T end)
00063 {
00064 double res = 0.0;
00065 for (; beg != end; ++beg)
00066 {
00067 double tmp = *beg;
00068 res += tmp * tmp;
00069 }
00070 return sqrt(res);
00071 }
00072
00073 struct mySqrt :
00074 std::unary_function<double, double>
00075 {
00076 double operator()(double x)
00077 {
00078 return sqrt(x);
00079 }
00080
00081 };
00082
00086 template <typename Texp, typename Ttheo>
00087 double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo)
00088 {
00089 std::vector<double> res(std::distance(intExpBeg, intExpEnd));
00090 std::transform(intExpBeg, intExpEnd, intTheo, res.begin(), std::multiplies<double>());
00091 double sum = std::accumulate(res.begin(), res.end(), 0.);
00092 return sum;
00093 }
00094
00102 OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector<double> intExp, std::vector<double> theorint);
00103
00107 template <typename Texp, typename Ttheo>
00108 double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
00109 {
00110 double sum = 0.0;
00111 for (std::size_t i = 0; itExpBeg < itExpEnd; ++itExpBeg, ++itTheo, ++i)
00112 {
00113 double x = *itExpBeg - *itTheo;
00114 x = fabs(x);
00115 sum += x;
00116 }
00117 return sum;
00118 }
00119
00127 OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector<double> intExp, std::vector<double> theorint);
00128
00129
00133 template <typename TInputIterator, typename TInputIteratorY>
00134 typename std::iterator_traits<TInputIterator>::value_type cor_pearson(
00135 TInputIterator xBeg,
00136 TInputIterator xEnd,
00137 TInputIteratorY yBeg
00138 )
00139 {
00140 typedef typename std::iterator_traits<TInputIterator>::value_type value_type;
00141 value_type m1, m2;
00142 value_type s1, s2;
00143 value_type corr;
00144 m1 = m2 = s1 = s2 = 0.0;
00145 corr = 0.0;
00146 ptrdiff_t n = std::distance(xBeg, xEnd);
00147 value_type nd = static_cast<value_type>(n);
00148 for (; xBeg != xEnd; ++xBeg, ++yBeg)
00149 {
00150 corr += *xBeg * *yBeg;
00151 m1 += *xBeg;
00152 m2 += *yBeg;
00153 s1 += *xBeg * *xBeg;
00154 s2 += *yBeg * *yBeg;
00155 }
00156 m1 /= nd;
00157 m2 /= nd;
00158 s1 -= m1 * m1 * nd;
00159 s2 -= m2 * m2 * nd;
00160
00161 if (s1 < 1.0e-12 || s2 < 1.0e-12)
00162 return 0.0;
00163 else
00164 {
00165 corr -= m1 * m2 * (double)n;
00166 corr /= sqrt(s1 * s2);
00167 return corr;
00168 }
00169 }
00170
00174 class OPENSWATHALGO_DLLAPI mean_and_stddev
00175 {
00176 double m_, q_;
00177 unsigned long c_;
00178 public:
00179 typedef double argument_type, result_type;
00180 mean_and_stddev() :
00181 m_(0.0), q_(0.0), c_(0u)
00182 {
00183 }
00184
00185 void operator()(double sample)
00186 {
00187 double const delta = sample - m_;
00188 m_ += delta / ++c_;
00189 q_ += delta * (sample - m_);
00190 }
00191
00192 double sample_variance() const
00193 {
00194 return (c_ > 1u) ? (q_ / (c_ - 1)) : 0;
00195 }
00196
00197 double standard_variance() const
00198 {
00199 return (c_ > 1u) ? (q_ / c_) : 0;
00200 }
00201
00202 double sample_stddev() const
00203 {
00204 return std::sqrt(sample_variance());
00205 }
00206
00207 double standard_stddev() const
00208 {
00209 return std::sqrt(standard_variance());
00210 }
00211
00212 double mean() const
00213 {
00214 return m_;
00215 }
00216
00217 unsigned long count() const
00218 {
00219 return c_;
00220 }
00221
00222 double variance() const
00223 {
00224 return sample_variance();
00225 }
00226
00227 double stddev() const
00228 {
00229 return sample_stddev();
00230 }
00231
00232 double operator()() const
00233 {
00234 return stddev();
00235 }
00236
00237 };
00238
00239 }
00240
00241 #endif // OPENMS_ANALYSIS_OPENSWATH_OPENSWATHALGO_ALGO_STATSHELPERS_H