24#if defined(SEQAN3_HAS_BZIP2)
25 #include <seqan3/contrib/stream/bz2_istream.hpp>
27#if defined(SEQAN3_HAS_ZLIB)
28 #include <seqan3/contrib/stream/bgzf_istream.hpp>
30 #include <seqan3/contrib/stream/gz_istream.hpp>
36namespace seqan3::detail
44template <std::ranges::forward_range ref_t, std::ranges::forward_range query_t>
45inline bool starts_with(ref_t && reference, query_t && query)
47 requires std::equality_comparable_with<std::ranges::range_reference_t<ref_t>,
48 std::ranges::range_reference_t<query_t>>
52 auto rend = std::ranges::end(reference);
55 auto qend = std::ranges::end(query);
80template <builtin_
character
char_t>
84 assert(primary_stream.good());
93 std::array<char, bgzf_compression::magic_header.
size()> magic_number{};
94 size_t read_chars = 0;
95 for (; read_chars < magic_number.size(); ++read_chars)
100 magic_number[read_chars] = *it;
105 for (
size_t i = 0 ; i < read_chars; ++i)
106 primary_stream.unget();
109 if (filename.has_extension())
110 extension = filename.extension().
string().substr(1);
113 [[maybe_unused]]
auto contains_extension = [] (
auto compression_tag,
auto const & extension)
constexpr
115 return std::ranges::find(
decltype(compression_tag)::file_extensions, extension) !=
116 std::ranges::end(
decltype(compression_tag)::file_extensions);
120 if (read_chars == magic_number.size() && bgzf_compression::validate_header(
std::span{magic_number}))
122 #if defined(SEQAN3_HAS_ZLIB)
123 if (contains_extension(gz_compression{}, extension) || contains_extension(bgzf_compression{}, extension))
124 filename.replace_extension();
126 return {
new contrib::basic_bgzf_istream<char_t>{primary_stream},
127 stream_deleter_default};
129 throw file_open_error{
"Trying to read from a bgzf file, but no ZLIB available."};
132 else if (starts_with(magic_number, gz_compression::magic_header))
134 #if defined(SEQAN3_HAS_ZLIB)
135 if (contains_extension(gz_compression{}, extension) || contains_extension(bgzf_compression{}, extension))
136 filename.replace_extension();
138 return {
new contrib::basic_gz_istream<char_t>{primary_stream}, stream_deleter_default};
140 throw file_open_error{
"Trying to read from a gzipped file, but no ZLIB available."};
143 else if (starts_with(magic_number, bz2_compression::magic_header))
145 #if defined(SEQAN3_HAS_BZIP2)
146 if (contains_extension(bz2_compression{}, extension))
147 filename.replace_extension();
149 return {
new contrib::basic_bz2_istream<char_t>{primary_stream}, stream_deleter_default};
151 throw file_open_error{
"Trying to read from a bzipped file, but no libbz2 available."};
154 else if (starts_with(magic_number, zstd_compression::magic_header))
156 throw file_open_error{
"Trying to read from a zst'ed file, but SeqAn does not yet support this."};
159 return {&primary_stream, stream_deleter_noop};
163template <builtin_
character
char_t>
167 return make_secondary_istream(primary_stream, p);
The <algorithm> header from C++20's standard library.
Provides stream compression utilities.
The <concepts> header from C++20's standard library.
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
The <filesystem> header from C++17's standard library.
constexpr ptrdiff_t find
Get the index of the first occurrence of a type in a pack.
Definition: traits.hpp:187
Provides exceptions used in the I/O module.
The <ranges> header from C++20's standard library.
Provides std::span from the C++20 standard library.