OpenMS
FeatureFinderIdentificationAlgorithm.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Hendrik Weisser $
7 // --------------------------------------------------------------------------
8 
9 #ifndef OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEATUREFINDERIDENTIFICATIONALGORITHM_H
10 #define OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEATUREFINDERIDENTIFICATIONALGORITHM_H
11 
19 
20 #include <vector>
21 #include <fstream>
22 #include <map>
23 
24 namespace OpenMS
25 {
26  class IsotopeDistribution;
27 
28 
29 
31  public DefaultParamHandler
32 {
33 public:
36 
49  void run(
50  std::vector<PeptideIdentification> peptides,
51  const std::vector<ProteinIdentification>& proteins,
52  std::vector<PeptideIdentification> peptides_ext,
53  std::vector<ProteinIdentification> proteins_ext,
54  FeatureMap& features,
55  const FeatureMap& seeds = FeatureMap(),
56  const String& spectra_file = ""
57  );
58 
59  void runOnCandidates(FeatureMap& features);
60 
62  const PeakMap& getMSData() const;
63 
65  void setMSData(const PeakMap& ms_data); // for pyOpenMS
66  void setMSData(PeakMap&& ms_data); // moves peak data and saves the copy. Note that getMSData() will give back a processed/modified version.
67 
69  const PeakMap& getChromatograms() const;
70 
73 
76 
77 protected:
78 
81 
83  typedef std::multimap<double, PeptideIdentification*> RTMap;
85  typedef std::map<Int, std::pair<RTMap, RTMap> > ChargeMap;
87  typedef std::map<AASequence, ChargeMap> PeptideMap;
89  typedef std::map<String, std::pair<RTMap, RTMap> > PeptideRefRTMap;
90 
92 
95 
97  double rt_window_;
98  double mz_window_;
100 
102 
103  double isotope_pmin_;
105 
106  double rt_quantile_;
107 
108  double peak_width_;
111 
113 
114  // SVM related parameters
121 
122  // output file (before filtering)
124 
126 
127  void updateMembers_() override;
128 
130  struct RTRegion
131  {
132  double start, end;
134  };
135 
138  {
139  bool operator()(const Feature& feature)
140  {
141  return feature.getOverallQuality() == 0.0;
142  }
143  } feature_filter_quality_;
144 
147  {
148  bool operator()(const Feature& feature)
149  {
150  return feature.getPeptideIdentifications().empty();
151  }
152  } feature_filter_peptides_;
153 
156  {
158  const PeptideIdentification& p2)
159  {
160  const String& seq1 = p1.getHits()[0].getSequence().toString();
161  const String& seq2 = p2.getHits()[0].getSequence().toString();
162  if (seq1 == seq2)
163  {
164  Int charge1 = p1.getHits()[0].getCharge();
165  Int charge2 = p2.getHits()[0].getCharge();
166  if (charge1 == charge2)
167  {
168  return p1.getRT() < p2.getRT();
169  }
170  return charge1 < charge2;
171  }
172  return seq1 < seq2;
173  }
174  } peptide_compare_;
175 
178  {
179  bool operator()(const Feature& f1, const Feature& f2)
180  {
181  const String& ref1 = f1.getMetaValue("PeptideRef");
182  const String& ref2 = f2.getMetaValue("PeptideRef");
183  if (ref1 == ref2)
184  {
185  return f1.getRT() < f2.getRT();
186  }
187  return ref1 < ref2;
188  }
189  } feature_compare_;
190 
194 
196  double add_mass_offset_peptides_{0.0};
199  std::vector<PeptideIdentification> unassignedIDs_;
200 
201  const double seed_rt_window_ = 60.0;
202 
204  std::map<double, std::pair<Size, Size> > svm_probs_internal_;
206  std::multiset<double> svm_probs_external_;
211  std::map<String, double> isotope_probs_;
213 
215 
217  void generateTransitions_(const String& peptide_id, double mz, Int charge,
218  const IsotopeDistribution& iso_dist);
219 
220  void addPeptideRT_(TargetedExperiment::Peptide& peptide, double rt) const;
221 
223  void getRTRegions_(ChargeMap& peptide_data, std::vector<RTRegion>& rt_regions, bool clear_IDs = true) const;
224 
226  FeatureMap& features,
227  std::map<Size, std::vector<PeptideIdentification*> >& feat_ids,
228  RTMap& rt_internal);
229 
231  void annotateFeatures_(FeatureMap& features, PeptideRefRTMap& ref_rt_map);
232 
233  void ensureConvexHulls_(Feature& feature) const;
234 
235  void postProcess_(FeatureMap& features, bool with_external_ids);
236 
238  void statistics_(const FeatureMap& features) const;
239 
243  void createAssayLibrary_(const PeptideMap::iterator& begin, const PeptideMap::iterator& end, PeptideRefRTMap& ref_rt_map, bool clear_IDs = true);
244 
249  PeptideMap& peptide_map,
250  bool external = false);
251 
252  void checkNumObservations_(Size n_pos, Size n_neg, const String& note = "") const;
253 
254  void getUnbiasedSample_(const std::multimap<double, std::pair<Size, bool> >& valid_obs,
255  std::map<Size, double>& training_labels);
256 
257  void getRandomSample_(std::map<Size, double>& training_labels) const;
258 
259  void classifyFeatures_(FeatureMap& features);
260 
261  void filterFeaturesFinalizeAssay_(Feature& best_feature, double best_quality,
262  const double quality_cutoff);
263 
264  void filterFeatures_(FeatureMap& features, bool classified);
265 
266  void calculateFDR_(FeatureMap& features);
267 
268  // seeds for untargeted extraction
269  Size addSeeds_(std::vector<PeptideIdentification>& peptides, const FeatureMap& seeds);
270 
271  // quant. decoys
272  Size addOffsetPeptides_(std::vector<PeptideIdentification>& peptides, double offset);
273 
276  template <typename It>
277  std::vector<std::pair<It,It>>
278  chunk_(It range_from, It range_to, const std::ptrdiff_t batch_size)
279  {
280  /* Aliases, to make the rest of the code more readable. */
281  using std::vector;
282  using std::pair;
283  using std::make_pair;
284  using std::distance;
285  using diff_t = std::ptrdiff_t;
286 
287  /* Total item number and batch_size size. */
288  const diff_t total {distance(range_from, range_to)};
289  const diff_t num {total / batch_size};
290 
291  vector<pair<It,It>> chunks(num);
292 
293  It batch_end {range_from};
294 
295  /* Use the 'generate' algorithm to create batches. */
296  std::generate(begin(chunks), end(chunks), [&batch_end, batch_size]()
297  {
298  It batch_start {batch_end };
299 
300  std::advance(batch_end, batch_size);
301  return make_pair(batch_start, batch_end);
302  });
303 
304  /* The last batch_size's end must always be 'range_to'. */
305  if (chunks.empty())
306  {
307  chunks.emplace_back(range_from, range_to);
308  }
309  else
310  {
311  chunks.back().second = range_to;
312  }
313 
314  return chunks;
315  }
316 };
317 
318 } // namespace OpenMS
319 
320 #endif
321 
const std::vector< PeptideIdentification > & getPeptideIdentifications() const
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:66
Definition: FeatureFinderIdentificationAlgorithm.h:32
FeatureFinderAlgorithmPickedHelperStructs::MassTraces MassTraces
Definition: FeatureFinderIdentificationAlgorithm.h:80
void postProcess_(FeatureMap &features, bool with_external_ids)
double rt_window_
RT window width.
Definition: FeatureFinderIdentificationAlgorithm.h:97
void getRTRegions_(ChargeMap &peptide_data, std::vector< RTRegion > &rt_regions, bool clear_IDs=true) const
get regions in which peptide eludes (ideally only one) by clustering RT elution times
void runOnCandidates(FeatureMap &features)
std::map< AASequence, ChargeMap > PeptideMap
mapping: sequence -> charge -> internal/external ID information
Definition: FeatureFinderIdentificationAlgorithm.h:87
const TargetedExperiment & getLibrary() const
void run(std::vector< PeptideIdentification > peptides, const std::vector< ProteinIdentification > &proteins, std::vector< PeptideIdentification > peptides_ext, std::vector< ProteinIdentification > proteins_ext, FeatureMap &features, const FeatureMap &seeds=FeatureMap(), const String &spectra_file="")
void createAssayLibrary_(const PeptideMap::iterator &begin, const PeptideMap::iterator &end, PeptideRefRTMap &ref_rt_map, bool clear_IDs=true)
String svm_xval_out_
Definition: FeatureFinderIdentificationAlgorithm.h:117
String elution_model_
Definition: FeatureFinderIdentificationAlgorithm.h:112
PeptideMap peptide_map_
Definition: FeatureFinderIdentificationAlgorithm.h:91
void filterFeaturesFinalizeAssay_(Feature &best_feature, double best_quality, const double quality_cutoff)
double end
Definition: FeatureFinderIdentificationAlgorithm.h:132
MRMFeatureFinderScoring feat_finder_
OpenSWATH feature finder.
Definition: FeatureFinderIdentificationAlgorithm.h:212
FeatureFinderIdentificationAlgorithm()
default constructor
void filterFeatures_(FeatureMap &features, bool classified)
Size n_external_peps_
number of external peptides
Definition: FeatureFinderIdentificationAlgorithm.h:94
double signal_to_noise_
Definition: FeatureFinderIdentificationAlgorithm.h:110
void generateTransitions_(const String &peptide_id, double mz, Int charge, const IsotopeDistribution &iso_dist)
generate transitions (isotopic traces) for a peptide ion and add them to the library:
const ProgressLogger & getProgressLogger() const
TransformationDescription trafo_external_
TransformationDescription trafo_; // RT transformation (to range 0-1)
Definition: FeatureFinderIdentificationAlgorithm.h:210
bool quantify_decoys_
Definition: FeatureFinderIdentificationAlgorithm.h:195
TargetedExperiment library_
accumulated assays for peptides
Definition: FeatureFinderIdentificationAlgorithm.h:193
void annotateFeaturesFinalizeAssay_(FeatureMap &features, std::map< Size, std::vector< PeptideIdentification * > > &feat_ids, RTMap &rt_internal)
double min_peak_width_
Definition: FeatureFinderIdentificationAlgorithm.h:109
void calculateFDR_(FeatureMap &features)
void getRandomSample_(std::map< Size, double > &training_labels) const
StringList svm_predictor_names_
Definition: FeatureFinderIdentificationAlgorithm.h:116
std::vector< PeptideIdentification > unassignedIDs_
Definition: FeatureFinderIdentificationAlgorithm.h:199
void getUnbiasedSample_(const std::multimap< double, std::pair< Size, bool > > &valid_obs, std::map< Size, double > &training_labels)
void setMSData(const PeakMap &ms_data)
set the MS data used for feature detection
std::map< Int, std::pair< RTMap, RTMap > > ChargeMap
mapping: charge -> internal/external: (RT -> pointer to peptide)
Definition: FeatureFinderIdentificationAlgorithm.h:85
Size svm_n_parts_
number of partitions for SVM cross-validation
Definition: FeatureFinderIdentificationAlgorithm.h:119
Size svm_n_samples_
number of samples for SVM training
Definition: FeatureFinderIdentificationAlgorithm.h:120
Size n_internal_peps_
number of internal peptide
Definition: FeatureFinderIdentificationAlgorithm.h:93
Size addSeeds_(std::vector< PeptideIdentification > &peptides, const FeatureMap &seeds)
Size n_internal_features_
internal feature counter (for FDR calculation)
Definition: FeatureFinderIdentificationAlgorithm.h:207
ProgressLogger prog_log_
Definition: FeatureFinderIdentificationAlgorithm.h:214
PeakMap ms_data_
input LC-MS data
Definition: FeatureFinderIdentificationAlgorithm.h:191
void statistics_(const FeatureMap &features) const
some statistics on detected features
Size addOffsetPeptides_(std::vector< PeptideIdentification > &peptides, double offset)
Size batch_size_
nr of peptides to use at the same time during chromatogram extraction
Definition: FeatureFinderIdentificationAlgorithm.h:96
double mz_window_
m/z window width
Definition: FeatureFinderIdentificationAlgorithm.h:98
double svm_min_prob_
Definition: FeatureFinderIdentificationAlgorithm.h:115
std::map< String, double > isotope_probs_
isotope probabilities of transitions
Definition: FeatureFinderIdentificationAlgorithm.h:211
void checkNumObservations_(Size n_pos, Size n_neg, const String &note="") const
double peak_width_
Definition: FeatureFinderIdentificationAlgorithm.h:108
Size n_external_features_
Definition: FeatureFinderIdentificationAlgorithm.h:208
FeatureFinderAlgorithmPickedHelperStructs::MassTrace MassTrace
Definition: FeatureFinderIdentificationAlgorithm.h:79
Size n_isotopes_
number of isotopes for peptide assay
Definition: FeatureFinderIdentificationAlgorithm.h:104
double mapping_tolerance_
RT tolerance for mapping IDs to features.
Definition: FeatureFinderIdentificationAlgorithm.h:101
std::map< String, std::pair< RTMap, RTMap > > PeptideRefRTMap
mapping: peptide ref. -> int./ext.: (RT -> pointer to peptide)
Definition: FeatureFinderIdentificationAlgorithm.h:89
Size debug_level_
Definition: FeatureFinderIdentificationAlgorithm.h:125
double rt_quantile_
Definition: FeatureFinderIdentificationAlgorithm.h:106
std::vector< std::pair< It, It > > chunk_(It range_from, It range_to, const std::ptrdiff_t batch_size)
Definition: FeatureFinderIdentificationAlgorithm.h:278
std::multiset< double > svm_probs_external_
SVM probabilities for "external" features (for FDR calculation):
Definition: FeatureFinderIdentificationAlgorithm.h:206
ChargeMap ids
internal/external peptide IDs (per charge) in this region
Definition: FeatureFinderIdentificationAlgorithm.h:133
bool mz_window_ppm_
m/z window width is given in PPM (not Da)?
Definition: FeatureFinderIdentificationAlgorithm.h:99
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
bool use_psm_cutoff_
Definition: FeatureFinderIdentificationAlgorithm.h:197
void classifyFeatures_(FeatureMap &features)
void addPeptideRT_(TargetedExperiment::Peptide &peptide, double rt) const
PeakMap chrom_data_
accumulated chromatograms (XICs)
Definition: FeatureFinderIdentificationAlgorithm.h:192
void ensureConvexHulls_(Feature &feature) const
std::multimap< double, PeptideIdentification * > RTMap
mapping: RT (not necessarily unique) -> pointer to peptide
Definition: FeatureFinderIdentificationAlgorithm.h:83
void addPeptideToMap_(PeptideIdentification &peptide, PeptideMap &peptide_map, bool external=false)
double psm_score_cutoff_
Definition: FeatureFinderIdentificationAlgorithm.h:198
void annotateFeatures_(FeatureMap &features, PeptideRefRTMap &ref_rt_map)
annotate identified features with m/z, isotope probabilities, etc.
String candidates_out_
Definition: FeatureFinderIdentificationAlgorithm.h:123
double isotope_pmin_
min. isotope probability for peptide assay
Definition: FeatureFinderIdentificationAlgorithm.h:103
double svm_quality_cutoff
Definition: FeatureFinderIdentificationAlgorithm.h:118
std::map< double, std::pair< Size, Size > > svm_probs_internal_
SVM probability -> number of pos./neg. features (for FDR calculation):
Definition: FeatureFinderIdentificationAlgorithm.h:204
region in RT in which a peptide elutes:
Definition: FeatureFinderIdentificationAlgorithm.h:131
A container for features.
Definition: FeatureMap.h:80
An LC-MS feature.
Definition: Feature.h:46
QualityType getOverallQuality() const
Non-mutable access to the overall quality.
Definition: IsotopeDistribution.h:39
The MRMFeatureFinder finds and scores peaks of transitions that co-elute.
Definition: MRMFeatureFinderScoring.h:66
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:46
const DataValue & getMetaValue(const String &name) const
Returns the value corresponding to a string, or DataValue::EMPTY if not found.
CoordinateType getRT() const
Returns the RT coordinate (index 0)
Definition: Peak2D.h:184
Represents the peptide hits for a spectrum.
Definition: PeptideIdentification.h:39
double getRT() const
returns the RT of the MS2 spectrum where the identification occurred
const std::vector< PeptideHit > & getHits() const
returns the peptide hits as const
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:27
A more convenient string class.
Definition: String.h:34
Represents a peptide (amino acid sequence)
Definition: TargetedExperimentHelper.h:333
A description of a targeted experiment containing precursor and production ions.
Definition: TargetedExperiment.h:39
Generic description of a coordinate transformation.
Definition: TransformationDescription.h:37
int Int
Signed integer type.
Definition: Types.h:72
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:44
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Helper struct for mass traces used in FeatureFinderAlgorithmPicked.
Definition: FeatureFinderAlgorithmPickedHelperStructs.h:54
Helper struct for a collection of mass traces used in FeatureFinderAlgorithmPicked.
Definition: FeatureFinderAlgorithmPickedHelperStructs.h:85
comparison functor for features
Definition: FeatureFinderIdentificationAlgorithm.h:178
bool operator()(const Feature &f1, const Feature &f2)
Definition: FeatureFinderIdentificationAlgorithm.h:179
predicate for filtering features by assigned peptides:
Definition: FeatureFinderIdentificationAlgorithm.h:147
bool operator()(const Feature &feature)
Definition: FeatureFinderIdentificationAlgorithm.h:148
predicate for filtering features by overall quality:
Definition: FeatureFinderIdentificationAlgorithm.h:138
bool operator()(const Feature &feature)
Definition: FeatureFinderIdentificationAlgorithm.h:139
comparison functor for (unassigned) peptide IDs
Definition: FeatureFinderIdentificationAlgorithm.h:156
bool operator()(const PeptideIdentification &p1, const PeptideIdentification &p2)
Definition: FeatureFinderIdentificationAlgorithm.h:157