OpenMS  2.7.0
ChromatogramTools.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-2021.
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: Timo Sachsenberg $
32 // $Authors: Andreas Bertsch $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
38 #include <OpenMS/CONCEPT/Types.h>
43 
44 namespace OpenMS
45 {
55  {
56 public:
58 
61  {}
62 
65  {}
66 
69  {}
70 
72 
74 
75 
84  template <typename ExperimentType>
85  void convertChromatogramsToSpectra(ExperimentType & exp)
86  {
87  for (std::vector<MSChromatogram >::const_iterator it = exp.getChromatograms().begin(); it != exp.getChromatograms().end(); ++it)
88  {
89  // for each peak add a new spectrum
90  for (typename ExperimentType::ChromatogramType::const_iterator pit = it->begin(); pit != it->end(); ++pit)
91  {
92  typename ExperimentType::SpectrumType spec;
93 
94  // add precursor and product peaks to spectrum settings
95  spec.getPrecursors().push_back(it->getPrecursor());
96  spec.getProducts().push_back(it->getProduct());
97  spec.setRT(pit->getRT());
98  spec.setMSLevel(2);
99  spec.setInstrumentSettings(it->getInstrumentSettings());
100  spec.setAcquisitionInfo(it->getAcquisitionInfo());
101  spec.setSourceFile(it->getSourceFile());
102 
103  // TODO implement others
105  {
106  spec.getInstrumentSettings().setScanMode(InstrumentSettings::SRM);
107  }
108  if (it->getChromatogramType() == ChromatogramSettings::SELECTED_ION_MONITORING_CHROMATOGRAM)
109  {
110  spec.getInstrumentSettings().setScanMode(InstrumentSettings::SIM);
111  }
112 
113  // new spec contains one peak, with product m/z and intensity
114  typename ExperimentType::PeakType peak;
115  peak.setMZ(it->getMZ());
116  peak.setIntensity(pit->getIntensity());
117  spec.push_back(peak);
118  exp.addSpectrum(spec);
119  }
120  }
121 
122  exp.setChromatograms(std::vector<MSChromatogram >());
123  }
124 
136  template <typename ExperimentType>
137  void convertSpectraToChromatograms(ExperimentType & exp, bool remove_spectra = false, bool force_conversion = false)
138  {
141  Map<double, MSChromatogram > chroms_xic;
142  for (typename ExperimentType::ConstIterator it = exp.begin(); it != exp.end(); ++it)
143  {
144  // TODO other types
145  if (it->getInstrumentSettings().getScanMode() == InstrumentSettings::SRM || force_conversion)
146  {
147  // exactly one precursor and one product ion
148  if (it->getPrecursors().size() == 1 && it->size() == 1)
149  {
150  chroms[it->getPrecursors().begin()->getMZ()][it->begin()->getMZ()].push_back(*it);
151  }
152  // Exactly one precursor and more than one product ion.
153  // This is how some converters (e.g. ReAdW 4.0.2) store SRM data,
154  // collecting all information from one precursor in a single
155  // pseudo-spectrum
156  else if (it->getPrecursors().size() == 1 && it->size() > 0)
157  {
158  for (Size peak_idx = 0; peak_idx < it->size(); peak_idx++)
159  {
160  // copy spectrum and delete all data, but keep metadata, then add single peak
161  SpectrumType dummy = *it;
162  dummy.clear(false);
163  dummy.push_back((*it)[peak_idx]);
164  chroms[it->getPrecursors().begin()->getMZ()][(*it)[peak_idx].getMZ()].push_back(dummy);
165  }
166  }
167  // We have no precursor, so this may be a MS1 chromatogram scan (as encountered in GC-MS)
168  else if (force_conversion)
169  {
170  for (auto& p : *it)
171  {
172  double mz = p.getMZ();
173  ChromatogramPeak chr_p;
174  chr_p.setRT(it->getRT());
175  chr_p.setIntensity(p.getIntensity());
176  if (chroms_xic.find(mz) == chroms_xic.end())
177  {
178  // new chromatogram
179  chroms_xic[mz].getPrecursor().setMZ(mz);
180  // chroms_xic[mz].setProduct(prod); // probably no product
181  chroms_xic[mz].setInstrumentSettings(it->getInstrumentSettings());
182  chroms_xic[mz].getPrecursor().setMetaValue("description", String("XIC @ " + String(mz)));
183  chroms_xic[mz].setAcquisitionInfo(it->getAcquisitionInfo());
184  chroms_xic[mz].setSourceFile(it->getSourceFile());
185  }
186  chroms_xic[mz].push_back(chr_p);
187  }
188  }
189  else
190  {
191  OPENMS_LOG_WARN << "ChromatogramTools: need exactly one precursor (given " << it->getPrecursors().size() <<
192  ") and one or more product ions (" << it->size() << "), skipping conversion of this spectrum to chromatogram. If this is a MS1 chromatogram, please force conversion (e.g. with -convert_to_chromatograms)." << std::endl;
193  }
194  }
195  else
196  {
197  // This does not makes sense to warn here, because it would also warn on simple mass spectra...
198  // TODO think what to to here
199  //OPENMS_LOG_WARN << "ChromatogramTools: cannot convert other chromatogram spectra types than 'Selected Reaction Monitoring', skipping conversion." << std::endl;
200  //
201  }
202  }
203 
204  // Add the XIC chromatograms
205  for (auto & chrom: chroms_xic) exp.addChromatogram(chrom.second);
206 
207  // Add the SRM chromatograms
208  typename Map<double, Map<double, std::vector<SpectrumType> > >::const_iterator it1 = chroms.begin();
209  for (; it1 != chroms.end(); ++it1)
210  {
211  typename Map<double, std::vector<SpectrumType> >::const_iterator it2 = it1->second.begin();
212  for (; it2 != it1->second.end(); ++it2)
213  {
214  typename ExperimentType::ChromatogramType chrom;
215  chrom.setPrecursor(*it2->second.begin()->getPrecursors().begin());
216  Product prod;
217  prod.setMZ(it2->first);
218  chrom.setProduct(prod);
219  chrom.setInstrumentSettings(it2->second.begin()->getInstrumentSettings());
220  chrom.setAcquisitionInfo(it2->second.begin()->getAcquisitionInfo());
221  chrom.setSourceFile(it2->second.begin()->getSourceFile());
222 
223  typename std::vector<SpectrumType>::const_iterator it3 = it2->second.begin();
224  for (; it3 != it2->second.end(); ++it3)
225  {
227  p.setRT(it3->getRT());
228  p.setIntensity(it3->begin()->getIntensity());
229  chrom.push_back(p);
230  }
231 
232  chrom.setNativeID("chromatogram=" + it2->second.begin()->getNativeID()); // TODO native id?
234  exp.addChromatogram(chrom);
235  }
236  }
237 
238  if (remove_spectra)
239  {
240  exp.getSpectra().erase(remove_if(exp.begin(), exp.end(), HasScanMode<SpectrumType>(InstrumentSettings::SRM)), exp.end());
241  }
242  }
243 
245  };
246 } // namespace OpenMS
247 
#define OPENMS_LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged.
Definition: LogStream.h:460
A 1-dimensional raw data point or peak for chromatograms.
Definition: ChromatogramPeak.h:54
void setRT(CoordinateType rt)
Mutable access to RT.
Definition: ChromatogramPeak.h:121
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: ChromatogramPeak.h:112
void setPrecursor(const Precursor &precursor)
sets the precursors
@ SELECTED_REACTION_MONITORING_CHROMATOGRAM
Definition: ChromatogramSettings.h:74
@ SELECTED_ION_MONITORING_CHROMATOGRAM
Definition: ChromatogramSettings.h:73
Conversion class to convert chromatograms.
Definition: ChromatogramTools.h:55
void convertChromatogramsToSpectra(ExperimentType &exp)
converts the chromatogram to a list of spectra with instrument settings
Definition: ChromatogramTools.h:85
ChromatogramTools()
default constructor
Definition: ChromatogramTools.h:60
virtual ~ChromatogramTools()
destructor
Definition: ChromatogramTools.h:68
void convertSpectraToChromatograms(ExperimentType &exp, bool remove_spectra=false, bool force_conversion=false)
converts e.g. SRM spectra to chromatograms
Definition: ChromatogramTools.h:137
ChromatogramTools(const ChromatogramTools &)
copy constructor
Definition: ChromatogramTools.h:64
Predicate that determines if a spectrum has a certain scan mode.
Definition: RangeUtils.h:204
@ SRM
Selected reaction monitoring scan Synonyms: 'Multiple reaction monitoring scan',...
Definition: InstrumentSettings.h:59
@ SIM
Selected ion monitoring scan Synonyms: 'Multiple ion monitoring scan', 'SIM scan',...
Definition: InstrumentSettings.h:58
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
void clear(bool clear_meta_data)
Clears all data and meta data.
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:52
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:202
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:214
Product meta information.
Definition: Product.h:50
void setMZ(double mz)
sets the target m/z
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
A more convenient string class.
Definition: String.h:61
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
MSChromatogram ChromatogramType
Definition: MzDataHandler.h:61
MSSpectrum SpectrumType
Definition: MzDataHandler.h:60
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Peak2D PeakType
Definition: MassTrace.h:47