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
21#include <utility>
22
23#ifdef _OPENMP
24#include <omp.h>
25#endif
26
27namespace OpenMS
28{
29
58 class OPENMS_DLLAPI ChromatogramExtractor :
59 public ProgressLogger
60 {
61
62public:
63
65
66
84 std::vector< OpenSwath::ChromatogramPtr >& output,
85 const std::vector<ExtractionCoordinates>& extraction_coordinates,
86 double mz_extraction_window,
87 bool ppm,
88 const std::string& filter)
89 {
91 extraction_coordinates, mz_extraction_window, ppm, -1, filter);
92 }
93
113 std::vector< OpenSwath::ChromatogramPtr >& output,
114 const std::vector<ExtractionCoordinates>& extraction_coordinates,
115 double mz_extraction_window,
116 bool ppm,
117 double im_extraction_window,
118 const std::string& filter)
119 {
121 extraction_coordinates, mz_extraction_window, ppm, im_extraction_window, filter);
122 }
123
148 static void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > & output_chromatograms,
149 std::vector< ExtractionCoordinates > & coordinates,
150 const OpenSwath::LightTargetedExperiment & transition_exp_used,
151 const double rt_extraction_window,
152 const bool ms1 = false,
153 const int ms1_isotopes = 0);
154
169 template <typename TransitionExpT>
170 static void return_chromatogram(const std::vector< OpenSwath::ChromatogramPtr > & chromatograms,
171 const std::vector< ChromatogramExtractor::ExtractionCoordinates > & coordinates,
172 TransitionExpT& transition_exp_used,
173 SpectrumSettings settings,
174 std::vector<OpenMS::MSChromatogram > & output_chromatograms,
175 bool ms1,
176 double im_extraction_width = 0.0)
177 {
178 typedef std::map<std::string, const typename TransitionExpT::Transition* > TransitionMapType;
179 TransitionMapType trans_map;
180 for (Size i = 0; i < transition_exp_used.getTransitions().size(); i++)
181 {
182 trans_map[transition_exp_used.getTransitions()[i].getNativeID()] = &transition_exp_used.getTransitions()[i];
183 }
184
185 // The lookups of the peptide / compound (and the data processing) are not thread-safe: collect
186 // them first, then build the chromatograms in parallel (if called from a single thread).
187 struct ChromatogramIds
188 {
189 const typename TransitionExpT::Transition* transition = nullptr;
190 bool has_id = false;
191 int prec_charge = 0;
192 std::string sequence;
193 };
194 std::vector<ChromatogramIds> ids(chromatograms.size());
195 for (Size i = 0; i < chromatograms.size(); i++)
196 {
197 const ChromatogramExtractor::ExtractionCoordinates & coord = coordinates[i];
198 ChromatogramIds& id = ids[i];
199 if (ms1)
200 {
201 // extract compound / peptide id from transition and store in
202 // more-or-less default field
203 std::string transition_group_id = OpenSwathHelper::computeTransitionGroupId(coord.id);
204 if (!transition_group_id.empty())
205 {
206 id.has_id = true;
207 id.sequence = extract_id_(transition_exp_used, transition_group_id, id.prec_charge);
208 }
209 }
210 else
211 {
212 id.transition = trans_map[coord.id];
213 // extract compound / peptide id from transition and store in
214 // more-or-less default field
215 id.has_id = true;
216 if (!id.transition->getPeptideRef().empty())
217 {
218 id.sequence = extract_id_(transition_exp_used, id.transition->getPeptideRef(), id.prec_charge);
219 }
220 else
221 {
222 id.sequence = extract_id_(transition_exp_used, id.transition->getCompoundRef(), id.prec_charge);
223 }
224 }
225 }
226 if (!chromatograms.empty())
227 {
228 for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
229 {
230 settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
231 }
232 }
233
234 const Size offset = output_chromatograms.size();
235 output_chromatograms.resize(offset + chromatograms.size());
236 [[maybe_unused]] bool parallel = false;
237#ifdef _OPENMP
238 parallel = omp_in_parallel() == 0 && chromatograms.size() >= 256;
239#pragma omp parallel for schedule(dynamic, 64) if (parallel)
240#endif
241 for (SignedSize i = 0; i < static_cast<SignedSize>(chromatograms.size()); i++)
242 {
243 const OpenSwath::ChromatogramPtr & chromptr = chromatograms[i];
244 const ChromatogramExtractor::ExtractionCoordinates & coord = coordinates[i];
245 const ChromatogramIds& id = ids[i];
246
247 // copy data
249 OpenSwathDataAccessHelper::convertToOpenMSChromatogram(chromptr, chrom);
250 chrom.setNativeID(coord.id);
251
252 // Create precursor and set
253 // 1) the target m/z
254 // 2) the isolation window (upper/lower)
255 // 3) the peptide sequence
256 Precursor prec;
257 if (ms1)
258 {
259 prec.setMZ(coord.mz);
260 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::BASEPEAK_CHROMATOGRAM);
261 }
262 else
263 {
264 const typename TransitionExpT::Transition& transition = *id.transition;
265
266 prec.setMZ(transition.getPrecursorMZ());
267 if (!settings.getPrecursors().empty())
268 {
269 prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
270 prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
271 }
272
273 // Create product and set its m/z
274 Product prod;
275 prod.setMZ(transition.getProductMZ());
276 chrom.setProduct(prod);
277 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::SELECTED_REACTION_MONITORING_CHROMATOGRAM);
278 }
279 if (id.has_id)
280 {
281 prec.setCharge(id.prec_charge);
282 prec.setMetaValue("peptide_sequence", id.sequence);
283 }
284
285 if (coord.ion_mobility >= 0 && im_extraction_width > 0.0)
286 {
287 prec.setDriftTime(coord.ion_mobility);
288 prec.setDriftTimeWindowLowerOffset(im_extraction_width / 2.0);
289 prec.setDriftTimeWindowUpperOffset(im_extraction_width / 2.0);
290 }
291 chrom.setPrecursor(prec);
292
293 // Set the rest of the meta-data
295 chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
296 chrom.setSourceFile(settings.getSourceFile());
297
298 for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
299 {
300 chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
301 }
302 output_chromatograms[offset + i] = std::move(chrom);
303 }
304 }
305
306
307private:
315 template <typename TransitionExpT>
316 static std::string extract_id_(TransitionExpT& transition_exp_used, const std::string& id, int& prec_charge);
317
332 template <class SpectrumSettingsT, class ChromatogramT>
333 void prepareSpectra_(SpectrumSettingsT& settings,
334 std::vector<ChromatogramT>& chromatograms,
335 OpenMS::TargetedExperiment& transition_exp)
336 {
337 // first prepare all the spectra (but leave them empty)
338 for (Size i = 0; i < transition_exp.getTransitions().size(); i++)
339 {
340 const ReactionMonitoringTransition* transition = &transition_exp.getTransitions()[i];
341
342 // 1) and 2) Extract precursor m/z and isolation window
343 ChromatogramT chrom;
344 Precursor prec;
345 prec.setMZ(transition->getPrecursorMZ());
346 if (settings.getPrecursors().size() > 0)
347 {
348 prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
349 prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
350 }
351
352 // 3) set precursor peptide sequence / compound id in more-or-less default field
353 std::string pepref = transition->getPeptideRef();
354 for (Size pep_idx = 0; pep_idx < transition_exp.getPeptides().size(); pep_idx++)
355 {
356 const OpenMS::TargetedExperiment::Peptide* pep = &transition_exp.getPeptides()[pep_idx];
357 if (pep->id == pepref)
358 {
359 prec.setMetaValue("peptide_sequence", pep->sequence);
360 break;
361 }
362 }
363 std::string compref = transition->getCompoundRef();
364 for (Size comp_idx = 0; comp_idx < transition_exp.getCompounds().size(); comp_idx++)
365 {
366 const OpenMS::TargetedExperiment::Compound* comp = &transition_exp.getCompounds()[comp_idx];
367 if (comp->id == compref)
368 {
369 prec.setMetaValue("peptide_sequence",std::string(comp->id) );
370 break;
371 }
372 }
373
374 // add precursor to spectrum
375 chrom.setPrecursor(prec);
376
377 // 4) Create product and set its m/z
378 Product prod;
379 prod.setMZ(transition->getProductMZ());
380 chrom.setProduct(prod);
381
382 // 5) Set the rest of the meta-data
383 chrom.setInstrumentSettings(settings.getInstrumentSettings());
384 chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
385 chrom.setSourceFile(settings.getSourceFile());
386
387 for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
388 {
389 settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
390 chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
391 }
392
393 // Set the id of the chromatogram, using the id of the transition (this gives directly the mapping of the two)
394 chrom.setNativeID(transition->getNativeID());
395 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::SELECTED_REACTION_MONITORING_CHROMATOGRAM);
396 chromatograms.push_back(chrom);
397 }
398
399 }
400
403 double current_rt,
404 const TransformationDescription& trafo,
405 double rt_extraction_window);
406
408 int getFilterNr_(const std::string& filter);
409
412 double rt_extraction_window);
413
414 std::map<std::string, double> PeptideRTMap_;
415
416 };
417
418 // Specialization for template (LightTargetedExperiment)
419 template<>
420 inline std::string ChromatogramExtractor::extract_id_<OpenSwath::LightTargetedExperiment>(OpenSwath::LightTargetedExperiment& transition_exp_used,
421 const std::string& id,
422 int & prec_charge)
423 {
424 const OpenSwath::LightCompound& comp = transition_exp_used.getCompoundByRef(id);
425 prec_charge = comp.charge;
426 if (!comp.sequence.empty())
427 {
428 return comp.sequence; // peptide path
429 }
430 // Fall through to compound_name (may itself be empty — that is intentional
431 // for iRT calibration peptides, which carry empty sequence and no
432 // CompoundName user-param. Downstream consumers expect an empty
433 // peptide_sequence userParam in that case).
434 return comp.compound_name;
435 }
436
437 // Const-qualified template specialization for extract_id_.
438 // This specialization handles const LightTargetedExperiment parameters by forwarding
439 // to the non-const implementation (via const_cast) to avoid linker errors from
440 // duplicate template instantiations when both const and non-const versions are used.
441 template<>
442 inline std::string ChromatogramExtractor::extract_id_<const OpenSwath::LightTargetedExperiment>(const OpenSwath::LightTargetedExperiment& transition_exp_used,
443 const std::string& id,
444 int & prec_charge)
445 {
446 // forward to non-const implementation
447 return extract_id_<OpenSwath::LightTargetedExperiment>(const_cast<OpenSwath::LightTargetedExperiment&>(transition_exp_used), id, prec_charge);
448 }
449
450 // Specialization for template (TargetedExperiment)
451 template<>
452 inline std::string ChromatogramExtractor::extract_id_<OpenMS::TargetedExperiment>(OpenMS::TargetedExperiment& transition_exp_used,
453 const std::string& id,
454 int & prec_charge)
455 {
456 if (transition_exp_used.hasPeptide(id))
457 {
458 const TargetedExperiment::Peptide p = transition_exp_used.getPeptideByRef(id);
459 if (p.hasCharge()) {prec_charge = p.getChargeState();}
460 return p.sequence;
461 }
462 else if (transition_exp_used.hasCompound(id))
463 {
464 const TargetedExperiment::Compound c = transition_exp_used.getCompoundByRef(id);
465 if (c.hasCharge()) {prec_charge = c.getChargeState();}
466 return c.id;
467 }
468 else
469 {
470 return "";
471 }
472 }
473}
474
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:60
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:112
std::map< std::string, double > PeptideRTMap_
Definition ChromatogramExtractor.h:414
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:83
ChromatogramExtractorAlgorithm::ExtractionCoordinates ExtractionCoordinates
Definition ChromatogramExtractor.h:64
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:333
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:170
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:34
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:28
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:42
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:297
int getChargeState() const
Return the peptide or compound charge state.
Definition TargetedExperimentHelper.h:217
bool hasCharge() const
Whether peptide or compound has set charge state.
Definition TargetedExperimentHelper.h:211
std::string id
Definition TargetedExperimentHelper.h:280
Represents a peptide (amino acid sequence)
Definition TargetedExperimentHelper.h:334
std::string sequence
Definition TargetedExperimentHelper.h:394
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
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition Types.h:104
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:420
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:146
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