OpenMS
Loading...
Searching...
No Matches
IDFreeMassErrorEstimator.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: Justin Sing $
6// $Authors: Justin Sing $
7// --------------------------------------------------------------------------
8
9#pragma once
10
13
14#include <deque>
15#include <map>
16#include <limits>
17#include <optional>
18#include <string>
19#include <utility>
20#include <vector>
21
22namespace OpenMS
23{
43 class OPENMS_DLLAPI IDFreeMassErrorEstimator
44 {
45 public:
47 struct OPENMS_DLLAPI Parameters
48 {
49 double rt_window_seconds{120.0};
50 double precursor_candidate_ppm{20.0};
51 double fragment_match_da{0.2};
52 double fragment_low_res_match_da{0.5};
53 double fragment_very_low_res_match_da{1.0};
54 Size top_peaks{50};
55 Size min_matched_peaks{8};
56 double min_overlap_fraction{0.25};
57 Size max_candidates_per_bin{32};
58 Size min_spectrum_pairs{25};
59 Size min_precursor_clusters{10};
60 Size min_precursor_cluster_size{3};
61 Size min_fragment_pairs{200};
62 Size min_tolerance_pairs{200};
63 Size min_tolerance_clusters{100};
64 double tolerance_sigma_multiplier{6.0};
65 Size min_fragment_tolerance_pairs{1000};
66 Size min_fragment_tolerance_spectra{50};
67 double fragment_tolerance_sigma_multiplier{6.0};
68 double fragment_high_res_max_sigma_da{0.01};
69 double fragment_high_res_max_sigma_ppm{10.0};
70 double fragment_high_intensity_quantile{0.75};
71 double fragment_low_res_min_sigma_da{0.01};
72 double fragment_low_res_min_sigma_ppm{20.0};
73 };
74
76 struct OPENMS_DLLAPI RobustError
77 {
78 double pairwise_median{0.0};
79 double pairwise_sigma{0.0};
80 double single_measurement_sigma{0.0};
81 double pairwise_p95_abs{0.0};
82 double robust_inlier_threshold_3sigma{0.0};
83 Size robust_inlier_count{0};
84 Size robust_outlier_count{0};
85 double robust_inlier_fraction{0.0};
86 double robust_inlier_p95_abs_centered{0.0};
87 };
88
90 {
91 UNAVAILABLE,
92 HIGH_RESOLUTION,
93 LOW_RESOLUTION
94 };
95
97 struct OPENMS_DLLAPI ToleranceSuggestion
98 {
99 double tolerance{0.0};
100 double single_measurement_sigma{0.0};
101 double sigma_multiplier{0.0};
102 std::string unit;
103 std::string confidence;
104 Size support{0};
105 };
106
108 struct OPENMS_DLLAPI Diagnostics
109 {
110 Size precursor_eligible_ms2{0};
111 Size precursor_paired_spectra{0};
112 Size precursor_clusters_used{0};
113 Size fragment_eligible_ms2{0};
114 Size fragment_paired_spectra{0};
115 Size fragment_pairs{0};
116 Size fragment_mixture_pairs{0};
117 Size fragment_low_res_pairs{0};
118 Size fragment_very_low_res_pairs{0};
119 Size fragment_centroid_spectra{0};
120 Size fragment_profile_spectra{0};
121 Size fragment_profile_centroids{0};
122 Size fragment_zero_delta_pairs{0};
123 Size fragment_mixture_signal_pairs{0};
124 double fragment_zero_delta_fraction{0.0};
125 double fragment_mixture_mean_ppm{0.0};
126 double fragment_mixture_reference_mz{0.0};
127 double fragment_mixture_signal_fraction{0.0};
128 Size fragment_mixture_iterations{0};
129 bool fragment_mixture_converged{false};
130 bool fragment_mixture_rejected_zero_quantization{false};
131 Size fragment_high_intensity_pairs{0};
132 Size fragment_high_intensity_signal_pairs{0};
133 double fragment_high_intensity_threshold{0.0};
134 double fragment_high_intensity_signal_fraction{0.0};
135 bool fragment_high_intensity_mixture_converged{false};
136 bool fragment_high_intensity_fallback_used{false};
137 Size excluded_fragment_profile_or_unknown{0};
138 Size excluded_fragment_unknown{0};
139 Size excluded_profile_peak_pick_failure{0};
140 Size excluded_low_fragment_peaks{0};
141 Size excluded_missing_precursor{0};
142 };
143
145 struct OPENMS_DLLAPI Result
146 {
147 std::optional<RobustError> precursor_ppm;
148 std::optional<RobustError> precursor_da;
149 std::optional<RobustError> fragment_ppm;
150 std::optional<RobustError> fragment_da;
151 std::optional<ToleranceSuggestion> precursor_tolerance_ppm;
152 std::optional<ToleranceSuggestion> fragment_tolerance_ppm;
153 std::optional<ToleranceSuggestion> fragment_tolerance_da;
154 FragmentResolutionRegime fragment_resolution_regime{FragmentResolutionRegime::UNAVAILABLE};
155 double fragment_match_window_da{0.2};
156 bool fragment_window_censored{false};
158 };
159
161 explicit IDFreeMassErrorEstimator(const Parameters& parameters);
162
164 void setParameters(const Parameters& parameters);
165
166 const Parameters& getParameters() const;
167
169 void reset();
170
175 void consumeSpectrum(const MSSpectrum& spectrum);
176
178 Result compute(const MSExperiment& experiment);
179
182
184 std::optional<RobustError> getPrecursorPrecisionPPM() const;
185
186 private:
188 {
189 double last_rt{0.0};
190 double mean_mz{0.0};
191 Size count{1};
192 double last_mz{0.0};
193 std::vector<double> pending_da;
194 std::vector<double> pending_ppm;
195 bool committed{false};
196 };
197
199 {
200 double rt{0.0};
201 double precursor_mz{0.0};
202 std::vector<double> mz;
203 std::vector<double> relative_intensity;
204 };
205
207 {
208 double mean{0.0};
209 double pairwise_sigma{0.0};
210 double signal_fraction{0.0};
211 Size iterations{0};
212 bool converged{false};
213 double log_likelihood{-std::numeric_limits<double>::infinity()};
214 };
215
216 using BinKey = std::pair<Int, Int>;
217
219 std::map<BinKey, std::vector<PrecursorCluster>> precursor_bins_;
220 std::map<BinKey, std::deque<FragmentFingerprint>> fragment_bins_;
221 std::vector<double> precursor_errors_ppm_;
222 std::vector<double> precursor_errors_da_;
223 std::vector<double> fragment_errors_da_;
224 std::vector<double> fragment_errors_ppm_;
225 std::vector<double> fragment_mixture_errors_ppm_;
226 std::vector<double> fragment_mixture_mean_mz_;
228 std::vector<double> fragment_low_res_errors_da_;
229 std::vector<double> fragment_low_res_errors_ppm_;
233
234 static void validateParameters_(const Parameters& parameters);
235 static std::optional<RobustError> robustError_(const std::vector<double>& values);
236 static std::optional<GaussianUniformFit> gaussianUniformFit_(const std::vector<double>& values);
238 const std::vector<double>& values,
239 const GaussianUniformFit& fit);
240 static std::pair<std::vector<double>, std::vector<double>> centroidTopPeaks_(const MSSpectrum& spectrum, Size limit);
241 static std::pair<std::vector<double>, std::vector<double>> profilePeakCenters_(const MSSpectrum& spectrum, Size limit);
242 static std::pair<std::vector<double>, std::vector<double>> matchedDeltas_(
243 const std::vector<double>& left_mz,
244 const std::vector<double>& right_mz,
245 double tolerance_da);
246 static std::pair<std::vector<double>, std::vector<double>> coarseBinnedDeltas_(
247 const std::vector<double>& left_mz,
248 const std::vector<double>& left_relative_intensity,
249 const std::vector<double>& right_mz,
250 const std::vector<double>& right_relative_intensity,
251 double bin_width_da,
252 std::vector<double>& shared_relative_intensity);
253
255 void consumePrecursor_(const MSSpectrum& spectrum, double precursor_mz, Int charge);
256 std::pair<std::vector<double>, std::vector<double>> fragmentPeaks_(const MSSpectrum& spectrum);
257 void consumeFragments_(const MSSpectrum& spectrum, double precursor_mz, Int charge);
258 };
259} // namespace OpenMS
Identification-free precursor and fragment mass-precision estimator.
Definition IDFreeMassErrorEstimator.h:44
std::vector< double > precursor_errors_da_
Definition IDFreeMassErrorEstimator.h:222
std::map< BinKey, std::deque< FragmentFingerprint > > fragment_bins_
Definition IDFreeMassErrorEstimator.h:220
std::vector< double > fragment_mixture_errors_ppm_
Definition IDFreeMassErrorEstimator.h:225
Parameters parameters_
Definition IDFreeMassErrorEstimator.h:218
std::pair< Int, Int > BinKey
Definition IDFreeMassErrorEstimator.h:216
std::optional< RobustError > getPrecursorPrecisionPPM() const
Return only the current precursor-ppm precision summary.
static void validateParameters_(const Parameters &parameters)
std::string unit
Definition IDFreeMassErrorEstimator.h:102
FragmentResolutionRegime
Definition IDFreeMassErrorEstimator.h:90
std::vector< double > fragment_low_res_errors_da_
Definition IDFreeMassErrorEstimator.h:228
std::optional< ToleranceSuggestion > precursor_tolerance_ppm
Definition IDFreeMassErrorEstimator.h:151
static std::optional< RobustError > robustError_(const std::vector< double > &values)
std::vector< double > fragment_errors_ppm_
Definition IDFreeMassErrorEstimator.h:224
std::vector< double > fragment_very_low_res_errors_ppm_
Definition IDFreeMassErrorEstimator.h:231
Diagnostics diagnostics_
Definition IDFreeMassErrorEstimator.h:232
static std::pair< std::vector< double >, std::vector< double > > matchedDeltas_(const std::vector< double > &left_mz, const std::vector< double > &right_mz, double tolerance_da)
std::optional< ToleranceSuggestion > fragment_tolerance_ppm
Definition IDFreeMassErrorEstimator.h:152
Result getResult() const
Return the current complete estimate without mutating accumulated state.
std::string confidence
Definition IDFreeMassErrorEstimator.h:103
void setParameters(const Parameters &parameters)
Replace estimator parameters and clear all accumulated evidence.
static RobustError errorFromGaussianUniformFit_(const std::vector< double > &values, const GaussianUniformFit &fit)
const Parameters & getParameters() const
std::vector< double > pending_da
Definition IDFreeMassErrorEstimator.h:193
std::vector< double > fragment_errors_da_
Definition IDFreeMassErrorEstimator.h:223
std::vector< double > precursor_errors_ppm_
Definition IDFreeMassErrorEstimator.h:221
static std::pair< std::vector< double >, std::vector< double > > profilePeakCenters_(const MSSpectrum &spectrum, Size limit)
std::map< BinKey, std::vector< PrecursorCluster > > precursor_bins_
Definition IDFreeMassErrorEstimator.h:219
std::vector< double > mz
Definition IDFreeMassErrorEstimator.h:202
void consumePrecursor_(const MSSpectrum &spectrum, double precursor_mz, Int charge)
std::vector< double > fragment_low_res_errors_ppm_
Definition IDFreeMassErrorEstimator.h:229
static std::optional< GaussianUniformFit > gaussianUniformFit_(const std::vector< double > &values)
std::optional< RobustError > precursor_da
Definition IDFreeMassErrorEstimator.h:148
Result compute(const MSExperiment &experiment)
Reset, consume all spectra in experiment, and return the resulting estimate.
std::vector< double > fragment_very_low_res_errors_da_
Definition IDFreeMassErrorEstimator.h:230
void consumeSpectrum(const MSSpectrum &spectrum)
std::vector< double > fragment_mixture_mean_mz_
Definition IDFreeMassErrorEstimator.h:226
IDFreeMassErrorEstimator(const Parameters &parameters)
void commitPrecursorCluster_(PrecursorCluster &cluster)
std::vector< double > pending_ppm
Definition IDFreeMassErrorEstimator.h:194
std::vector< double > fragment_mixture_shared_relative_intensity_
Definition IDFreeMassErrorEstimator.h:227
Diagnostics diagnostics
Definition IDFreeMassErrorEstimator.h:157
std::optional< ToleranceSuggestion > fragment_tolerance_da
Definition IDFreeMassErrorEstimator.h:153
void consumeFragments_(const MSSpectrum &spectrum, double precursor_mz, Int charge)
void reset()
Clear all accumulated evidence while retaining the current parameters.
std::optional< RobustError > fragment_ppm
Definition IDFreeMassErrorEstimator.h:149
std::optional< RobustError > precursor_ppm
Definition IDFreeMassErrorEstimator.h:147
static std::pair< std::vector< double >, std::vector< double > > centroidTopPeaks_(const MSSpectrum &spectrum, Size limit)
std::vector< double > relative_intensity
Definition IDFreeMassErrorEstimator.h:203
std::optional< RobustError > fragment_da
Definition IDFreeMassErrorEstimator.h:150
static std::pair< std::vector< double >, std::vector< double > > coarseBinnedDeltas_(const std::vector< double > &left_mz, const std::vector< double > &left_relative_intensity, const std::vector< double > &right_mz, const std::vector< double > &right_relative_intensity, double bin_width_da, std::vector< double > &shared_relative_intensity)
std::pair< std::vector< double >, std::vector< double > > fragmentPeaks_(const MSSpectrum &spectrum)
Estimator support/accounting counters.
Definition IDFreeMassErrorEstimator.h:109
Definition IDFreeMassErrorEstimator.h:199
Definition IDFreeMassErrorEstimator.h:207
Tunable estimator thresholds. Defaults match the validated prideQC implementation.
Definition IDFreeMassErrorEstimator.h:48
Definition IDFreeMassErrorEstimator.h:188
Current precision estimates, optional tolerance suggestions and diagnostics.
Definition IDFreeMassErrorEstimator.h:146
Robust summary of pairwise repeated-measurement differences.
Definition IDFreeMassErrorEstimator.h:77
Precision-derived search-tolerance suggestion for one unit system.
Definition IDFreeMassErrorEstimator.h:98
In-Memory representation of a mass spectrometry run.
Definition MSExperiment.h:49
The representation of a 1D spectrum.
Definition MSSpectrum.h:45
int Int
Signed integer type.
Definition Types.h:72
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