takane
Validators for ArtifactDB file formats
Loading...
Searching...
No Matches
fasta_file.hpp
Go to the documentation of this file.
1#ifndef TAKANE_FASTA_FILE_HPP
2#define TAKANE_FASTA_FILE_HPP
3
4#include "utils_files.hpp"
5#include "ritsuko/ritsuko.hpp"
6#include "byteme/byteme.hpp"
7
8#include <filesystem>
9#include <stdexcept>
10#include <string>
11
17namespace takane {
18
23namespace fasta_file {
24
33inline void validate(const std::filesystem::path& path, const ObjectMetadata& metadata, Options& options) {
34 const std::string type_name = "fasta_file"; // use a separate variable to avoid dangling reference warnings from GCC.
35 const auto& famap = 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(famap, 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 internal_files::check_sequence_type(famap, type_name.c_str());
45
46 // Check if it's indexed.
47 bool indexed = internal_files::is_indexed(famap);
48 auto fpath = path / "file.fasta.";
49 if (indexed) {
50 fpath += "bgz";
51 } else {
52 fpath += "gz";
53 }
54
55 internal_files::check_gzip_signature(fpath);
56 {
57 auto reader = internal_other::open_reader<byteme::GzipFileReader>(fpath, byteme::GzipFileReaderOptions());
58 char first_char;
59 if (reader->read(reinterpret_cast<unsigned char*>(&first_char), 1) == 0 || first_char != '>') {
60 throw std::runtime_error("FASTA file does not start with '>'");
61 }
62 }
63
64 if (indexed) {
65 auto fixpath = path / "file.fasta.fai";
66 if (!std::filesystem::exists(fixpath)) {
67 throw std::runtime_error("missing FASTA index file");
68 }
69
70 auto ixpath = fpath;
71 ixpath += ".gzi";
72 if (!std::filesystem::exists(ixpath)) {
73 throw std::runtime_error("missing BGZF index file");
74 }
75 }
76
77 if (options.fasta_file_strict_check) {
78 options.fasta_file_strict_check(path, metadata, options, indexed);
79 }
80}
81
82}
83
84}
85
86#endif
void validate(const std::filesystem::path &path, const ObjectMetadata &metadata, Options &options)
Definition fasta_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)> fasta_file_strict_check
Definition utils_public.hpp:198