OpenMS
Loading...
Searching...
No Matches
ConfidenceScoring.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: Hendrik Weisser $
6// $Authors: Hannes Roest, Hendrik Weisser $
7// --------------------------------------------------------------------------
8
9#pragma once
10
12#include <cmath> // for "exp"
13#include <limits> // for "infinity"
14#include <map>
15
21
23
24namespace OpenMS
25{
26
55 class OPENMS_DLLAPI ConfidenceScoring :
56 public ProgressLogger
57 {
58 public:
59
68 explicit ConfidenceScoring(bool test_mode_ = false);
69
71 ~ConfidenceScoring() override {}
72
73 protected:
74
81 struct GLM_
82 {
83 double intercept;
84 double rt_coef;
85 double int_coef;
86
88 double operator()(double diff_rt, double dist_int) const
89 {
90 double lm = intercept + rt_coef * diff_rt * diff_rt +
91 int_coef * dist_int;
92 return 1.0 / (1.0 + exp(-lm));
93 }
94 } glm_;
95
103 struct RTNorm_
104 {
105 double min_rt;
106 double max_rt;
107
109 double operator()(double rt) const
110 {
111 return (rt - min_rt) / (max_rt - min_rt) * 100;
112 }
113 } rt_norm_;
114
116
118
120
121 std::map<std::string, IntList> transition_map_;
122
124
126
128
131
134
137
153 double feature_rt, DoubleList& feature_intensities,
154 const std::set<std::string>& transition_ids = std::set<std::string>());
155
157 void scoreFeature_(Feature& feature);
158
159 public:
160
169 void initialize(const TargetedExperiment& library, const Size n_decoys, const Size n_transitions, const TransformationDescription& rt_trafo)
170 {
171 library_ = library;
172 n_decoys_ = n_decoys;
173 n_transitions_ = n_transitions;
174 rt_trafo_ = rt_trafo;
175 }
176
187 void initializeGlm(double intercept, double rt_coef, double int_coef)
188 {
189 glm_.intercept = intercept;
190 glm_.rt_coef = rt_coef;
191 glm_.int_coef = int_coef;
192 }
193
220 void scoreMap(FeatureMap & features)
221 {
222 // are there enough assays in the library?
223 Size n_assays = library_.getPeptides().size();
224 if (n_assays < 2)
225 {
226 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
227 "There need to be at least 2 assays in the library for ConfidenceScoring.");
228
229 }
230 if (n_assays - 1 < n_decoys_)
231 {
232 OPENMS_LOG_WARN << "Warning: Parameter 'decoys' (" << n_decoys_
233 << ") is higher than the number of unrelated assays in the "
234 << "library (" << n_assays - 1 << "). "
235 << "Using all unrelated assays as decoys." << std::endl;
236 }
237 if (n_assays - 1 <= n_decoys_) n_decoys_ = 0; // use all available assays
238
239 decoy_index_.resize(n_assays);
240 for (Size i = 0; i < n_assays; ++i)
241 decoy_index_[i] = OpenMS::checkedCast<Int>(i);
242
243 // build mapping between assays and transitions:
244 OPENMS_LOG_DEBUG << "Building transition map..." << std::endl;
245 for (Size i = 0; i < library_.getTransitions().size(); ++i)
246 {
247 const std::string& ref = library_.getTransitions()[i].getPeptideRef();
248 transition_map_[ref].push_back(OpenMS::checkedCast<Int>(i));
249 }
250 // find min./max. RT in the library:
251 OPENMS_LOG_DEBUG << "Determining retention time range..." << std::endl;
252 rt_norm_.min_rt = std::numeric_limits<double>::infinity();
253 rt_norm_.max_rt = -std::numeric_limits<double>::infinity();
254 for (std::vector<TargetedExperiment::Peptide>::const_iterator it =
255 library_.getPeptides().begin(); it != library_.getPeptides().end();
256 ++it)
257 {
258 double current_rt = getAssayRT_(*it);
259 if (current_rt == -1.0) continue; // indicates a missing value
260 rt_norm_.min_rt = std::min(rt_norm_.min_rt, current_rt);
261 rt_norm_.max_rt = std::max(rt_norm_.max_rt, current_rt);
262 }
263
264 // log scoring progress:
265 OPENMS_LOG_DEBUG << "Scoring features..." << std::endl;
266 startProgress(0, features.size(), "scoring features");
267
268 for (FeatureMap::Iterator feat_it = features.begin();
269 feat_it != features.end(); ++feat_it)
270 {
271 OPENMS_LOG_DEBUG << "Feature " << feat_it - features.begin() + 1
272 << " (ID '" << feat_it->getUniqueId() << "')"<< std::endl;
273 scoreFeature_(*feat_it);
274 setProgress(feat_it - features.begin());
275 }
276 endProgress();
277
278 }
279
280 };
281
282}
#define OPENMS_LOG_DEBUG
Definition LogStream.h:714
#define OPENMS_LOG_WARN
Macro for warnings.
Definition LogStream.h:691
Confidence scoring for SRM/MRM/PRM features against a targeted assay library.
Definition ConfidenceScoring.h:57
void scoreMap(FeatureMap &features)
Score every feature in features in place, writing per-feature scores and an updated overall-quality v...
Definition ConfidenceScoring.h:220
void chooseDecoys_()
Permute decoy_index_ in place to pick a fresh random decoy sample for the next feature.
TargetedExperiment library_
Targeted-assay library: one peptide per assay, each with its transitions.
Definition ConfidenceScoring.h:115
Math::RandomShuffler shuffler_
Random shuffler used to draw decoy samples (seed depends on test mode — see ctor)
Definition ConfidenceScoring.h:127
IntList decoy_index_
Indexes into library_.getPeptides() used as decoys for the current feature.
Definition ConfidenceScoring.h:117
Size n_decoys_
Number of decoy assays to sample per feature (0 = use all unrelated assays as decoys)
Definition ConfidenceScoring.h:119
double getAssayRT_(const TargetedExperiment::Peptide &assay)
Read the (single) retention time from an assay's TargetedExperiment::Peptide; the assay is required t...
~ConfidenceScoring() override
Destructor.
Definition ConfidenceScoring.h:71
TransformationDescription rt_trafo_
Optional RT transformation applied to measured feature RTs before comparison with library RTs.
Definition ConfidenceScoring.h:125
double scoreAssay_(const TargetedExperiment::Peptide &assay, double feature_rt, DoubleList &feature_intensities, const std::set< std::string > &transition_ids=std::set< std::string >())
Score one feature against one candidate assay.
double manhattanDist_(DoubleList x, DoubleList y)
Manhattan (L1) distance between two equal-length vectors.
ConfidenceScoring(bool test_mode_=false)
Construct an empty scorer.
void scoreFeature_(Feature &feature)
Score one feature against its matching assay plus a random decoy sample; writes the per-assay scores ...
void initializeGlm(double intercept, double rt_coef, double int_coef)
Install the GLM coefficients fitted externally on a training set.
Definition ConfidenceScoring.h:187
Size n_transitions_
Number of top-intensity transitions to keep when computing the intensity-distance term (0 = keep all)
Definition ConfidenceScoring.h:123
std::map< std::string, IntList > transition_map_
Lookup assay-id -> indexes into library_.getTransitions()
Definition ConfidenceScoring.h:121
void initialize(const TargetedExperiment &library, const Size n_decoys, const Size n_transitions, const TransformationDescription &rt_trafo)
Install the configuration needed before scoreMap() can run.
Definition ConfidenceScoring.h:169
A method or algorithm argument contains illegal values.
Definition Exception.h:633
size_t size() const noexcept
Definition ExposedVector.h:128
iterator begin() noexcept
Definition ExposedVector.h:104
iterator end() noexcept
Definition ExposedVector.h:108
A container for features.
Definition FeatureMap.h:78
iterator Iterator
Definition FeatureMap.h:85
An LC-MS feature.
Definition Feature.h:46
Portable random shuffle with a reproducible, platform-independent random sequence.
Definition MathFunctions.h:479
Base class for all classes that want to report their progress.
Definition ProgressLogger.h:28
Represents a peptide (amino acid sequence)
Definition TargetedExperimentHelper.h:334
A description of a targeted experiment containing precursor and production ions.
Definition TargetedExperiment.h:40
const std::vector< ReactionMonitoringTransition > & getTransitions() const
returns the transition list
const std::vector< Peptide > & getPeptides() const
Generic description of a coordinate transformation.
Definition TransformationDescription.h:38
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
std::vector< Int > IntList
Vector of signed integers.
Definition TypeAliases.h:24
std::vector< double > DoubleList
Vector of double precision real types.
Definition TypeAliases.h:31
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Binomial GLM used to map (squared-normalised-RT-diff, intensity-distance) -> [0, 1] confidence.
Definition ConfidenceScoring.h:82
double rt_coef
GLM coefficient on the squared RT difference (units: 1/RT²)
Definition ConfidenceScoring.h:84
double int_coef
GLM coefficient on the Manhattan intensity distance.
Definition ConfidenceScoring.h:85
double operator()(double diff_rt, double dist_int) const
Evaluate the GLM at (diff_rt, dist_int); returns a probability in [0, 1].
Definition ConfidenceScoring.h:88
double intercept
GLM intercept term.
Definition ConfidenceScoring.h:83
Map RT values into the [0, 100] interval using min/max RT of the assay library.
Definition ConfidenceScoring.h:104
double min_rt
Smallest assay RT in the library; set by scoreMap()
Definition ConfidenceScoring.h:105
double max_rt
Largest assay RT in the library; set by scoreMap()
Definition ConfidenceScoring.h:106
double operator()(double rt) const
Map rt into [0, 100] using the cached min/max library RTs.
Definition ConfidenceScoring.h:109