OpenMS
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-2023.
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 OPENMS_DLLAPI 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 OPENMS_DLLAPI BinnedSpectrumComparator : public Comparator
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  private:
138  std::vector<BinnedSpectrum> bs_library_;
139  double bin_size_ = 0.02; // Default for nominal mass: 1.0;
140  UInt peak_spread_ = 0;
141  double bin_offset_ = 0.0; // Default for nominal mass resolution: 0.4;
142  };
143 
144  void getDefaultParameters(Param& params) const;
145 
163  const std::vector<MSSpectrum>& spectra,
164  const TargetedExperiment& targeted_exp,
165  std::vector<MSSpectrum>& annotated_spectra,
166  FeatureMap& features,
167  bool compute_features = true
168  ) const;
169 
185  const std::vector<MSSpectrum>& spectra,
186  const TargetedExperiment& targeted_exp,
187  std::vector<MSSpectrum>& annotated_spectra
188  ) const;
189 
203  const std::vector<MSSpectrum>& spectra,
204  const FeatureMap& ms1_features,
205  FeatureMap& ms2_features,
206  std::vector<MSSpectrum>& annotated_spectra) const;
207 
216  OpenMS::FeatureMap& feat_map,
217  OpenMS::FeatureMap& feat_map_output,
218  bool add_unidentified_features = false) const;
219 
237  void pickSpectrum(const MSSpectrum& spectrum, MSSpectrum& picked_spectrum) const;
238 
258  const std::vector<MSSpectrum>& annotated_spectra,
259  const std::vector<MSSpectrum>& picked_spectra,
260  FeatureMap& features,
261  std::vector<MSSpectrum>& scored_spectra,
262  bool compute_features = true
263  ) const;
264 
279  const std::vector<MSSpectrum>& annotated_spectra,
280  const std::vector<MSSpectrum>& picked_spectra,
281  std::vector<MSSpectrum>& scored_spectra
282  ) const;
283 
297  const std::vector<MSSpectrum>& scored_spectra,
298  const FeatureMap& features,
299  std::vector<MSSpectrum>& selected_spectra,
300  FeatureMap& selected_features,
301  bool compute_features = true
302  ) const;
303 
312  const std::vector<MSSpectrum>& scored_spectra,
313  std::vector<MSSpectrum>& selected_spectra
314  ) const;
315 
333  const MSExperiment& experiment,
334  const TargetedExperiment& targeted_exp,
335  std::vector<MSSpectrum>& extracted_spectra,
336  FeatureMap& extracted_features,
337  bool compute_features = true
338  ) const;
339 
354  const MSExperiment& experiment,
355  const TargetedExperiment& targeted_exp,
356  std::vector<MSSpectrum>& extracted_spectra
357  ) const;
358 
374  const MSExperiment& experiment,
375  const FeatureMap& ms1_features,
376  std::vector<MSSpectrum>& extracted_spectra
377  ) const;
378 
395  const MSExperiment& experiment,
396  const FeatureMap& ms1_features,
397  std::vector<MSSpectrum>& extracted_spectra,
398  FeatureMap& extracted_features
399  ) const;
400 
410  const MSSpectrum& input_spectrum,
411  const Comparator& cmp,
412  std::vector<Match>& matches
413  ) const;
414 
438  const std::vector<MSSpectrum>& spectra,
439  const Comparator& cmp,
440  FeatureMap& features
441  );
442 
465  const std::vector<MSSpectrum>& spectra,
466  const Comparator& cmp,
467  FeatureMap& features
468  );
469 
477  void constructTransitionsList(const OpenMS::FeatureMap& ms1_features, const OpenMS::FeatureMap& ms2_features, TargetedExperiment& t_exp) const;
478 
485  void storeSpectraMSP(const String& filename, MSExperiment& experiment) const;
486 
493  void mergeFeatures(const OpenMS::FeatureMap& fmap_input, OpenMS::FeatureMap& fmap_output) const;
494 
495 protected:
497  void updateMembers_() override;
498 
500  void deisotopeMS2Spectra_(MSExperiment& experiment) const;
501 
503  void removeMS2SpectraPeaks_(MSExperiment& experiment) const;
504 
506  void organizeMapWithSameIdentifier(const OpenMS::FeatureMap& fmap_input, std::map<OpenMS::String, std::vector<OpenMS::Feature>>& fmapmap) const;
507 
508  private:
526  const MSExperiment& experiment,
527  const FeatureMap& ms1_features,
528  std::vector<MSSpectrum>& extracted_spectra,
529  FeatureMap& extracted_features,
530  const bool compute_features
531  ) const;
532 
533  private:
538 
546  double rt_window_;
547 
556 
562 
568 
574 
575  double tic_weight_;
576  double fwhm_weight_;
577  double snr_weight_;
586 
593 
599 
602 
605 
607 
617 
619  };
620 }
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:81
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
A container for features.
Definition: FeatureMap.h:106
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:72
The representation of a 1D spectrum.
Definition: MSSpectrum.h:70
Management and storage of parameters / INI files.
Definition: Param.h:70
A more convenient string class.
Definition: String.h:60
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:138
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
BinnedSpectralContrastAngle cmp_bs_
Definition: TargetedSpectraExtractor.h:137
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:613
double snr_weight_
Definition: TargetedSpectraExtractor.h:577
double rt_window_
Definition: TargetedSpectraExtractor.h:546
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:575
bool deisotoping_use_deisotoper_
Definition: TargetedSpectraExtractor.h:608
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:567
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:537
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:598
double relative_allowable_product_mass_
Definition: TargetedSpectraExtractor.h:606
double peak_height_min_
Definition: TargetedSpectraExtractor.h:561
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:573
bool deisotoping_annotate_charge_
Definition: TargetedSpectraExtractor.h:616
double deisotoping_fragment_tolerance_
Definition: TargetedSpectraExtractor.h:609
bool use_gauss_
Definition: TargetedSpectraExtractor.h:592
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:604
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:611
bool deisotoping_keep_only_deisotoped_
Definition: TargetedSpectraExtractor.h:615
double mz_tolerance_
Definition: TargetedSpectraExtractor.h:555
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:618
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:612
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:601
double fwhm_weight_
Definition: TargetedSpectraExtractor.h:576
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:603
int deisotoping_max_isopeaks_
Definition: TargetedSpectraExtractor.h:614
void storeSpectraMSP(const String &filename, MSExperiment &experiment) const
store spectra in MSP format
double min_select_score_
Definition: TargetedSpectraExtractor.h:585
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:610
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: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:48
Definition: TargetedSpectraExtractor.h:81
Match(MSSpectrum a, double b)
Definition: TargetedSpectraExtractor.h:83
MSSpectrum spectrum
Definition: TargetedSpectraExtractor.h:84