OpenMS
Loading...
Searching...
No Matches
ImzMLHandler.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: Aditya Sarna $
7// --------------------------------------------------------------------------
8
9#pragma once
10
14
16
17#include <cstdio>
18#include <cstdint>
19#include <memory>
20#include <string>
21#include <unordered_map>
22#include <vector>
23
24namespace OpenMS
25{
26namespace Internal
27{
28
29 class ImzMLInterceptConsumer; // defined in ImzMLHandler.cpp
30
63 class OPENMS_DLLAPI ImzMLHandler final : public MzMLHandler
64 {
65 friend class ImzMLInterceptConsumer;
66
67 public:
68
77 const std::string& filename,
78 const ProgressLogger& logger);
79
80 ~ImzMLHandler() override;
81
82 ImzMLHandler(const ImzMLHandler&) = delete;
84
95 void openIBD(const std::string& ibd_path);
96
98 const ImzMLMeta& getImzMLMeta() const noexcept { return meta_; }
99
101 ImzMLMeta& getImzMLMeta() noexcept { return meta_; }
102
104 bool hasPixelSizeY() const noexcept { return pixel_size_y_given_; }
105
107 const std::vector<ImzMLSpectrumIndex>& getIndex() const noexcept { return index_; }
108
118 bool append_spectra_to_map,
119 bool decode_ibd = true);
120
121 // ------------------------------------------------------------------
122 // XML event callbacks (MzMLHandler is called through for MS:* terms)
123 // ------------------------------------------------------------------
124
125 void onStartElement(const char16_t* qname, const XMLAttributes& attrs) override;
126
127 void onEndElement(const char16_t* qname) override;
128
129 private:
130
131 // ------------------------------------------------------------------
132 // IMS CV dispatch
133 // ------------------------------------------------------------------
134 void handleIMSCvParam_(const std::string& acc, const std::string& name, const std::string& val,
135 const std::string& unit_accession = "");
136 void applyRefGroup_ (const std::string& id);
137
138 // ------------------------------------------------------------------
139 // Per-binaryDataArray IMS metadata
140 // ------------------------------------------------------------------
142 {
143 ImzMLSpectrumIndex::DataType dt { ImzMLSpectrumIndex::DataType::UNKNOWN };
144 bool is_mz { false };
145 bool is_int { false };
146 bool is_ext { false };
147 uint64_t offset { 0 };
148 uint64_t count { 0 };
149 uint64_t encoded_bytes { 0 };
151 bool compressed { false };
153 std::string accession;
156 std::string name;
158 std::string unit_accession;
159
160 void reset() noexcept { *this = ArrayMeta{}; }
161 };
162
163 // ------------------------------------------------------------------
164 // Per-spectrum IMS state snapshot (stored at endElement("spectrum"))
165 // ------------------------------------------------------------------
166 struct SpecIMS
167 {
168 uint32_t x {0}, y {0}, z {1};
172 std::vector<ArrayMeta> aux_meta;
174 std::vector<std::string> inline_aux_names;
176 bool has_scan_start_time {false};
177 };
178
179 // ------------------------------------------------------------------
180 // State
181 // ------------------------------------------------------------------
182 FILE* ibd_ { nullptr };
184
185 bool in_spectrum_ { false };
186 bool in_scan_ { false };
187 bool in_bda_ { false };
188 uint32_t cur_x_ { 0 };
189 uint32_t cur_y_ { 0 };
190 uint32_t cur_z_ { 1 };
194 std::vector<ArrayMeta> cur_aux_metas_;
195 std::vector<std::string> cur_inline_aux_names_;
196
197 std::vector<SpecIMS> spec_ims_;
198 std::vector<ImzMLSpectrumIndex> index_;
199
201 struct CvEntry
202 {
203 std::string accession;
204 std::string name;
205 std::string value;
206 std::string unit_accession;
207 };
208 std::unordered_map<std::string, std::vector<CvEntry>> ref_groups_;
209 std::string cur_ref_id_;
210 bool in_ref_group_ { false };
211 bool decode_ibd_ { true };
212 bool cur_x_seen_ { false };
213 bool cur_y_seen_ { false };
214 bool pixel_size_y_given_ { false };
215
216 std::unique_ptr<ImzMLInterceptConsumer> decode_bridge_;
217 };
218
219} // namespace Internal
220} // namespace OpenMS
The interface of a consumer of spectra and chromatograms.
Definition IMSDataConsumer.h:46
XML event handler for imzML 1.1.0 files, extending OpenMS MzMLHandler.
Definition ImzMLHandler.h:64
ImzMLMeta & getImzMLMeta() noexcept
Non-const access for ImzMLFile to set the .ibd path before parse.
Definition ImzMLHandler.h:101
ImzMLMeta meta_
Definition ImzMLHandler.h:183
ImzMLHandler(const ImzMLHandler &)=delete
void onStartElement(const char16_t *qname, const XMLAttributes &attrs) override
Parsing method for opening tags.
ImzMLHandler(PeakMap &exp, const std::string &filename, const ProgressLogger &logger)
Construct an ImzMLHandler.
void applyRefGroup_(const std::string &id)
std::vector< SpecIMS > spec_ims_
IMS state per spectrum (document order)
Definition ImzMLHandler.h:197
void openIBD(const std::string &ibd_path)
Open the companion .ibd file for random-access binary reads.
ArrayMeta int_meta
Definition ImzMLHandler.h:170
bool hasPixelSizeY() const noexcept
True if the file has a pixel size y (IMS:1000047), even if its value or unit was rejected.
Definition ImzMLHandler.h:104
std::unordered_map< std::string, std::vector< CvEntry > > ref_groups_
Definition ImzMLHandler.h:208
std::vector< ArrayMeta > cur_aux_metas_
Definition ImzMLHandler.h:194
std::string value
Definition ImzMLHandler.h:205
const std::vector< ImzMLSpectrumIndex > & getIndex() const noexcept
Per-spectrum binary index built during parse (one entry per spectrum).
Definition ImzMLHandler.h:107
std::string unit_accession
Definition ImzMLHandler.h:206
std::vector< ImzMLSpectrumIndex > index_
Full index for OnDiscImzMLExperiment.
Definition ImzMLHandler.h:198
ArrayMeta cur_mz_meta_
Definition ImzMLHandler.h:192
ArrayMeta cur_int_meta_
Definition ImzMLHandler.h:193
const ImzMLMeta & getImzMLMeta() const noexcept
Imaging metadata accumulated during SAX parse (read after parse_() returns).
Definition ImzMLHandler.h:98
ArrayMeta cur_array_
Definition ImzMLHandler.h:191
~ImzMLHandler() override
Closes the .ibd FILE* if open.
void connectDecodeConsumer(Interfaces::IMSDataConsumer *downstream, bool append_spectra_to_map, bool decode_ibd=true)
Wire ImzMLInterceptConsumer between MzMLHandler and an optional downstream consumer.
std::vector< std::string > inline_aux_names
Non-external aux arrays seen while peaks come from the .ibd (warned and dropped).
Definition ImzMLHandler.h:174
std::string name
Definition ImzMLHandler.h:204
std::string cur_ref_id_
Definition ImzMLHandler.h:209
ImzMLHandler & operator=(const ImzMLHandler &)=delete
std::vector< ArrayMeta > aux_meta
Auxiliary external arrays (ion mobility, SNR, …) in document order.
Definition ImzMLHandler.h:172
void handleIMSCvParam_(const std::string &acc, const std::string &name, const std::string &val, const std::string &unit_accession="")
void onEndElement(const char16_t *qname) override
Parsing method for closing tags.
std::vector< std::string > cur_inline_aux_names_
Definition ImzMLHandler.h:195
std::unique_ptr< ImzMLInterceptConsumer > decode_bridge_
Definition ImzMLHandler.h:216
ArrayMeta mz_meta
Definition ImzMLHandler.h:169
std::string accession
Definition ImzMLHandler.h:203
A cvParam captured inside a referenceableParamGroup.
Definition ImzMLHandler.h:202
Definition ImzMLHandler.h:167
Handler for mzML file format.
Definition MzMLHandler.h:93
Non-owning attribute view for an XML start-element callback.
Definition XMLAttributes.h:29
In-Memory representation of a mass spectrometry run.
Definition MSExperiment.h:49
Base class for all classes that want to report their progress.
Definition ProgressLogger.h:27
Dataset-level metadata for imzML mass spectrometry imaging files.
Definition ImzMLHandlerHelper.h:38
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
DataType
Scalar type identifier for binary array elements.
Definition ImzMLHandlerHelper.h:121
Definition ImzMLHandler.h:142
void reset() noexcept
Definition ImzMLHandler.h:160
std::string unit_accession
unitAccession on the array-identity cvParam (copied to FloatDataArray as unit_accession).
Definition ImzMLHandler.h:158
std::string name
Definition ImzMLHandler.h:156
std::string accession
PSI-MS accession of the array type (child of MS:1000513, or MS:1000786).
Definition ImzMLHandler.h:153