OpenMS
TargetedSpectraExtractor.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Douglas McCloskey, Pasquale Domenico Colaianni $
6 // $Authors: Douglas McCloskey, Pasquale Domenico Colaianni $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/config.h> // OPENMS_DLLAPI
18 
19 namespace OpenMS
20 {
43  class OPENMS_DLLAPI TargetedSpectraExtractor :
44  public DefaultParamHandler
45  {
46 public:
48  ~TargetedSpectraExtractor() override = default;
49 
54  struct Match
55  {
56  Match() = default;
57  Match(MSSpectrum a, double b) : spectrum(std::move(a)), score(b) {}
59  double score = 0.0;
60  };
61 
62  class OPENMS_DLLAPI Comparator
63  {
64  public:
65  virtual ~Comparator() = default;
66  virtual void generateScores(
67  const MSSpectrum& spec,
68  std::vector<std::pair<Size,double>>& scores,
69  double min_score
70  ) const = 0;
71 
72  virtual void init(
73  const std::vector<MSSpectrum>& library,
74  const std::map<String,DataValue>& options
75  ) = 0;
76 
77  const std::vector<MSSpectrum>& getLibrary() const
78  {
79  return library_;
80  }
81 
82  protected:
83  std::vector<MSSpectrum> library_;
84  };
85 
86  class OPENMS_DLLAPI BinnedSpectrumComparator : public Comparator
87  {
88  public:
89  ~BinnedSpectrumComparator() override = default;
91  const MSSpectrum& spec,
92  std::vector<std::pair<Size,double>>& scores,
93  double min_score
94  ) const override
95  {
96  scores.clear();
97  const BinnedSpectrum in_bs(spec, bin_size_, false, peak_spread_, bin_offset_);
98  for (Size i = 0; i < bs_library_.size(); ++i)
99  {
100  const double cmp_score = cmp_bs_(in_bs, bs_library_[i]);
101  if (cmp_score >= min_score)
102  {
103  scores.emplace_back(i, cmp_score);
104  }
105  }
106  }
107 
108  void init(const std::vector<MSSpectrum>& library, const std::map<String,DataValue>& options) override;
109 
110  private:
112  std::vector<BinnedSpectrum> bs_library_;
113  double bin_size_ = 0.02; // Default for nominal mass: 1.0;
114  UInt peak_spread_ = 0;
115  double bin_offset_ = 0.0; // Default for nominal mass resolution: 0.4;
116  };
117 
118  void getDefaultParameters(Param& params) const;
119 
137  const std::vector<MSSpectrum>& spectra,
138  const TargetedExperiment& targeted_exp,
139  std::vector<MSSpectrum>& annotated_spectra,
140  FeatureMap& features,
141  bool compute_features = true
142  ) const;
143 
159  const std::vector<MSSpectrum>& spectra,
160  const TargetedExperiment& targeted_exp,
161  std::vector<MSSpectrum>& annotated_spectra
162  ) const;
163 
177  const std::vector<MSSpectrum>& spectra,
178  const FeatureMap& ms1_features,
179  FeatureMap& ms2_features,
180  std::vector<MSSpectrum>& annotated_spectra) const;
181 
190  OpenMS::FeatureMap& feat_map,
191  OpenMS::FeatureMap& feat_map_output,
192  bool add_unidentified_features = false) const;
193 
211  void pickSpectrum(const MSSpectrum& spectrum, MSSpectrum& picked_spectrum) const;
212 
232  const std::vector<MSSpectrum>& annotated_spectra,
233  const std::vector<MSSpectrum>& picked_spectra,
234  FeatureMap& features,
235  std::vector<MSSpectrum>& scored_spectra,
236  bool compute_features = true
237  ) const;
238 
253  const std::vector<MSSpectrum>& annotated_spectra,
254  const std::vector<MSSpectrum>& picked_spectra,
255  std::vector<MSSpectrum>& scored_spectra
256  ) const;
257 
271  const std::vector<MSSpectrum>& scored_spectra,
272  const FeatureMap& features,
273  std::vector<MSSpectrum>& selected_spectra,
274  FeatureMap& selected_features,
275  bool compute_features = true
276  ) const;
277 
286  const std::vector<MSSpectrum>& scored_spectra,
287  std::vector<MSSpectrum>& selected_spectra
288  ) const;
289 
307  const MSExperiment& experiment,
308  const TargetedExperiment& targeted_exp,
309  std::vector<MSSpectrum>& extracted_spectra,
310  FeatureMap& extracted_features,
311  bool compute_features = true
312  ) const;
313 
328  const MSExperiment& experiment,
329  const TargetedExperiment& targeted_exp,
330  std::vector<MSSpectrum>& extracted_spectra
331  ) const;
332 
348  const MSExperiment& experiment,
349  const FeatureMap& ms1_features,
350  std::vector<MSSpectrum>& extracted_spectra
351  ) const;
352 
369  const MSExperiment& experiment,
370  const FeatureMap& ms1_features,
371  std::vector<MSSpectrum>& extracted_spectra,
372  FeatureMap& extracted_features
373  ) const;
374 
384  const MSSpectrum& input_spectrum,
385  const Comparator& cmp,
386  std::vector<Match>& matches
387  ) const;
388 
412  const std::vector<MSSpectrum>& spectra,
413  const Comparator& cmp,
414  FeatureMap& features
415  );
416 
439  const std::vector<MSSpectrum>& spectra,
440  const Comparator& cmp,
441  FeatureMap& features
442  );
443 
451  void constructTransitionsList(const OpenMS::FeatureMap& ms1_features, const OpenMS::FeatureMap& ms2_features, TargetedExperiment& t_exp) const;
452 
459  void storeSpectraMSP(const String& filename, MSExperiment& experiment) const;
460 
467  void mergeFeatures(const OpenMS::FeatureMap& fmap_input, OpenMS::FeatureMap& fmap_output) const;
468 
469 protected:
471  void updateMembers_() override;
472 
474  void deisotopeMS2Spectra_(MSExperiment& experiment) const;
475 
477  void removeMS2SpectraPeaks_(MSExperiment& experiment) const;
478 
480  void organizeMapWithSameIdentifier(const OpenMS::FeatureMap& fmap_input, std::map<OpenMS::String, std::vector<OpenMS::Feature>>& fmapmap) const;
481 
482  private:
500  const MSExperiment& experiment,
501  const FeatureMap& ms1_features,
502  std::vector<MSSpectrum>& extracted_spectra,
503  FeatureMap& extracted_features,
504  const bool compute_features
505  ) const;
506 
507  private:
512 
520  double rt_window_;
521 
530 
536 
542 
548 
549  double tic_weight_;
550  double fwhm_weight_;
551  double snr_weight_;
560 
567 
573 
576 
579 
581 
591 
593  };
594 }
Compare functor scoring the spectral contrast angle for similarity measurement.
Definition: BinnedSpectralContrastAngle.h:33
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:55
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:66
A container for features.
Definition: FeatureMap.h:80
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:46
The representation of a 1D spectrum.
Definition: MSSpectrum.h:44
Management and storage of parameters / INI files.
Definition: Param.h:44
A more convenient string class.
Definition: String.h:34
A description of a targeted experiment containing precursor and production ions.
Definition: TargetedExperiment.h:39
Definition: TargetedSpectraExtractor.h:87
std::vector< BinnedSpectrum > bs_library_
Definition: TargetedSpectraExtractor.h:112
void generateScores(const MSSpectrum &spec, std::vector< std::pair< Size, double >> &scores, double min_score) const override
Definition: TargetedSpectraExtractor.h:90
void init(const std::vector< MSSpectrum > &library, const std::map< String, DataValue > &options) override
BinnedSpectralContrastAngle cmp_bs_
Definition: TargetedSpectraExtractor.h:111
Definition: TargetedSpectraExtractor.h:63
std::vector< MSSpectrum > library_
Definition: TargetedSpectraExtractor.h:83
const std::vector< MSSpectrum > & getLibrary() const
Definition: TargetedSpectraExtractor.h:77
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:45
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:587
double snr_weight_
Definition: TargetedSpectraExtractor.h:551
double rt_window_
Definition: TargetedSpectraExtractor.h:520
void extractSpectra(const MSExperiment &experiment, const FeatureMap &ms1_features, std::vector< MSSpectrum > &extracted_spectra) const
Combines the functionalities given by all the other methods implemented in this class.
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:549
bool deisotoping_use_deisotoper_
Definition: TargetedSpectraExtractor.h:582
void constructTransitionsList(const OpenMS::FeatureMap &ms1_features, const OpenMS::FeatureMap &ms2_features, TargetedExperiment &t_exp) const
compute transitions list from MS1 and the associated MS2 features
double peak_height_max_
Definition: TargetedSpectraExtractor.h:541
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:511
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:572
double relative_allowable_product_mass_
Definition: TargetedSpectraExtractor.h:580
double peak_height_min_
Definition: TargetedSpectraExtractor.h:535
void extractSpectra(const MSExperiment &experiment, const FeatureMap &ms1_features, std::vector< MSSpectrum > &extracted_spectra, FeatureMap &extracted_features, const bool compute_features) const
Combines the functionalities given by all the other methods implemented in this class.
double fwhm_threshold_
Definition: TargetedSpectraExtractor.h:547
bool deisotoping_annotate_charge_
Definition: TargetedSpectraExtractor.h:590
double deisotoping_fragment_tolerance_
Definition: TargetedSpectraExtractor.h:583
bool use_gauss_
Definition: TargetedSpectraExtractor.h:566
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:578
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.
void matchSpectrum(const MSSpectrum &input_spectrum, const Comparator &cmp, std::vector< Match > &matches) const
Searches the spectral library for the top scoring candidates that match the input spectrum.
int deisotoping_min_charge_
Definition: TargetedSpectraExtractor.h:585
bool deisotoping_keep_only_deisotoped_
Definition: TargetedSpectraExtractor.h:589
double mz_tolerance_
Definition: TargetedSpectraExtractor.h:529
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.
double max_precursor_mass_threashold_
Definition: TargetedSpectraExtractor.h:592
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:586
void extractSpectra(const MSExperiment &experiment, const FeatureMap &ms1_features, std::vector< MSSpectrum > &extracted_spectra, FeatureMap &extracted_features) const
Combines the functionalities given by all the other methods implemented in this class.
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:575
double fwhm_weight_
Definition: TargetedSpectraExtractor.h:550
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 organizeMapWithSameIdentifier(const OpenMS::FeatureMap &fmap_input, std::map< OpenMS::String, std::vector< OpenMS::Feature >> &fmapmap) const
organize into a map by combining features and subordinates with the same `identifier`
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:577
int deisotoping_max_isopeaks_
Definition: TargetedSpectraExtractor.h:588
void storeSpectraMSP(const String &filename, MSExperiment &experiment) const
store spectra in MSP format
double min_select_score_
Definition: TargetedSpectraExtractor.h:559
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:584
void getDefaultParameters(Param &params) const
void searchSpectrum(OpenMS::FeatureMap &feat_map, OpenMS::FeatureMap &feat_map_output, bool add_unidentified_features=false) const
Search accurate masses and add identification (peptide hits) as features/sub-features.
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Definition: TargetedSpectraExtractor.h:55
Match(MSSpectrum a, double b)
Definition: TargetedSpectraExtractor.h:57
MSSpectrum spectrum
Definition: TargetedSpectraExtractor.h:58