OpenMS
Loading...
Searching...
No Matches
ChromatogramExtractor.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: Hannes Roest $
6// $Authors: Hannes Roest $
7// --------------------------------------------------------------------------
8
9#pragma once
10
12
17
20
21namespace OpenMS
22{
23
52 class OPENMS_DLLAPI ChromatogramExtractor :
53 public ProgressLogger
54 {
55
56public:
57
59
60
78 std::vector< OpenSwath::ChromatogramPtr >& output,
79 const std::vector<ExtractionCoordinates>& extraction_coordinates,
80 double mz_extraction_window,
81 bool ppm,
82 const std::string& filter)
83 {
85 extraction_coordinates, mz_extraction_window, ppm, -1, filter);
86 }
87
107 std::vector< OpenSwath::ChromatogramPtr >& output,
108 const std::vector<ExtractionCoordinates>& extraction_coordinates,
109 double mz_extraction_window,
110 bool ppm,
111 double im_extraction_window,
112 const std::string& filter)
113 {
115 extraction_coordinates, mz_extraction_window, ppm, im_extraction_window, filter);
116 }
117
142 static void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > & output_chromatograms,
143 std::vector< ExtractionCoordinates > & coordinates,
144 const OpenSwath::LightTargetedExperiment & transition_exp_used,
145 const double rt_extraction_window,
146 const bool ms1 = false,
147 const int ms1_isotopes = 0);
148
163 template <typename TransitionExpT>
164 static void return_chromatogram(const std::vector< OpenSwath::ChromatogramPtr > & chromatograms,
165 const std::vector< ChromatogramExtractor::ExtractionCoordinates > & coordinates,
166 TransitionExpT& transition_exp_used,
167 SpectrumSettings settings,
168 std::vector<OpenMS::MSChromatogram > & output_chromatograms,
169 bool ms1,
170 double im_extraction_width = 0.0)
171 {
172 typedef std::map<std::string, const typename TransitionExpT::Transition* > TransitionMapType;
173 TransitionMapType trans_map;
174 for (Size i = 0; i < transition_exp_used.getTransitions().size(); i++)
175 {
176 trans_map[transition_exp_used.getTransitions()[i].getNativeID()] = &transition_exp_used.getTransitions()[i];
177 }
178
179 for (Size i = 0; i < chromatograms.size(); i++)
180 {
181 const OpenSwath::ChromatogramPtr & chromptr = chromatograms[i];
182 const ChromatogramExtractor::ExtractionCoordinates & coord = coordinates[i];
183
184 // copy data
186 OpenSwathDataAccessHelper::convertToOpenMSChromatogram(chromptr, chrom);
187 chrom.setNativeID(coord.id);
188
189 // Create precursor and set
190 // 1) the target m/z
191 // 2) the isolation window (upper/lower)
192 // 3) the peptide sequence
193 Precursor prec;
194 if (ms1)
195 {
196 prec.setMZ(coord.mz);
197 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::BASEPEAK_CHROMATOGRAM);
198
199 // extract compound / peptide id from transition and store in
200 // more-or-less default field
201 std::string transition_group_id = OpenSwathHelper::computeTransitionGroupId(coord.id);
202 if (!transition_group_id.empty())
203 {
204 int prec_charge = 0;
205 std::string r = extract_id_(transition_exp_used, transition_group_id, prec_charge);
206 prec.setCharge(prec_charge);
207 prec.setMetaValue("peptide_sequence", r);
208 }
209 }
210 else
211 {
212 typename TransitionExpT::Transition transition = (*trans_map[coord.id]);
213
214 prec.setMZ(transition.getPrecursorMZ());
215 if (!settings.getPrecursors().empty())
216 {
217 prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
218 prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
219 }
220
221 // Create product and set its m/z
222 Product prod;
223 prod.setMZ(transition.getProductMZ());
224 chrom.setProduct(prod);
225 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::SELECTED_REACTION_MONITORING_CHROMATOGRAM);
226
227 // extract compound / peptide id from transition and store in
228 // more-or-less default field
229 if (!transition.getPeptideRef().empty())
230 {
231 int prec_charge = 0;
232 std::string r = extract_id_(transition_exp_used, transition.getPeptideRef(), prec_charge);
233 prec.setCharge(prec_charge);
234 prec.setMetaValue("peptide_sequence", r);
235 }
236 else
237 {
238 int prec_charge = 0;
239 std::string r = extract_id_(transition_exp_used, transition.getCompoundRef(), prec_charge);
240 prec.setCharge(prec_charge);
241 prec.setMetaValue("peptide_sequence", r);
242 }
243 }
244
245 if (coord.ion_mobility >= 0 && im_extraction_width > 0.0)
246 {
247 prec.setDriftTime(coord.ion_mobility);
248 prec.setDriftTimeWindowLowerOffset(im_extraction_width / 2.0);
249 prec.setDriftTimeWindowUpperOffset(im_extraction_width / 2.0);
250 }
251 chrom.setPrecursor(prec);
252
253 // Set the rest of the meta-data
255 chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
256 chrom.setSourceFile(settings.getSourceFile());
257
258 for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
259 {
260 settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
261 chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
262 }
263 output_chromatograms.push_back(chrom);
264 }
265 }
266
267
268private:
276 template <typename TransitionExpT>
277 static std::string extract_id_(TransitionExpT& transition_exp_used, const std::string& id, int& prec_charge);
278
293 template <class SpectrumSettingsT, class ChromatogramT>
294 void prepareSpectra_(SpectrumSettingsT& settings,
295 std::vector<ChromatogramT>& chromatograms,
296 OpenMS::TargetedExperiment& transition_exp)
297 {
298 // first prepare all the spectra (but leave them empty)
299 for (Size i = 0; i < transition_exp.getTransitions().size(); i++)
300 {
301 const ReactionMonitoringTransition* transition = &transition_exp.getTransitions()[i];
302
303 // 1) and 2) Extract precursor m/z and isolation window
304 ChromatogramT chrom;
305 Precursor prec;
306 prec.setMZ(transition->getPrecursorMZ());
307 if (settings.getPrecursors().size() > 0)
308 {
309 prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
310 prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
311 }
312
313 // 3) set precursor peptide sequence / compound id in more-or-less default field
314 std::string pepref = transition->getPeptideRef();
315 for (Size pep_idx = 0; pep_idx < transition_exp.getPeptides().size(); pep_idx++)
316 {
317 const OpenMS::TargetedExperiment::Peptide* pep = &transition_exp.getPeptides()[pep_idx];
318 if (pep->id == pepref)
319 {
320 prec.setMetaValue("peptide_sequence", pep->sequence);
321 break;
322 }
323 }
324 std::string compref = transition->getCompoundRef();
325 for (Size comp_idx = 0; comp_idx < transition_exp.getCompounds().size(); comp_idx++)
326 {
327 const OpenMS::TargetedExperiment::Compound* comp = &transition_exp.getCompounds()[comp_idx];
328 if (comp->id == compref)
329 {
330 prec.setMetaValue("peptide_sequence",std::string(comp->id) );
331 break;
332 }
333 }
334
335 // add precursor to spectrum
336 chrom.setPrecursor(prec);
337
338 // 4) Create product and set its m/z
339 Product prod;
340 prod.setMZ(transition->getProductMZ());
341 chrom.setProduct(prod);
342
343 // 5) Set the rest of the meta-data
344 chrom.setInstrumentSettings(settings.getInstrumentSettings());
345 chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
346 chrom.setSourceFile(settings.getSourceFile());
347
348 for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
349 {
350 settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
351 chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
352 }
353
354 // Set the id of the chromatogram, using the id of the transition (this gives directly the mapping of the two)
355 chrom.setNativeID(transition->getNativeID());
356 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::SELECTED_REACTION_MONITORING_CHROMATOGRAM);
357 chromatograms.push_back(chrom);
358 }
359
360 }
361
364 double current_rt,
365 const TransformationDescription& trafo,
366 double rt_extraction_window);
367
369 int getFilterNr_(const std::string& filter);
370
373 double rt_extraction_window);
374
375 std::map<std::string, double> PeptideRTMap_;
376
377 };
378
379 // Specialization for template (LightTargetedExperiment)
380 template<>
381 inline std::string ChromatogramExtractor::extract_id_<OpenSwath::LightTargetedExperiment>(OpenSwath::LightTargetedExperiment& transition_exp_used,
382 const std::string& id,
383 int & prec_charge)
384 {
385 const OpenSwath::LightCompound comp = transition_exp_used.getCompoundByRef(id);
386 prec_charge = comp.charge;
387 if (!comp.sequence.empty())
388 {
389 return comp.sequence; // peptide path
390 }
391 // Fall through to compound_name (may itself be empty — that is intentional
392 // for iRT calibration peptides, which carry empty sequence and no
393 // CompoundName user-param. Downstream consumers expect an empty
394 // peptide_sequence userParam in that case).
395 return comp.compound_name;
396 }
397
398 // Const-qualified template specialization for extract_id_.
399 // This specialization handles const LightTargetedExperiment parameters by forwarding
400 // to the non-const implementation (via const_cast) to avoid linker errors from
401 // duplicate template instantiations when both const and non-const versions are used.
402 template<>
403 inline std::string ChromatogramExtractor::extract_id_<const OpenSwath::LightTargetedExperiment>(const OpenSwath::LightTargetedExperiment& transition_exp_used,
404 const std::string& id,
405 int & prec_charge)
406 {
407 // forward to non-const implementation
408 return extract_id_<OpenSwath::LightTargetedExperiment>(const_cast<OpenSwath::LightTargetedExperiment&>(transition_exp_used), id, prec_charge);
409 }
410
411 // Specialization for template (TargetedExperiment)
412 template<>
413 inline std::string ChromatogramExtractor::extract_id_<OpenMS::TargetedExperiment>(OpenMS::TargetedExperiment& transition_exp_used,
414 const std::string& id,
415 int & prec_charge)
416 {
417 if (transition_exp_used.hasPeptide(id))
418 {
419 const TargetedExperiment::Peptide p = transition_exp_used.getPeptideByRef(id);
420 if (p.hasCharge()) {prec_charge = p.getChargeState();}
421 return p.sequence;
422 }
423 else if (transition_exp_used.hasCompound(id))
424 {
425 const TargetedExperiment::Compound c = transition_exp_used.getCompoundByRef(id);
426 if (c.hasCharge()) {prec_charge = c.getChargeState();}
427 return c.id;
428 }
429 else
430 {
431 return "";
432 }
433 }
434}
435
The ChromatogramExtractorAlgorithm extracts chromatograms from a MS data.
Definition ChromatogramExtractorAlgorithm.h:34
void extractChromatograms(const OpenSwath::SpectrumAccessPtr &input, std::vector< OpenSwath::ChromatogramPtr > &output, const std::vector< ExtractionCoordinates > &extraction_coordinates, double mz_extraction_window, bool ppm, double im_extraction_window, const std::string &filter)
Extract chromatograms at the m/z and RT defined by the ExtractionCoordinates.
The ChromatogramExtractor extracts chromatograms (intensity vs retention time) from mass spectrometry...
Definition ChromatogramExtractor.h:54
void extractChromatograms(const OpenSwath::SpectrumAccessPtr input, std::vector< OpenSwath::ChromatogramPtr > &output, const std::vector< ExtractionCoordinates > &extraction_coordinates, double mz_extraction_window, bool ppm, double im_extraction_window, const std::string &filter)
Extract chromatograms at the m/z and RT defined by the ExtractionCoordinates.
Definition ChromatogramExtractor.h:106
std::map< std::string, double > PeptideRTMap_
Definition ChromatogramExtractor.h:375
void extractChromatograms(const OpenSwath::SpectrumAccessPtr input, std::vector< OpenSwath::ChromatogramPtr > &output, const std::vector< ExtractionCoordinates > &extraction_coordinates, double mz_extraction_window, bool ppm, const std::string &filter)
Extract chromatograms at the m/z and RT defined by the ExtractionCoordinates.
Definition ChromatogramExtractor.h:77
ChromatogramExtractorAlgorithm::ExtractionCoordinates ExtractionCoordinates
Definition ChromatogramExtractor.h:58
static std::string extract_id_(TransitionExpT &transition_exp_used, const std::string &id, int &prec_charge)
Extracts id (peptide sequence or compound name) for a compound.
static void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > &output_chromatograms, std::vector< ExtractionCoordinates > &coordinates, const OpenSwath::LightTargetedExperiment &transition_exp_used, const double rt_extraction_window, const bool ms1=false, const int ms1_isotopes=0)
Prepare the extraction coordinates from a LightTargetedExperiment.
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:294
bool outsideExtractionWindow_(const ReactionMonitoringTransition &transition, double current_rt, const TransformationDescription &trafo, double rt_extraction_window)
static void return_chromatogram(const std::vector< OpenSwath::ChromatogramPtr > &chromatograms, const std::vector< ChromatogramExtractor::ExtractionCoordinates > &coordinates, TransitionExpT &transition_exp_used, SpectrumSettings settings, std::vector< OpenMS::MSChromatogram > &output_chromatograms, bool ms1, double im_extraction_width=0.0)
This converts the ChromatogramPtr to MSChromatogram and adds meta-information.
Definition ChromatogramExtractor.h:164
void populatePeptideRTMap_(OpenMS::TargetedExperiment &transition_exp, double rt_extraction_window)
int getFilterNr_(const std::string &filter)
void setPrecursor(const Precursor &precursor)
sets the precursors
void setProduct(const Product &product)
sets the products
std::vector< DataProcessingPtr > & getDataProcessing()
returns a mutable reference to the description of the applied processing
void setInstrumentSettings(const InstrumentSettings &instrument_settings)
sets the instrument settings of the current spectrum
void setSourceFile(const SourceFile &source_file)
sets the source file
void setChromatogramType(ChromatogramType type)
sets the chromatogram type
void setAcquisitionInfo(const AcquisitionInfo &acquisition_info)
sets the acquisition info
void setNativeID(const std::string &native_id)
sets the native identifier for the spectrum, used by the acquisition software.
The representation of a chromatogram.
Definition MSChromatogram.h:30
void setMetaValue(const std::string &name, const DataValue &value)
Sets the DataValue corresponding to a name.
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition Peak1D.h:95
Precursor meta information.
Definition Precursor.h:37
void setIsolationWindowUpperOffset(double bound)
sets the upper offset from the target m/z
void setDriftTime(double drift_time)
sets the ion mobility drift time in milliseconds
void setDriftTimeWindowUpperOffset(double drift_time)
sets the upper offset from the target ion mobility
void setDriftTimeWindowLowerOffset(double drift_time)
sets the lower offset from the target ion mobility
void setIsolationWindowLowerOffset(double bound)
sets the lower offset from the target m/z
void setCharge(Int charge)
Mutable access to the charge.
Product meta information.
Definition Product.h:26
void setMZ(double mz)
sets the target m/z
Base class for all classes that want to report their progress.
Definition ProgressLogger.h:27
This class stores a SRM/MRM transition.
Definition ReactionMonitoringTransition.h:34
double getPrecursorMZ() const
get the precursor mz (Q1 value)
const std::string & getPeptideRef() const
const std::string & getCompoundRef() const
const std::string & getNativeID() const
Representation of 1D spectrum settings.
Definition SpectrumSettings.h:43
std::vector< DataProcessingPtr > & getDataProcessing()
returns a mutable reference to the description of the applied processing
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
const InstrumentSettings & getInstrumentSettings() const
returns a const reference to the instrument settings of the current spectrum
const SourceFile & getSourceFile() const
returns a const reference to the source file
const AcquisitionInfo & getAcquisitionInfo() const
returns a const reference to the acquisition info
Represents a compound (small molecule)
Definition TargetedExperimentHelper.h:298
int getChargeState() const
Return the peptide or compound charge state.
Definition TargetedExperimentHelper.h:218
bool hasCharge() const
Whether peptide or compound has set charge state.
Definition TargetedExperimentHelper.h:212
std::string id
Definition TargetedExperimentHelper.h:281
Represents a peptide (amino acid sequence)
Definition TargetedExperimentHelper.h:335
std::string sequence
Definition TargetedExperimentHelper.h:395
A description of a targeted experiment containing precursor and production ions.
Definition TargetedExperiment.h:40
bool hasPeptide(const std::string &ref) const
const Compound & getCompoundByRef(const std::string &ref) const
bool hasCompound(const std::string &ref) const
const std::vector< ReactionMonitoringTransition > & getTransitions() const
returns the transition list
const std::vector< Compound > & getCompounds() const
const std::vector< Peptide > & getPeptides() const
const Peptide & getPeptideByRef(const std::string &ref) const
Generic description of a coordinate transformation.
Definition TransformationDescription.h:38
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::string ChromatogramExtractor::extract_id_< OpenSwath::LightTargetedExperiment >(OpenSwath::LightTargetedExperiment &transition_exp_used, const std::string &id, int &prec_charge)
Definition ChromatogramExtractor.h:381
std::shared_ptr< Chromatogram > ChromatogramPtr
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/DataStructures.h:153
std::shared_ptr< ISpectrumAccess > SpectrumAccessPtr
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:131
Definition ChromatogramExtractorAlgorithm.h:39
double mz
m/z value around which should be extracted
Definition ChromatogramExtractorAlgorithm.h:40
double ion_mobility
ion mobility value around which should be extracted
Definition ChromatogramExtractorAlgorithm.h:41
std::string id
identifier
Definition ChromatogramExtractorAlgorithm.h:45
Definition TransitionExperiment.h:269
std::string sequence
Definition TransitionExperiment.h:292
std::string compound_name
Definition TransitionExperiment.h:301
int charge
Definition TransitionExperiment.h:291
Definition TransitionExperiment.h:369
const LightCompound & getCompoundByRef(const std::string &ref)
Definition TransitionExperiment.h:416