takane
Validators for ArtifactDB file formats
Loading...
Searching...
No Matches
utils_public.hpp
Go to the documentation of this file.
1#ifndef TAKANE_UTILS_PUBLIC_HPP
2#define TAKANE_UTILS_PUBLIC_HPP
3
4#include <string>
5#include <filesystem>
6#include <unordered_map>
7#include <functional>
8
9#include "H5Cpp.h"
10
11#include "millijson/millijson.hpp"
12#include "byteme/byteme.hpp"
13#include "chihaya/chihaya.hpp"
14#include "utils_json.hpp"
15
21namespace takane {
22
30 std::string type;
31
35 std::unordered_map<std::string, std::shared_ptr<millijson::Base> > other;
36};
37
45inline ObjectMetadata reformat_object_metadata(millijson::Base* raw) {
46 if (raw->type() != millijson::OBJECT) {
47 throw std::runtime_error("metadata should be a JSON object");
48 }
49
50 ObjectMetadata output;
51 output.other = std::move(reinterpret_cast<millijson::Object*>(raw)->values);
52
53 auto tIt = output.other.find("type");
54 if (tIt == output.other.end()) {
55 throw std::runtime_error("metadata should have a 'type' property");
56 }
57
58 const auto& tval = tIt->second;
59 if (tval->type() != millijson::STRING) {
60 throw std::runtime_error("metadata should have a 'type' string");
61 }
62
63 output.type = std::move(reinterpret_cast<millijson::String*>(tval.get())->value);
64 output.other.erase(tIt);
65 return output;
66}
67
74inline ObjectMetadata read_object_metadata(const std::filesystem::path& path) try {
75 std::shared_ptr<millijson::Base> obj = internal_json::parse_file(path / "OBJECT");
76 return reformat_object_metadata(obj.get());
77} catch (std::exception& e) {
78 throw std::runtime_error("failed to read the OBJECT file at '" + path.string() + "'; " + std::string(e.what()));
79}
80
94struct Options {
98 bool parallel_reads = true;
99
103 hsize_t hdf5_buffer_size = 10000;
104
105public:
111 std::unordered_map<std::string, std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&)> > custom_validate;
112
117 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&)> custom_global_validate;
118
119public:
125 std::unordered_map<std::string, std::function<std::vector<size_t>(const std::filesystem::path&, const ObjectMetadata&, Options&)> > custom_dimensions;
126
132 std::unordered_map<std::string, std::function<size_t(const std::filesystem::path&, const ObjectMetadata& m, Options&)> > custom_height;
133
134public:
140 std::unordered_map<std::string, std::unordered_set<std::string> > custom_derived_from;
141
147 std::unordered_map<std::string, std::unordered_set<std::string> > custom_satisfies_interface;
148
149public:
155 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&)> bam_file_strict_check;
156
162 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&)> bcf_file_strict_check;
163
170 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&, bool)> bed_file_strict_check;
171
177 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&)> bigbed_file_strict_check;
178
184 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&)> bigwig_file_strict_check;
185
191 std::function<bool(const std::filesystem::path&, const ObjectMetadata&, Options& options)> data_frame_factor_any_duplicated;
192
199 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&, bool)> fasta_file_strict_check;
200
207 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&, bool)> fastq_file_strict_check;
208
215 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&, bool)> gff_file_strict_check;
216
222 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&)> gmt_file_strict_check;
223
229 std::function<void(const std::filesystem::path&, const ObjectMetadata&, Options&)> rds_file_strict_check;
230
234 chihaya::Options delayed_array_options;
235};
236
237}
238
239#endif
takane validation functions.
Definition _derived_from.hpp:15
ObjectMetadata read_object_metadata(const std::filesystem::path &path)
Definition utils_public.hpp:74
ObjectMetadata reformat_object_metadata(millijson::Base *raw)
Definition utils_public.hpp:45
Object metadata, including the type and other fields.
Definition utils_public.hpp:26
std::unordered_map< std::string, std::shared_ptr< millijson::Base > > other
Definition utils_public.hpp:35
std::string type
Definition utils_public.hpp:30
Validation options.
Definition utils_public.hpp:94
chihaya::Options delayed_array_options
Definition utils_public.hpp:234
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> bigwig_file_strict_check
Definition utils_public.hpp:184
bool parallel_reads
Definition utils_public.hpp:98
std::unordered_map< std::string, std::unordered_set< std::string > > custom_satisfies_interface
Definition utils_public.hpp:147
std::unordered_map< std::string, std::function< size_t(const std::filesystem::path &, const ObjectMetadata &m, Options &)> > custom_height
Definition utils_public.hpp:132
hsize_t hdf5_buffer_size
Definition utils_public.hpp:103
std::function< bool(const std::filesystem::path &, const ObjectMetadata &, Options &options)> data_frame_factor_any_duplicated
Definition utils_public.hpp:191
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> bam_file_strict_check
Definition utils_public.hpp:155
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> gmt_file_strict_check
Definition utils_public.hpp:222
std::unordered_map< std::string, std::function< std::vector< size_t >(const std::filesystem::path &, const ObjectMetadata &, Options &)> > custom_dimensions
Definition utils_public.hpp:125
std::unordered_map< std::string, std::unordered_set< std::string > > custom_derived_from
Definition utils_public.hpp:140
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &, bool)> gff_file_strict_check
Definition utils_public.hpp:215
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> bcf_file_strict_check
Definition utils_public.hpp:162
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> bigbed_file_strict_check
Definition utils_public.hpp:177
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &, bool)> fastq_file_strict_check
Definition utils_public.hpp:207
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> custom_global_validate
Definition utils_public.hpp:117
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> rds_file_strict_check
Definition utils_public.hpp:229
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &, bool)> fasta_file_strict_check
Definition utils_public.hpp:199
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &, bool)> bed_file_strict_check
Definition utils_public.hpp:170
std::unordered_map< std::string, std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> > custom_validate
Definition utils_public.hpp:111