takane
Validators for ArtifactDB file formats
Loading...
Searching...
No Matches
gff_file.hpp
Go to the documentation of this file.
1#ifndef TAKANE_GFF_FILE_HPP
2#define TAKANE_GFF_FILE_HPP
3
4#include "utils_files.hpp"
5
6#include "ritsuko/ritsuko.hpp"
7
8#include <filesystem>
9#include <stdexcept>
10#include <string>
11
17namespace takane {
18
23namespace gff_file {
24
33inline void validate(const std::filesystem::path& path, const ObjectMetadata& metadata, Options& options) {
34 const std::string type_name = "gff_file"; // use a separate variable to avoid dangling reference warnings from GCC.
35 const auto& gffmap = internal_json::extract_typed_object_from_metadata(metadata.other, type_name);
36
37 const std::string version_name = "version"; // again, avoid dangling reference warnings.
38 const std::string& vstring = internal_json::extract_string_from_typed_object(gffmap, version_name, type_name);
39 auto version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(), /* skip_patch = */ true);
40 if (version.major != 1) {
41 throw std::runtime_error("unsupported version string '" + vstring + "'");
42 }
43
44 auto fpath = path / "file.";
45 const std::string format_name = "format"; // again, avoid dangling reference warnings.
46 const std::string& fstring = internal_json::extract_string_from_typed_object(gffmap, format_name, type_name);
47 if (fstring == "GFF2") {
48 fpath += "gff2";
49 } else if (fstring == "GFF3") {
50 fpath += "gff3";
51 } else {
52 throw std::runtime_error("unknown value '" + fstring + "' for 'gff_file.format' property");
53 }
54
55 // Check if it's indexed.
56 bool indexed = internal_files::is_indexed(gffmap);
57 fpath += ".";
58 if (indexed) {
59 fpath += "bgz";
60 } else {
61 fpath += "gz";
62 }
63
64 // Check magic numbers.
65 internal_files::check_gzip_signature(fpath);
66
67 if (fstring == "GFF3") {
68 const std::string signature = "##gff-version 3";
69 internal_files::check_gunzipped_signature(fpath, signature.c_str(), signature.size(), "GFF3");
70 }
71
72 if (indexed) {
73 auto ixpath = fpath;
74 ixpath += ".tbi";
75 internal_files::check_gzip_signature(ixpath);
76 internal_files::check_gunzipped_signature(ixpath, "TBI\1", 4, "tabix");
77 }
78
79 if (options.gff_file_strict_check) {
80 options.gff_file_strict_check(path, metadata, options, indexed);
81 }
82}
83
84}
85
86}
87
88#endif
void validate(const std::filesystem::path &path, const ObjectMetadata &metadata, Options &options)
Definition gff_file.hpp:33
takane validation functions.
Definition _derived_from.hpp:15
Object metadata, including the type and other fields.
Definition utils_public.hpp:25
std::unordered_map< std::string, std::shared_ptr< millijson::Base > > other
Definition utils_public.hpp:34
Validation options.
Definition utils_public.hpp:93
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &, bool)> gff_file_strict_check
Definition utils_public.hpp:214