OpenMS
Loading...
Searching...
No Matches
PosteriorErrorProbabilityModel.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: David Wojnar $
7// --------------------------------------------------------------------------
8
9#pragma once
10
17
18#include <vector>
19#include <map>
20#include <set>
21
22namespace OpenMS
23{
24 class TextFile;
25 class PeptideIdentification;
26 class ProteinIdentification;
27 class PeptideHit;
28 class PeptideIdentificationList;
29 namespace Math
30 {
31
32
77 class OPENMS_DLLAPI PosteriorErrorProbabilityModel :
79 {
80public:
81
83 enum class ModelType
84 {
85 NONE,
86 MIXTURE,
87 TARGET_DECOY_ISOTONIC
88 };
89
91 static std::string modelTypeToString(ModelType type);
92
95
98
121 static std::map<std::string, std::vector<std::vector<double>>> extractAndTransformScores(
122 const std::vector<ProteinIdentification> & protein_ids,
123 const PeptideIdentificationList & peptide_ids,
124 const bool split_charge,
125 const bool top_hits_only,
126 const bool peptide_level = false);
127
143 static std::map<std::string, std::vector<std::vector<double>>> extractAndTransformScores(
144 const std::vector<ProteinIdentification> & protein_ids,
145 const PeptideIdentificationList & peptide_ids,
146 const bool split_charge,
147 const bool top_hits_only,
148 const bool target_decoy_available,
149 const double fdr_for_targets_smaller);
150
163 static void updateScores(
164 const PosteriorErrorProbabilityModel & PEP_model,
165 const std::string & search_engine,
166 const Int charge,
167 const bool prob_correct,
168 const bool split_charge,
169 std::vector<ProteinIdentification> & protein_ids,
170 PeptideIdentificationList & peptide_ids,
171 bool & unable_to_fit_data,
172 bool & data_might_not_be_well_fit);
173
191 static void updateScores(
192 const PosteriorErrorProbabilityModel & PEP_model,
193 const std::string & search_engine,
194 const Int charge,
195 const bool prob_correct,
196 const bool split_charge,
197 std::vector<ProteinIdentification> & protein_ids,
198 PeptideIdentificationList & peptide_ids,
199 bool & unable_to_fit_data,
200 bool & data_might_not_be_well_fit,
201 std::set<std::pair<Size, Size>> & updated_hits);
202
209 static std::string getSearchEngineKey(const ProteinIdentification & protein_id);
210
225 bool fit(std::vector<double>& search_engine_scores, const std::string& outlier_handling);
226
236 bool fitGumbelGauss(std::vector<double>& search_engine_scores, const std::string& outlier_handling);
237
246 bool fit(std::vector<double>& search_engine_scores, std::vector<double>& probabilities, const std::string& outlier_handling);
247
262 bool fitTargetDecoy(const std::vector<double>& target_scores, const std::vector<double>& decoy_scores, const std::string& outlier_handling);
263
266 {
267 return model_type_;
268 }
269
276 {
277 return td_incorrect_fraction_;
278 }
279
293 {
294 return calibration_warning_;
295 }
296
304 double getTargetDecoyAUC() const
305 {
306 return td_auc_;
307 }
308
315 std::vector<std::pair<double, double>> getIsotonicPEPCurve() const;
316
318 void fillDensities(const std::vector<double> & x_scores, std::vector<double> & incorrect_density, std::vector<double> & correct_density);
320 void fillLogDensities(const std::vector<double> & x_scores, std::vector<double> & incorrect_density, std::vector<double> & correct_density);
322 void fillLogDensitiesGumbel(const std::vector<double> & x_scores, std::vector<double> & incorrect_density, std::vector<double> & correct_density);
324 double computeLogLikelihood(const std::vector<double> & incorrect_density, const std::vector<double> & correct_density) const;
325
333 const std::vector<double>& incorrect_log_density,
334 const std::vector<double>& correct_log_density,
335 std::vector<double>& incorrect_posterior) const;
336
343 std::pair<double, double> pos_neg_mean_weighted_posteriors(const std::vector<double> &x_scores,
344 const std::vector<double> &incorrect_posteriors);
345
353 std::pair<double, double> pos_neg_sigma_weighted_posteriors(const std::vector<double> &x_scores,
354 const std::vector<double> &incorrect_posteriors,
355 const std::pair<double, double>& pos_neg_mean);
356
359 {
360 return correctly_assigned_fit_param_;
361 }
362
365 {
366 return incorrectly_assigned_fit_param_;
367 }
368
371 {
372 return incorrectly_assigned_fit_gumbel_param_;
373 }
374
376 double getNegativePrior() const
377 {
378 return negative_prior_;
379 }
380
382 static double getGumbel_(double x, const GaussFitter::GaussFitResult & params)
383 {
384 double z = exp((params.x0 - x) / params.sigma);
385 return (z * exp(-1 * z)) / params.sigma;
386 }
387
394 double computeProbability(double score) const;
395
407 double computeDecoyProbability(double score) const;
408
410 TextFile initPlots(std::vector<double> & x_scores);
411
413 const std::string getGumbelGnuplotFormula(const GaussFitter::GaussFitResult & params) const;
414
416 const std::string getGaussGnuplotFormula(const GaussFitter::GaussFitResult & params) const;
417
419 const std::string getBothGnuplotFormula(const GaussFitter::GaussFitResult & incorrect, const GaussFitter::GaussFitResult & correct) const;
420
428 void plotTargetDecoyEstimation(std::vector<double> & target, std::vector<double> & decoy);
429
431 inline double getSmallestScore() const
432 {
433 return smallest_score_;
434 }
435
437 void tryGnuplot(const std::string& gp_file);
438
439private:
441 void resetFit_();
442
444 void processOutliers_(std::vector<double>& x_scores, const std::string& outlier_handling) const;
445
454 bool fitMixture_(std::vector<double> scores, bool use_gumbel, const std::string& outlier_handling);
455
457 bool fitTargetDecoyModel_(const std::vector<double>& targets, const std::vector<double>& decoys, const std::string& outlier_handling);
458
460 bool fitIsotonic_(const std::vector<double>& target_scores, const std::vector<double>& decoy_scores);
461
463 double logIncorrectDensity_(double x) const;
464
466 double logCorrectDensity_(double x) const;
467
469 double incorrectMode_() const;
470
478 static double transformScore_(const std::string& engine, const PeptideHit& hit, const std::string& current_score_type, bool higher_score_better, const std::string& score_source);
479
481 static void updateScores_(
482 const PosteriorErrorProbabilityModel & PEP_model,
483 const std::string & search_engine,
484 const Int charge,
485 const bool prob_correct,
486 const bool split_charge,
487 std::vector<ProteinIdentification> & protein_ids,
488 PeptideIdentificationList & peptide_ids,
489 bool & unable_to_fit_data,
490 bool & data_might_not_be_well_fit,
491 bool unusable_to_one,
492 std::set<std::pair<Size, Size>> & updated_hits);
493
498 static std::map<std::string, std::string> chooseScoreSources_(const std::map<std::string, std::string>& run_to_engine, const PeptideIdentificationList& peptide_ids);
499
506 static bool getScore_(const std::vector<std::string>& requested_score_types, const PeptideHit & hit, const std::string& actual_score_type, double& value);
507
522 const std::string (PosteriorErrorProbabilityModel::* getNegativeGnuplotFormula_)(const GaussFitter::GaussFitResult & params) const;
524 const std::string (PosteriorErrorProbabilityModel::* getPositiveGnuplotFormula_)(const GaussFitter::GaussFitResult & params) const;
530 std::vector<double> isotonic_scores_;
531 std::vector<double> isotonic_peps_;
533 std::vector<double> decoy_scores_;
534 std::vector<double> decoy_peps_;
538 double td_auc_;
541 };
542 }
543}
A base class for all classes handling default parameters.
Definition DefaultParamHandler.h:66
Implements a fitter for Gaussian functions.
Definition GaussFitter.h:35
Estimates posterior error probabilities (PEPs) of peptide-spectrum matches from search engine scores.
Definition PosteriorErrorProbabilityModel.h:79
PosteriorErrorProbabilityModel & operator=(const PosteriorErrorProbabilityModel &rhs)
assignment operator (not implemented)
static void updateScores_(const PosteriorErrorProbabilityModel &PEP_model, const std::string &search_engine, const Int charge, const bool prob_correct, const bool split_charge, std::vector< ProteinIdentification > &protein_ids, PeptideIdentificationList &peptide_ids, bool &unable_to_fit_data, bool &data_might_not_be_well_fit, bool unusable_to_one, std::set< std::pair< Size, Size > > &updated_hits)
implementation of updateScores(); hits without a usable score get a PEP of 1 if unusable_to_one is se...
bool fitIsotonic_(const std::vector< double > &target_scores, const std::vector< double > &decoy_scores)
estimates the non-parametric target-decoy model; returns false if it cannot be estimated
double computeLLAndIncorrectPosteriorsFromLogDensities(const std::vector< double > &incorrect_log_density, const std::vector< double > &correct_log_density, std::vector< double > &incorrect_posterior) const
std::vector< double > isotonic_scores_
knots of the non-parametric PEP curve: scores (ascending) and PEPs (non-increasing)
Definition PosteriorErrorProbabilityModel.h:530
bool fitGumbelGauss(std::vector< double > &search_engine_scores, const std::string &outlier_handling)
fits the distributions to the data points(search_engine_scores). Estimated parameters for the distrib...
static bool getScore_(const std::vector< std::string > &requested_score_types, const PeptideHit &hit, const std::string &actual_score_type, double &value)
std::vector< double > decoy_peps_
Definition PosteriorErrorProbabilityModel.h:534
double td_incorrect_fraction_
target-decoy estimate of the fraction of incorrect targets (-1 if not available)
Definition PosteriorErrorProbabilityModel.h:536
static std::map< std::string, std::vector< std::vector< double > > > extractAndTransformScores(const std::vector< ProteinIdentification > &protein_ids, const PeptideIdentificationList &peptide_ids, const bool split_charge, const bool top_hits_only, const bool target_decoy_available, const double fdr_for_targets_smaller)
extract and transform score types to a range and score orientation that the PEP model can handle
double logCorrectDensity_(double x) const
log density of the correct component at x (in the shifted score space)
double td_auc_
area under the ROC curve of targets vs. decoys of the last target-decoy fit (-1 if not available)
Definition PosteriorErrorProbabilityModel.h:538
GumbelMaxLikelihoodFitter::GumbelDistributionFitResult incorrectly_assigned_fit_gumbel_param_
Definition PosteriorErrorProbabilityModel.h:514
TextFile initPlots(std::vector< double > &x_scores)
initializes the plots
void plotTargetDecoyEstimation(std::vector< double > &target, std::vector< double > &decoy)
plots the score distributions of target and decoy PSMs together with the estimated model
GaussFitter::GaussFitResult incorrectly_assigned_fit_param_
stores parameters for incorrectly assigned sequences. If gumbel fit was used, A can be ignored....
Definition PosteriorErrorProbabilityModel.h:513
double getTargetDecoyIncorrectFraction() const
returns the target-decoy estimate of the fraction of incorrect target PSMs, i.e. (number of decoys) /...
Definition PosteriorErrorProbabilityModel.h:275
static std::map< std::string, std::vector< std::vector< double > > > extractAndTransformScores(const std::vector< ProteinIdentification > &protein_ids, const PeptideIdentificationList &peptide_ids, const bool split_charge, const bool top_hits_only, const bool peptide_level=false)
extract and transform score types to a range and score orientation that the PEP model can handle
std::vector< double > decoy_scores_
knots of the PEP curve of decoys (label-swapped estimates at the scores of the fitted decoys): scores...
Definition PosteriorErrorProbabilityModel.h:533
bool fit(std::vector< double > &search_engine_scores, const std::string &outlier_handling)
fits the distributions to the data points(search_engine_scores). Estimated parameters for the distrib...
const std::string getBothGnuplotFormula(const GaussFitter::GaussFitResult &incorrect, const GaussFitter::GaussFitResult &correct) const
returns the gnuplot formula of the fitted mixture distribution.
bool hasCalibrationWarning() const
returns true if the last call to fitTargetDecoy() found the target-decoy assumptions to be violated a...
Definition PosteriorErrorProbabilityModel.h:292
void resetFit_()
resets the state of the last fit (model type, target-decoy diagnostics, non-parametric curve)
double computeProbability(double score) const
void fillDensities(const std::vector< double > &x_scores, std::vector< double > &incorrect_density, std::vector< double > &correct_density)
Writes the distributions densities into the two vectors for a set of scores. Incorrect_densities repr...
bool fit(std::vector< double > &search_engine_scores, std::vector< double > &probabilities, const std::string &outlier_handling)
fits the distributions to the data points(search_engine_scores) and writes the computed probabilities...
std::vector< std::pair< double, double > > getIsotonicPEPCurve() const
returns the PEP curve of the non-parametric target-decoy estimate as pairs (score,...
bool fitMixture_(std::vector< double > scores, bool use_gumbel, const std::string &outlier_handling)
fits a two-component mixture model with the EM algorithm
const std::string getGumbelGnuplotFormula(const GaussFitter::GaussFitResult &params) const
returns the gnuplot formula of the fitted gumbel distribution. Only x0 and sigma are used as local pa...
static void updateScores(const PosteriorErrorProbabilityModel &PEP_model, const std::string &search_engine, const Int charge, const bool prob_correct, const bool split_charge, std::vector< ProteinIdentification > &protein_ids, PeptideIdentificationList &peptide_ids, bool &unable_to_fit_data, bool &data_might_not_be_well_fit)
update score entries with PEP (or 1-PEP) estimates
PosteriorErrorProbabilityModel(const PosteriorErrorProbabilityModel &rhs)
Copy constructor (not implemented)
void processOutliers_(std::vector< double > &x_scores, const std::string &outlier_handling) const
remove (or clamp) outliers for fitting
bool calibration_warning_
whether the last target-decoy fit found too few decoys among the lowest-scoring PSMs (see hasCalibrat...
Definition PosteriorErrorProbabilityModel.h:540
const std::string getGaussGnuplotFormula(const GaussFitter::GaussFitResult &params) const
returns the gnuplot formula of the fitted gauss distribution.
GaussFitter::GaussFitResult getIncorrectlyAssignedFitResult() const
returns estimated parameters for incorrectly assigned sequences (for the Gumbel distribution,...
Definition PosteriorErrorProbabilityModel.h:364
double incorrectMode_() const
mode of the incorrect component (in the shifted score space)
double getNegativePrior() const
returns the estimated negative prior probability.
Definition PosteriorErrorProbabilityModel.h:376
void fillLogDensities(const std::vector< double > &x_scores, std::vector< double > &incorrect_density, std::vector< double > &correct_density)
Writes the log distributions densities into the two vectors for a set of scores. Incorrect_densities ...
double computeDecoyProbability(double score) const
returns the posterior error probability of a decoy PSM with the given score
double negative_prior_
stores final prior probability for negative peptides
Definition PosteriorErrorProbabilityModel.h:518
void fillLogDensitiesGumbel(const std::vector< double > &x_scores, std::vector< double > &incorrect_density, std::vector< double > &correct_density)
Writes the log distributions of gumbel and gauss densities into the two vectors for a set of scores....
ModelType
The kind of model that was estimated by the last call to one of the fit functions.
Definition PosteriorErrorProbabilityModel.h:84
~PosteriorErrorProbabilityModel() override
Destructor.
std::vector< double > isotonic_peps_
Definition PosteriorErrorProbabilityModel.h:531
bool incorrect_is_gumbel_
whether the incorrect component of the (last) mixture model is a Gumbel distribution (otherwise Gauss...
Definition PosteriorErrorProbabilityModel.h:528
bool fitTargetDecoyModel_(const std::vector< double > &targets, const std::vector< double > &decoys, const std::string &outlier_handling)
fits the target-decoy model (or, without decoys or with decoy_handling 'ignore', the mixture model) t...
GaussFitter::GaussFitResult correctly_assigned_fit_param_
stores gauss parameters
Definition PosteriorErrorProbabilityModel.h:516
static void updateScores(const PosteriorErrorProbabilityModel &PEP_model, const std::string &search_engine, const Int charge, const bool prob_correct, const bool split_charge, std::vector< ProteinIdentification > &protein_ids, PeptideIdentificationList &peptide_ids, bool &unable_to_fit_data, bool &data_might_not_be_well_fit, std::set< std::pair< Size, Size > > &updated_hits)
update score entries with PEP (or 1-PEP) estimates and report which hits were updated
double logIncorrectDensity_(double x) const
log density of the incorrect component at x (in the shifted score space)
static std::string getSearchEngineKey(const ProteinIdentification &protein_id)
the key under which the scores of a search run are reported by extractAndTransformScores()
ModelType getModelType() const
returns the kind of model estimated by the last fit
Definition PosteriorErrorProbabilityModel.h:265
PosteriorErrorProbabilityModel()
default constructor
bool fitTargetDecoy(const std::vector< double > &target_scores, const std::vector< double > &decoy_scores, const std::string &outlier_handling)
estimates the PEP model from scores of target and decoy PSMs. computeProbability() (targets) and comp...
GaussFitter::GaussFitResult getCorrectlyAssignedFitResult() const
returns estimated parameters for correctly assigned sequences. Fit should be used before.
Definition PosteriorErrorProbabilityModel.h:358
static std::string modelTypeToString(ModelType type)
human-readable name of a ModelType
static double getGumbel_(double x, const GaussFitter::GaussFitResult &params)
computes the gumbel density at position x with parameters params.
Definition PosteriorErrorProbabilityModel.h:382
double smallest_score_
smallest score which was used for fitting the model
Definition PosteriorErrorProbabilityModel.h:520
std::pair< double, double > pos_neg_mean_weighted_posteriors(const std::vector< double > &x_scores, const std::vector< double > &incorrect_posteriors)
static double transformScore_(const std::string &engine, const PeptideHit &hit, const std::string &current_score_type, bool higher_score_better, const std::string &score_source)
double getSmallestScore() const
returns the smallest score used in the last fit
Definition PosteriorErrorProbabilityModel.h:431
double getTargetDecoyAUC() const
returns the area under the ROC curve for separating target from decoy PSMs by score (ties count half)...
Definition PosteriorErrorProbabilityModel.h:304
static std::map< std::string, std::string > chooseScoreSources_(const std::map< std::string, std::string > &run_to_engine, const PeptideIdentificationList &peptide_ids)
GumbelMaxLikelihoodFitter::GumbelDistributionFitResult getIncorrectlyAssignedGumbelFitResult() const
returns estimated Gumbel parameters for incorrectly assigned sequences. Fit should be used before.
Definition PosteriorErrorProbabilityModel.h:370
void tryGnuplot(const std::string &gp_file)
try to invoke 'gnuplot' on the file to create PDF automatically
ModelType model_type_
kind of the last estimated model
Definition PosteriorErrorProbabilityModel.h:526
std::pair< double, double > pos_neg_sigma_weighted_posteriors(const std::vector< double > &x_scores, const std::vector< double > &incorrect_posteriors, const std::pair< double, double > &pos_neg_mean)
double computeLogLikelihood(const std::vector< double > &incorrect_density, const std::vector< double > &correct_density) const
computes the Likelihood with a log-likelihood function.
Represents a single spectrum match (candidate) for a specific tandem mass spectrum (MS/MS).
Definition PeptideHit.h:52
Container for peptide identifications from multiple spectra.
Definition PeptideIdentificationList.h:66
Representation of a protein identification run.
Definition ProteinIdentification.h:55
Definition TextFile.h:21
int Int
Signed integer type.
Definition Types.h:72
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
struct of parameters of a Gaussian distribution
Definition GaussFitter.h:40
double sigma
parameter sigma of Gaussian distribution (width)
Definition GaussFitter.h:54
double x0
parameter x0 of Gaussian distribution (center position)
Definition GaussFitter.h:51
struct to represent the parameters of a gumbel distribution
Definition GumbelMaxLikelihoodFitter.h:38