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
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 
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:
427 
435  double rt_window_;
436 
445 
451 
457 
463 
464  double tic_weight_;
465  double fwhm_weight_;
466  double snr_weight_;
475 
482 
488 
491  };
492 }
DefaultParamHandler.h
OpenMS::TargetedSpectraExtractor::use_gauss_
bool use_gauss_
Definition: TargetedSpectraExtractor.h:481
OpenMS::TransitionTSVFile::validateTargetedExperiment
void validateTargetedExperiment(const OpenMS::TargetedExperiment &targeted_exp)
Validate a TargetedExperiment (check that all ids are unique)
OpenMS::TargetedSpectraExtractor::tic_weight_
double tic_weight_
Definition: TargetedSpectraExtractor.h:464
OpenMS::TransitionTSVFile
This class supports reading and writing of OpenSWATH transition lists.
Definition: TransitionTSVFile.h:144
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:144
OpenMS::TransitionPQPFile
This class supports reading and writing of PQP files.
Definition: TransitionPQPFile.h:219
TargetedExperiment.h
OpenMS::Param::copy
Param copy(const String &prefix, bool remove_prefix=false) const
Returns a new Param object containing all entries that start with prefix.
FileHandler.h
OpenMS::TargetedSpectraExtractor::peak_height_max_
double peak_height_max_
Definition: TargetedSpectraExtractor.h:456
FileTypes.h
OpenMS::TraMLFile::store
void store(const String &filename, const TargetedExperiment &id) const
Stores a map in a TraML file.
BinnedSpectralContrastAngle.h
OpenMS::TargetedSpectraExtractor::Match::Match
Match(MSSpectrum a, double b)
Definition: TargetedSpectraExtractor.h:83
TransitionTSVFile.h
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator::init
void init(const std::vector< MSSpectrum > &library, const std::map< String, DataValue > &options) override
Definition: TargetedSpectraExtractor.h:134
OpenMS::TargetedSpectraExtractor::mz_unit_is_Da_
bool mz_unit_is_Da_
Definition: TargetedSpectraExtractor.h:426
OpenMS::TargetedSpectraExtractor::fwhm_weight_
double fwhm_weight_
Definition: TargetedSpectraExtractor.h:465
OpenMS::BinnedSpectrum
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:75
OpenMS::FileHandler::getTypeByFileName
static FileTypes::Type getTypeByFileName(const String &filename)
Determines the file type from a file name.
OpenMS::TargetedSpectraExtractor::Match
Definition: TargetedSpectraExtractor.h:80
OpenMS::FileTypes::MRM
SpectraST MRM List.
Definition: FileTypes.h:102
OpenMS::String
A more convenient string class.
Definition: String.h:58
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::MSExperiment
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::TraMLFile::load
void load(const String &filename, TargetedExperiment &id)
Loads a map from a TraML file.
OpenMS::FileTypes::TSV
any TSV file, for example msInspect file or OpenSWATH transition file (see TransitionTSVFile)
Definition: FileTypes.h:87
OpenMS::TransitionTSVFile::convertTargetedExperimentToTSV
void convertTargetedExperimentToTSV(const char *filename, OpenMS::TargetedExperiment &targeted_exp)
Write out a targeted experiment (TraML structure) into a tsv file.
OpenMS::TargetedSpectraExtractor::min_match_score_
double min_match_score_
Minimum score for a match to be considered valid in `matchSpectrum()`.
Definition: TargetedSpectraExtractor.h:490
BinnedSpectrum.h
OpenMS::FileTypes::UNKNOWN
Unknown file extension.
Definition: FileTypes.h:60
OpenMS::TargetedSpectraExtractor
This class filters, annotates, picks, and scores spectra (e.g., taken from a DDA experiment) based on...
Definition: TargetedSpectraExtractor.h:69
ListUtils.h
OpenMS::DefaultParamHandler
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::TraMLFile
File adapter for HUPO PSI TraML files.
Definition: TraMLFile.h:63
OpenMS::TargetedSpectraExtractor::Comparator::library_
std::vector< MSSpectrum > library_
Definition: TargetedSpectraExtractor.h:109
Exception.h
OpenMS::TargetedSpectraExtractor::Comparator
Definition: TargetedSpectraExtractor.h:88
ProgressLogger.h
OpenMS::TargetedSpectraExtractor::Comparator::getLibrary
const std::vector< MSSpectrum > & getLibrary() const
Definition: TargetedSpectraExtractor.h:103
OpenMS::FileHandler
Facilitates file handling by file type recognition.
Definition: FileHandler.h:62
FeatureMap.h
OpenMS::FileTypes::Type
Type
Actual file types enum.
Definition: FileTypes.h:58
OpenMS::FileHandler::getType
static FileTypes::Type getType(const String &filename)
Tries to determine the file type (by name or content)
OpenMS::TargetedSpectraExtractor::mz_tolerance_
double mz_tolerance_
Definition: TargetedSpectraExtractor.h:444
OpenMS::DefaultParamHandler::setParameters
void setParameters(const Param &param)
Sets the parameters.
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator::bs_library_
std::vector< BinnedSpectrum > bs_library_
Definition: TargetedSpectraExtractor.h:158
OpenMS::DefaultParamHandler::getDefaults
const Param & getDefaults() const
Non-mutable access to the default parameters.
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator
Definition: TargetedSpectraExtractor.h:112
OpenMS::TransitionPQPFile::convertPQPToTargetedExperiment
void convertPQPToTargetedExperiment(const char *filename, OpenMS::TargetedExperiment &targeted_exp, bool legacy_traml_id=false)
Read in a PQP file and construct a targeted experiment (TraML structure)
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::TransitionTSVFile::convertTSVToTargetedExperiment
void convertTSVToTargetedExperiment(const char *filename, FileTypes::Type filetype, OpenMS::TargetedExperiment &targeted_exp)
Read in a tsv/mrm file and construct a targeted experiment (TraML structure)
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
MSExperiment.h
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:95
OpenMS::FileTypes::PQP
OpenSWATH Peptide Query Parameter (PQP) SQLite DB, see TransitionPQPFile.
Definition: FileTypes.h:104
OpenMS::TargetedSpectraExtractor::rt_window_
double rt_window_
Definition: TargetedSpectraExtractor.h:435
OpenMS::FileTypes::nameToType
static Type nameToType(const String &name)
Converts a file type name into a Type.
OpenMS::TransitionPQPFile::convertTargetedExperimentToPQP
void convertTargetedExperimentToPQP(const char *filename, OpenMS::TargetedExperiment &targeted_exp)
Write out a targeted experiment (TraML structure) into a PQP file.
OpenMS::FileTypes::TRAML
TraML (HUPO PSI format) for transitions (.traML)
Definition: FileTypes.h:81
OpenMS::TargetedSpectraExtractor::top_matches_to_report_
Size top_matches_to_report_
Definition: TargetedSpectraExtractor.h:487
OpenMS::Param
Management and storage of parameters / INI files.
Definition: Param.h:73
OpenMS::TargetedSpectraExtractor::peak_height_min_
double peak_height_min_
Definition: TargetedSpectraExtractor.h:450
OpenMS::TargetedSpectraExtractor::snr_weight_
double snr_weight_
Definition: TargetedSpectraExtractor.h:466
OpenMS::TargetedSpectraExtractor::Match::spectrum
MSSpectrum spectrum
Definition: TargetedSpectraExtractor.h:84
OpenMS::TargetedSpectraExtractor::fwhm_threshold_
double fwhm_threshold_
Definition: TargetedSpectraExtractor.h:462
OpenMS::TargetedExperiment
A description of a targeted experiment containing precursor and production ions.
Definition: TargetedExperiment.h:64
OPENMS_LOG_INFO
#define OPENMS_LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:465
TransitionPQPFile.h
OpenMS::BinnedSpectralContrastAngle
Compare functor scoring the spectral contrast angle for similarity measurement.
Definition: BinnedSpectralContrastAngle.h:57
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
TraMLFile.h
OpenMS::TargetedSpectraExtractor::min_select_score_
double min_select_score_
Definition: TargetedSpectraExtractor.h:474
OpenMS::TargetedSpectraExtractor::BinnedSpectrumComparator::cmp_bs_
BinnedSpectralContrastAngle cmp_bs_
Definition: TargetedSpectraExtractor.h:157
OpenMS::ProgressLogger::setLogType
void setLogType(LogType type) const
Sets the progress log that should be used. The default type is NONE!
TOPPBase.h
OpenMS::FileTypes::typeToName
static String typeToName(Type type)
Returns the name/extension of the type.