OpenMS
ChromatogramExtractor.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: Hannes Roest $
6 // $Authors: Hannes Roest $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 
16 
19 
20 namespace OpenMS
21 {
22 
53  class OPENMS_DLLAPI ChromatogramExtractor :
54  public ProgressLogger
55  {
56 
57 public:
58 
60 
61 
79  std::vector< OpenSwath::ChromatogramPtr >& output,
80  const std::vector<ExtractionCoordinates>& extraction_coordinates,
81  double mz_extraction_window,
82  bool ppm,
83  const String& filter)
84  {
86  extraction_coordinates, mz_extraction_window, ppm, -1, filter);
87  }
88 
108  std::vector< OpenSwath::ChromatogramPtr >& output,
109  const std::vector<ExtractionCoordinates>& extraction_coordinates,
110  double mz_extraction_window,
111  bool ppm,
112  double im_extraction_window,
113  const String& filter)
114  {
116  extraction_coordinates, mz_extraction_window, ppm, im_extraction_window, filter);
117  }
118 
141  static void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > & output_chromatograms,
142  std::vector< ExtractionCoordinates > & coordinates,
143  const OpenMS::TargetedExperiment & transition_exp,
144  const double rt_extraction_window,
145  const bool ms1 = false,
146  const int ms1_isotopes = 0);
147 
148  static void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > & output_chromatograms,
149  std::vector< ExtractionCoordinates > & coordinates,
150  const OpenSwath::LightTargetedExperiment & transition_exp_used,
151  const double rt_extraction_window,
152  const bool ms1 = false,
153  const int ms1_isotopes = 0);
154 
169  template <typename TransitionExpT>
170  static void return_chromatogram(const std::vector< OpenSwath::ChromatogramPtr > & chromatograms,
171  const std::vector< ChromatogramExtractor::ExtractionCoordinates > & coordinates,
172  TransitionExpT& transition_exp_used,
173  SpectrumSettings settings,
174  std::vector<OpenMS::MSChromatogram > & output_chromatograms,
175  bool ms1,
176  double im_extraction_width = 0.0)
177  {
178  typedef std::map<String, const typename TransitionExpT::Transition* > TransitionMapType;
179  TransitionMapType trans_map;
180  for (Size i = 0; i < transition_exp_used.getTransitions().size(); i++)
181  {
182  trans_map[transition_exp_used.getTransitions()[i].getNativeID()] = &transition_exp_used.getTransitions()[i];
183  }
184 
185  for (Size i = 0; i < chromatograms.size(); i++)
186  {
187  const OpenSwath::ChromatogramPtr & chromptr = chromatograms[i];
188  const ChromatogramExtractor::ExtractionCoordinates & coord = coordinates[i];
189 
190  // copy data
193  chrom.setNativeID(coord.id);
194 
195  // Create precursor and set
196  // 1) the target m/z
197  // 2) the isolation window (upper/lower)
198  // 3) the peptide sequence
199  Precursor prec;
200  if (ms1)
201  {
202  prec.setMZ(coord.mz);
204 
205  // extract compound / peptide id from transition and store in
206  // more-or-less default field
207  String transition_group_id = OpenSwathHelper::computeTransitionGroupId(coord.id);
208  if (!transition_group_id.empty())
209  {
210  int prec_charge = 0;
211  String r = extract_id_(transition_exp_used, transition_group_id, prec_charge);
212  prec.setCharge(prec_charge);
213  prec.setMetaValue("peptide_sequence", r);
214  }
215  }
216  else
217  {
218  typename TransitionExpT::Transition transition = (*trans_map[coord.id]);
219 
220  prec.setMZ(transition.getPrecursorMZ());
221  if (!settings.getPrecursors().empty())
222  {
223  prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
224  prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
225  }
226 
227  // Create product and set its m/z
228  Product prod;
229  prod.setMZ(transition.getProductMZ());
230  chrom.setProduct(prod);
232 
233  // extract compound / peptide id from transition and store in
234  // more-or-less default field
235  if (!transition.getPeptideRef().empty())
236  {
237  int prec_charge = 0;
238  String r = extract_id_(transition_exp_used, transition.getPeptideRef(), prec_charge);
239  prec.setCharge(prec_charge);
240  prec.setMetaValue("peptide_sequence", r);
241  }
242  else
243  {
244  int prec_charge = 0;
245  String r = extract_id_(transition_exp_used, transition.getCompoundRef(), prec_charge);
246  prec.setCharge(prec_charge);
247  prec.setMetaValue("peptide_sequence", r);
248  }
249  }
250 
251  if (coord.ion_mobility >= 0 && im_extraction_width > 0.0)
252  {
253  prec.setDriftTime(coord.ion_mobility);
254  prec.setDriftTimeWindowLowerOffset(im_extraction_width / 2.0);
255  prec.setDriftTimeWindowUpperOffset(im_extraction_width / 2.0);
256  }
257  chrom.setPrecursor(prec);
258 
259  // Set the rest of the meta-data
261  chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
262  chrom.setSourceFile(settings.getSourceFile());
263 
264  for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
265  {
266  settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
267  chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
268  }
269  output_chromatograms.push_back(chrom);
270  }
271  }
272 
273 
274 private:
283  template <typename TransitionExpT>
284  static String extract_id_(TransitionExpT& transition_exp_used, const String& id, int& prec_charge);
285 
300  template <class SpectrumSettingsT, class ChromatogramT>
301  void prepareSpectra_(SpectrumSettingsT& settings,
302  std::vector<ChromatogramT>& chromatograms,
303  OpenMS::TargetedExperiment& transition_exp)
304  {
305  // first prepare all the spectra (but leave them empty)
306  for (Size i = 0; i < transition_exp.getTransitions().size(); i++)
307  {
308  const ReactionMonitoringTransition* transition = &transition_exp.getTransitions()[i];
309 
310  // 1) and 2) Extract precursor m/z and isolation window
311  ChromatogramT chrom;
312  Precursor prec;
313  prec.setMZ(transition->getPrecursorMZ());
314  if (settings.getPrecursors().size() > 0)
315  {
316  prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
317  prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
318  }
319 
320  // 3) set precursor peptide sequence / compound id in more-or-less default field
321  String pepref = transition->getPeptideRef();
322  for (Size pep_idx = 0; pep_idx < transition_exp.getPeptides().size(); pep_idx++)
323  {
324  const OpenMS::TargetedExperiment::Peptide* pep = &transition_exp.getPeptides()[pep_idx];
325  if (pep->id == pepref)
326  {
327  prec.setMetaValue("peptide_sequence", pep->sequence);
328  break;
329  }
330  }
331  String compref = transition->getCompoundRef();
332  for (Size comp_idx = 0; comp_idx < transition_exp.getCompounds().size(); comp_idx++)
333  {
334  const OpenMS::TargetedExperiment::Compound* comp = &transition_exp.getCompounds()[comp_idx];
335  if (comp->id == compref)
336  {
337  prec.setMetaValue("peptide_sequence", String(comp->id) );
338  break;
339  }
340  }
341 
342  // add precursor to spectrum
343  chrom.setPrecursor(prec);
344 
345  // 4) Create product and set its m/z
346  Product prod;
347  prod.setMZ(transition->getProductMZ());
348  chrom.setProduct(prod);
349 
350  // 5) Set the rest of the meta-data
351  chrom.setInstrumentSettings(settings.getInstrumentSettings());
352  chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
353  chrom.setSourceFile(settings.getSourceFile());
354 
355  for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
356  {
357  settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
358  chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
359  }
360 
361  // Set the id of the chromatogram, using the id of the transition (this gives directly the mapping of the two)
362  chrom.setNativeID(transition->getNativeID());
364  chromatograms.push_back(chrom);
365  }
366 
367  }
368 
371  double current_rt,
372  const TransformationDescription& trafo,
373  double rt_extraction_window);
374 
376  int getFilterNr_(const String& filter);
377 
380  double rt_extraction_window);
381 
382  std::map<OpenMS::String, double> PeptideRTMap_;
383 
384  };
385 
386  // Specialization for template (LightTargetedExperiment)
387  template<>
388  inline String ChromatogramExtractor::extract_id_<OpenSwath::LightTargetedExperiment>(OpenSwath::LightTargetedExperiment& transition_exp_used,
389  const String& id,
390  int & prec_charge)
391  {
392  const OpenSwath::LightCompound comp = transition_exp_used.getCompoundByRef(id);
393  prec_charge = comp.charge;
394  if (!comp.sequence.empty())
395  {
396  return comp.sequence;
397  }
398  else
399  {
400  return comp.compound_name;
401  }
402  }
403 
404 
405  // Specialization for template (TargetedExperiment)
406  template<>
407  inline String ChromatogramExtractor::extract_id_<OpenMS::TargetedExperiment>(OpenMS::TargetedExperiment& transition_exp_used,
408  const String& id,
409  int & prec_charge)
410  {
411  if (transition_exp_used.hasPeptide(id))
412  {
413  const TargetedExperiment::Peptide p = transition_exp_used.getPeptideByRef(id);
414  if (p.hasCharge()) {prec_charge = p.getChargeState();}
415  return p.sequence;
416  }
417  else if (transition_exp_used.hasCompound(id))
418  {
419  const TargetedExperiment::Compound c = transition_exp_used.getCompoundByRef(id);
420  if (c.hasCharge()) {prec_charge = c.getChargeState();}
421  return c.id;
422  }
423  else
424  {
425  return "";
426  }
427  }
428 }
429 
The ChromatogramExtractorAlgorithm extracts chromatograms from a MS data.
Definition: ChromatogramExtractorAlgorithm.h:34
void extractChromatograms(const OpenSwath::SpectrumAccessPtr &input, std::vector< OpenSwath::ChromatogramPtr > &output, const std::vector< ExtractionCoordinates > &extraction_coordinates, double mz_extraction_window, bool ppm, double im_extraction_window, const String &filter)
Extract chromatograms at the m/z and RT defined by the ExtractionCoordinates.
The ChromatogramExtractor extracts chromatograms (intensity vs retention time) from mass spectrometry...
Definition: ChromatogramExtractor.h:55
static void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > &output_chromatograms, std::vector< ExtractionCoordinates > &coordinates, const OpenMS::TargetedExperiment &transition_exp, const double rt_extraction_window, const bool ms1=false, const int ms1_isotopes=0)
Prepare the extraction coordinates from a TargetedExperiment.
ChromatogramExtractorAlgorithm::ExtractionCoordinates ExtractionCoordinates
Definition: ChromatogramExtractor.h:59
int getFilterNr_(const String &filter)
static void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > &output_chromatograms, std::vector< ExtractionCoordinates > &coordinates, const OpenSwath::LightTargetedExperiment &transition_exp_used, const double rt_extraction_window, const bool ms1=false, const int ms1_isotopes=0)
void extractChromatograms(const OpenSwath::SpectrumAccessPtr input, std::vector< OpenSwath::ChromatogramPtr > &output, const std::vector< ExtractionCoordinates > &extraction_coordinates, double mz_extraction_window, bool ppm, const String &filter)
Extract chromatograms at the m/z and RT defined by the ExtractionCoordinates.
Definition: ChromatogramExtractor.h:78
void extractChromatograms(const OpenSwath::SpectrumAccessPtr input, std::vector< OpenSwath::ChromatogramPtr > &output, const std::vector< ExtractionCoordinates > &extraction_coordinates, double mz_extraction_window, bool ppm, double im_extraction_window, const String &filter)
Extract chromatograms at the m/z and RT defined by the ExtractionCoordinates.
Definition: ChromatogramExtractor.h:107
void prepareSpectra_(SpectrumSettingsT &settings, std::vector< ChromatogramT > &chromatograms, OpenMS::TargetedExperiment &transition_exp)
This populates the chromatograms vector with empty chromatograms (but sets their meta-information)
Definition: ChromatogramExtractor.h:301
bool outsideExtractionWindow_(const ReactionMonitoringTransition &transition, double current_rt, const TransformationDescription &trafo, double rt_extraction_window)
static void return_chromatogram(const std::vector< OpenSwath::ChromatogramPtr > &chromatograms, const std::vector< ChromatogramExtractor::ExtractionCoordinates > &coordinates, TransitionExpT &transition_exp_used, SpectrumSettings settings, std::vector< OpenMS::MSChromatogram > &output_chromatograms, bool ms1, double im_extraction_width=0.0)
This converts the ChromatogramPtr to MSChromatogram and adds meta-information.
Definition: ChromatogramExtractor.h:170
std::map< OpenMS::String, double > PeptideRTMap_
Definition: ChromatogramExtractor.h:382
void populatePeptideRTMap_(OpenMS::TargetedExperiment &transition_exp, double rt_extraction_window)
static String extract_id_(TransitionExpT &transition_exp_used, const String &id, int &prec_charge)
Extracts id (peptide sequence or compound name) for a compound.
void setPrecursor(const Precursor &precursor)
sets the precursors
void setProduct(const Product &product)
sets the products
void setInstrumentSettings(const InstrumentSettings &instrument_settings)
sets the instrument settings of the current spectrum
@ BASEPEAK_CHROMATOGRAM
Definition: ChromatogramSettings.h:46
@ SELECTED_REACTION_MONITORING_CHROMATOGRAM
Definition: ChromatogramSettings.h:48
void setSourceFile(const SourceFile &source_file)
sets the source file
void setChromatogramType(ChromatogramType type)
sets the chromatogram type
std::vector< DataProcessingPtr > & getDataProcessing()
returns a mutable reference to the description of the applied processing
void setAcquisitionInfo(const AcquisitionInfo &acquisition_info)
sets the acquisition info
void setNativeID(const String &native_id)
sets the native identifier for the spectrum, used by the acquisition software.
The representation of a chromatogram.
Definition: MSChromatogram.h:30
void setMetaValue(const String &name, const DataValue &value)
Sets the DataValue corresponding to a name.
static void convertToOpenMSChromatogram(const OpenSwath::ChromatogramPtr &cptr, OpenMS::MSChromatogram &chromatogram)
Convert a ChromatogramPtr to an OpenMS Chromatogram.
static String computeTransitionGroupId(const String &precursor_id)
Compute transition group id.
Definition: OpenSwathHelper.h:56
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:93
Precursor meta information.
Definition: Precursor.h:35
void setIsolationWindowUpperOffset(double bound)
sets the upper offset from the target m/z
void setDriftTime(double drift_time)
sets the ion mobility drift time in milliseconds
void setDriftTimeWindowUpperOffset(double drift_time)
sets the upper offset from the target ion mobility
void setDriftTimeWindowLowerOffset(double drift_time)
sets the lower offset from the target ion mobility
void setIsolationWindowLowerOffset(double bound)
sets the lower offset from the target m/z
void setCharge(Int charge)
Mutable access to the charge.
Product meta information.
Definition: Product.h:24
void setMZ(double mz)
sets the target m/z
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:27
This class stores a SRM/MRM transition.
Definition: ReactionMonitoringTransition.h:32
double getPrecursorMZ() const
get the precursor mz (Q1 value)
const String & getCompoundRef() const
const String & getPeptideRef() const
const String & getNativeID() const
Representation of 1D spectrum settings.
Definition: SpectrumSettings.h:39
const SourceFile & getSourceFile() const
returns a const reference to the source file
const AcquisitionInfo & getAcquisitionInfo() const
returns a const reference to the acquisition info
const InstrumentSettings & getInstrumentSettings() const
returns a const reference to the instrument settings of the current spectrum
std::vector< DataProcessingPtr > & getDataProcessing()
returns a mutable reference to the description of the applied processing
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
A more convenient string class.
Definition: String.h:34
Represents a compound (small molecule)
Definition: TargetedExperimentHelper.h:296
String id
Definition: TargetedExperimentHelper.h:279
int getChargeState() const
Return the peptide or compound charge state.
Definition: TargetedExperimentHelper.h:216
bool hasCharge() const
Whether peptide or compound has set charge state.
Definition: TargetedExperimentHelper.h:210
Represents a peptide (amino acid sequence)
Definition: TargetedExperimentHelper.h:333
String sequence
Definition: TargetedExperimentHelper.h:393
A description of a targeted experiment containing precursor and production ions.
Definition: TargetedExperiment.h:39
const Peptide & getPeptideByRef(const String &ref) const
const std::vector< Peptide > & getPeptides() const
const Compound & getCompoundByRef(const String &ref) const
const std::vector< ReactionMonitoringTransition > & getTransitions() const
returns the transition list
bool hasCompound(const String &ref) const
bool hasPeptide(const String &ref) const
const std::vector< Compound > & getCompounds() const
Generic description of a coordinate transformation.
Definition: TransformationDescription.h:37
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
const double c
Definition: Constants.h:188
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
boost::shared_ptr< Chromatogram > ChromatogramPtr
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/DataStructures.h:146
boost::shared_ptr< ISpectrumAccess > SpectrumAccessPtr
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:131
Definition: ChromatogramExtractorAlgorithm.h:39
double mz
m/z value around which should be extracted
Definition: ChromatogramExtractorAlgorithm.h:40
double ion_mobility
ion mobility value around which should be extracted
Definition: ChromatogramExtractorAlgorithm.h:41
std::string id
identifier
Definition: ChromatogramExtractorAlgorithm.h:45
Definition: TransitionExperiment.h:127
std::string sequence
Definition: TransitionExperiment.h:138
std::string compound_name
Definition: TransitionExperiment.h:147
int charge
Definition: TransitionExperiment.h:137
Definition: TransitionExperiment.h:185
const LightCompound & getCompoundByRef(const std::string &ref)
Definition: TransitionExperiment.h:232