OpenMS
Loading...
Searching...
No Matches
ImzMLHandlerHelper.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
11#include <OpenMS/OpenMSConfig.h>
13
14#include <cstdint>
15#include <cstdio>
16#include <optional>
17#include <string>
18#include <vector>
19
20namespace OpenMS
21{
22 class MSSpectrum;
23
37 struct OPENMS_DLLAPI ImzMLMeta
38 {
39 // -------------------------------------------------------------------------
40 // Imaging geometry
41 // -------------------------------------------------------------------------
42
43 uint32_t max_count_x {0};
44 uint32_t max_count_y {0};
45 uint32_t max_count_z {1};
46 double pixel_size_x {-1};
47 double pixel_size_y {-1};
48 std::string pixel_size_unit {"unknown"};
49 double max_dim_x {0};
50 double max_dim_y {0};
51
52 // -------------------------------------------------------------------------
53 // Imaging mode
54 // -------------------------------------------------------------------------
55
57 std::string imaging_mode;
58
59 // -------------------------------------------------------------------------
60 // IBD companion file
61 // -------------------------------------------------------------------------
62
63 std::string ibd_file_path;
64 std::string ibd_sha1;
65 std::string ibd_md5;
66 std::string uuid;
67
68 // -------------------------------------------------------------------------
69 // Array data types (first occurrence, dataset-level summary)
70 // -------------------------------------------------------------------------
71
72 std::string mz_data_type;
73 std::string int_data_type;
74
75 // -------------------------------------------------------------------------
76 // Acquisition geometry
77 // -------------------------------------------------------------------------
78
79 std::string scan_pattern;
80 std::string scan_direction;
81 std::string line_scan_direction;
82 std::string polarity;
83 };
84
96 OPENMS_DLLAPI std::optional<int> lengthUnitExponent(const std::string& unit_accession);
97
98
118 struct OPENMS_DLLAPI ImzMLSpectrumIndex
119 {
121 enum class DataType : uint8_t { FLOAT32, FLOAT64, INT32, INT64, UNKNOWN };
122
130 struct AuxArray
131 {
132 std::string name;
133 std::string accession;
134 std::string unit_accession;
135 uint64_t offset {0};
136 uint64_t length {0};
137 uint64_t encoded_bytes {0};
138 DataType type {DataType::UNKNOWN};
139 bool compressed {false};
140 };
141
142 int32_t index {0};
143 uint32_t x {0};
144 uint32_t y {0};
145 uint32_t z {1};
146 uint64_t mz_offset {0};
147 uint64_t mz_length {0};
148 DataType mz_type {DataType::UNKNOWN};
149 bool mz_compressed {false};
150 uint64_t int_offset {0};
151 uint64_t int_length {0};
152 DataType int_type {DataType::UNKNOWN};
153 bool int_compressed {false};
154 std::vector<AuxArray> aux;
155 };
156
162 class OPENMS_DLLAPI ImzMLBinaryIO
163 {
164 public:
177 static void readMzArray(FILE* ibd,
178 uint64_t offset,
179 uint64_t count,
181 std::vector<double>& out,
182 const std::string& ibd_path);
183
189 static void readIntArray(FILE* ibd,
190 uint64_t offset,
191 uint64_t count,
193 std::vector<float>& out,
194 const std::string& ibd_path);
195
220 static void readAuxArray(FILE* ibd,
221 uint64_t offset,
222 uint64_t count,
224 std::vector<float>& out,
225 const std::string& ibd_path,
226 const std::string& array_name);
227
233 static void writeFloat32Array(FILE* ibd,
234 const float* data,
235 uint64_t count,
236 const std::string& ibd_path);
237
243 static void writeMzAsFloat32(FILE* ibd,
244 const std::vector<double>& mz,
245 const std::string& ibd_path);
246
252 static void writeFloat64Array(FILE* ibd,
253 const double* data,
254 uint64_t count,
255 const std::string& ibd_path);
256
262 static void writeMzAsFloat64(FILE* ibd,
263 const std::vector<double>& mz,
264 const std::string& ibd_path);
265 };
266
273 class OPENMS_DLLAPI ImzMLSpectrumRules
274 {
275 public:
286 static double scanStartTime(const MSSpectrum& s);
287
296 static bool isLoadableAuxArray(const std::string& name, uint64_t length,
297 ImzMLSpectrumIndex::DataType type, uint64_t n_peaks,
298 bool warn = false, uint32_t x = 0, uint32_t y = 0, uint32_t z = 0);
299
305 static void applyIMPeakType(MSSpectrum& s, bool has_im_array);
306
314 static void attachAuxArray(MSSpectrum& s, const std::string& name, const std::string& unit_accession,
315 const std::vector<float>& values);
316
319 };
320
321} // namespace OpenMS
Shared .ibd binary array decode utilities for imzML handlers.
Definition ImzMLHandlerHelper.h:163
static void readMzArray(FILE *ibd, uint64_t offset, uint64_t count, ImzMLSpectrumIndex::DataType dt, std::vector< double > &out, const std::string &ibd_path)
Read an m/z array from the companion .ibd file.
static void readAuxArray(FILE *ibd, uint64_t offset, uint64_t count, ImzMLSpectrumIndex::DataType dt, std::vector< float > &out, const std::string &ibd_path, const std::string &array_name)
Read an auxiliary (non-m/z, non-intensity) array from the .ibd file.
static void writeFloat32Array(FILE *ibd, const float *data, uint64_t count, const std::string &ibd_path)
Write a float32 array to the companion .ibd file (little-endian).
static void writeMzAsFloat32(FILE *ibd, const std::vector< double > &mz, const std::string &ibd_path)
Write m/z values as float32 to the companion .ibd file.
static void writeMzAsFloat64(FILE *ibd, const std::vector< double > &mz, const std::string &ibd_path)
Write m/z values as float64 to the companion .ibd file.
static void readIntArray(FILE *ibd, uint64_t offset, uint64_t count, ImzMLSpectrumIndex::DataType dt, std::vector< float > &out, const std::string &ibd_path)
Read an intensity array from the companion .ibd file.
static void writeFloat64Array(FILE *ibd, const double *data, uint64_t count, const std::string &ibd_path)
Write a float64 array to the companion .ibd file (little-endian).
Per-spectrum rules shared by every imzML access path (in-memory, streaming, metadata-only,...
Definition ImzMLHandlerHelper.h:274
static void attachAuxArray(MSSpectrum &s, const std::string &name, const std::string &unit_accession, const std::vector< float > &values)
Attach the values of an external auxiliary array read from the .ibd.
static double scanStartTime(const MSSpectrum &s)
Acquisition (scan start) time of an imaging spectrum in seconds.
static void pruneEmptyDataArrays(MSSpectrum &s)
Drop the empty Float/Integer/StringDataArrays left by MzMLHandler for aux arrays that were not filled...
static bool isLoadableAuxArray(const std::string &name, uint64_t length, ImzMLSpectrumIndex::DataType type, uint64_t n_peaks, bool warn=false, uint32_t x=0, uint32_t y=0, uint32_t z=0)
Whether an external auxiliary array is read from the .ibd for a spectrum with n_peaks peaks (it needs...
static void applyIMPeakType(MSSpectrum &s, bool has_im_array)
Set the IM peak type after the auxiliary arrays are known: with an ion mobility array,...
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
@ UNKNOWN
Unknown or unrecognized ion type.
std::string int_data_type
same set as mz_data_type
Definition ImzMLHandlerHelper.h:73
std::string scan_pattern
"top down" | "bottom up" (IMS:1000401/402)
Definition ImzMLHandlerHelper.h:79
std::string mz_data_type
"float32" | "float64" | "int32" | "int64"
Definition ImzMLHandlerHelper.h:72
std::string line_scan_direction
"left-right" | "right-left" (IMS:1000491/492)
Definition ImzMLHandlerHelper.h:81
std::string polarity
"positive" | "negative" (MS:1000130/129)
Definition ImzMLHandlerHelper.h:82
std::string uuid
Dataset UUID (IMS:1000080)
Definition ImzMLHandlerHelper.h:66
std::string imaging_mode
"continuous" (shared m/z array) or "processed" (per-spectrum m/z).
Definition ImzMLHandlerHelper.h:57
std::string scan_direction
"flyback" | "meander" | "horizontal" | "vertical"
Definition ImzMLHandlerHelper.h:80
std::string ibd_md5
MD5 checksum (IMS:1000090), empty if absent.
Definition ImzMLHandlerHelper.h:65
std::string ibd_file_path
Absolute path to the .ibd file.
Definition ImzMLHandlerHelper.h:63
std::string ibd_sha1
SHA-1 checksum (IMS:1000091), empty if absent.
Definition ImzMLHandlerHelper.h:64
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
std::optional< int > lengthUnitExponent(const std::string &unit_accession)
SI exponent of the UO length unit unit_accession, i.e. its power of ten relative to meter.
Per-spectrum binary index entry for an imzML dataset.
Definition ImzMLHandlerHelper.h:119
std::vector< AuxArray > aux
Extra external arrays (IM, …) in document order.
Definition ImzMLHandlerHelper.h:154
std::string unit_accession
unitAccession on the array-identity cvParam (may be empty)
Definition ImzMLHandlerHelper.h:134
DataType
Scalar type identifier for binary array elements.
Definition ImzMLHandlerHelper.h:121
std::string name
Ontology / free-text array name (FloatDataArray::getName)
Definition ImzMLHandlerHelper.h:132
std::string accession
MS accession (e.g. MS:1003006), may be empty.
Definition ImzMLHandlerHelper.h:133
Index entry for one auxiliary (non-m/z, non-intensity) external array.
Definition ImzMLHandlerHelper.h:131