Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
ChromatogramExtractor.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-2015.
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: Hannes Roest $
32 // $Authors: Hannes Roest $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_ANALYSIS_OPENSWATH_CHROMATOGRAMEXTRACTOR_H
36 #define OPENMS_ANALYSIS_OPENSWATH_CHROMATOGRAMEXTRACTOR_H
37 
39 
43 
45 
46 namespace OpenMS
47 {
48 
67  class OPENMS_DLLAPI ChromatogramExtractor :
68  public ProgressLogger
69  {
70 
71 public:
72 
74 
88  template <typename ExperimentT>
89  void extractChromatograms(const ExperimentT& input, ExperimentT& output,
90  OpenMS::TargetedExperiment& transition_exp, double mz_extraction_window, bool ppm,
91  TransformationDescription trafo, double rt_extraction_window, String filter)
92  {
93  // invert the trafo because we want to transform nRT values to "real" RT values
94  trafo.invert();
95 
96  Size input_size = input.size();
97  if (input_size < 1)
98  {
99  return;
100  }
101 
102  int used_filter = getFilterNr_(filter);
103  populatePeptideRTMap_(transition_exp, rt_extraction_window);
104 
105  // sort the transition experiment by product mass
106  // this is essential because the algorithm assumes sorted transitions!
107  transition_exp.sortTransitionsByProductMZ();
108 
109  // prepare all the spectra (but leave them empty)
110  SpectrumSettings settings = input[0];
111  std::vector<typename ExperimentT::ChromatogramType> chromatograms;
112  prepareSpectra_(settings, chromatograms, transition_exp);
113 
114  //go through all spectra
115  startProgress(0, input_size, "Extracting chromatograms");
116  for (Size scan_idx = 0; scan_idx < input_size; ++scan_idx)
117  {
118  setProgress(scan_idx);
119 
120  if (input[scan_idx].size() == 0)
121  continue;
122 
123  Size peak_idx = 0;
124 
125  double mz;
126  double integrated_intensity = 0;
127 
128  // go through all transitions / chromatograms which are sorted by
129  // ProductMZ. We can use this to step through the spectrum and at the
130  // same time step through the transitions. We increase the peak counter
131  // until we hit the next transition and then extract the signal.
132  for (Size k = 0; k < chromatograms.size(); ++k)
133  {
134 
135  double current_rt = input[scan_idx].getRT();
136  if (outsideExtractionWindow_(transition_exp.getTransitions()[k], current_rt, trafo, rt_extraction_window))
137  {
138  continue;
139  }
140 
142  mz = transition_exp.getTransitions()[k].getProductMZ();
143 
144  if (used_filter == 1)
145  {
146  extract_value_tophat(input[scan_idx], mz, peak_idx, integrated_intensity, mz_extraction_window, ppm);
147  }
148  else if (used_filter == 2)
149  {
150  extract_value_bartlett(input[scan_idx], mz, peak_idx, integrated_intensity, mz_extraction_window, ppm);
151  }
152 
153 
154  p.setRT(current_rt);
155  p.setIntensity(integrated_intensity);
156  chromatograms[k].push_back(p);
157  }
158  }
159  endProgress();
160 
161  // add all the chromatograms to the output
162  output.setChromatograms(chromatograms);
163  }
164 
172  std::vector< OpenSwath::ChromatogramPtr >& output,
173  std::vector<ExtractionCoordinates> extraction_coordinates,
174  double mz_extraction_window, bool ppm, String filter)
175  {
177  extraction_coordinates, mz_extraction_window, ppm, filter);
178  }
179 
180 public:
181 
201  void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > & output_chromatograms,
202  std::vector< ExtractionCoordinates > & coordinates,
203  OpenMS::TargetedExperiment & transition_exp,
204  const double rt_extraction_window,
205  const bool ms1) const;
206 
220  template <typename TransitionExpT>
221  static void return_chromatogram(std::vector< OpenSwath::ChromatogramPtr > & chromatograms,
222  std::vector< ChromatogramExtractor::ExtractionCoordinates > & coordinates,
223  TransitionExpT& transition_exp_used, SpectrumSettings settings,
224  std::vector<OpenMS::MSChromatogram<> > & output_chromatograms, bool ms1)
225  {
226  typedef std::map<String, const typename TransitionExpT::Transition* > TransitionMapType;
227  TransitionMapType trans_map;
228  for (Size i = 0; i < transition_exp_used.getTransitions().size(); i++)
229  {
230  trans_map[transition_exp_used.getTransitions()[i].getNativeID()] = &transition_exp_used.getTransitions()[i];
231  }
232 
233  for (Size i = 0; i < chromatograms.size(); i++)
234  {
235  const OpenSwath::ChromatogramPtr & chromptr = chromatograms[i];
236  const ChromatogramExtractor::ExtractionCoordinates & coord = coordinates[i];
237 
238  typename TransitionExpT::Peptide pep;
239  typename TransitionExpT::Transition transition;
241 
242  // copy data
244  chrom.setNativeID(coord.id);
245 
246  // Create precursor and set
247  // 1) the target m/z
248  // 2) the isolation window (upper/lower)
249  // 3) the peptide sequence
250  Precursor prec;
251  if (ms1)
252  {
253  pep = transition_exp_used.getPeptideByRef(coord.id);
254  prec.setMZ(coord.mz);
256  }
257  else
258  {
259  transition = (*trans_map[coord.id]);
260  pep = transition_exp_used.getPeptideByRef(transition.getPeptideRef());
261 
262  prec.setMZ(transition.getPrecursorMZ());
263  if (settings.getPrecursors().size() > 0)
264  {
265  prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
266  prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
267  }
268 
269  // Create product and set its m/z
270  Product prod;
271  prod.setMZ(transition.getProductMZ());
272  chrom.setProduct(prod);
274  }
275  prec.setMetaValue("peptide_sequence", pep.sequence);
276  chrom.setPrecursor(prec);
277 
278  // Set the rest of the meta-data
280  chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
281  chrom.setSourceFile(settings.getSourceFile());
282 
283  for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
284  {
285  DataProcessing dp = settings.getDataProcessing()[j];
286  dp.setMetaValue("performed_on_spectra", "true");
287  chrom.getDataProcessing().push_back(dp);
288  }
289  output_chromatograms.push_back(chrom);
290  }
291  }
292 
294  template <typename SpectrumT>
295  void extract_value_tophat(const SpectrumT& input, const double& mz, Size& peak_idx,
296  double& integrated_intensity, const double& extract_window, const bool ppm)
297  {
298  integrated_intensity = 0;
299  if (input.size() == 0)
300  {
301  return;
302  }
303 
304  // calculate extraction window
305  double left, right;
306  if (ppm)
307  {
308  left = mz - mz * extract_window / 2.0 * 1.0e-6;
309  right = mz + mz * extract_window / 2.0 * 1.0e-6;
310  }
311  else
312  {
313  left = mz - extract_window / 2.0;
314  right = mz + extract_window / 2.0;
315  }
316 
317  Size walker;
318 
319  // advance the peak_idx until we hit the m/z value of the next transition
320  while (peak_idx < input.size() && input[peak_idx].getMZ() < mz)
321  {
322  peak_idx++;
323  }
324 
325  // walk right and left and add to our intensity
326  walker = peak_idx;
327  // if we moved past the end of the spectrum, we need to try the last peak of the spectrum (it could still be within the window)
328  if (peak_idx >= input.size())
329  {
330  walker = input.size() - 1;
331  }
332 
333  // add the current peak if it is between right and left
334  if (input[walker].getMZ() > left && input[walker].getMZ() < right)
335  {
336  integrated_intensity += input[walker].getIntensity();
337  }
338 
339  // walk to the left until we go outside the window, then start walking to the right until we are outside the window
340  walker = peak_idx;
341  if (walker > 0)
342  {
343  walker--;
344  }
345  while (walker > 0 && input[walker].getMZ() > left && input[walker].getMZ() < right)
346  {
347  integrated_intensity += input[walker].getIntensity(); walker--;
348  }
349  walker = peak_idx;
350  if (walker < input.size() )
351  {
352  walker++;
353  }
354  while (walker<input.size() && input[walker].getMZ()> left && input[walker].getMZ() < right)
355  {
356  integrated_intensity += input[walker].getIntensity(); walker++;
357  }
358  }
359 
361  template <typename SpectrumT>
362  void extract_value_bartlett(const SpectrumT& input, const double& mz, Size& peak_idx,
363  double& integrated_intensity, const double& extract_window, const bool ppm)
364  {
365  integrated_intensity = 0;
366  if (input.size() == 0)
367  {
368  return;
369  }
370 
371  // calculate extraction window
372  double left, right, half_window_size, weight;
373  if (ppm)
374  {
375  half_window_size = mz * extract_window / 2.0 * 1.0e-6;
376  left = mz - mz * extract_window / 2.0 * 1.0e-6;
377  right = mz + mz * extract_window / 2.0 * 1.0e-6;
378  }
379  else
380  {
381  half_window_size = extract_window / 2.0;
382  left = mz - extract_window / 2.0;
383  right = mz + extract_window / 2.0;
384  }
385 
386  Size walker;
387 
388  // advance the peak_idx until we hit the m/z value of the next transition
389  while (peak_idx < input.size() && input[peak_idx].getMZ() < mz)
390  {
391  peak_idx++;
392  }
393 
394  // walk right and left and add to our intensity
395  walker = peak_idx;
396  // if we moved past the end of the spectrum, we need to try the last peak of the spectrum (it could still be within the window)
397  if (peak_idx >= input.size())
398  {
399  walker = input.size() - 1;
400  }
401 
402  // add the current peak if it is between right and left
403  if (input[walker].getMZ() > left && input[walker].getMZ() < right)
404  {
405  weight = 1 - fabs(input[walker].getMZ() - mz) / half_window_size;
406  integrated_intensity += input[walker].getIntensity() * weight;
407  }
408 
409  // walk to the left until we go outside the window, then start walking to the right until we are outside the window
410  walker = peak_idx;
411  if (walker > 0 )
412  {
413  walker--;
414  }
415  while (walker > 0 && input[walker].getMZ() > left && input[walker].getMZ() < right)
416  {
417  weight = 1 - fabs(input[walker].getMZ() - mz) / half_window_size;
418  integrated_intensity += input[walker].getIntensity() * weight; walker--;
419  }
420  walker = peak_idx;
421  if (walker < input.size() )
422  {
423  walker++;
424  }
425  while (walker<input.size() && input[walker].getMZ()> left && input[walker].getMZ() < right)
426  {
427  weight = 1 - fabs(input[walker].getMZ() - mz) / half_window_size;
428  integrated_intensity += input[walker].getIntensity() * weight; walker++;
429  }
430  }
431 
432 private:
433 
448  template <class SpectrumSettingsT, class ChromatogramT>
449  void prepareSpectra_(SpectrumSettingsT& settings, std::vector<ChromatogramT>& chromatograms, OpenMS::TargetedExperiment& transition_exp)
450  {
451 
452  // first prepare all the spectra (but leave them empty)
453  for (Size i = 0; i < transition_exp.getTransitions().size(); i++)
454  {
455  const ReactionMonitoringTransition* transition = &transition_exp.getTransitions()[i];
456 
457  // 1) and 2) Extract precursor m/z and isolation window
458  ChromatogramT chrom;
459  Precursor prec;
460  prec.setMZ(transition->getPrecursorMZ());
461  if (settings.getPrecursors().size() > 0)
462  {
463  prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
464  prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
465  }
466 
467  // 3) set precursor peptide sequence
468  String pepref = transition->getPeptideRef();
469  for (Size pep_idx = 0; pep_idx < transition_exp.getPeptides().size(); pep_idx++)
470  {
471  const OpenMS::TargetedExperiment::Peptide* pep = &transition_exp.getPeptides()[pep_idx];
472  if (pep->id == pepref)
473  {
474  prec.setMetaValue("peptide_sequence", pep->sequence);
475  break;
476  }
477  }
478  // add precursor to spectrum
479  chrom.setPrecursor(prec);
480 
481  // 4) Create product and set its m/z
482  Product prod;
483  prod.setMZ(transition->getProductMZ());
484  chrom.setProduct(prod);
485 
486  // 5) Set the rest of the meta-data
487  chrom.setInstrumentSettings(settings.getInstrumentSettings());
488  chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
489  chrom.setSourceFile(settings.getSourceFile());
490 
491  for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
492  {
493  DataProcessing dp = settings.getDataProcessing()[j];
494  dp.setMetaValue("performed_on_spectra", "true");
495  chrom.getDataProcessing().push_back(dp);
496  }
497 
498  // Set the id of the chromatogram, using the id of the transition (this gives directly the mapping of the two)
499  chrom.setNativeID(transition->getNativeID());
501  chromatograms.push_back(chrom);
502  }
503 
504  }
505 
507  bool outsideExtractionWindow_(const ReactionMonitoringTransition& transition, double current_rt,
508  const TransformationDescription& trafo, double rt_extraction_window);
509 
511  int getFilterNr_(String filter);
512 
514  void populatePeptideRTMap_(OpenMS::TargetedExperiment& transition_exp, double rt_extraction_window);
515 
516  std::map<OpenMS::String, double> PeptideRTMap_;
517 
518  };
519 
520 }
521 
522 #endif
const double k
std::map< OpenMS::String, double > PeptideRTMap_
Definition: ChromatogramExtractor.h:516
Description of the applied preprocessing steps.
Definition: DataProcessing.h:51
const std::vector< DataProcessing > & getDataProcessing() const
returns a const reference to the description of the applied processing
void setMetaValue(const String &name, const DataValue &value)
sets the DataValue corresponding to a name
void extract_value_tophat(const SpectrumT &input, const double &mz, Size &peak_idx, double &integrated_intensity, const double &extract_window, const bool ppm)
Definition: ChromatogramExtractor.h:295
A more convenient string class.
Definition: String.h:57
Precursor meta information.
Definition: Precursor.h:56
The ChromatogramExtractorAlgorithm extracts chromatograms from a MS data.
Definition: ChromatogramExtractorAlgorithm.h:59
Product meta information.
Definition: Product.h:49
boost::shared_ptr< ISpectrumAccess > SpectrumAccessPtr
Definition: openswathalgo/include/OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:90
The representation of a chromatogram.
Definition: MSChromatogram.h:52
void setChromatogramType(ChromatogramType type)
sets the chromatogram type
void setProduct(const Product &product)
sets the products
void extractChromatograms(const ExperimentT &input, ExperimentT &output, OpenMS::TargetedExperiment &transition_exp, double mz_extraction_window, bool ppm, TransformationDescription trafo, double rt_extraction_window, String filter)
Extract chromatograms defined by the TargetedExperiment from the input map and write them to the outp...
Definition: ChromatogramExtractor.h:89
Peak2D PeakType
Definition: MassTrace.h:48
const AcquisitionInfo & getAcquisitionInfo() const
returns a const reference to the acquisition info
Representation of 1D spectrum settings.
Definition: SpectrumSettings.h:64
void setMZ(double mz)
sets the target m/z
void setSourceFile(const SourceFile &source_file)
sets the source file
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Definition: ChromatogramExtractorAlgorithm.h:65
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:120
Definition: ChromatogramSettings.h:72
const std::vector< DataProcessing > & getDataProcessing() const
returns a const reference to the description of the applied processing
void sortTransitionsByProductMZ()
Lexicographically sorts the transitions by their product m/z.
const String & getPeptideRef() const
void setInstrumentSettings(const InstrumentSettings &instrument_settings)
sets the instrument settings of the current spectrum
ChromatogramExtractorAlgorithm::ExtractionCoordinates ExtractionCoordinates
Definition: ChromatogramExtractor.h:73
void setIsolationWindowLowerOffset(double bound)
sets the lower offset from the target m/z
const SourceFile & getSourceFile() const
returns a const reference to the source file
void extractChromatograms(const OpenSwath::SpectrumAccessPtr input, std::vector< OpenSwath::ChromatogramPtr > &output, std::vector< ExtractionCoordinates > extraction_coordinates, double mz_extraction_window, bool ppm, String filter)
Extract chromatograms at the m/z and RT defined by the ExtractionCoordinates.
String id
Definition: TargetedExperimentHelper.h:321
void extractChromatograms(const OpenSwath::SpectrumAccessPtr input, std::vector< OpenSwath::ChromatogramPtr > &output, std::vector< ExtractionCoordinates > extraction_coordinates, double mz_extraction_window, bool ppm, String filter)
Extract chromatograms at the m/z and RT defined by the ExtractionCoordinates.
Definition: ChromatogramExtractor.h:171
const std::vector< ReactionMonitoringTransition > & getTransitions() const
returns the transition list
double mz
Definition: ChromatogramExtractorAlgorithm.h:67
void setNativeID(const String &native_id)
sets the native identifier for the spectrum, used by the acquisition software.
boost::shared_ptr< Chromatogram > ChromatogramPtr
Definition: openswathalgo/include/OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/DATAACCESS/DataStructures.h:154
void setIsolationWindowUpperOffset(double bound)
sets the upper offset from the target m/z
void setAcquisitionInfo(const AcquisitionInfo &acquisition_info)
sets the acquisition info
The ChromatogramExtractor extracts chromatograms from a spectra file.
Definition: ChromatogramExtractor.h:67
String sequence
Definition: TargetedExperimentHelper.h:324
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:449
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
This class stores an prediction of an SRM/MRM transition.
Definition: TargetedExperiment.h:53
static void return_chromatogram(std::vector< OpenSwath::ChromatogramPtr > &chromatograms, std::vector< ChromatogramExtractor::ExtractionCoordinates > &coordinates, TransitionExpT &transition_exp_used, SpectrumSettings settings, std::vector< OpenMS::MSChromatogram<> > &output_chromatograms, bool ms1)
This converts the ChromatogramPtr to MSChromatogram and adds meta-information.
Definition: ChromatogramExtractor.h:221
const String & getNativeID() const
const InstrumentSettings & getInstrumentSettings() const
returns a const reference to the instrument settings of the current spectrum
Generic description of a coordinate transformation.
Definition: TransformationDescription.h:58
void extract_value_bartlett(const SpectrumT &input, const double &mz, Size &peak_idx, double &integrated_intensity, const double &extract_window, const bool ppm)
Definition: ChromatogramExtractor.h:362
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
void setPrecursor(const Precursor &precursor)
sets the precursors
const std::vector< Peptide > & getPeptides() const
static void convertToOpenMSChromatogram(OpenMS::MSChromatogram<> &chromatogram, const OpenSwath::ChromatogramPtr cptr)
Convert a ChromatogramPtr to an OpenMS Chromatogram.
std::string id
rt end of extraction (in seconds)
Definition: ChromatogramExtractorAlgorithm.h:70
void invert()
Computes an (approximate) inverse of the transformation.
Definition: TargetedExperimentHelper.h:211
This class stores a SRM/MRM transition.
Definition: ReactionMonitoringTransition.h:55

OpenMS / TOPP release 2.0.0 Documentation generated on Wed Mar 30 2016 12:49:22 using doxygen 1.8.11