OpenMS
Loading...
Searching...
No Matches
FragmentIonLikelihoodModel.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: Timo Sachsenberg, Oliver Kohlbacher $
7// --------------------------------------------------------------------------
8
9#pragma once
10
14
15#include <cstdint>
16#include <string>
17#include <vector>
18
19namespace OpenMS
20{
21 class AASequence;
22
55 class OPENMS_DLLAPI FragmentIonLikelihoodModel
56 {
57 public:
59 enum class ContextSet
60 {
61 BASIC,
62 RICH
63 };
64
65 static constexpr Size SERIES = 2;
66 static constexpr Size PRECURSOR_BUCKETS = 3;
67 static constexpr Size FRAGMENT_CHARGES = 2;
68 static constexpr Size POSITION_BINS = 10;
69 static constexpr Size CLEAVAGE_SITES = 3;
70 static constexpr Size COMPLEMENT_STATES = 2;
71 static constexpr Size RANK_BINS = 7;
72 static constexpr Size ERROR_BINS = 3;
73
81 class OPENMS_DLLAPI PeakLists
82 {
83 public:
85 void reset(Size spectra);
86
88 void clear();
89
91 Size size() const { return mz_.size(); }
92
102 void assign(Size index, const MSSpectrum& spectrum);
103
105 Size peaks(Size index) const { return mz_[index].size(); }
106
108 double mz(Size index, Size peak) const { return mz_[index][peak]; }
109
111 Size rankBin(Size index, Size peak) const { return rank_bins_[index][peak]; }
112
120 Int findNearest(Size index, double mz, double tolerance) const;
121
124
127
128 private:
129 std::vector<std::vector<float>> mz_;
130 std::vector<std::vector<std::uint8_t>> rank_bins_;
131 };
132
134 struct Ion
135 {
138 };
139
141 struct Features
142 {
143 double log_likelihood_ratio = 0.0;
144 double explained_presence = 0.0;
145 double top_predicted_observed = 0.0;
146 Size matched_ions = 0;
147 Size theoretical_ions = 0;
148 };
149
157 explicit FragmentIonLikelihoodModel(ContextSet context_set = ContextSet::RICH, double pseudo_count = 20.0);
158
160 ContextSet contextSet() const { return context_set_; }
161
163 Size contexts() const { return contexts_; }
164
166 Size outcomes() const { return outcomes_; }
167
169 Size absentOutcome() const { return outcomes_ - 1; }
170
172 static Size rankBin(Size rank);
173
175 static Size errorBin(double relative_error);
176
183 static bool parseIonName(const std::string& name, bool& prefix, Size& ordinal);
184
201 void matchIons(const PeakLists& peaks,
202 Size index,
203 const MSSpectrum& theoretical,
204 const AASequence& peptide,
205 int precursor_charge,
206 double tolerance,
207 bool ppm,
208 std::vector<Ion>& ions) const;
209
211 void addObservations(const std::vector<Ion>& ions, bool noise);
212
214 void finalize();
215
217 bool isTrained() const { return finalized_; }
218
220 Size signalPsms() const { return signal_psms_; }
221
223 Size noisePsms() const { return noise_psms_; }
224
226 double pseudoCount() const { return pseudo_count_; }
227
232 double logLikelihoodRatio(Size context, Size outcome) const;
233
238 double presenceProbability(Size context) const;
239
244 Features score(const std::vector<Ion>& ions, Size top_k = 6) const;
245
246 private:
248 Size level1Context_(Size context) const;
249 Size level2Context_(Size context) const;
250
252 std::vector<double> smooth_(const std::vector<double>& counts) const;
253
255 void requireTrained_() const;
256
263 std::vector<double> signal_counts_;
264 std::vector<double> noise_counts_;
265 std::vector<double> signal_logp_;
266 std::vector<double> noise_logp_;
267 Size signal_psms_ = 0;
268 Size noise_psms_ = 0;
269 double pseudo_count_ = 20.0;
270 bool finalized_ = false;
271 };
272} // namespace OpenMS
Definition AASequence.h:96
Compact peak lists of the spectra of one run: per peak only its m/z and its intensity-rank bin.
Definition FragmentIonLikelihoodModel.h:82
Size totalPeaks() const
Number of peaks over all lists.
std::vector< std::vector< float > > mz_
m/z per peak, ascending, per spectrum
Definition FragmentIonLikelihoodModel.h:129
double mz(Size index, Size peak) const
m/z of peak peak of list index
Definition FragmentIonLikelihoodModel.h:108
Size rankBin(Size index, Size peak) const
Intensity-rank bin (see rankBin()) of peak peak of list index.
Definition FragmentIonLikelihoodModel.h:111
std::vector< std::vector< std::uint8_t > > rank_bins_
intensity-rank bin per peak, per spectrum
Definition FragmentIonLikelihoodModel.h:130
void assign(Size index, const MSSpectrum &spectrum)
Store the peaks of spectrum as list index, replacing what was there.
Size peaks(Size index) const
Number of peaks of list index.
Definition FragmentIonLikelihoodModel.h:105
void clear()
Remove all peak lists and release their memory.
Size size() const
Number of peak lists (spectra)
Definition FragmentIonLikelihoodModel.h:91
void reset(Size spectra)
Remove all peak lists and make room for spectra empty ones.
Int findNearest(Size index, double mz, double tolerance) const
The peak of list index nearest to mz, if it lies within [mz - tolerance, mz + tolerance].
Size memoryUsage() const
Bytes held by the peak lists (peak data, list headers)
Per-run likelihood model of fragment ion presence, intensity rank and mass error, learned from confid...
Definition FragmentIonLikelihoodModel.h:56
std::vector< double > signal_counts_
contexts_ * outcomes_ counts of confident ions
Definition FragmentIonLikelihoodModel.h:263
Size signalPsms() const
Number of confident PSMs added.
Definition FragmentIonLikelihoodModel.h:220
static Size rankBin(Size rank)
Rank bin (0 .. RANK_BINS - 1) of a matched peak with the given 1-based intensity rank.
Size absentOutcome() const
Outcome of an unmatched ion.
Definition FragmentIonLikelihoodModel.h:169
FragmentIonLikelihoodModel(ContextSet context_set=ContextSet::RICH, double pseudo_count=20.0)
Untrained model.
bool isTrained() const
Whether finalize() was called after the last observation.
Definition FragmentIonLikelihoodModel.h:217
void finalize()
Turn the counts into smoothed log-probabilities. Required before scoring; observations added later ne...
std::vector< double > signal_logp_
smoothed ln P(outcome | signal, context)
Definition FragmentIonLikelihoodModel.h:265
Size complement_
complement states of the context set (1 for BASIC)
Definition FragmentIonLikelihoodModel.h:259
Size outcomes_
Definition FragmentIonLikelihoodModel.h:262
double pseudoCount() const
Pseudo-count of the back-off smoothing.
Definition FragmentIonLikelihoodModel.h:226
void requireTrained_() const
Throws unless finalize() was called after the last observation.
double presenceProbability(Size context) const
Probability that an ion of this context is present in the spectrum of its peptide.
void addObservations(const std::vector< Ion > &ions, bool noise)
Add the matched ions of one PSM to the signal (noise false) or noise counts.
Size level1Context_(Size context) const
Contexts of the coarser back-off levels: level 1 is the coarsest above the flat prior.
ContextSet contextSet() const
The context set of this model.
Definition FragmentIonLikelihoodModel.h:160
Size noisePsms() const
Number of reversed PSMs added.
Definition FragmentIonLikelihoodModel.h:223
double logLikelihoodRatio(Size context, Size outcome) const
ln P(outcome | signal, context) - ln P(outcome | noise, context)
Size errors_
error bins of the context set (1 for BASIC)
Definition FragmentIonLikelihoodModel.h:260
static Size errorBin(double relative_error)
Error bin (0 .. ERROR_BINS - 1) of a match with the given |mass error| / tolerance (at most 1)
Size contexts_
Definition FragmentIonLikelihoodModel.h:261
static bool parseIonName(const std::string &name, bool &prefix, Size &ordinal)
Series and ordinal of an ion name as written by TheoreticalSpectrumGenerator, e.g....
ContextSet context_set_
Definition FragmentIonLikelihoodModel.h:257
UInt32 context
flat context index, below contexts()
Definition FragmentIonLikelihoodModel.h:136
Size sites_
cleavage-site classes of the context set (1 for BASIC)
Definition FragmentIonLikelihoodModel.h:258
Size outcomes() const
Number of outcomes, absence included.
Definition FragmentIonLikelihoodModel.h:166
Size contexts() const
Number of ion contexts.
Definition FragmentIonLikelihoodModel.h:163
std::vector< double > noise_logp_
smoothed ln P(outcome | noise, context)
Definition FragmentIonLikelihoodModel.h:266
ContextSet
Ion contexts and outcomes the model distinguishes.
Definition FragmentIonLikelihoodModel.h:60
std::vector< double > noise_counts_
contexts_ * outcomes_ counts of reversed ions
Definition FragmentIonLikelihoodModel.h:264
UInt32 outcome
outcome index, below outcomes(); absentOutcome() if unmatched
Definition FragmentIonLikelihoodModel.h:137
std::vector< double > smooth_(const std::vector< double > &counts) const
Smoothed log-probabilities of one count table, backing off over the context hierarchy.
void matchIons(const PeakLists &peaks, Size index, const MSSpectrum &theoretical, const AASequence &peptide, int precursor_charge, double tolerance, bool ppm, std::vector< Ion > &ions) const
Match the named ions of a theoretical spectrum against peak list index: one Ion per recognised ion.
Size level2Context_(Size context) const
Features score(const std::vector< Ion > &ions, Size top_k=6) const
Features of one PSM from its matched ions (see matchIons()); top_k bounds top_predicted_observed.
Features of one PSM.
Definition FragmentIonLikelihoodModel.h:142
One theoretical ion after matching.
Definition FragmentIonLikelihoodModel.h:135
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
int Int
Signed integer type.
Definition Types.h:72
uint32_t UInt32
Unsigned integer type (32bit)
Definition Types.h:33
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19