takane
Validators for ArtifactDB file formats
Loading...
Searching...
No Matches
fastq_file.hpp
Go to the documentation of this file.
1#ifndef TAKANE_FASTQ_FILE_HPP
2#define TAKANE_FASTQ_FILE_HPP
3
4#include "utils_files.hpp"
5#include "ritsuko/ritsuko.hpp"
6
7#include <filesystem>
8#include <stdexcept>
9#include <string>
10
16namespace takane {
17
22namespace fastq_file {
23
32inline void validate(const std::filesystem::path& path, const ObjectMetadata& metadata, Options& options) {
33 const std::string type_name = "fastq_file"; // use a separate variable to avoid dangling reference warnings from GCC.
34 const auto& fqmap = internal_json::extract_typed_object_from_metadata(metadata.other, type_name);
35
36 const std::string version_name = "version"; // again, avoid dangling reference warnings.
37 const std::string& vstring = internal_json::extract_string_from_typed_object(fqmap, version_name, type_name);
38 auto version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(), /* skip_patch = */ true);
39 if (version.major != 1) {
40 throw std::runtime_error("unsupported version string '" + vstring + "'");
41 }
42
43 internal_files::check_sequence_type(fqmap, type_name.c_str());
44
45 // Checking the quality type and offset.
46 {
47 const std::string qtype_name = "quality_type"; // again, avoid dangling reference warnings.
48 const std::string& qtype = internal_json::extract_string(fqmap, qtype_name, [&](std::exception& e) -> void {
49 throw std::runtime_error("failed to extract 'fastq_file." + qtype_name + "' from the object metadata; " + std::string(e.what()));
50 });
51
52 if (qtype == "phred") {
53 auto oIt = fqmap.find("quality_offset");
54 if (oIt == fqmap.end()) {
55 throw std::runtime_error("expected a 'fastq_file.quality_offset' property");
56 }
57
58 const auto& val = oIt->second;
59 if (val->type() != millijson::NUMBER) {
60 throw std::runtime_error("'fastq_file.quality_offset' property should be a JSON number");
61 }
62
63 double offset = reinterpret_cast<const millijson::Number*>(val.get())->value;
64 if (offset != 33 && offset != 64) {
65 throw std::runtime_error("'fastq_file.quality_offset' property should be either 33 or 64");
66 }
67 } else if (qtype != "solexa") {
68 throw std::runtime_error("unknown value '" + qtype + "' for the 'fastq_file." + qtype_name + "' property");
69 }
70 }
71
72 // Check if it's indexed.
73 bool indexed = internal_files::is_indexed(fqmap);
74 auto fpath = path / "file.fastq.";
75 if (indexed) {
76 fpath += "bgz";
77 } else {
78 fpath += "gz";
79 }
80
81 internal_files::check_gzip_signature(fpath);
82 auto reader = internal_other::open_reader<byteme::GzipFileReader>(fpath, 10);
83 byteme::PerByte<> pb(&reader);
84 if (!pb.valid() || pb.get() != '@') {
85 throw std::runtime_error("FASTQ file does not start with '@'");
86 }
87
88 if (indexed) {
89 auto fixpath = path / "file.fastq.fai";
90 if (!std::filesystem::exists(fixpath)) {
91 throw std::runtime_error("missing FASTQ index file");
92 }
93
94 auto ixpath = fpath;
95 ixpath += ".gzi";
96 if (!std::filesystem::exists(ixpath)) {
97 throw std::runtime_error("missing BGZF index file");
98 }
99 }
100
101 if (options.fastq_file_strict_check) {
102 options.fastq_file_strict_check(path, metadata, options, indexed);
103 }
104}
105
106}
107
108}
109
110#endif
void validate(const std::filesystem::path &path, const ObjectMetadata &metadata, Options &options)
Definition fastq_file.hpp:32
takane validation functions.
Definition _derived_from.hpp:15
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
Validation options.
Definition utils_public.hpp:94
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &, bool)> fastq_file_strict_check
Definition utils_public.hpp:207