OpenMS  2.7.0
EmgScoring.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: Hannes Roest $
32 // $Authors: Hannes Roest $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <vector>
38 #include <boost/math/special_functions/fpclassify.hpp> // for isnan
42 
45 
47 
48 
49 namespace OpenMS
50 {
51 
59  class EmgScoring
60  {
61 
62  public :
63 
64  EmgScoring() = default;
65 
66  ~EmgScoring() = default;
67 
70  void setFitterParam(const Param& param)
71  {
72  fitter_emg1D_params_ = param;
73  }
74 
77  {
78  return EmgFitter1D().getDefaults();
79  }
80 
82  template<typename SpectrumType, class TransitionT>
83  double calcElutionFitScore(MRMFeature & mrmfeature, MRMTransitionGroup<SpectrumType, TransitionT> & transition_group) const
84  {
85  double avg_score = 0;
86  bool smooth_data = false;
87 
88  for (Size k = 0; k < transition_group.size(); k++)
89  {
90  // get the id, then find the corresponding transition and features within this peakgroup
91  String native_id = transition_group.getChromatograms()[k].getNativeID();
92  Feature f = mrmfeature.getFeature(native_id);
93  OPENMS_PRECONDITION(f.getConvexHulls().size() == 1, "Convex hulls need to have exactly one hull point structure");
94 
95  //TODO think about penalizing aborted fits even more. Currently -1 is just the "lowest" pearson correlation to
96  // a fit that you can have.
97  double fscore = elutionModelFit(f.getConvexHulls()[0].getHullPoints(), smooth_data);
98  avg_score += fscore;
99  }
100 
101  avg_score /= transition_group.size();
102  return avg_score;
103  }
104 
105  // Fxn from FeatureFinderAlgorithmMRM
106  // TODO: check whether we can leave out some of the steps here, e.g. gaussian smoothing
107  double elutionModelFit(const ConvexHull2D::PointArrayType& current_section, bool smooth_data) const
108  {
109  // We need at least 2 datapoints in order to create a fit
110  if (current_section.size() < 2)
111  {
112  return -1;
113  }
114 
115  // local PeakType is a small hack since here we *need* data of type
116  // Peak1D, otherwise our fitter will not accept it.
117  typedef Peak1D LocalPeakType;
118 
119  // -- cut line 301 of FeatureFinderAlgorithmMRM
120  std::vector<LocalPeakType> data_to_fit;
121  prepareFit_(current_section, data_to_fit, smooth_data);
122  std::unique_ptr<InterpolationModel> model_rt;
123  double quality = fitRT_(data_to_fit, model_rt);
124  // cut line 354 of FeatureFinderAlgorithmMRM
125 
126  return quality;
127  }
128 
129  protected:
130  template<class LocalPeakType>
131  double fitRT_(std::vector<LocalPeakType>& rt_input_data, std::unique_ptr<InterpolationModel>& model) const
132  {
133  EmgFitter1D fitter_emg1D;
134  fitter_emg1D.setParameters(fitter_emg1D_params_);
135  // Construct model for rt
136  // NaN is checked in fit1d: if (boost::math::isnan(quality)) quality = -1.0;
137  return fitter_emg1D.fit1d(rt_input_data, model);
138  }
139 
140  // Fxn from FeatureFinderAlgorithmMRM
141  // TODO: check whether we can leave out some of the steps here, e.g. gaussian smoothing
142  template<class LocalPeakType>
143  void prepareFit_(const ConvexHull2D::PointArrayType & current_section, std::vector<LocalPeakType> & data_to_fit, bool smooth_data) const
144  {
145  // typedef Peak1D LocalPeakType;
146  PeakSpectrum filter_spec;
147  // first smooth the data to prevent outliers from destroying the fit
148  for (ConvexHull2D::PointArrayType::const_iterator it = current_section.begin(); it != current_section.end(); it++)
149  {
150  LocalPeakType p;
151  p.setMZ(it->getX());
152  p.setIntensity(it->getY());
153  filter_spec.push_back(p);
154  }
155 
156  // add two peaks at the beginning and at the end for better fit
157  // therefore calculate average distance first
158  std::vector<double> distances;
159  for (Size j = 1; j < filter_spec.size(); ++j)
160  {
161  distances.push_back(filter_spec[j].getMZ() - filter_spec[j - 1].getMZ());
162  }
163  double dist_average = std::accumulate(distances.begin(), distances.end(), 0.0) / (double) distances.size();
164 
165  // append peaks
166  Peak1D new_peak;
167  new_peak.setIntensity(0);
168  new_peak.setMZ(filter_spec.back().getMZ() + dist_average);
169  filter_spec.push_back(new_peak);
170  new_peak.setMZ(filter_spec.back().getMZ() + dist_average);
171  filter_spec.push_back(new_peak);
172  new_peak.setMZ(filter_spec.back().getMZ() + dist_average);
173  filter_spec.push_back(new_peak);
174 
175  // prepend peaks
176  new_peak.setMZ(filter_spec.front().getMZ() - dist_average);
177  filter_spec.insert(filter_spec.begin(), new_peak);
178  new_peak.setMZ(filter_spec.front().getMZ() - dist_average);
179  filter_spec.insert(filter_spec.begin(), new_peak);
180  new_peak.setMZ(filter_spec.front().getMZ() - dist_average);
181  filter_spec.insert(filter_spec.begin(), new_peak);
182 
183  // To get an estimate of the peak quality, we probably should not smooth
184  // and/or transform the data.
185  if (smooth_data)
186  {
187  GaussFilter filter;
188  Param filter_param(filter.getParameters());
189  filter.setParameters(filter_param);
190  filter_param.setValue("gaussian_width", 4 * dist_average);
191  filter.setParameters(filter_param);
192  filter.filter(filter_spec);
193  }
194 
195  // transform the data for fitting and fit RT profile
196  for (Size j = 0; j != filter_spec.size(); ++j)
197  {
198  LocalPeakType p;
199  p.setPosition(filter_spec[j].getMZ());
200  p.setIntensity(filter_spec[j].getIntensity());
201  data_to_fit.push_back(p);
202  }
203  }
204 
206  };
207 
208 }
209 
std::vector< PointType > PointArrayType
Definition: ConvexHull2D.h:76
const Param & getParameters() const
Non-mutable access to the parameters.
void setParameters(const Param &param)
Sets the parameters.
const Param & getDefaults() const
Non-mutable access to the default parameters.
Exponentially modified gaussian distribution fitter (1-dim.) using Levenberg-Marquardt algorithm (Eig...
Definition: EmgFitter1D.h:49
QualityType fit1d(const RawDataArrayType &range, std::unique_ptr< InterpolationModel > &model) override
return interpolation model
Scoring of an elution peak using an exponentially modified gaussian distribution model.
Definition: EmgScoring.h:60
EmgScoring()=default
void setFitterParam(const Param &param)
Definition: EmgScoring.h:70
double elutionModelFit(const ConvexHull2D::PointArrayType &current_section, bool smooth_data) const
Definition: EmgScoring.h:107
Param fitter_emg1D_params_
Definition: EmgScoring.h:205
Param getDefaults()
Get default params for the Emg1D fitting.
Definition: EmgScoring.h:76
double calcElutionFitScore(MRMFeature &mrmfeature, MRMTransitionGroup< SpectrumType, TransitionT > &transition_group) const
calculate the elution profile fit score
Definition: EmgScoring.h:83
double fitRT_(std::vector< LocalPeakType > &rt_input_data, std::unique_ptr< InterpolationModel > &model) const
Definition: EmgScoring.h:131
~EmgScoring()=default
void prepareFit_(const ConvexHull2D::PointArrayType &current_section, std::vector< LocalPeakType > &data_to_fit, bool smooth_data) const
Definition: EmgScoring.h:143
An LC-MS feature.
Definition: Feature.h:72
const std::vector< ConvexHull2D > & getConvexHulls() const
Non-mutable access to the convex hulls.
This class represents a Gaussian lowpass-filter which works on uniform as well as on non-uniform prof...
Definition: GaussFilter.h:77
void filter(MSSpectrum &spectrum)
Smoothes an MSSpectrum containing profile data.
Definition: GaussFilter.h:92
A multi-chromatogram MRM feature.
Definition: MRMFeature.h:52
Feature & getFeature(const String &key)
get a specified feature
The representation of a group of transitions in a targeted proteomics experiment.
Definition: MRMTransitionGroup.h:68
Size size() const
Definition: MRMTransitionGroup.h:125
std::vector< ChromatogramType > & getChromatograms()
Definition: MRMTransitionGroup.h:186
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
Management and storage of parameters / INI files.
Definition: Param.h:70
void setValue(const std::string &key, const ParamValue &value, const std::string &description="", const std::vector< std::string > &tags=std::vector< std::string >())
Sets a value.
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:54
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:104
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:113
A more convenient string class.
Definition: String.h:61
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:120
const double k
Definition: Constants.h:153
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47