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
15
16#include <cstdint>
17#include <string>
18#include <vector>
19
20namespace OpenMS
21{
22 class AASequence;
23
63 class OPENMS_DLLAPI FragmentIonLikelihoodModel
64 {
65 public:
67 enum class ContextSet
68 {
69 BASIC,
70 RICH
71 };
72
75 static constexpr Size SERIES = 4;
76 static constexpr Size PRECURSOR_BUCKETS = 3;
77 static constexpr Size FRAGMENT_CHARGES = 2;
78 static constexpr Size POSITION_BINS = 10;
79 static constexpr Size CLEAVAGE_SITES = 3;
80 static constexpr Size COMPLEMENT_STATES = 2;
81 static constexpr Size RANK_BINS = 7;
82 static constexpr Size ERROR_BINS = 3;
83
91 class OPENMS_DLLAPI PeakLists
92 {
93 public:
95 void reset(Size spectra);
96
98 void clear();
99
101 Size size() const { return mz_.size(); }
102
112 void assign(Size index, const MSSpectrum& spectrum);
113
115 Size peaks(Size index) const { return mz_[index].size(); }
116
118 double mz(Size index, Size peak) const { return mz_[index][peak]; }
119
121 Size rankBin(Size index, Size peak) const { return rank_bins_[index][peak]; }
122
130 Int findNearest(Size index, double mz, double tolerance) const;
131
134
137
138 private:
139 std::vector<std::vector<float>> mz_;
140 std::vector<std::vector<std::uint8_t>> rank_bins_;
141 };
142
144 struct Ion
145 {
148 };
149
151 struct Features
152 {
153 double log_likelihood_ratio = 0.0;
154 double explained_presence = 0.0;
155 double top_predicted_observed = 0.0;
156 Size matched_ions = 0;
157 Size theoretical_ions = 0;
158 };
159
167 explicit FragmentIonLikelihoodModel(ContextSet context_set = ContextSet::RICH, double pseudo_count = 20.0);
168
170 ContextSet contextSet() const { return context_set_; }
171
173 Size contexts() const { return contexts_; }
174
176 Size outcomes() const { return outcomes_; }
177
179 Size absentOutcome() const { return outcomes_ - 1; }
180
182 static Size rankBin(Size rank);
183
185 static Size errorBin(double relative_error);
186
193 static bool parseIonName(const std::string& name, bool& prefix, Size& ordinal);
194
199 static bool parseIonSeries(const std::string& name, Size& series, Size& ordinal);
200
203
220 void matchIons(const PeakLists& peaks,
221 Size index,
222 const MSSpectrum& theoretical,
223 const AASequence& peptide,
224 int precursor_charge,
225 double tolerance,
226 bool ppm,
227 std::vector<Ion>& ions) const;
228
236 void matchIons(const PeakLists& peaks,
237 Size index,
238 const MSSpectrum& theoretical,
239 const std::vector<Residue::ResidueType>& ion_types,
240 const std::vector<UInt>& ion_ordinals,
241 const std::vector<Int>& ion_charges,
242 const AASequence& peptide,
243 int precursor_charge,
244 double tolerance,
245 bool ppm,
246 std::vector<Ion>& ions) const;
247
249 void addObservations(const std::vector<Ion>& ions, bool noise);
250
252 void finalize();
253
255 bool isTrained() const { return finalized_; }
256
258 Size signalPsms() const { return signal_psms_; }
259
261 Size noisePsms() const { return noise_psms_; }
262
264 double pseudoCount() const { return pseudo_count_; }
265
270 double logLikelihoodRatio(Size context, Size outcome) const;
271
276 double presenceProbability(Size context) const;
277
282 Features score(const std::vector<Ion>& ions, Size top_k = 6) const;
283
284 private:
286 template <typename IonOf, typename ChargeOf>
287 void matchIons_(const PeakLists& peaks, Size index, const MSSpectrum& theoretical, IonOf&& ion_of, ChargeOf&& charge_of,
288 const AASequence& peptide, int precursor_charge, double tolerance, bool ppm, std::vector<Ion>& ions) const;
289
291 Size level1Context_(Size context) const;
292 Size level2Context_(Size context) const;
293
295 std::vector<double> smooth_(const std::vector<double>& counts) const;
296
298 void requireTrained_() const;
299
306 std::vector<double> signal_counts_;
307 std::vector<double> noise_counts_;
308 std::vector<double> signal_logp_;
309 std::vector<double> noise_logp_;
310 Size signal_psms_ = 0;
311 Size noise_psms_ = 0;
312 double pseudo_count_ = 20.0;
313 bool finalized_ = false;
314 };
315} // 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:92
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:139
double mz(Size index, Size peak) const
m/z of peak peak of list index
Definition FragmentIonLikelihoodModel.h:118
Size rankBin(Size index, Size peak) const
Intensity-rank bin (see rankBin()) of peak peak of list index.
Definition FragmentIonLikelihoodModel.h:121
std::vector< std::vector< std::uint8_t > > rank_bins_
intensity-rank bin per peak, per spectrum
Definition FragmentIonLikelihoodModel.h:140
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:115
void clear()
Remove all peak lists and release their memory.
Size size() const
Number of peak lists (spectra)
Definition FragmentIonLikelihoodModel.h:101
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:64
std::vector< double > signal_counts_
contexts_ * outcomes_ counts of confident ions
Definition FragmentIonLikelihoodModel.h:306
Size signalPsms() const
Number of confident PSMs added.
Definition FragmentIonLikelihoodModel.h:258
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:179
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:255
void finalize()
Turn the counts into smoothed log-probabilities. Required before scoring; observations added later ne...
static Size seriesOf(Residue::ResidueType type)
Ion series (see SERIES) of an ion type (Residue::AIon ... Residue::Zp2Ion); SERIES for any other type...
std::vector< double > signal_logp_
smoothed ln P(outcome | signal, context)
Definition FragmentIonLikelihoodModel.h:308
Size complement_
complement states of the context set (1 for BASIC)
Definition FragmentIonLikelihoodModel.h:302
Size outcomes_
Definition FragmentIonLikelihoodModel.h:305
double pseudoCount() const
Pseudo-count of the back-off smoothing.
Definition FragmentIonLikelihoodModel.h:264
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.
void matchIons(const PeakLists &peaks, Size index, const MSSpectrum &theoretical, const std::vector< Residue::ResidueType > &ion_types, const std::vector< UInt > &ion_ordinals, const std::vector< Int > &ion_charges, const AASequence &peptide, int precursor_charge, double tolerance, bool ppm, std::vector< Ion > &ions) const
matchIons() with the ions given as codes instead of names: per peak of theoretical its ion type,...
ContextSet contextSet() const
The context set of this model.
Definition FragmentIonLikelihoodModel.h:170
static bool parseIonSeries(const std::string &name, Size &series, Size &ordinal)
parseIonName() that returns the ion series (see SERIES) instead of the terminus, e....
Size noisePsms() const
Number of reversed PSMs added.
Definition FragmentIonLikelihoodModel.h:261
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:303
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:304
static bool parseIonName(const std::string &name, bool &prefix, Size &ordinal)
Terminus and ordinal of an ion name as written by TheoreticalSpectrumGenerator, e....
ContextSet context_set_
Definition FragmentIonLikelihoodModel.h:300
UInt32 context
flat context index, below contexts()
Definition FragmentIonLikelihoodModel.h:146
Size sites_
cleavage-site classes of the context set (1 for BASIC)
Definition FragmentIonLikelihoodModel.h:301
void matchIons_(const PeakLists &peaks, Size index, const MSSpectrum &theoretical, IonOf &&ion_of, ChargeOf &&charge_of, const AASequence &peptide, int precursor_charge, double tolerance, bool ppm, std::vector< Ion > &ions) const
matchIons() for ion(i, series, ordinal) (false: not an ion of a series) and charge(i) of the peaks
Size outcomes() const
Number of outcomes, absence included.
Definition FragmentIonLikelihoodModel.h:176
Size contexts() const
Number of ion contexts.
Definition FragmentIonLikelihoodModel.h:173
std::vector< double > noise_logp_
smoothed ln P(outcome | noise, context)
Definition FragmentIonLikelihoodModel.h:309
ContextSet
Ion contexts and outcomes the model distinguishes.
Definition FragmentIonLikelihoodModel.h:68
std::vector< double > noise_counts_
contexts_ * outcomes_ counts of reversed ions
Definition FragmentIonLikelihoodModel.h:307
UInt32 outcome
outcome index, below outcomes(); absentOutcome() if unmatched
Definition FragmentIonLikelihoodModel.h:147
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:152
One theoretical ion after matching.
Definition FragmentIonLikelihoodModel.h:145
The representation of a 1D spectrum.
Definition MSSpectrum.h:45
ResidueType
Definition Residue.h:153
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