OpenMS
MZTrafoModel.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: Chris Bielow $
32 // $Authors: Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 
40 #include <vector>
41 
42 namespace OpenMS
43 {
44 
64  class OPENMS_DLLAPI MZTrafoModel
65  {
66 
67  private:
68  std::vector<double> coeff_;
69  bool use_ppm_;
70  double rt_;
71 
73  static int ransac_seed_;
74  static double limit_offset_;
75  static double limit_scale_;
76  static double limit_power_;
77 
78  public:
79 
84 
95  MZTrafoModel(bool ppm_model);
96 
97  enum MODELTYPE { LINEAR, LINEAR_WEIGHTED, QUADRATIC, QUADRATIC_WEIGHTED, SIZE_OF_MODELTYPE };
98  static const std::string names_of_modeltype[];
106  static MODELTYPE nameToEnum(const std::string& name);
113  static const std::string& enumToName(MODELTYPE mt);
114 
115 
125  static void setRANSACParams(const Math::RANSACParam& p);
126 
130  static void setRANSACSeed(int seed);
131 
140  static void setCoefficientLimits(double offset, double scale, double power);
151  static bool isValidModel(const MZTrafoModel& trafo);
152 
159  bool isTrained() const;
160 
164  double getRT() const;
165 
178  double predict(double mz) const;
179 
191  static Size findNearest(const std::vector<MZTrafoModel>& tms, double rt);
192 
193 
195  struct RTLess
196  {
197  inline bool operator()(const double& left, const MZTrafoModel& right) const
198  {
199  return left < right.rt_;
200  }
201  inline bool operator()(const MZTrafoModel& left, const double& right) const
202  {
203  return left.rt_ < right;
204  }
205  inline bool operator()(const MZTrafoModel& left, const MZTrafoModel& right) const
206  {
207  return left.rt_ < right.rt_;
208  }
209  };
231  bool train(const CalibrationData& cd, MODELTYPE md, bool use_RANSAC,
232  double rt_left = -std::numeric_limits<double>::max(),
233  double rt_right = std::numeric_limits<double>::max()
234  );
235 
260  bool train(std::vector<double> error_mz,
261  std::vector<double> theo_mz,
262  std::vector<double> weights,
263  MODELTYPE md,
264  bool use_RANSAC);
265 
277  void getCoefficients(double& intercept, double& slope, double& power);
278 
282  void setCoefficients(const MZTrafoModel& rhs);
283 
295  void setCoefficients(double intercept, double slope, double power);
296 
303  String toString() const;
304 
305  }; // MZTrafoModel
306 
307 } // namespace OpenMS
308 
A helper class, holding all calibration points.
Definition: CalibrationData.h:65
Create and apply models of a mass recalibration function.
Definition: MZTrafoModel.h:65
MZTrafoModel()
Default constructor.
static void setRANSACParams(const Math::RANSACParam &p)
Set the global (program wide) parameters for RANSAC.
void getCoefficients(double &intercept, double &slope, double &power)
Get model coefficients.
double getRT() const
Get RT associated with the model (training region)
bool use_ppm_
during training, model is build on absolute or relative(ppm) predictions. predict(),...
Definition: MZTrafoModel.h:69
static void setCoefficientLimits(double offset, double scale, double power)
Set coefficient boundaries for which the model coefficient must not exceed to be considered a valid m...
String toString() const
String representation of the model parameters.
bool isTrained() const
Does the model have coefficients (i.e. was trained successfully).
static double limit_offset_
acceptable boundary for the estimated offset; if estimated offset is larger (absolute) the model does...
Definition: MZTrafoModel.h:74
MZTrafoModel(bool ppm_model)
Default constructor.
static double limit_power_
acceptable boundary for the estimated power; if estimated power is larger (absolute) the model does n...
Definition: MZTrafoModel.h:76
static int ransac_seed_
seed used for all RANSAC invocations
Definition: MZTrafoModel.h:73
double predict(double mz) const
Apply the model to an uncalibrated m/z value.
bool train(const CalibrationData &cd, MODELTYPE md, bool use_RANSAC, double rt_left=-std::numeric_limits< double >::max(), double rt_right=std::numeric_limits< double >::max())
Train a model using calibrant data.
void setCoefficients(double intercept, double slope, double power)
Manually set model coefficients.
static bool isValidModel(const MZTrafoModel &trafo)
Predicate to decide if the model has valid parameters, i.e. coefficients.
bool train(std::vector< double > error_mz, std::vector< double > theo_mz, std::vector< double > weights, MODELTYPE md, bool use_RANSAC)
Train a model using calibrant data.
void setCoefficients(const MZTrafoModel &rhs)
Copy model coefficients from another model.
static double limit_scale_
acceptable boundary for the estimated scale; if estimated scale is larger (absolute) the model does n...
Definition: MZTrafoModel.h:75
std::vector< double > coeff_
Model coefficients (for both linear and quadratic models), estimated from the data.
Definition: MZTrafoModel.h:68
static Size findNearest(const std::vector< MZTrafoModel > &tms, double rt)
Binary search for the model nearest to a specific RT.
double rt_
retention time associated to the model (i.e. where the calibrant data was taken from)
Definition: MZTrafoModel.h:70
static const std::string & enumToName(MODELTYPE mt)
Convert enum to string.
static Math::RANSACParam * ransac_params_
global pointer, init to NULL at startup; set class-global RANSAC params
Definition: MZTrafoModel.h:72
static MODELTYPE nameToEnum(const std::string &name)
Convert string to enum.
MODELTYPE
Definition: MZTrafoModel.h:97
@ LINEAR
Definition: MZTrafoModel.h:97
static void setRANSACSeed(int seed)
Set RANSAC seed.
A more convenient string class.
Definition: String.h:60
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
Comparator by position. As this class has dimension 1, this is basically an alias for MZLess.
Definition: MZTrafoModel.h:196
bool operator()(const double &left, const MZTrafoModel &right) const
Definition: MZTrafoModel.h:197
bool operator()(const MZTrafoModel &left, const double &right) const
Definition: MZTrafoModel.h:201
bool operator()(const MZTrafoModel &left, const MZTrafoModel &right) const
Definition: MZTrafoModel.h:205
A simple struct to carry all the parameters required for a RANSAC run.
Definition: RANSAC.h:59