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
215 OPENMS_DLLAPI bool validateQPXTable(
216 const std::shared_ptr<arrow::Table>& table,
217 const std::string& filename,
218 const ParquetWriteConfig& config = ParquetWriteConfig{},
219 const std::shared_ptr<const arrow::KeyValueMetadata>& metadata = nullptr);
220
236 class OPENMS_DLLAPI ParquetTableStream
237 {
238 public:
241 ParquetTableStream(std::string filename, const ParquetWriteConfig& config,
242 std::shared_ptr<const arrow::KeyValueMetadata> metadata);
246
250 bool append(const std::shared_ptr<arrow::Table>& table, int threads = 0);
252 bool close(const std::map<std::string, std::string>& keys = {});
254 void abandon();
256 Size rows() const;
258 bool failed() const;
260 bool closed() const;
262 const std::string& filename() const;
263
264 private:
265 struct Impl;
266 std::unique_ptr<Impl> impl_;
267 };
268
269 // ---------------------------------------------------------------------------
270 // QPX file metadata
271 // ---------------------------------------------------------------------------
272
295 OPENMS_DLLAPI std::shared_ptr<const arrow::KeyValueMetadata> qpxFileMetadata(
296 const std::string& file_type,
297 const ParquetWriteConfig& config = ParquetWriteConfig{},
298 const std::map<std::string, std::string>& extra = {});
299
309 OPENMS_DLLAPI std::string qpxScanFormat(const std::string& native_id);
310
318 OPENMS_DLLAPI std::string qpxScanFormat(const std::vector<std::string>& native_ids);
319
322 OPENMS_DLLAPI std::string qpxScanFormat(const std::vector<std::string>& native_ids, int threads);
323
334 OPENMS_DLLAPI std::string qpxRunFileName(const std::string& ms_run_path);
335
352 OPENMS_DLLAPI std::vector<Int32> qpxScanComponents(const std::string& spectrum_reference);
353
370
391 OPENMS_DLLAPI std::string qpxPsmRunFileName(const MetaInfoInterface& hit,
392 const MetaInfoInterface& identification,
393 const std::string& resolved_run_file,
394 const QPXRunFileNameKeys& keys);
395
409 OPENMS_DLLAPI bool qpxWarnOnRunNameCollisions(
410 const std::string& context,
411 const std::vector<std::string>& ms_run_paths);
412
413
436 OPENMS_DLLAPI std::string qpxIntensityLabel(
437 const std::string& column_label,
438 const std::string& channel_name);
439
454 OPENMS_DLLAPI std::map<std::uint64_t, std::string> qpxIntensityLabels(
455 const ConsensusMap& cmap);
456
467 OPENMS_DLLAPI bool qpxIsCanonicalIntensityLabel(const std::string& label);
468
483 OPENMS_DLLAPI std::vector<std::pair<std::string, std::string>> qpxCvParams(const MetaInfoInterface& hit);
484
486 OPENMS_DLLAPI std::vector<std::pair<std::string, std::string>> qpxCvParams(const MetaInfoInterface& hit,
487 const MS1LabelState::Keys& keys);
488
489 // ---------------------------------------------------------------------------
490 // Read helpers
491 // ---------------------------------------------------------------------------
492
499 OPENMS_DLLAPI std::shared_ptr<arrow::Array> getColumn(
500 const std::shared_ptr<arrow::Table>& table,
501 const std::string& name,
502 bool required = true);
503
505 OPENMS_DLLAPI std::string getStringValue(
506 const std::shared_ptr<arrow::Array>& array,
507 int64_t row);
508
510 OPENMS_DLLAPI double getDoubleValue(
511 const std::shared_ptr<arrow::Array>& array,
512 int64_t row,
513 double default_val = 0.0);
514
516 OPENMS_DLLAPI float getFloatValue(
517 const std::shared_ptr<arrow::Array>& array,
518 int64_t row,
519 float default_val = 0.0f);
520
522 OPENMS_DLLAPI int32_t getInt32Value(
523 const std::shared_ptr<arrow::Array>& array,
524 int64_t row,
525 int32_t default_val = 0);
526
528 OPENMS_DLLAPI int64_t getInt64Value(
529 const std::shared_ptr<arrow::Array>& array,
530 int64_t row,
531 int64_t default_val = 0);
532
534 OPENMS_DLLAPI bool getBoolValue(
535 const std::shared_ptr<arrow::Array>& array,
536 int64_t row,
537 bool default_val = false);
538
540 OPENMS_DLLAPI bool isNull(
541 const std::shared_ptr<arrow::Array>& array,
542 int64_t row);
543
551 OPENMS_DLLAPI std::string metaValueString(const DataValue& value);
552
559 OPENMS_DLLAPI void readMetaValues(
560 const std::shared_ptr<arrow::Array>& array,
561 int64_t row,
562 MetaInfoInterface& target,
563 const std::unordered_set<std::string>& excluded_keys = {});
564}
565
566} // namespace OpenMS
A Parquet file written table by table: the merged QPX psm file of a multi-file search,...
Definition ArrowIOHelpers.h:237
bool failed() const
A write was refused or failed (the file is gone)
ParquetTableStream & operator=(const ParquetTableStream &)=delete
const std::string & filename() const
The file.
std::unique_ptr< Impl > impl_
Definition ArrowIOHelpers.h:266
Size rows() const
The rows appended so far.
ParquetTableStream(const ParquetTableStream &)=delete
ParquetTableStream(std::string filename, const ParquetWriteConfig &config, std::shared_ptr< const arrow::KeyValueMetadata > metadata)
bool close(const std::map< std::string, std::string > &keys={})
Ends the file with keys added to its footer. False (logged) if that failed (file removed,...
bool append(const std::shared_ptr< arrow::Table > &table, int threads=0)
void abandon()
Ends an unfinished stream as failed and removes its file (nothing after close())
bool closed() const
The file is written (close() succeeded)
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 validateQPXTable(const std::shared_ptr< arrow::Table > &table, const std::string &filename, const ParquetWriteConfig &config=ParquetWriteConfig{}, const std::shared_ptr< const arrow::KeyValueMetadata > &metadata=nullptr)
The QPX value validation of writeTableToParquet() alone (QPXValueValidation of the view the table's f...
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:362
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:368
UInt reference_file_name
Index of reference_file_name, or UInt(-1) when it is not registered.
Definition ArrowIOHelpers.h:366
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