OpenMS
Loading...
Searching...
No Matches
ArrowIOHelpers.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: Timo Sachsenberg $
7// --------------------------------------------------------------------------
8
9#pragma once
10
11#include <OpenMS/config.h>
14#include <OpenMS/FORMAT/MSExperimentArrowExport.h> // for ParquetWriteConfig
15
16#include <cstdint>
17#include <map>
18#include <memory>
19#include <optional>
20#include <string>
21#include <utility>
22#include <variant>
23#include <unordered_set>
24#include <vector>
25
26// Forward declarations
27namespace arrow
28{
29 class Array;
30 class KeyValueMetadata;
31 class Table;
32}
33
34namespace OpenMS
35{
36class ConsensusMap;
37class DataValue;
38class MetaInfoInterface;
39namespace MS1LabelState { struct Keys; }
40
50namespace ArrowIOHelpers
51{
59 OPENMS_DLLAPI std::string generateUuidV4();
60
73 OPENMS_DLLAPI Size tableRowCount(const std::shared_ptr<arrow::Table>& table);
74
83 OPENMS_DLLAPI bool writeTableToParquet(
84 const std::shared_ptr<arrow::Table>& table,
85 const std::string& filename,
86 const ParquetWriteConfig& config = ParquetWriteConfig{});
87
93 struct OPENMS_DLLAPI ParquetColumn
94 {
96 struct Score
97 {
98 std::string name;
99 double value = 0.0;
100 std::optional<bool> higher_better;
101 };
104 enum class Type { INT16, INT64, UTF8, FLOAT_LIST, UTF8_LIST, SCORE_LIST, INT32, BOOL, DOUBLE, STRUCT_LIST, FLOAT, INT32_LIST };
108 {
109 StructField() = default;
110 StructField(std::string field_name, Type field_type, bool field_nullable = true, std::vector<StructField> struct_fields = {}) :
111 name(std::move(field_name)), type(field_type), nullable(field_nullable), fields(std::move(struct_fields)) {}
112 std::string name;
113 Type type = Type::UTF8;
114 bool nullable = true;
115 std::vector<StructField> fields;
116 };
117 struct StructValue;
119 using StructEntry = std::vector<StructValue>;
122 struct OPENMS_DLLAPI StructValue
123 {
124 std::variant<std::monostate, std::string, Int32, bool, double, std::vector<StructEntry>> value;
125 StructValue() = default;
126 StructValue(std::string v) : value(std::move(v)) {}
127 StructValue(const char* v) : value(std::string(v)) {}
128 StructValue(Int32 v) : value(v) {}
129 StructValue(bool v) : value(v) {}
130 StructValue(double v) : value(v) {}
131 StructValue(std::vector<StructEntry> v) : value(std::move(v)) {}
132 template <typename T> StructValue(const std::optional<T>& v) { if (v) { *this = StructValue(*v); } }
133 };
134 std::string name;
135 Type type = Type::INT64;
137 bool nullable = true;
138 std::vector<Int64> int64_values;
139 std::vector<double> double_values;
140 std::vector<std::string> utf8_values;
141 std::vector<std::vector<float>> float_lists;
142 std::vector<std::vector<Int32>> int32_lists;
143 std::vector<std::vector<std::string>> utf8_lists;
144 std::vector<std::vector<Score>> score_lists;
145 std::vector<StructField> struct_fields;
146 std::vector<std::vector<StructEntry>> struct_lists;
147 std::vector<bool> null_rows;
148 };
149
162 OPENMS_DLLAPI std::shared_ptr<arrow::Table> withColumns(
163 const std::shared_ptr<arrow::Table>& table,
164 const std::vector<ParquetColumn>& columns);
165
175 OPENMS_DLLAPI std::shared_ptr<arrow::Table> tableOfColumns(
176 const std::vector<ParquetColumn>& columns,
177 Size rows);
178
195 const std::vector<std::shared_ptr<arrow::Table>>& tables,
196 const std::string& filename,
197 const ParquetWriteConfig& config = ParquetWriteConfig{},
198 const std::shared_ptr<const arrow::KeyValueMetadata>& metadata = nullptr);
199
200 // ---------------------------------------------------------------------------
201 // QPX file metadata
202 // ---------------------------------------------------------------------------
203
226 OPENMS_DLLAPI std::shared_ptr<const arrow::KeyValueMetadata> qpxFileMetadata(
227 const std::string& file_type,
228 const ParquetWriteConfig& config = ParquetWriteConfig{},
229 const std::map<std::string, std::string>& extra = {});
230
240 OPENMS_DLLAPI std::string qpxScanFormat(const std::string& native_id);
241
249 OPENMS_DLLAPI std::string qpxScanFormat(const std::vector<std::string>& native_ids);
250
261 OPENMS_DLLAPI std::string qpxRunFileName(const std::string& ms_run_path);
262
279 OPENMS_DLLAPI std::vector<Int32> qpxScanComponents(const std::string& spectrum_reference);
280
297
318 OPENMS_DLLAPI std::string qpxPsmRunFileName(const MetaInfoInterface& hit,
319 const MetaInfoInterface& identification,
320 const std::string& resolved_run_file,
321 const QPXRunFileNameKeys& keys);
322
336 OPENMS_DLLAPI bool qpxWarnOnRunNameCollisions(
337 const std::string& context,
338 const std::vector<std::string>& ms_run_paths);
339
340
363 OPENMS_DLLAPI std::string qpxIntensityLabel(
364 const std::string& column_label,
365 const std::string& channel_name);
366
381 OPENMS_DLLAPI std::map<std::uint64_t, std::string> qpxIntensityLabels(
382 const ConsensusMap& cmap);
383
394 OPENMS_DLLAPI bool qpxIsCanonicalIntensityLabel(const std::string& label);
395
410 OPENMS_DLLAPI std::vector<std::pair<std::string, std::string>> qpxCvParams(const MetaInfoInterface& hit);
411
413 OPENMS_DLLAPI std::vector<std::pair<std::string, std::string>> qpxCvParams(const MetaInfoInterface& hit,
414 const MS1LabelState::Keys& keys);
415
416 // ---------------------------------------------------------------------------
417 // Read helpers
418 // ---------------------------------------------------------------------------
419
426 OPENMS_DLLAPI std::shared_ptr<arrow::Array> getColumn(
427 const std::shared_ptr<arrow::Table>& table,
428 const std::string& name,
429 bool required = true);
430
432 OPENMS_DLLAPI std::string getStringValue(
433 const std::shared_ptr<arrow::Array>& array,
434 int64_t row);
435
437 OPENMS_DLLAPI double getDoubleValue(
438 const std::shared_ptr<arrow::Array>& array,
439 int64_t row,
440 double default_val = 0.0);
441
443 OPENMS_DLLAPI float getFloatValue(
444 const std::shared_ptr<arrow::Array>& array,
445 int64_t row,
446 float default_val = 0.0f);
447
449 OPENMS_DLLAPI int32_t getInt32Value(
450 const std::shared_ptr<arrow::Array>& array,
451 int64_t row,
452 int32_t default_val = 0);
453
455 OPENMS_DLLAPI int64_t getInt64Value(
456 const std::shared_ptr<arrow::Array>& array,
457 int64_t row,
458 int64_t default_val = 0);
459
461 OPENMS_DLLAPI bool getBoolValue(
462 const std::shared_ptr<arrow::Array>& array,
463 int64_t row,
464 bool default_val = false);
465
467 OPENMS_DLLAPI bool isNull(
468 const std::shared_ptr<arrow::Array>& array,
469 int64_t row);
470
478 OPENMS_DLLAPI std::string metaValueString(const DataValue& value);
479
486 OPENMS_DLLAPI void readMetaValues(
487 const std::shared_ptr<arrow::Array>& array,
488 int64_t row,
489 MetaInfoInterface& target,
490 const std::unordered_set<std::string>& excluded_keys = {});
491}
492
493} // namespace OpenMS
A container for consensus elements.
Definition ConsensusMap.h:67
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:33
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition MetaInfoInterface.h:35
int32_t Int32
Signed integer type (32bit)
Definition Types.h:26
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
std::string qpxPsmRunFileName(const MetaInfoInterface &hit, const MetaInfoInterface &identification, const std::string &resolved_run_file, const QPXRunFileNameKeys &keys)
The QPX run_file_name the psm view writes for one identification and hit.
int32_t getInt32Value(const std::shared_ptr< arrow::Array > &array, int64_t row, int32_t default_val=0)
Read an int32 at row, or default_val if null.
bool writeTableToParquet(const std::shared_ptr< arrow::Table > &table, const std::string &filename, const ParquetWriteConfig &config=ParquetWriteConfig{})
Write an Arrow table to a Parquet file.
int64_t getInt64Value(const std::shared_ptr< arrow::Array > &array, int64_t row, int64_t default_val=0)
Read an int64 at row, or default_val if null.
bool getBoolValue(const std::shared_ptr< arrow::Array > &array, int64_t row, bool default_val=false)
Read a bool at row, or default_val if null.
std::vector< Int32 > qpxScanComponents(const std::string &spectrum_reference)
The QPX scan column value for one spectrum reference.
float getFloatValue(const std::shared_ptr< arrow::Array > &array, int64_t row, float default_val=0.0f)
Read a float at row, or default_val if null.
std::vector< std::pair< std::string, std::string > > qpxCvParams(const MetaInfoInterface &hit)
The meta values of an identification that QPX carries as cv_params.
double getDoubleValue(const std::shared_ptr< arrow::Array > &array, int64_t row, double default_val=0.0)
Read a double at row, or default_val if null.
std::string generateUuidV4()
Generate a lowercase hyphenated RFC 4122 version-4 UUID string.
void readMetaValues(const std::shared_ptr< arrow::Array > &array, int64_t row, MetaInfoInterface &target, const std::unordered_set< std::string > &excluded_keys={})
Read metavalues from a list<struct{name,value,value_type}> column.
std::shared_ptr< arrow::Array > getColumn(const std::shared_ptr< arrow::Table > &table, const std::string &name, bool required=true)
Fetch a named column from a table, combining chunks if needed.
std::string qpxIntensityLabel(const std::string &column_label, const std::string &channel_name)
QPX intensities[].label for a ConsensusMap column header.
std::string qpxRunFileName(const std::string &ms_run_path)
Reduce an MS run path to the QPX run_file_name form.
std::string getStringValue(const std::shared_ptr< arrow::Array > &array, int64_t row)
Read a string at row, or "" if null/out-of-bounds.
bool qpxIsCanonicalIntensityLabel(const std::string &label)
Test whether a string is in the canonical SDRF/QPX intensity-label vocabulary.
std::shared_ptr< arrow::Table > tableOfColumns(const std::vector< ParquetColumn > &columns, Size rows)
A table of columns (in their order, without schema metadata), for callers outside libOpenMS that cann...
std::string qpxScanFormat(const std::string &native_id)
Classify a spectrum native ID into a QPX scan_format token.
std::shared_ptr< arrow::Table > withColumns(const std::shared_ptr< arrow::Table > &table, const std::vector< ParquetColumn > &columns)
The table with columns added: a column of the name of an existing one replaces it (in its place),...
bool concatenateAndWriteToParquet(const std::vector< std::shared_ptr< arrow::Table > > &tables, const std::string &filename, const ParquetWriteConfig &config=ParquetWriteConfig{}, const std::shared_ptr< const arrow::KeyValueMetadata > &metadata=nullptr)
Concatenate a vector of Arrow tables and write the result to a Parquet file.
std::shared_ptr< const arrow::KeyValueMetadata > qpxFileMetadata(const std::string &file_type, const ParquetWriteConfig &config=ParquetWriteConfig{}, const std::map< std::string, std::string > &extra={})
Build the canonical QPX file-level key-value metadata.
bool qpxWarnOnRunNameCollisions(const std::string &context, const std::vector< std::string > &ms_run_paths)
Warn once per export about source files that collapse onto one run_file_name.
bool isNull(const std::shared_ptr< arrow::Array > &array, int64_t row)
Whether array is null at row (or unset)
Size tableRowCount(const std::shared_ptr< arrow::Table > &table)
Number of rows in an Arrow table, or 0 when it is null.
std::map< std::uint64_t, std::string > qpxIntensityLabels(const ConsensusMap &cmap)
Derive canonical QPX intensity labels for every ConsensusMap column.
std::string metaValueString(const DataValue &value)
The text a metavalue is written as in a list<struct{name,value,value_type}> column.
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Definition ArrowIOHelpers.h:28
STL namespace.
std::vector< StructField > fields
STRUCT_LIST: the fields of its struct.
Definition ArrowIOHelpers.h:115
StructField(std::string field_name, Type field_type, bool field_nullable=true, std::vector< StructField > struct_fields={})
Definition ArrowIOHelpers.h:110
std::string name
Definition ArrowIOHelpers.h:112
std::variant< std::monostate, std::string, Int32, bool, double, std::vector< StructEntry > > value
Definition ArrowIOHelpers.h:124
StructValue(Int32 v)
Definition ArrowIOHelpers.h:128
StructValue(const char *v)
(not a bool)
Definition ArrowIOHelpers.h:127
StructValue(const std::optional< T > &v)
Definition ArrowIOHelpers.h:132
StructValue(bool v)
Definition ArrowIOHelpers.h:129
StructValue(std::string v)
Definition ArrowIOHelpers.h:126
StructValue(double v)
Definition ArrowIOHelpers.h:130
StructValue(std::vector< StructEntry > v)
Definition ArrowIOHelpers.h:131
One column for withColumns(), built without Arrow types.
Definition ArrowIOHelpers.h:94
std::vector< std::vector< StructEntry > > struct_lists
STRUCT_LIST: per row its entries.
Definition ArrowIOHelpers.h:146
std::vector< std::vector< std::string > > utf8_lists
UTF8_LIST.
Definition ArrowIOHelpers.h:143
Type
Definition ArrowIOHelpers.h:104
std::vector< Int64 > int64_values
INT16 and INT32 (narrowed, range-checked), INT64, BOOL (0: false)
Definition ArrowIOHelpers.h:138
std::vector< bool > null_rows
true marks a null row (empty: no null row)
Definition ArrowIOHelpers.h:147
std::vector< StructValue > StructEntry
An entry of a STRUCT_LIST: one value per field.
Definition ArrowIOHelpers.h:119
std::optional< bool > higher_better
none: unknown (null)
Definition ArrowIOHelpers.h:100
std::vector< double > double_values
DOUBLE, FLOAT (narrowed)
Definition ArrowIOHelpers.h:139
std::string name
Definition ArrowIOHelpers.h:98
std::vector< std::vector< Score > > score_lists
SCORE_LIST.
Definition ArrowIOHelpers.h:144
std::vector< std::string > utf8_values
UTF8.
Definition ArrowIOHelpers.h:140
std::vector< std::vector< Int32 > > int32_lists
INT32_LIST.
Definition ArrowIOHelpers.h:142
std::vector< std::vector< float > > float_lists
FLOAT_LIST.
Definition ArrowIOHelpers.h:141
std::vector< StructField > struct_fields
STRUCT_LIST: the fields of its struct.
Definition ArrowIOHelpers.h:145
An entry of a SCORE_LIST row: the struct of the QPX additional_scores (QPXPSMSchema::additionalScores...
Definition ArrowIOHelpers.h:97
MetaInfo indices qpxPsmRunFileName() consults, resolved once.
Definition ArrowIOHelpers.h:289
QPXRunFileNameKeys()
Resolve the keys against the current registry.
UInt run_file_name
Index of run_file_name, or UInt(-1) when it is not registered.
Definition ArrowIOHelpers.h:295
UInt reference_file_name
Index of reference_file_name, or UInt(-1) when it is not registered.
Definition ArrowIOHelpers.h:293
The registry indices of the three keys, resolved once for a loop over many hits.
Definition MS1LabelState.h:65
Configuration for Parquet file writing.
Definition MSExperimentArrowExport.h:136