OpenMS
Loading...
Searching...
No Matches
MzMLHandler.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: Marc Sturm, Chris Bielow, Hannes Roest $
7// --------------------------------------------------------------------------
8
9#pragma once
10
13
15
18
21
25
26#include <map>
27
28
29//MISSING:
30// - more than one selected ion per precursor (warning if more than one)
31// - scanWindowList for each acquisition separately (currently for the whole spectrum only)
32// - scanSettingsRef attribute for instrumentConfiguration tag (currently no information there because of missing mapping file entry - warning if used)
33
34// xs:id/xs:idref prefix list
35// - sf_ru : sourceFile (run)
36// - sf_sp : sourceFile (spectrum)
37// - sf_pr : sourceFile (precursor)
38// - sf_ac : sourceFile (acquisition)
39// - sa : sample
40// - ic : instrumentConfiguration
41// - so_dp : software (data processing)
42// - so_in : software (instrument)
43// - dp_sp : dataProcessing (spectrum)
44// - dp_bi : dataProcessing (binary data array)
45// - dp_ch : dataProcessing (chromatogram)
46
47namespace OpenMS
48{
49 namespace Interfaces
50 {
51 class IMSDataConsumer;
52 }
53
54 namespace Internal
55 {
56 class MzMLValidator;
57
58 typedef PeakMap MapType;
59 typedef MSSpectrum SpectrumType;
60 typedef MSChromatogram ChromatogramType;
61
91 class OPENMS_DLLAPI MzMLHandler :
92 public XMLHandler
93 {
94public:
95
98
100 MzMLHandler(MapType& exp, const std::string& filename, const std::string& version, const ProgressLogger& logger);
101
103 MzMLHandler(const MapType& exp, const std::string& filename, const std::string& version, const ProgressLogger& logger);
104
106 ~MzMLHandler() override;
108
114
116 void onEndElement(const char16_t* qname) override;
117
119 void onStartElement(const char16_t* qname, const XMLAttributes& attributes) override;
120
122 void onCharacters(const char16_t* chars, Size length) override;
123
125 bool tryOnCharactersUTF8(const char* chars, Size length) override;
126
128 bool tryOnStartElementUTF8(std::string_view qname, const XMLAttributes& attributes) override;
129
131 bool tryOnEndElementUTF8(std::string_view qname) override;
132
134 void writeTo(std::ostream& os) override;
135
137
148
150 void setOptions(const PeakFileOptions& opt);
151
154
156
158 void getCounts(Size& spectra_counts, Size& chromatogram_counts);
159
163
173
177
179 LOADDETAIL getLoadDetail() const override;
180
182 void setLoadDetail(const LOADDETAIL d) override;
183
184protected:
185
187 MzMLHandler(const std::string& filename, const std::string& version, const ProgressLogger& logger);
188
197
199
204
212 void requiredAttributeAsString_(std::string& value, const XMLAttributes& attributes, const char16_t* name) const;
213
216 {
217 std::string accession, name, value, unit_accession, type;
218 };
220
222 void startElement_(std::string_view tag_name, const XMLAttributes& attributes);
223
225 void endElement_(std::string_view tag_name);
226
228
236
252 void populateSpectraWithData_(std::vector<MzMLHandlerHelper::BinaryData>& input_data,
253 Size& length,
254 const PeakFileOptions& peak_file_options,
255 SpectrumType& spectrum);
256
269 void populateChromatogramsWithData_(std::vector<MzMLHandlerHelper::BinaryData>& input_data,
270 Size& length,
271 const PeakFileOptions& peak_file_options,
272 ChromatogramType& chromatogram);
273
276
278 void handleCVParam_(const std::string& parent_parent_tag,
279 const std::string& parent_tag,
280 const std::string& accession,
281 const std::string& name,
282 const std::string& value,
283 const std::string& unit_accession = "");
284
286 void handleUserParam_(const std::string& parent_parent_tag,
287 const std::string& parent_tag,
288 const std::string& name,
289 const std::string& type,
290 const std::string& value,
291 const std::string& unit_accession = "");
293
299
301 void writeHeader_(std::ostream& os,
302 const MapType& exp,
303 std::vector<std::vector< ConstDataProcessingPtr > >& dps,
304 const Internal::MzMLValidator& validator);
305
306
308 void writeSpectrum_(std::ostream& os,
309 const SpectrumType& spec,
310 Size spec_idx,
311 const Internal::MzMLValidator& validator,
312 bool renew_native_ids,
313 std::vector<std::vector< ConstDataProcessingPtr > >& dps);
314
316 void writeChromatogram_(std::ostream& os,
317 const ChromatogramType& chromatogram,
318 Size chrom_idx,
319 const Internal::MzMLValidator& validator);
320
321 template <typename ContainerT>
322 void writeContainerData_(std::ostream& os, const PeakFileOptions& pf_options_, const ContainerT& container, const std::string& array_type);
323
336 template <typename DataType>
337 void writeBinaryDataArray_(std::ostream& os,
338 const PeakFileOptions& options,
339 std::vector<DataType>& data,
340 bool is32bit,
341 std::string array_type);
342
357 void writeBinaryFloatDataArray_(std::ostream& os,
358 const PeakFileOptions& options,
360 const Size spec_chrom_idx,
361 const Size array_idx,
362 bool is_spectrum,
363 const Internal::MzMLValidator& validator);
364
366 void writeUserParam_(std::ostream& os, const MetaInfoInterface& meta, UInt indent, const std::string& path, const Internal::MzMLValidator& validator, const std::set<std::string>& exclude = {}) const;
367
369 void writeSoftware_(std::ostream& os, const std::string& id, const Software& software, const Internal::MzMLValidator& validator);
370
372 void writeInstrument_(std::ostream& os, const std::string& id, const Instrument& instrument,
373 const std::string& software_id, const Internal::MzMLValidator& validator);
374
376 void writeSourceFile_(std::ostream& os, const std::string& id, const SourceFile& software, const Internal::MzMLValidator& validator);
377
379 void writeDataProcessing_(std::ostream& os, const std::string& id, const std::vector< ConstDataProcessingPtr >& dps, const Internal::MzMLValidator& validator);
380
382 void writePrecursor_(std::ostream& os, const Precursor& precursor, const Internal::MzMLValidator& validator);
383
385 void writeProduct_(std::ostream& os, const Product& product, const Internal::MzMLValidator& validator);
386
388 std::string writeCV_(const ControlledVocabulary::CVTerm& c, const DataValue& metaValue) const;
389
391 bool validateCV_(const ControlledVocabulary::CVTerm& c, const std::string& path, const Internal::MzMLValidator& validator) const;
392
394 ControlledVocabulary::CVTerm getChildWithName_(const std::string& parent_accession, const std::string& name) const;
395
397
398 // MEMBERS
399
401 MapType* exp_{ nullptr };
402
404 const MapType* cexp_{ nullptr };
405
408
416 std::vector<BinaryData> bin_data_;
420 bool in_spectrum_list_{ false };
422 bool skip_spectrum_{ false };
424 Size spectra_read_{ 0 };
426 bool skip_chromatogram_{ false };
428 bool rt_set_{ false };
430 std::string current_id_;
432 std::map<std::string, std::vector<SemanticValidator::CVTerm> > ref_param_;
434 std::map<std::string, SourceFile> source_files_;
436 std::map<std::string, Sample> samples_;
438 std::map<std::string, Software> software_;
440 std::map<std::string, Instrument> instruments_;
443 mutable std::map<std::pair<std::string, std::string>, bool> cached_terms_;
445 std::map<std::string, std::vector< DataProcessingPtr > > processing_;
449 UInt selected_ion_count_{ 0 };
450
459 {
460 std::vector<BinaryData> data;
463 };
464
466 std::vector<SpectrumData> spectrum_data_;
467
481
483 std::vector<ChromatogramData> chromatogram_data_;
484
486
494 std::vector<std::pair<std::string, Int64> > spectra_offsets_;
495 std::vector<std::pair<std::string, Int64> > chromatograms_offsets_;
497
500
502 Interfaces::IMSDataConsumer* consumer_{ nullptr };
503
505 UInt scan_count_{ 0 };
506 UInt chromatogram_count_{ 0 };
507 Int scan_count_total_{ -1 };
508 Int chrom_count_total_{ -1 };
510
514
515 };
516
517 //--------------------------------------------------------------------------------
518
519 } // namespace Internal
520} // namespace OpenMS
521
Representation of controlled vocabulary mapping rules (for PSI formats)
Definition CVMappings.h:31
A 1-dimensional raw data point or peak for chromatograms.
Definition ChromatogramPeak.h:29
Definition ControlledVocabulary.h:31
Float data array class.
Definition DataArrays.h:25
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:33
Description of a MS instrument.
Definition Instrument.h:40
The interface of a consumer of spectra and chromatograms.
Definition IMSDataConsumer.h:46
Handler for mzML file format.
Definition MzMLHandler.h:93
void handleUserParam_(const std::string &parent_parent_tag, const std::string &parent_tag, const std::string &name, const std::string &type, const std::string &value, const std::string &unit_accession="")
Handles user terms.
ControlledVocabulary::CVTerm getChildWithName_(const std::string &parent_accession, const std::string &name) const
Helper method to look up a child CV term of parent_accession with the name name. If no such term is f...
Size default_array_length_
The default number of peaks in the current spectrum.
Definition MzMLHandler.h:418
ChromatogramType chromatogram_
The current chromatogram.
Definition MzMLHandler.h:414
void writeSoftware_(std::ostream &os, const std::string &id, const Software &software, const Internal::MzMLValidator &validator)
Helper method that writes a software.
MzMLHandler(const std::string &filename, const std::string &version, const ProgressLogger &logger)
delegated constructor for the two public versions
void requiredAttributeAsString_(std::string &value, const XMLAttributes &attributes, const char16_t *name) const
Populate all spectra on the stack with data from input.
void writeProduct_(std::ostream &os, const Product &product, const Internal::MzMLValidator &validator)
Helper method that write precursor information from spectra and chromatograms.
ChromatogramType chromatogram
Definition MzMLHandler.h:479
MSChromatogram ChromatogramType
Spectrum type.
Definition MzMLHandler.h:196
void writeInstrument_(std::ostream &os, const std::string &id, const Instrument &instrument, const std::string &software_id, const Internal::MzMLValidator &validator)
Write one instrument configuration, including its components.
std::map< std::string, SourceFile > source_files_
The source files: id => SourceFile.
Definition MzMLHandler.h:434
MzMLHandler(const MapType &exp, const std::string &filename, const std::string &version, const ProgressLogger &logger)
Constructor for a write-only handler.
LOADDETAIL getLoadDetail() const override
handler which support partial loading, implement this method
bool validateCV_(const ControlledVocabulary::CVTerm &c, const std::string &path, const Internal::MzMLValidator &validator) const
Helper method to validate if the given CV is allowed in the current location (path)
std::vector< ChromatogramData > chromatogram_data_
Vector of chromatogram data stored for later parallel processing.
Definition MzMLHandler.h:483
ParamBuffers_ param_buffers_
Definition MzMLHandler.h:219
const ControlledVocabulary & cv_
Controlled vocabulary (psi-ms from OpenMS/share/OpenMS/CV/psi-ms.obo)
Definition MzMLHandler.h:512
std::map< std::string, std::vector< SemanticValidator::CVTerm > > ref_param_
The referencing param groups: id => array (accession, value)
Definition MzMLHandler.h:432
~MzMLHandler() override
Destructor.
void writeTo(std::ostream &os) override
Docu in base class XMLHandler::writeTo.
std::string current_id_
Id of the current list. Used for referencing param group, source file, sample, software,...
Definition MzMLHandler.h:430
void endElement_(std::string_view tag_name)
Shared implementation of onEndElement() and tryOnEndElementUTF8()
std::vector< std::pair< std::string, Int64 > > chromatograms_offsets_
Stores binary offsets for each <chromatogram> tag.
Definition MzMLHandler.h:495
void writeUserParam_(std::ostream &os, const MetaInfoInterface &meta, UInt indent, const std::string &path, const Internal::MzMLValidator &validator, const std::set< std::string > &exclude={}) const
Writes user terms.
bool tryOnStartElementUTF8(std::string_view qname, const XMLAttributes &attributes) override
Handle the opening tag by its UTF-8 name; preserve derived UTF-16 overrides.
void writeHeader_(std::ostream &os, const MapType &exp, std::vector< std::vector< ConstDataProcessingPtr > > &dps, const Internal::MzMLValidator &validator)
Write out XML header including (everything up to spectrumList / chromatogramList.
void populateChromatogramsWithData_()
Populate all chromatograms on the stack with data from input.
std::vector< BinaryData > data
Definition MzMLHandler.h:460
std::vector< std::pair< std::string, Int64 > > spectra_offsets_
Stores binary offsets for each <spectrum> tag.
Definition MzMLHandler.h:494
CVMappings mapping_
Definition MzMLHandler.h:513
MzMLHandler(MapType &exp, const std::string &filename, const std::string &version, const ProgressLogger &logger)
Constructor for a read-only handler.
const ProgressLogger & logger_
Progress logger.
Definition MzMLHandler.h:499
void writeBinaryDataArray_(std::ostream &os, const PeakFileOptions &options, std::vector< DataType > &data, bool is32bit, std::string array_type)
Write a single <binaryDataArray> element to the output.
SpectrumType spec_
The current spectrum.
Definition MzMLHandler.h:412
void setLoadDetail(const LOADDETAIL d) override
handler which support partial loading, implement this method
void writeDataProcessing_(std::ostream &os, const std::string &id, const std::vector< ConstDataProcessingPtr > &dps, const Internal::MzMLValidator &validator)
Helper method that writes a data processing list.
MSSpectrum SpectrumType
Spectrum type.
Definition MzMLHandler.h:194
void setMSDataConsumer(Interfaces::IMSDataConsumer *consumer)
Set the IMSDataConsumer consumer which will consume the read data.
PeakFileOptions options_
Options that can be set for loading/storing.
Definition MzMLHandler.h:407
void getCounts(Size &spectra_counts, Size &chromatogram_counts)
Get the spectra and chromatogram counts of a file.
Size default_array_length
Definition MzMLHandler.h:461
MapType::PeakType PeakType
Peak type.
Definition MzMLHandler.h:190
std::vector< SpectrumData > spectrum_data_
Vector of spectrum data stored for later parallel processing.
Definition MzMLHandler.h:466
MzMLHandlerHelper::BinaryData BinaryData
Definition MzMLHandler.h:198
void populateSpectraWithData_(std::vector< MzMLHandlerHelper::BinaryData > &input_data, Size &length, const PeakFileOptions &peak_file_options, SpectrumType &spectrum)
Fill a single spectrum with data from input.
void writeBinaryFloatDataArray_(std::ostream &os, const PeakFileOptions &options, const OpenMS::DataArrays::FloatDataArray &array, const Size spec_chrom_idx, const Size array_idx, bool is_spectrum, const Internal::MzMLValidator &validator)
Write a single <binaryDataArray> element for a float data array to the output.
void writeSpectrum_(std::ostream &os, const SpectrumType &spec, Size spec_idx, const Internal::MzMLValidator &validator, bool renew_native_ids, std::vector< std::vector< ConstDataProcessingPtr > > &dps)
Write out a single spectrum.
void writeSourceFile_(std::ostream &os, const std::string &id, const SourceFile &software, const Internal::MzMLValidator &validator)
Helper method that writes a source file.
void handleCVParam_(const std::string &parent_parent_tag, const std::string &parent_tag, const std::string &accession, const std::string &name, const std::string &value, const std::string &unit_accession="")
Handles CV terms.
PeakFileOptions & getOptions()
Get the peak file options.
void fillChromatogramData_()
Fills the current chromatogram with data points and meta data.
void startElement_(std::string_view tag_name, const XMLAttributes &attributes)
Shared implementation of onStartElement() and tryOnStartElementUTF8()
std::string writeCV_(const ControlledVocabulary::CVTerm &c, const DataValue &metaValue) const
Helper method to write an CV based on a meta value.
void writeChromatogram_(std::ostream &os, const ChromatogramType &chromatogram, Size chrom_idx, const Internal::MzMLValidator &validator)
Write out a single chromatogram.
std::map< std::string, Sample > samples_
The sample list: id => Sample.
Definition MzMLHandler.h:436
std::vector< BinaryData > bin_data_
The spectrum data (or chromatogram data)
Definition MzMLHandler.h:416
std::map< std::string, Instrument > instruments_
The data processing list: id => Instrument.
Definition MzMLHandler.h:440
std::string default_processing_
id of the default data processing (used when no processing is defined)
Definition MzMLHandler.h:447
std::map< std::string, std::vector< DataProcessingPtr > > processing_
The data processing list: id => Instrument.
Definition MzMLHandler.h:445
void onStartElement(const char16_t *qname, const XMLAttributes &attributes) override
Docu in base class XMLHandler::startElelement.
bool tryOnEndElementUTF8(std::string_view qname) override
Handle the closing tag by its UTF-8 name; preserve derived UTF-16 overrides.
void onEndElement(const char16_t *qname) override
Docu in base class XMLHandler::endElement.
void writeContainerData_(std::ostream &os, const PeakFileOptions &pf_options_, const ContainerT &container, const std::string &array_type)
void setOptions(const PeakFileOptions &opt)
Set the peak file options.
std::map< std::string, Software > software_
The software list: id => Software.
Definition MzMLHandler.h:438
std::map< std::pair< std::string, std::string >, bool > cached_terms_
CV terms-path-combinations that have been checked in validateCV_()
Definition MzMLHandler.h:443
std::string accession
Definition MzMLHandler.h:217
void onCharacters(const char16_t *chars, Size length) override
Docu in base class XMLHandler::characters.
void populateChromatogramsWithData_(std::vector< MzMLHandlerHelper::BinaryData > &input_data, Size &length, const PeakFileOptions &peak_file_options, ChromatogramType &chromatogram)
Fill a single chromatogram with data from input.
MapType::ChromatogramPeakType ChromatogramPeakType
Chromatogram peak type.
Definition MzMLHandler.h:192
bool tryOnCharactersUTF8(const char *chars, Size length) override
Consume ASCII character data directly; preserve derived UTF-16 overrides.
void writePrecursor_(std::ostream &os, const Precursor &precursor, const Internal::MzMLValidator &validator)
Helper method that write precursor information from spectra and chromatograms.
SpectrumType spectrum
Definition MzMLHandler.h:462
std::string default_instrument_configuration_ref_
Definition MzMLHandler.h:441
Data necessary to generate a single chromatogram.
Definition MzMLHandler.h:476
Reused attribute buffers for cvParam/userParam elements (avoids allocating per element)
Definition MzMLHandler.h:216
Data necessary to generate a single spectrum.
Definition MzMLHandler.h:459
Semantic validator for mzML files.
Definition MzMLValidator.h:41
Non-owning attribute view for an XML start-element callback.
Definition XMLAttributes.h:29
Base class for XML handlers.
Definition XMLHandler.h:41
LOADDETAIL
Definition XMLHandler.h:64
The representation of a chromatogram.
Definition MSChromatogram.h:33
In-Memory representation of a mass spectrometry run.
Definition MSExperiment.h:49
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition MetaInfoInterface.h:35
A 1-dimensional raw data point or peak.
Definition Peak1D.h:30
Options for loading files containing peak data.
Definition PeakFileOptions.h:22
Precursor meta information.
Definition Precursor.h:37
Product meta information.
Definition Product.h:26
Base class for all classes that want to report their progress.
Definition ProgressLogger.h:28
Description of the software used for processing.
Definition Software.h:26
Description of a file location, used to store the origin of (meta) data.
Definition SourceFile.h:23
int Int
Signed integer type.
Definition Types.h:72
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
MSExperiment PeakMap
Two-dimensional map of raw data points or peaks.
Definition StandardTypes.h:35
IMSDataConsumer IMSDataConsumer
Definition IMSDataConsumer.h:103
MSChromatogram ChromatogramType
Definition MzDataHandler.h:34
MSSpectrum SpectrumType
Definition MzDataHandler.h:33
PeakMap MapType
XML handler for MzDataFile.
Definition MzDataHandler.h:32
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Representation of a CV term.
Definition ControlledVocabulary.h:52
Representation for binary data in mzML.
Definition MzMLHandlerHelper.h:45