OpenMS  2.8.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 
137  template <typename ExperimentType>
138  void convertSpectraToChromatograms(ExperimentType & exp, bool remove_spectra = false, bool force_conversion = false)
139  {
142  Map<double, MSChromatogram > chroms_xic;
143  for (typename ExperimentType::ConstIterator it = exp.begin(); it != exp.end(); ++it)
144  {
145  // TODO other types
146  if (it->getInstrumentSettings().getScanMode() == InstrumentSettings::SRM || force_conversion)
147  {
148  // exactly one precursor and one product ion
149  if (it->getPrecursors().size() == 1 && it->size() == 1)
150  {
151  chroms[it->getPrecursors().begin()->getMZ()][it->begin()->getMZ()].push_back(*it);
152  }
153  // Exactly one precursor and more than one product ion.
154  // This is how some converters (e.g. ReAdW 4.0.2) store SRM data,
155  // collecting all information from one precursor in a single
156  // pseudo-spectrum
157  else if (it->getPrecursors().size() == 1 && it->size() > 0)
158  {
159  for (Size peak_idx = 0; peak_idx < it->size(); peak_idx++)
160  {
161  // copy spectrum and delete all data, but keep metadata, then add single peak
162  SpectrumType dummy = *it;
163  dummy.clear(false);
164  dummy.push_back((*it)[peak_idx]);
165  chroms[it->getPrecursors().begin()->getMZ()][(*it)[peak_idx].getMZ()].push_back(dummy);
166  }
167  }
168  // We have no precursor, so this may be a MS1 chromatogram scan (as encountered in GC-MS)
169  else if (force_conversion)
170  {
171  for (auto& p : *it)
172  {
173  double mz = p.getMZ();
174  ChromatogramPeak chr_p;
175  chr_p.setRT(it->getRT());
176  chr_p.setIntensity(p.getIntensity());
177  if (chroms_xic.find(mz) == chroms_xic.end())
178  {
179  // new chromatogram
180  chroms_xic[mz].getPrecursor().setMZ(mz);
181  // chroms_xic[mz].setProduct(prod); // probably no product
182  chroms_xic[mz].setInstrumentSettings(it->getInstrumentSettings());
183  chroms_xic[mz].getPrecursor().setMetaValue("description", String("XIC @ " + String(mz)));
184  chroms_xic[mz].setAcquisitionInfo(it->getAcquisitionInfo());
185  chroms_xic[mz].setSourceFile(it->getSourceFile());
186  }
187  chroms_xic[mz].push_back(chr_p);
188  }
189  }
190  else
191  {
192  OPENMS_LOG_WARN << "ChromatogramTools: need exactly one precursor (given " << it->getPrecursors().size() <<
193  ") 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;
194  }
195  }
196  else
197  {
198  // This does not makes sense to warn here, because it would also warn on simple mass spectra...
199  // TODO think what to to here
200  //OPENMS_LOG_WARN << "ChromatogramTools: cannot convert other chromatogram spectra types than 'Selected Reaction Monitoring', skipping conversion." << std::endl;
201  //
202  }
203  }
204 
205  // Add the XIC chromatograms
206  for (auto & chrom: chroms_xic) exp.addChromatogram(chrom.second);
207 
208  // Add the SRM chromatograms
209  typename Map<double, Map<double, std::vector<SpectrumType> > >::const_iterator it1 = chroms.begin();
210  for (; it1 != chroms.end(); ++it1)
211  {
212  typename Map<double, std::vector<SpectrumType> >::const_iterator it2 = it1->second.begin();
213  for (; it2 != it1->second.end(); ++it2)
214  {
215  typename ExperimentType::ChromatogramType chrom;
216  chrom.setPrecursor(*it2->second.begin()->getPrecursors().begin());
217  Product prod;
218  prod.setMZ(it2->first);
219  chrom.setProduct(prod);
220  chrom.setInstrumentSettings(it2->second.begin()->getInstrumentSettings());
221  chrom.setAcquisitionInfo(it2->second.begin()->getAcquisitionInfo());
222  chrom.setSourceFile(it2->second.begin()->getSourceFile());
223 
224  typename std::vector<SpectrumType>::const_iterator it3 = it2->second.begin();
225  for (; it3 != it2->second.end(); ++it3)
226  {
228  p.setRT(it3->getRT());
229  p.setIntensity(it3->begin()->getIntensity());
230  chrom.push_back(p);
231  }
232 
233  chrom.setNativeID("chromatogram=" + it2->second.begin()->getNativeID()); // TODO native id?
235  exp.addChromatogram(chrom);
236  }
237  }
238 
239  if (remove_spectra)
240  {
241  exp.getSpectra().erase(remove_if(exp.begin(), exp.end(), HasScanMode<SpectrumType>(InstrumentSettings::SRM)), exp.end());
242  }
243  }
244 
246  };
247 } // namespace OpenMS
248 
#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:138
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:70
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: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:47
Peak2D PeakType
Definition: MassTrace.h:47