| OpenMS 2.5.0

TargetedSpectraExtractor.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2020.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Douglas McCloskey, Pasquale Domenico Colaianni $
32 // $Authors: Douglas McCloskey, Pasquale Domenico Colaianni $
33 // --------------------------------------------------------------------------
34
35 #pragma once
36
37 #include <OpenMS/config.h> // OPENMS_DLLAPI
38 #include <OpenMS/ANALYSIS/TARGETED/TargetedExperiment.h>
39 #include <OpenMS/COMPARISON/SPECTRA/BinnedSpectralContrastAngle.h>
40 #include <OpenMS/COMPARISON/SPECTRA/BinnedSpectrum.h>
41 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
42 #include <OpenMS/KERNEL/MSExperiment.h>
43 #include <OpenMS/KERNEL/FeatureMap.h>
44
45 namespace OpenMS
46 {
69 class OPENMS_DLLAPI TargetedSpectraExtractor :
70 public DefaultParamHandler
71 {
72 public:
73 TargetedSpectraExtractor();
74 ~TargetedSpectraExtractor() override = default;
75
80 struct Match
81 {
82 Match() = default;
83 Match(MSSpectrum a, double b) : spectrum(std::move(a)), score(b) {}
84 MSSpectrum spectrum;
85 double score = 0.0;
86 };
87
88 class Comparator
89 {
90 public:
91 virtual ~Comparator() = default;
92 virtual void generateScores(
93 const MSSpectrum& spec,
94 std::vector<std::pair<Size,double>>& scores,
95 double min_score
96 ) const = 0;
97
98 virtual void init(
99 const std::vector<MSSpectrum>& library,
100 const std::map<String,DataValue>& options
101 ) = 0;
102
103 const std::vector<MSSpectrum>& getLibrary() const
104 {
105 return library_;
106 }
107
108 protected:
109 std::vector<MSSpectrum> library_;
110 };
111
112 class BinnedSpectrumComparator : public Comparator
113 {
114 public:
115 ~BinnedSpectrumComparator() override = default;
116 void generateScores (
117 const MSSpectrum& spec,
118 std::vector<std::pair<Size,double>>& scores,
119 double min_score
120 ) const override
121 {
122 scores.clear();
123 const BinnedSpectrum in_bs(spec, bin_size_, false, peak_spread_, bin_offset_);
124 for (Size i = 0; i < bs_library_.size(); ++i)
125 {
126 const double cmp_score = cmp_bs_(in_bs, bs_library_[i]);
127 if (cmp_score >= min_score)
128 {
129 scores.emplace_back(i, cmp_score);
130 }
131 }
132 }
133
134 void init(const std::vector<MSSpectrum>& library, const std::map<String,DataValue>& options) override
135 {
136 if (options.count("bin_size"))
137 {
138 bin_size_ = options.at("bin_size");
139 }
140 if (options.count("peak_spread"))
141 {
142 peak_spread_ = options.at("peak_spread");
143 }
144 if (options.count("bin_offset"))
145 {
146 bin_offset_ = options.at("bin_offset");
147 }
148 library_ = library;
149 bs_library_.clear();
150 for (const MSSpectrum& s : library_)
151 {
152 bs_library_.emplace_back(s, bin_size_, false, peak_spread_, bin_offset_);
153 }
154 OPENMS_LOG_INFO << "The library contains " << bs_library_.size() << " spectra." << std::endl;
155 }
156 private:
157 BinnedSpectralContrastAngle cmp_bs_;
158 std::vector<BinnedSpectrum> bs_library_;
159 double bin_size_ = 1.0;
160 UInt peak_spread_ = 0;
161 double bin_offset_ = 0.4;
162 };
163
164 void getDefaultParameters(Param& params) const;
165
182 void annotateSpectra(
183 const std::vector<MSSpectrum>& spectra,
184 const TargetedExperiment& targeted_exp,
185 std::vector<MSSpectrum>& annotated_spectra,
186 FeatureMap& features,
187 bool compute_features = true
188 ) const;
189
204 void annotateSpectra(
205 const std::vector<MSSpectrum>& spectra,
206 const TargetedExperiment& targeted_exp,
207 std::vector<MSSpectrum>& annotated_spectra
208 ) const;
209
227 void pickSpectrum(const MSSpectrum& spectrum, MSSpectrum& picked_spectrum) const;
228
247 void scoreSpectra(
248 const std::vector<MSSpectrum>& annotated_spectra,
249 const std::vector<MSSpectrum>& picked_spectra,
250 FeatureMap& features,
251 std::vector<MSSpectrum>& scored_spectra,
252 bool compute_features = true
253 ) const;
254
268 void scoreSpectra(
269 const std::vector<MSSpectrum>& annotated_spectra,
270 const std::vector<MSSpectrum>& picked_spectra,
271 std::vector<MSSpectrum>& scored_spectra
272 ) const;
273
286 void selectSpectra(
287 const std::vector<MSSpectrum>& scored_spectra,
288 const FeatureMap& features,
289 std::vector<MSSpectrum>& selected_spectra,
290 FeatureMap& selected_features,
291 bool compute_features = true
292 ) const;
293
301 void selectSpectra(
302 const std::vector<MSSpectrum>& scored_spectra,
303 std::vector<MSSpectrum>& selected_spectra
304 ) const;
305
322 void extractSpectra(
323 const MSExperiment& experiment,
324 const TargetedExperiment& targeted_exp,
325 std::vector<MSSpectrum>& extracted_spectra,
326 FeatureMap& extracted_features,
327 bool compute_features = true
328 ) const;
329
343 void extractSpectra(
344 const MSExperiment& experiment,
345 const TargetedExperiment& targeted_exp,
346 std::vector<MSSpectrum>& extracted_spectra
347 ) const;
348
357 void matchSpectrum(
358 const MSSpectrum& input_spectrum,
359 const Comparator& cmp,
360 std::vector<Match>& matches
361 );
362
385 void targetedMatching(
386 const std::vector<MSSpectrum>& spectra,
387 const Comparator& cmp,
388 FeatureMap& features
389 );
390
412 void untargetedMatching(
413 const std::vector<MSSpectrum>& spectra,
414 const Comparator& cmp,
415 FeatureMap& features
416 );
417
418 protected:
420 void updateMembers_() override;
421
422 private:
426 bool mz_unit_is_Da_;
427
435 double rt_window_;
436
444 double mz_tolerance_;
445
450 double peak_height_min_;
451
456 double peak_height_max_;
457
462 double fwhm_threshold_;
463
464 double tic_weight_;
465 double fwhm_weight_;
466 double snr_weight_;
474 double min_select_score_;
475
481 bool use_gauss_;
482
487 Size top_matches_to_report_;
488
490 double min_match_score_;
491 };
492 }
OpenMS::TargetedSpectraExtractor::Comparator::getLibrary
const std::vector< MSSpectrum > & getLibrary() const
Definition: TargetedSpectraExtractor.h:103
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator
Definition: TargetedSpectraExtractor.h:112
OpenMS::DefaultParamHandler
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
OpenMS::TargetedSpectraExtractor::peak_height_max_
double peak_height_max_
Definition: TargetedSpectraExtractor.h:456
TargetedExperiment.h
OpenMS::MSExperiment
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:95
OpenMS::TargetedSpectraExtractor
This class filters, annotates, picks, and scores spectra (e.g., taken from a DDA experiment) based on...
Definition: TargetedSpectraExtractor.h:69
std
STL namespace.
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator::init
void init(const std::vector< MSSpectrum > &library, const std::map< String, DataValue > &options) override
Definition: TargetedSpectraExtractor.h:134
OpenMS::TargetedSpectraExtractor::Comparator
Definition: TargetedSpectraExtractor.h:88
OpenMS::TargetedSpectraExtractor::Match::Match
Match(MSSpectrum a, double b)
Definition: TargetedSpectraExtractor.h:83
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
FeatureMap.h
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator::cmp_bs_
BinnedSpectralContrastAngle cmp_bs_
Definition: TargetedSpectraExtractor.h:157
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator::bs_library_
std::vector< BinnedSpectrum > bs_library_
Definition: TargetedSpectraExtractor.h:158
OpenMS::TargetedSpectraExtractor::mz_unit_is_Da_
bool mz_unit_is_Da_
Definition: TargetedSpectraExtractor.h:426
OpenMS::TargetedSpectraExtractor::Match::spectrum
MSSpectrum spectrum
Definition: TargetedSpectraExtractor.h:84
OpenMS::BinnedSpectralContrastAngle
Compare functor scoring the spectral contrast angle for similarity measurement.
Definition: BinnedSpectralContrastAngle.h:57
OpenMS::TargetedSpectraExtractor::peak_height_min_
double peak_height_min_
Definition: TargetedSpectraExtractor.h:450
OpenMS::TargetedExperiment
A description of a targeted experiment containing precursor and production ions.
Definition: TargetedExperiment.h:64
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::TargetedSpectraExtractor::Match
Definition: TargetedSpectraExtractor.h:80
MSExperiment.h
OPENMS_LOG_INFO
#define OPENMS_LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:465
OpenMS::TargetedSpectraExtractor::min_select_score_
double min_select_score_
Definition: TargetedSpectraExtractor.h:474
DefaultParamHandler.h
BinnedSpectrum.h
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::Param
Management and storage of parameters / INI files.
Definition: Param.h:73
OpenMS::TargetedSpectraExtractor::top_matches_to_report_
Size top_matches_to_report_
Definition: TargetedSpectraExtractor.h:487
OpenMS::BinnedSpectrum
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:75
OpenMS::TargetedSpectraExtractor::fwhm_threshold_
double fwhm_threshold_
Definition: TargetedSpectraExtractor.h:462
OpenMS::TargetedSpectraExtractor::use_gauss_
bool use_gauss_
Definition: TargetedSpectraExtractor.h:481
OpenMS::TargetedSpectraExtractor::Comparator::library_
std::vector< MSSpectrum > library_
Definition: TargetedSpectraExtractor.h:109
OpenMS::TargetedSpectraExtractor::mz_tolerance_
double mz_tolerance_
Definition: TargetedSpectraExtractor.h:444
OpenMS::TargetedSpectraExtractor::min_match_score_
double min_match_score_
Minimum score for a match to be considered valid in `matchSpectrum()`.
Definition: TargetedSpectraExtractor.h:490
BinnedSpectralContrastAngle.h
OpenMS::TargetedSpectraExtractor::snr_weight_
double snr_weight_
Definition: TargetedSpectraExtractor.h:466
OpenMS::TargetedSpectraExtractor::fwhm_weight_
double fwhm_weight_
Definition: TargetedSpectraExtractor.h:465
OpenMS::TargetedSpectraExtractor::tic_weight_
double tic_weight_
Definition: TargetedSpectraExtractor.h:464
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator::generateScores
void generateScores(const MSSpectrum &spec, std::vector< std::pair< Size, double >> &scores, double min_score) const override
Definition: TargetedSpectraExtractor.h:116
OpenMS::TargetedSpectraExtractor::rt_window_
double rt_window_
Definition: TargetedSpectraExtractor.h:435