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
23namespace OpenMS
24{
25
54 class OPENMS_DLLAPI ChromatogramExtractor :
55 public ProgressLogger
56 {
57
58public:
59
61
62
80 std::vector< OpenSwath::ChromatogramPtr >& output,
81 const std::vector<ExtractionCoordinates>& extraction_coordinates,
82 double mz_extraction_window,
83 bool ppm,
84 const std::string& filter)
85 {
87 extraction_coordinates, mz_extraction_window, ppm, -1, filter);
88 }
89
109 std::vector< OpenSwath::ChromatogramPtr >& output,
110 const std::vector<ExtractionCoordinates>& extraction_coordinates,
111 double mz_extraction_window,
112 bool ppm,
113 double im_extraction_window,
114 const std::string& filter)
115 {
117 extraction_coordinates, mz_extraction_window, ppm, im_extraction_window, filter);
118 }
119
144 static void prepare_coordinates(std::vector< OpenSwath::ChromatogramPtr > & output_chromatograms,
145 std::vector< ExtractionCoordinates > & coordinates,
146 const OpenSwath::LightTargetedExperiment & transition_exp_used,
147 const double rt_extraction_window,
148 const bool ms1 = false,
149 const int ms1_isotopes = 0);
150
165 template <typename TransitionExpT>
166 static void return_chromatogram(const std::vector< OpenSwath::ChromatogramPtr > & chromatograms,
167 const std::vector< ChromatogramExtractor::ExtractionCoordinates > & coordinates,
168 TransitionExpT& transition_exp_used,
169 SpectrumSettings settings,
170 std::vector<OpenMS::MSChromatogram > & output_chromatograms,
171 bool ms1,
172 double im_extraction_width = 0.0)
173 {
174 typedef std::map<std::string, const typename TransitionExpT::Transition* > TransitionMapType;
175 TransitionMapType trans_map;
176 for (Size i = 0; i < transition_exp_used.getTransitions().size(); i++)
177 {
178 trans_map[transition_exp_used.getTransitions()[i].getNativeID()] = &transition_exp_used.getTransitions()[i];
179 }
180
181 output_chromatograms.reserve(output_chromatograms.size() + chromatograms.size());
182 for (Size i = 0; i < chromatograms.size(); i++)
183 {
184 const OpenSwath::ChromatogramPtr & chromptr = chromatograms[i];
185 const ChromatogramExtractor::ExtractionCoordinates & coord = coordinates[i];
186
187 // copy data
189 OpenSwathDataAccessHelper::convertToOpenMSChromatogram(chromptr, chrom);
190 chrom.setNativeID(coord.id);
191
192 // Create precursor and set
193 // 1) the target m/z
194 // 2) the isolation window (upper/lower)
195 // 3) the peptide sequence
196 Precursor prec;
197 if (ms1)
198 {
199 prec.setMZ(coord.mz);
200 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::BASEPEAK_CHROMATOGRAM);
201
202 // extract compound / peptide id from transition and store in
203 // more-or-less default field
204 std::string transition_group_id = OpenSwathHelper::computeTransitionGroupId(coord.id);
205 if (!transition_group_id.empty())
206 {
207 int prec_charge = 0;
208 std::string r = extract_id_(transition_exp_used, transition_group_id, prec_charge);
209 prec.setCharge(prec_charge);
210 prec.setMetaValue("peptide_sequence", r);
211 }
212 }
213 else
214 {
215 typename TransitionExpT::Transition transition = (*trans_map[coord.id]);
216
217 prec.setMZ(transition.getPrecursorMZ());
218 if (!settings.getPrecursors().empty())
219 {
220 prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
221 prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
222 }
223
224 // Create product and set its m/z
225 Product prod;
226 prod.setMZ(transition.getProductMZ());
227 chrom.setProduct(prod);
228 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::SELECTED_REACTION_MONITORING_CHROMATOGRAM);
229
230 // extract compound / peptide id from transition and store in
231 // more-or-less default field
232 if (!transition.getPeptideRef().empty())
233 {
234 int prec_charge = 0;
235 std::string r = extract_id_(transition_exp_used, transition.getPeptideRef(), prec_charge);
236 prec.setCharge(prec_charge);
237 prec.setMetaValue("peptide_sequence", r);
238 }
239 else
240 {
241 int prec_charge = 0;
242 std::string r = extract_id_(transition_exp_used, transition.getCompoundRef(), prec_charge);
243 prec.setCharge(prec_charge);
244 prec.setMetaValue("peptide_sequence", r);
245 }
246 }
247
248 if (coord.ion_mobility >= 0 && im_extraction_width > 0.0)
249 {
250 prec.setDriftTime(coord.ion_mobility);
251 prec.setDriftTimeWindowLowerOffset(im_extraction_width / 2.0);
252 prec.setDriftTimeWindowUpperOffset(im_extraction_width / 2.0);
253 }
254 chrom.setPrecursor(prec);
255
256 // Set the rest of the meta-data
258 chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
259 chrom.setSourceFile(settings.getSourceFile());
260
261 for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
262 {
263 settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
264 chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
265 }
266 output_chromatograms.push_back(std::move(chrom));
267 }
268 }
269
270
271private:
279 template <typename TransitionExpT>
280 static std::string extract_id_(TransitionExpT& transition_exp_used, const std::string& id, int& prec_charge);
281
296 template <class SpectrumSettingsT, class ChromatogramT>
297 void prepareSpectra_(SpectrumSettingsT& settings,
298 std::vector<ChromatogramT>& chromatograms,
299 OpenMS::TargetedExperiment& transition_exp)
300 {
301 // first prepare all the spectra (but leave them empty)
302 for (Size i = 0; i < transition_exp.getTransitions().size(); i++)
303 {
304 const ReactionMonitoringTransition* transition = &transition_exp.getTransitions()[i];
305
306 // 1) and 2) Extract precursor m/z and isolation window
307 ChromatogramT chrom;
308 Precursor prec;
309 prec.setMZ(transition->getPrecursorMZ());
310 if (settings.getPrecursors().size() > 0)
311 {
312 prec.setIsolationWindowLowerOffset(settings.getPrecursors()[0].getIsolationWindowLowerOffset());
313 prec.setIsolationWindowUpperOffset(settings.getPrecursors()[0].getIsolationWindowUpperOffset());
314 }
315
316 // 3) set precursor peptide sequence / compound id in more-or-less default field
317 std::string pepref = transition->getPeptideRef();
318 for (Size pep_idx = 0; pep_idx < transition_exp.getPeptides().size(); pep_idx++)
319 {
320 const OpenMS::TargetedExperiment::Peptide* pep = &transition_exp.getPeptides()[pep_idx];
321 if (pep->id == pepref)
322 {
323 prec.setMetaValue("peptide_sequence", pep->sequence);
324 break;
325 }
326 }
327 std::string compref = transition->getCompoundRef();
328 for (Size comp_idx = 0; comp_idx < transition_exp.getCompounds().size(); comp_idx++)
329 {
330 const OpenMS::TargetedExperiment::Compound* comp = &transition_exp.getCompounds()[comp_idx];
331 if (comp->id == compref)
332 {
333 prec.setMetaValue("peptide_sequence",std::string(comp->id) );
334 break;
335 }
336 }
337
338 // add precursor to spectrum
339 chrom.setPrecursor(prec);
340
341 // 4) Create product and set its m/z
342 Product prod;
343 prod.setMZ(transition->getProductMZ());
344 chrom.setProduct(prod);
345
346 // 5) Set the rest of the meta-data
347 chrom.setInstrumentSettings(settings.getInstrumentSettings());
348 chrom.setAcquisitionInfo(settings.getAcquisitionInfo());
349 chrom.setSourceFile(settings.getSourceFile());
350
351 for (Size j = 0; j < settings.getDataProcessing().size(); ++j)
352 {
353 settings.getDataProcessing()[j]->setMetaValue("performed_on_spectra", "true");
354 chrom.getDataProcessing().push_back(settings.getDataProcessing()[j]);
355 }
356
357 // Set the id of the chromatogram, using the id of the transition (this gives directly the mapping of the two)
358 chrom.setNativeID(transition->getNativeID());
359 chrom.setChromatogramType(ChromatogramSettings::ChromatogramType::SELECTED_REACTION_MONITORING_CHROMATOGRAM);
360 chromatograms.push_back(chrom);
361 }
362
363 }
364
367 double current_rt,
368 const TransformationDescription& trafo,
369 double rt_extraction_window);
370
372 int getFilterNr_(const std::string& filter);
373
376 double rt_extraction_window);
377
378 std::map<std::string, double> PeptideRTMap_;
379
380 };
381
382 // Specialization for template (LightTargetedExperiment)
383 template<>
384 inline std::string ChromatogramExtractor::extract_id_<OpenSwath::LightTargetedExperiment>(OpenSwath::LightTargetedExperiment& transition_exp_used,
385 const std::string& id,
386 int & prec_charge)
387 {
388 const OpenSwath::LightCompound comp = transition_exp_used.getCompoundByRef(id);
389 prec_charge = comp.charge;
390 if (!comp.sequence.empty())
391 {
392 return comp.sequence; // peptide path
393 }
394 // Fall through to compound_name (may itself be empty — that is intentional
395 // for iRT calibration peptides, which carry empty sequence and no
396 // CompoundName user-param. Downstream consumers expect an empty
397 // peptide_sequence userParam in that case).
398 return comp.compound_name;
399 }
400
401 // Const-qualified template specialization for extract_id_.
402 // This specialization handles const LightTargetedExperiment parameters by forwarding
403 // to the non-const implementation (via const_cast) to avoid linker errors from
404 // duplicate template instantiations when both const and non-const versions are used.
405 template<>
406 inline std::string ChromatogramExtractor::extract_id_<const OpenSwath::LightTargetedExperiment>(const OpenSwath::LightTargetedExperiment& transition_exp_used,
407 const std::string& id,
408 int & prec_charge)
409 {
410 // forward to non-const implementation
411 return extract_id_<OpenSwath::LightTargetedExperiment>(const_cast<OpenSwath::LightTargetedExperiment&>(transition_exp_used), id, prec_charge);
412 }
413
414 // Specialization for template (TargetedExperiment)
415 template<>
416 inline std::string ChromatogramExtractor::extract_id_<OpenMS::TargetedExperiment>(OpenMS::TargetedExperiment& transition_exp_used,
417 const std::string& id,
418 int & prec_charge)
419 {
420 if (transition_exp_used.hasPeptide(id))
421 {
422 const TargetedExperiment::Peptide p = transition_exp_used.getPeptideByRef(id);
423 if (p.hasCharge()) {prec_charge = p.getChargeState();}
424 return p.sequence;
425 }
426 else if (transition_exp_used.hasCompound(id))
427 {
428 const TargetedExperiment::Compound c = transition_exp_used.getCompoundByRef(id);
429 if (c.hasCharge()) {prec_charge = c.getChargeState();}
430 return c.id;
431 }
432 else
433 {
434 return "";
435 }
436 }
437}
438
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:56
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:108
std::map< std::string, double > PeptideRTMap_
Definition ChromatogramExtractor.h:378
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:79
ChromatogramExtractorAlgorithm::ExtractionCoordinates ExtractionCoordinates
Definition ChromatogramExtractor.h:60
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:297
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:166
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:33
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: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
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:384
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