OpenMS
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-2023.
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 
37 #include <OpenMS/CONCEPT/Types.h>
42 
43 #include <map>
44 
45 namespace OpenMS
46 {
56  {
57 public:
59 
62  {}
63 
66  {}
67 
70  {}
71 
73 
75 
76 
85  template <typename ExperimentType>
86  void convertChromatogramsToSpectra(ExperimentType & exp)
87  {
88  for (std::vector<MSChromatogram >::const_iterator it = exp.getChromatograms().begin(); it != exp.getChromatograms().end(); ++it)
89  {
90  // for each peak add a new spectrum
91  for (typename ExperimentType::ChromatogramType::const_iterator pit = it->begin(); pit != it->end(); ++pit)
92  {
93  typename ExperimentType::SpectrumType spec;
94 
95  // add precursor and product peaks to spectrum settings
96  spec.getPrecursors().push_back(it->getPrecursor());
97  spec.getProducts().push_back(it->getProduct());
98  spec.setRT(pit->getRT());
99  spec.setMSLevel(2);
100  spec.setInstrumentSettings(it->getInstrumentSettings());
101  spec.setAcquisitionInfo(it->getAcquisitionInfo());
102  spec.setSourceFile(it->getSourceFile());
103 
104  // TODO implement others
106  {
107  spec.getInstrumentSettings().setScanMode(InstrumentSettings::SRM);
108  }
109  if (it->getChromatogramType() == ChromatogramSettings::SELECTED_ION_MONITORING_CHROMATOGRAM)
110  {
111  spec.getInstrumentSettings().setScanMode(InstrumentSettings::SIM);
112  }
113 
114  // new spec contains one peak, with product m/z and intensity
115  typename ExperimentType::PeakType peak;
116  peak.setMZ(it->getMZ());
117  peak.setIntensity(pit->getIntensity());
118  spec.push_back(peak);
119  exp.addSpectrum(spec);
120  }
121  }
122 
123  exp.setChromatograms(std::vector<MSChromatogram >());
124  }
125 
138  template <typename ExperimentType>
139  void convertSpectraToChromatograms(ExperimentType & exp, bool remove_spectra = false, bool force_conversion = false)
140  {
142  std::map<double, std::map<double, std::vector<SpectrumType> > > chroms;
143  std::map<double, MSChromatogram > chroms_xic;
144  for (typename ExperimentType::ConstIterator it = exp.begin(); it != exp.end(); ++it)
145  {
146  // TODO other types
147  if (it->getInstrumentSettings().getScanMode() == InstrumentSettings::SRM || force_conversion)
148  {
149  // exactly one precursor and one product ion
150  if (it->getPrecursors().size() == 1 && it->size() == 1)
151  {
152  chroms[it->getPrecursors().begin()->getMZ()][it->begin()->getMZ()].push_back(*it);
153  }
154  // Exactly one precursor and more than one product ion.
155  // This is how some converters (e.g. ReAdW 4.0.2) store SRM data,
156  // collecting all information from one precursor in a single
157  // pseudo-spectrum
158  else if (it->getPrecursors().size() == 1 && it->size() > 0)
159  {
160  for (Size peak_idx = 0; peak_idx < it->size(); peak_idx++)
161  {
162  // copy spectrum and delete all data, but keep metadata, then add single peak
163  SpectrumType dummy = *it;
164  dummy.clear(false);
165  dummy.push_back((*it)[peak_idx]);
166  chroms[it->getPrecursors().begin()->getMZ()][(*it)[peak_idx].getMZ()].push_back(dummy);
167  }
168  }
169  // We have no precursor, so this may be a MS1 chromatogram scan (as encountered in GC-MS)
170  else if (force_conversion)
171  {
172  for (auto& p : *it)
173  {
174  double mz = p.getMZ();
175  ChromatogramPeak chr_p;
176  chr_p.setRT(it->getRT());
177  chr_p.setIntensity(p.getIntensity());
178  if (chroms_xic.find(mz) == chroms_xic.end())
179  {
180  // new chromatogram
181  chroms_xic[mz].getPrecursor().setMZ(mz);
182  // chroms_xic[mz].setProduct(prod); // probably no product
183  chroms_xic[mz].setInstrumentSettings(it->getInstrumentSettings());
184  chroms_xic[mz].getPrecursor().setMetaValue("description", String("XIC @ " + String(mz)));
185  chroms_xic[mz].setAcquisitionInfo(it->getAcquisitionInfo());
186  chroms_xic[mz].setSourceFile(it->getSourceFile());
187  }
188  chroms_xic[mz].push_back(chr_p);
189  }
190  }
191  else
192  {
193  OPENMS_LOG_WARN << "ChromatogramTools: need exactly one precursor (given " << it->getPrecursors().size() <<
194  ") 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;
195  }
196  }
197  else
198  {
199  // This does not makes sense to warn here, because it would also warn on simple mass spectra...
200  // TODO think what to to here
201  //OPENMS_LOG_WARN << "ChromatogramTools: cannot convert other chromatogram spectra types than 'Selected Reaction Monitoring', skipping conversion." << std::endl;
202  //
203  }
204  }
205 
206  // Add the XIC chromatograms
207  for (auto & chrom: chroms_xic) exp.addChromatogram(chrom.second);
208 
209  // Add the SRM chromatograms
210  typename std::map<double, std::map<double, std::vector<SpectrumType> > >::const_iterator it1 = chroms.begin();
211  for (; it1 != chroms.end(); ++it1)
212  {
213  typename std::map<double, std::vector<SpectrumType> >::const_iterator it2 = it1->second.begin();
214  for (; it2 != it1->second.end(); ++it2)
215  {
216  typename ExperimentType::ChromatogramType chrom;
217  chrom.setPrecursor(*it2->second.begin()->getPrecursors().begin());
218  Product prod;
219  prod.setMZ(it2->first);
220  chrom.setProduct(prod);
221  chrom.setInstrumentSettings(it2->second.begin()->getInstrumentSettings());
222  chrom.setAcquisitionInfo(it2->second.begin()->getAcquisitionInfo());
223  chrom.setSourceFile(it2->second.begin()->getSourceFile());
224 
225  typename std::vector<SpectrumType>::const_iterator it3 = it2->second.begin();
226  for (; it3 != it2->second.end(); ++it3)
227  {
229  p.setRT(it3->getRT());
230  p.setIntensity(it3->begin()->getIntensity());
231  chrom.push_back(p);
232  }
233 
234  chrom.setNativeID("chromatogram=" + it2->second.begin()->getNativeID()); // TODO native id?
236  exp.addChromatogram(chrom);
237  }
238  }
239 
240  if (remove_spectra)
241  {
242  exp.getSpectra().erase(remove_if(exp.begin(), exp.end(), HasScanMode<SpectrumType>(InstrumentSettings::SRM)), exp.end());
243  }
244  }
245 
247  };
248 } // namespace OpenMS
249 
#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:470
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:56
void convertChromatogramsToSpectra(ExperimentType &exp)
converts the chromatogram to a list of spectra with instrument settings
Definition: ChromatogramTools.h:86
ChromatogramTools()
default constructor
Definition: ChromatogramTools.h:61
virtual ~ChromatogramTools()
destructor
Definition: ChromatogramTools.h:69
void convertSpectraToChromatograms(ExperimentType &exp, bool remove_spectra=false, bool force_conversion=false)
converts e.g. SRM spectra to chromatograms
Definition: ChromatogramTools.h:139
ChromatogramTools(const ChromatogramTools &)
copy constructor
Definition: ChromatogramTools.h:65
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:70
void clear(bool clear_meta_data)
Clears all data and meta data.
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:204
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:216
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:60
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:48
Peak2D PeakType
Definition: MassTrace.h:47