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
7#include <filesystem>
8#include <stdexcept>
9#include <string>
10
16namespace takane {
17
22namespace fasta_file {
23
32inline void validate(const std::filesystem::path& path, const ObjectMetadata& metadata, Options& options) {
33 const auto& famap = internal_json::extract_typed_object_from_metadata(metadata.other, "fasta_file");
34
35 const std::string& vstring = internal_json::extract_string_from_typed_object(famap, "version", "fasta_file");
36 auto version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(), /* skip_patch = */ true);
37 if (version.major != 1) {
38 throw std::runtime_error("unsupported version string '" + vstring + "'");
39 }
40
41 internal_files::check_sequence_type(famap, "fasta_file");
42
43 // Check if it's indexed.
44 bool indexed = internal_files::is_indexed(famap);
45 auto fpath = path / "file.fasta.";
46 if (indexed) {
47 fpath += "bgz";
48 } else {
49 fpath += "gz";
50 }
51
52 internal_files::check_gzip_signature(fpath);
53 auto reader = internal_other::open_reader<byteme::GzipFileReader>(fpath, 10);
54 byteme::PerByte<> pb(&reader);
55 if (!pb.valid() || pb.get() != '>') {
56 throw std::runtime_error("FASTA file does not start with '>'");
57 }
58
59 if (indexed) {
60 auto fixpath = path / "file.fasta.fai";
61 if (!std::filesystem::exists(fixpath)) {
62 throw std::runtime_error("missing FASTA index file");
63 }
64
65 auto ixpath = fpath;
66 ixpath += ".gzi";
67 if (!std::filesystem::exists(ixpath)) {
68 throw std::runtime_error("missing BGZF index file");
69 }
70 }
71
72 if (options.fasta_file_strict_check) {
73 options.fasta_file_strict_check(path, metadata, options, indexed);
74 }
75}
76
77}
78
79}
80
81#endif
void validate(const std::filesystem::path &path, const ObjectMetadata &metadata, Options &options)
Definition fasta_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)> fasta_file_strict_check
Definition utils_public.hpp:199