OpenMS
Loading...
Searching...
No Matches
ChromatogramTools.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Andreas Bertsch $
7// --------------------------------------------------------------------------
8
9#pragma once
10
16
17#include <map>
18
19namespace OpenMS
20{
30 {
31public:
33
34
37
40 {}
41
44 {}
45
47
49
50
59 template <typename ExperimentType>
60 void convertChromatogramsToSpectra(ExperimentType & exp)
61 {
62 for (std::vector<MSChromatogram >::const_iterator it = exp.getChromatograms().begin(); it != exp.getChromatograms().end(); ++it)
63 {
64 // for each peak add a new spectrum
65 for (typename ExperimentType::ChromatogramType::const_iterator pit = it->begin(); pit != it->end(); ++pit)
66 {
67 typename ExperimentType::SpectrumType spec;
68
69 // add precursor and product peaks to spectrum settings
70 spec.getPrecursors().push_back(it->getPrecursor());
71 spec.getProducts().push_back(it->getProduct());
72 spec.setRT(pit->getRT());
73 spec.setMSLevel(2);
74 spec.setInstrumentSettings(it->getInstrumentSettings());
75 spec.setAcquisitionInfo(it->getAcquisitionInfo());
76 spec.setSourceFile(it->getSourceFile());
77
78 // TODO implement others
80 {
81 spec.getInstrumentSettings().setScanMode(InstrumentSettings::ScanMode::SRM);
82 }
84 {
85 spec.getInstrumentSettings().setScanMode(InstrumentSettings::ScanMode::SIM);
86 }
87
88 // new spec contains one peak, with product m/z and intensity
89 spec.emplace_back(it->getMZ(), pit->getIntensity());
90 exp.addSpectrum(spec);
91 }
92 }
93
94 exp.setChromatograms(std::vector<MSChromatogram >());
95 }
96
109 template <typename ExperimentType>
110 void convertSpectraToChromatograms(ExperimentType & exp, bool remove_spectra = false, bool force_conversion = false)
111 {
112 typedef typename ExperimentType::SpectrumType SpectrumType;
113 std::map<double, std::map<double, std::vector<SpectrumType> > > chroms;
114 std::map<double, MSChromatogram > chroms_xic;
115 for (typename ExperimentType::ConstIterator it = exp.begin(); it != exp.end(); ++it)
116 {
117 // TODO other types
118 if (it->getInstrumentSettings().getScanMode() == InstrumentSettings::ScanMode::SRM || force_conversion)
119 {
120 // exactly one precursor and one product ion
121 if (it->getPrecursors().size() == 1 && it->size() == 1)
122 {
123 chroms[it->getPrecursors().begin()->getMZ()][it->begin()->getMZ()].push_back(*it);
124 }
125 // Exactly one precursor and more than one product ion.
126 // This is how some converters (e.g. ReAdW 4.0.2) store SRM data,
127 // collecting all information from one precursor in a single
128 // pseudo-spectrum
129 else if (it->getPrecursors().size() == 1 && it->size() > 0)
130 {
131 for (Size peak_idx = 0; peak_idx < it->size(); peak_idx++)
132 {
133 // copy spectrum and delete all data, but keep metadata, then add single peak
134 SpectrumType dummy = *it;
135 dummy.clear(false);
136 dummy.push_back((*it)[peak_idx]);
137 // the chromatograms of one spectrum would share its native ID otherwise (mzML requires unique ids)
138 dummy.setNativeID(it->getNativeID() + " product=" + StringUtils::toStr((*it)[peak_idx].getMZ()));
139 chroms[it->getPrecursors().begin()->getMZ()][(*it)[peak_idx].getMZ()].push_back(dummy);
140 }
141 }
142 // We have no precursor, so this may be a MS1 chromatogram scan (as encountered in GC-MS)
143 else if (force_conversion)
144 {
145 for (auto& p : *it)
146 {
147 double mz = p.getMZ();
148 ChromatogramPeak chr_p;
149 chr_p.setRT(it->getRT());
150 chr_p.setIntensity(p.getIntensity());
151 if (!chroms_xic.contains(mz))
152 {
153 // new chromatogram; the native ID must be unique (mzML requires it) and the m/z is the key here
154 chroms_xic[mz].setNativeID("XIC mz=" + StringUtils::toStr(mz));
155 chroms_xic[mz].getPrecursor().setMZ(mz);
156 // chroms_xic[mz].setProduct(prod); // probably no product
157 chroms_xic[mz].setInstrumentSettings(it->getInstrumentSettings());
158 chroms_xic[mz].getPrecursor().setMetaValue("description",std::string("XIC @ " + StringUtils::toStr(mz)));
159 chroms_xic[mz].setAcquisitionInfo(it->getAcquisitionInfo());
160 chroms_xic[mz].setSourceFile(it->getSourceFile());
161 }
162 chroms_xic[mz].push_back(chr_p);
163 }
164 }
165 else
166 {
167 OPENMS_LOG_WARN << "ChromatogramTools: need exactly one precursor (given " << it->getPrecursors().size() <<
168 ") 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;
169 }
170 }
171 else
172 {
173 // This does not makes sense to warn here, because it would also warn on simple mass spectra...
174 // TODO think what to to here
175 //OPENMS_LOG_WARN << "ChromatogramTools: cannot convert other chromatogram spectra types than 'Selected Reaction Monitoring', skipping conversion." << std::endl;
176 //
177 }
178 }
179
180 // Add the XIC chromatograms
181 for (auto & chrom: chroms_xic) exp.addChromatogram(chrom.second);
182
183 // Add the SRM chromatograms
184 typename std::map<double, std::map<double, std::vector<SpectrumType> > >::const_iterator it1 = chroms.begin();
185 for (; it1 != chroms.end(); ++it1)
186 {
187 typename std::map<double, std::vector<SpectrumType> >::const_iterator it2 = it1->second.begin();
188 for (; it2 != it1->second.end(); ++it2)
189 {
190 typename ExperimentType::ChromatogramType chrom;
191 chrom.setPrecursor(*it2->second.begin()->getPrecursors().begin());
192 Product prod;
193 prod.setMZ(it2->first);
194 chrom.setProduct(prod);
195 chrom.setInstrumentSettings(it2->second.begin()->getInstrumentSettings());
196 chrom.setAcquisitionInfo(it2->second.begin()->getAcquisitionInfo());
197 chrom.setSourceFile(it2->second.begin()->getSourceFile());
198
199 typename std::vector<SpectrumType>::const_iterator it3 = it2->second.begin();
200 for (; it3 != it2->second.end(); ++it3)
201 {
202 typename ExperimentType::ChromatogramType::PeakType p;
203 p.setRT(it3->getRT());
204 p.setIntensity(it3->begin()->getIntensity());
205 chrom.push_back(p);
206 }
207
208 chrom.setNativeID("chromatogram=" + it2->second.begin()->getNativeID()); // TODO native id?
210 exp.addChromatogram(chrom);
211 }
212 }
213
214 if (remove_spectra)
215 {
216 exp.getSpectra().erase(remove_if(exp.begin(), exp.end(), HasScanMode<SpectrumType>(static_cast<Int>(InstrumentSettings::ScanMode::SRM))), exp.end());
217 }
218 }
219
221 };
222} // namespace OpenMS
223
#define OPENMS_LOG_WARN
Macro for warnings.
Definition LogStream.h:691
A 1-dimensional raw data point or peak for chromatograms.
Definition ChromatogramPeak.h:29
void setRT(CoordinateType rt)
Mutable access to RT.
Definition ChromatogramPeak.h:93
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition ChromatogramPeak.h:84
Conversion class to convert chromatograms.
Definition ChromatogramTools.h:30
void convertChromatogramsToSpectra(ExperimentType &exp)
converts the chromatogram to a list of spectra with instrument settings
Definition ChromatogramTools.h:60
ChromatogramTools()
default constructor
Definition ChromatogramTools.h:35
virtual ~ChromatogramTools()
destructor
Definition ChromatogramTools.h:43
void convertSpectraToChromatograms(ExperimentType &exp, bool remove_spectra=false, bool force_conversion=false)
converts e.g. SRM spectra to chromatograms
Definition ChromatogramTools.h:110
ChromatogramTools(const ChromatogramTools &)
copy constructor
Definition ChromatogramTools.h:39
Predicate that determines if a spectrum has a certain scan mode.
Definition RangeUtils.h:179
@ SIM
Selected ion monitoring scan Synonyms: 'Multiple ion monitoring scan', 'SIM scan',...
@ SRM
Selected reaction monitoring scan Synonyms: 'Multiple reaction monitoring scan',...
The representation of a 1D spectrum.
Definition MSSpectrum.h:45
void clear(bool clear_meta_data)
Clears all data and meta data.
Product meta information.
Definition Product.h:26
void setMZ(double mz)
sets the target m/z
void setNativeID(const std::string &native_id)
sets the native identifier for the spectrum, used by the acquisition software.
int Int
Signed integer type.
Definition Types.h:72
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
MSSpectrum SpectrumType
Definition MzDataHandler.h:33
std::string toStr(int i)
Definition StringUtils.h:100
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19