OpenMS  2.7.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-2021.
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
44 
45 namespace OpenMS
46 {
69  class OPENMS_DLLAPI TargetedSpectraExtractor :
70  public DefaultParamHandler
71  {
72 public:
74  ~TargetedSpectraExtractor() override = default;
75 
80  struct Match
81  {
82  Match() = default;
83  Match(MSSpectrum a, double b) : spectrum(std::move(a)), score(b) {}
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 
113  {
114  public:
115  ~BinnedSpectrumComparator() override = default;
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:
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 
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 
205  const std::vector<MSSpectrum>& spectra,
206  const TargetedExperiment& targeted_exp,
207  std::vector<MSSpectrum>& annotated_spectra
208  ) const;
209 
223  const std::vector<MSSpectrum>& spectra,
224  const FeatureMap& ms1_features,
225  FeatureMap& ms2_features,
226  std::vector<MSSpectrum>& annotated_spectra) const;
227 
235  OpenMS::FeatureMap& feat_map,
236  OpenMS::FeatureMap& feat_map_output) const;
237 
255  void pickSpectrum(const MSSpectrum& spectrum, MSSpectrum& picked_spectrum) const;
256 
276  const std::vector<MSSpectrum>& annotated_spectra,
277  const std::vector<MSSpectrum>& picked_spectra,
278  FeatureMap& features,
279  std::vector<MSSpectrum>& scored_spectra,
280  bool compute_features = true
281  ) const;
282 
297  const std::vector<MSSpectrum>& annotated_spectra,
298  const std::vector<MSSpectrum>& picked_spectra,
299  std::vector<MSSpectrum>& scored_spectra
300  ) const;
301 
315  const std::vector<MSSpectrum>& scored_spectra,
316  const FeatureMap& features,
317  std::vector<MSSpectrum>& selected_spectra,
318  FeatureMap& selected_features,
319  bool compute_features = true
320  ) const;
321 
330  const std::vector<MSSpectrum>& scored_spectra,
331  std::vector<MSSpectrum>& selected_spectra
332  ) const;
333 
351  const MSExperiment& experiment,
352  const TargetedExperiment& targeted_exp,
353  std::vector<MSSpectrum>& extracted_spectra,
354  FeatureMap& extracted_features,
355  bool compute_features = true
356  ) const;
357 
372  const MSExperiment& experiment,
373  const TargetedExperiment& targeted_exp,
374  std::vector<MSSpectrum>& extracted_spectra
375  ) const;
376 
386  const MSSpectrum& input_spectrum,
387  const Comparator& cmp,
388  std::vector<Match>& matches
389  );
390 
414  const std::vector<MSSpectrum>& spectra,
415  const Comparator& cmp,
416  FeatureMap& features
417  );
418 
441  const std::vector<MSSpectrum>& spectra,
442  const Comparator& cmp,
443  FeatureMap& features
444  );
445 
453  void storeSpectraTraML(const String& filename, const OpenMS::FeatureMap& ms1_features, const OpenMS::FeatureMap& ms2_features) const;
454 
461  void storeSpectraMSP(const String& filename, MSExperiment& experiment) const;
462 
469  void mergeFeatures(const OpenMS::FeatureMap& fmap_input, OpenMS::FeatureMap& fmap_output) const;
470 
471 protected:
473  void updateMembers_() override;
474 
476  void deisotopeMS2Spectra_(MSExperiment& experiment) const;
477 
479  void removeMS2SpectraPeaks_(MSExperiment& experiment) const;
480 
482  void organizeMapWithSameIdentifier(const OpenMS::FeatureMap& fmap_input, std::map<std::string, std::vector<OpenMS::Feature>>& fmapmap) const;
483 
484  private:
489 
497  double rt_window_;
498 
507 
513 
519 
525 
526  double tic_weight_;
527  double fwhm_weight_;
528  double snr_weight_;
537 
544 
550 
553 
556 
558 
568 
570  };
571 }
#define OPENMS_LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:465
Compare functor scoring the spectral contrast angle for similarity measurement.
Definition: BinnedSpectralContrastAngle.h:59
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:76
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:93
A container for features.
Definition: FeatureMap.h:105
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:80
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
Management and storage of parameters / INI files.
Definition: Param.h:70
A more convenient string class.
Definition: String.h:61
A description of a targeted experiment containing precursor and production ions.
Definition: TargetedExperiment.h:65
Definition: TargetedSpectraExtractor.h:113
std::vector< BinnedSpectrum > bs_library_
Definition: TargetedSpectraExtractor.h:158
void generateScores(const MSSpectrum &spec, std::vector< std::pair< Size, double >> &scores, double min_score) const override
Definition: TargetedSpectraExtractor.h:116
void init(const std::vector< MSSpectrum > &library, const std::map< String, DataValue > &options) override
Definition: TargetedSpectraExtractor.h:134
BinnedSpectralContrastAngle cmp_bs_
Definition: TargetedSpectraExtractor.h:157
Definition: TargetedSpectraExtractor.h:89
std::vector< MSSpectrum > library_
Definition: TargetedSpectraExtractor.h:109
const std::vector< MSSpectrum > & getLibrary() const
Definition: TargetedSpectraExtractor.h:103
virtual void generateScores(const MSSpectrum &spec, std::vector< std::pair< Size, double >> &scores, double min_score) const =0
virtual void init(const std::vector< MSSpectrum > &library, const std::map< String, DataValue > &options)=0
This class filters, annotates, picks, and scores spectra (e.g., taken from a DDA experiment) based on...
Definition: TargetedSpectraExtractor.h:71
void mergeFeatures(const OpenMS::FeatureMap &fmap_input, OpenMS::FeatureMap &fmap_output) const
organize into a map by combining features and subordinates with the same `identifier`
int deisotoping_min_isopeaks_
Definition: TargetedSpectraExtractor.h:564
double snr_weight_
Definition: TargetedSpectraExtractor.h:528
double rt_window_
Definition: TargetedSpectraExtractor.h:497
void removeMS2SpectraPeaks_(MSExperiment &experiment) const
Remove peaks form MS2 which are at a higher mz than the precursor + 10 ppm.
double tic_weight_
Definition: TargetedSpectraExtractor.h:526
bool deisotoping_use_deisotoper_
Definition: TargetedSpectraExtractor.h:559
double peak_height_max_
Definition: TargetedSpectraExtractor.h:518
void targetedMatching(const std::vector< MSSpectrum > &spectra, const Comparator &cmp, FeatureMap &features)
Compares a list of spectra against a spectral library and updates the related features.
bool mz_unit_is_Da_
Definition: TargetedSpectraExtractor.h:488
void annotateSpectra(const std::vector< MSSpectrum > &spectra, const FeatureMap &ms1_features, FeatureMap &ms2_features, std::vector< MSSpectrum > &annotated_spectra) const
Annotates the MS2 spectra with the likely MS1 feature that it was derived from.
Size top_matches_to_report_
Definition: TargetedSpectraExtractor.h:549
void matchSpectrum(const MSSpectrum &input_spectrum, const Comparator &cmp, std::vector< Match > &matches)
Searches the spectral library for the top scoring candidates that match the input spectrum.
double relative_allowable_product_mass_
Definition: TargetedSpectraExtractor.h:557
double peak_height_min_
Definition: TargetedSpectraExtractor.h:512
double fwhm_threshold_
Definition: TargetedSpectraExtractor.h:524
bool deisotoping_annotate_charge_
Definition: TargetedSpectraExtractor.h:567
double deisotoping_fragment_tolerance_
Definition: TargetedSpectraExtractor.h:560
bool use_gauss_
Definition: TargetedSpectraExtractor.h:543
void extractSpectra(const MSExperiment &experiment, const TargetedExperiment &targeted_exp, std::vector< MSSpectrum > &extracted_spectra) const
Combines the functionalities given by all the other methods implemented in this class.
~TargetedSpectraExtractor() override=default
void untargetedMatching(const std::vector< MSSpectrum > &spectra, const Comparator &cmp, FeatureMap &features)
Compares a list of spectra against a spectral library and creates a `FeatureMap` with the relevant in...
double max_fragment_mz_
Definition: TargetedSpectraExtractor.h:555
void scoreSpectra(const std::vector< MSSpectrum > &annotated_spectra, const std::vector< MSSpectrum > &picked_spectra, std::vector< MSSpectrum > &scored_spectra) const
Assigns a score to the spectra given an input and saves them in scored_spectra.
int deisotoping_min_charge_
Definition: TargetedSpectraExtractor.h:562
bool deisotoping_keep_only_deisotoped_
Definition: TargetedSpectraExtractor.h:566
double mz_tolerance_
Definition: TargetedSpectraExtractor.h:506
void pickSpectrum(const MSSpectrum &spectrum, MSSpectrum &picked_spectrum) const
Picks a spectrum's peaks and saves them in picked_spectrum.
void scoreSpectra(const std::vector< MSSpectrum > &annotated_spectra, const std::vector< MSSpectrum > &picked_spectra, FeatureMap &features, std::vector< MSSpectrum > &scored_spectra, bool compute_features=true) const
Assigns a score to the spectra given an input and saves them in scored_spectra.
void organizeMapWithSameIdentifier(const OpenMS::FeatureMap &fmap_input, std::map< std::string, std::vector< OpenMS::Feature >> &fmapmap) const
organize into a map by combining features and subordinates with the same `identifier`
void storeSpectraTraML(const String &filename, const OpenMS::FeatureMap &ms1_features, const OpenMS::FeatureMap &ms2_features) const
store MS1 and the associated MS2 features
double max_precursor_mass_threashold_
Definition: TargetedSpectraExtractor.h:569
void annotateSpectra(const std::vector< MSSpectrum > &spectra, const TargetedExperiment &targeted_exp, std::vector< MSSpectrum > &annotated_spectra) const
Filters and annotates those spectra that could potentially match the transitions of the target list.
int deisotoping_max_charge_
Definition: TargetedSpectraExtractor.h:563
void selectSpectra(const std::vector< MSSpectrum > &scored_spectra, const FeatureMap &features, std::vector< MSSpectrum > &selected_spectra, FeatureMap &selected_features, bool compute_features=true) const
The method selects the highest scoring spectrum for each possible annotation (i.e....
double min_match_score_
Minimum score for a match to be considered valid in `matchSpectrum()`.
Definition: TargetedSpectraExtractor.h:552
double fwhm_weight_
Definition: TargetedSpectraExtractor.h:527
void deisotopeMS2Spectra_(MSExperiment &experiment) const
Deisotope MS2 spectra.
void updateMembers_() override
Overridden function from DefaultParamHandler to keep members up to date, when a parameter is changed.
void selectSpectra(const std::vector< MSSpectrum > &scored_spectra, std::vector< MSSpectrum > &selected_spectra) const
The method selects the highest scoring spectrum for each possible annotation (i.e....
void annotateSpectra(const std::vector< MSSpectrum > &spectra, const TargetedExperiment &targeted_exp, std::vector< MSSpectrum > &annotated_spectra, FeatureMap &features, bool compute_features=true) const
Filters and annotates those spectra that could potentially match the transitions of the target list.
double min_fragment_mz_
Definition: TargetedSpectraExtractor.h:554
int deisotoping_max_isopeaks_
Definition: TargetedSpectraExtractor.h:565
void storeSpectraMSP(const String &filename, MSExperiment &experiment) const
store spectra in MSP format
double min_select_score_
Definition: TargetedSpectraExtractor.h:536
void extractSpectra(const MSExperiment &experiment, const TargetedExperiment &targeted_exp, std::vector< MSSpectrum > &extracted_spectra, FeatureMap &extracted_features, bool compute_features=true) const
Combines the functionalities given by all the other methods implemented in this class.
std::string deisotoping_fragment_unit_
Definition: TargetedSpectraExtractor.h:561
void getDefaultParameters(Param &params) const
void searchSpectrum(OpenMS::FeatureMap &feat_map, OpenMS::FeatureMap &feat_map_output) const
Search accurate masses and add identification (peptide hits) as features/sub-features.
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Definition: TargetedSpectraExtractor.h:81
Match(MSSpectrum a, double b)
Definition: TargetedSpectraExtractor.h:83
MSSpectrum spectrum
Definition: TargetedSpectraExtractor.h:84