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-2016.
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  settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
286  chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
287  }
288  output_chromatograms.push_back(chrom);
289  }
290  }
291 
293  template <typename SpectrumT>
294  void extract_value_tophat(const SpectrumT& input, const double& mz, Size& peak_idx,
295  double& integrated_intensity, const double& extract_window, const bool ppm)
296  {
297  integrated_intensity = 0;
298  if (input.size() == 0)
299  {
300  return;
301  }
302 
303  // calculate extraction window
304  double left, right;
305  if (ppm)
306  {
307  left = mz - mz * extract_window / 2.0 * 1.0e-6;
308  right = mz + mz * extract_window / 2.0 * 1.0e-6;
309  }
310  else
311  {
312  left = mz - extract_window / 2.0;
313  right = mz + extract_window / 2.0;
314  }
315 
316  Size walker;
317 
318  // advance the peak_idx until we hit the m/z value of the next transition
319  while (peak_idx < input.size() && input[peak_idx].getMZ() < mz)
320  {
321  peak_idx++;
322  }
323 
324  // walk right and left and add to our intensity
325  walker = peak_idx;
326  // 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)
327  if (peak_idx >= input.size())
328  {
329  walker = input.size() - 1;
330  }
331 
332  // add the current peak if it is between right and left
333  if (input[walker].getMZ() > left && input[walker].getMZ() < right)
334  {
335  integrated_intensity += input[walker].getIntensity();
336  }
337 
338  // walk to the left until we go outside the window, then start walking to the right until we are outside the window
339  walker = peak_idx;
340  if (walker > 0)
341  {
342  walker--;
343  }
344  while (walker > 0 && input[walker].getMZ() > left && input[walker].getMZ() < right)
345  {
346  integrated_intensity += input[walker].getIntensity(); walker--;
347  }
348  walker = peak_idx;
349  if (walker < input.size() )
350  {
351  walker++;
352  }
353  while (walker<input.size() && input[walker].getMZ()> left && input[walker].getMZ() < right)
354  {
355  integrated_intensity += input[walker].getIntensity(); walker++;
356  }
357  }
358 
360  template <typename SpectrumT>
361  void extract_value_bartlett(const SpectrumT& input, const double& mz, Size& peak_idx,
362  double& integrated_intensity, const double& extract_window, const bool ppm)
363  {
364  integrated_intensity = 0;
365  if (input.size() == 0)
366  {
367  return;
368  }
369 
370  // calculate extraction window
371  double left, right, half_window_size, weight;
372  if (ppm)
373  {
374  half_window_size = mz * extract_window / 2.0 * 1.0e-6;
375  left = mz - mz * extract_window / 2.0 * 1.0e-6;
376  right = mz + mz * extract_window / 2.0 * 1.0e-6;
377  }
378  else
379  {
380  half_window_size = extract_window / 2.0;
381  left = mz - extract_window / 2.0;
382  right = mz + extract_window / 2.0;
383  }
384 
385  Size walker;
386 
387  // advance the peak_idx until we hit the m/z value of the next transition
388  while (peak_idx < input.size() && input[peak_idx].getMZ() < mz)
389  {
390  peak_idx++;
391  }
392 
393  // walk right and left and add to our intensity
394  walker = peak_idx;
395  // 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)
396  if (peak_idx >= input.size())
397  {
398  walker = input.size() - 1;
399  }
400 
401  // add the current peak if it is between right and left
402  if (input[walker].getMZ() > left && input[walker].getMZ() < right)
403  {
404  weight = 1 - fabs(input[walker].getMZ() - mz) / half_window_size;
405  integrated_intensity += input[walker].getIntensity() * weight;
406  }
407 
408  // walk to the left until we go outside the window, then start walking to the right until we are outside the window
409  walker = peak_idx;
410  if (walker > 0 )
411  {
412  walker--;
413  }
414  while (walker > 0 && input[walker].getMZ() > left && input[walker].getMZ() < right)
415  {
416  weight = 1 - fabs(input[walker].getMZ() - mz) / half_window_size;
417  integrated_intensity += input[walker].getIntensity() * weight; walker--;
418  }
419  walker = peak_idx;
420  if (walker < input.size() )
421  {
422  walker++;
423  }
424  while (walker<input.size() && input[walker].getMZ()> left && input[walker].getMZ() < right)
425  {
426  weight = 1 - fabs(input[walker].getMZ() - mz) / half_window_size;
427  integrated_intensity += input[walker].getIntensity() * weight; walker++;
428  }
429  }
430 
431 private:
432 
447  template <class SpectrumSettingsT, class ChromatogramT>
448  void prepareSpectra_(SpectrumSettingsT& settings, std::vector<ChromatogramT>& chromatograms, OpenMS::TargetedExperiment& transition_exp)
449  {
450 
451  // first prepare all the spectra (but leave them empty)
452  for (Size i = 0; i < transition_exp.getTransitions().size(); i++)
453  {
454  const ReactionMonitoringTransition* transition = &transition_exp.getTransitions()[i];
455 
456  // 1) and 2) Extract precursor m/z and isolation window
457  ChromatogramT chrom;
458  Precursor prec;
459  prec.setMZ(transition->getPrecursorMZ());
460  if (settings.getPrecursors().size() > 0)
461  {
462  prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
463  prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
464  }
465 
466  // 3) set precursor peptide sequence
467  String pepref = transition->getPeptideRef();
468  for (Size pep_idx = 0; pep_idx < transition_exp.getPeptides().size(); pep_idx++)
469  {
470  const OpenMS::TargetedExperiment::Peptide* pep = &transition_exp.getPeptides()[pep_idx];
471  if (pep->id == pepref)
472  {
473  prec.setMetaValue("peptide_sequence", pep->sequence);
474  break;
475  }
476  }
477  // add precursor to spectrum
478  chrom.setPrecursor(prec);
479 
480  // 4) Create product and set its m/z
481  Product prod;
482  prod.setMZ(transition->getProductMZ());
483  chrom.setProduct(prod);
484 
485  // 5) Set the rest of the meta-data
486  chrom.setInstrumentSettings(settings.getInstrumentSettings());
487  chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
488  chrom.setSourceFile(settings.getSourceFile());
489 
490  for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
491  {
492  settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
493  chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
494  }
495 
496  // Set the id of the chromatogram, using the id of the transition (this gives directly the mapping of the two)
497  chrom.setNativeID(transition->getNativeID());
499  chromatograms.push_back(chrom);
500  }
501 
502  }
503 
505  bool outsideExtractionWindow_(const ReactionMonitoringTransition& transition, double current_rt,
506  const TransformationDescription& trafo, double rt_extraction_window);
507 
509  int getFilterNr_(String filter);
510 
512  void populatePeptideRTMap_(OpenMS::TargetedExperiment& transition_exp, double rt_extraction_window);
513 
514  std::map<OpenMS::String, double> PeptideRTMap_;
515 
516  };
517 
518 }
519 
520 #endif
Definition: ChromatogramSettings.h:72
const double k
std::map< OpenMS::String, double > PeptideRTMap_
Definition: ChromatogramExtractor.h:514
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:294
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
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
std::vector< DataProcessingPtr > & getDataProcessing()
returns a mutable reference to the description of the applied processing
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
std::vector< DataProcessingPtr > & getDataProcessing()
returns a mutable reference to the description of the applied processing
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:448
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:361
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 Tue Jan 31 2017 06:39:42 using doxygen 1.8.8