SeqAn3 3.4.3-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
sequence_file/input.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <cassert>
13#include <filesystem>
14#include <fstream>
15#include <string>
16#include <variant>
17#include <vector>
18
38
39namespace seqan3
40{
41
42// ----------------------------------------------------------------------------
43// sequence_file_input_traits
44// ----------------------------------------------------------------------------
45
94template <typename t>
95concept sequence_file_input_traits = requires (t v) {
98 requires detail::is_char_adaptation_v<typename t::sequence_alphabet>
101
104
107};
109
110// ----------------------------------------------------------------------------
111// sequence_file_input_default_traits
112// ----------------------------------------------------------------------------
113
130{
138
141
143 template <typename _sequence_alphabet>
145
147 using id_alphabet = char;
148
150 template <typename _id_alphabet>
152
155
157 template <typename _quality_alphabet>
159
161};
162
179
180// ----------------------------------------------------------------------------
181// sequence_file_input
182// ----------------------------------------------------------------------------
183
206{
207public:
213 using traits_type = traits_type_;
215 using selected_field_ids = selected_field_ids_;
217 using valid_formats = valid_formats_;
219 using stream_char_type = char;
221
226
227 static_assert(
228 []() constexpr
229 {
230 for (field f : selected_field_ids::as_array)
231 if (!field_ids::contains(f))
232 return false;
233 return true;
234 }(),
235 "You selected a field that is not valid for sequence files, please refer to the documentation "
236 "of sequence_file_input::field_ids for the accepted values.");
237
246 using id_type = typename traits_type::template id_container<typename traits_type::id_alphabet>;
248 using quality_type = typename traits_type::template quality_container<typename traits_type::quality_alphabet>;
251
256
266 using const_reference = void;
268 using size_type = size_t;
274 using const_iterator = void;
276 using sentinel = std::default_sentinel_t;
278
294
312 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
314 {
315 primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
317 ->open(filename, std::ios_base::in | std::ios::binary);
318
319 if (!primary_stream->good())
320 throw file_open_error{"Could not open file " + filename.string() + " for reading."};
321
322 // possibly add intermediate compression stream
324
325 // initialise format handler or throw if format is not found
326 using format_variant_t =
327 typename detail::variant_from_tags<valid_formats, detail::sequence_file_input_format_exposer>::type;
328 format_variant_t format_variant{};
329 detail::set_format(format_variant, filename);
330
332 [&](auto && selected_format)
333 {
334 using format_t = std::remove_cvref_t<decltype(selected_format)>;
335 format = std::make_unique<selected_sequence_format<format_t>>();
336 },
337 format_variant);
338 }
339 /* NOTE(h-2): Curiously we do not need a user-defined deduction guide for the above constructor.
340 * A combination of default template parameters and auto-deduction guides works as expected,
341 * independent of whether the second/optional parameter is specified or not, i.e. it is possible
342 * to auto-deduct and overwrite a single template parameter out of the four if the optional parameter
343 * is specified and use the default otherwise.
344 */
345
360 template <input_stream stream_t, sequence_file_input_format file_format>
361 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
362 sequence_file_input(stream_t & stream,
363 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
364 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
366 format{std::make_unique<selected_sequence_format<file_format>>()}
367 {
368 static_assert(list_traits::contains<file_format, valid_formats>,
369 "You selected a format that is not in the valid_formats of this file.");
370
371 // possibly add intermediate compression stream
373 }
374
376 template <input_stream stream_t, sequence_file_input_format file_format>
377 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
378 sequence_file_input(stream_t && stream,
379 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
380 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
381 primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
382 format{std::make_unique<selected_sequence_format<file_format>>()}
383 {
384 static_assert(list_traits::contains<file_format, valid_formats>,
385 "You selected a format that is not in the valid_formats of this file.");
386
387 // possibly add intermediate compression stream
389 }
391
411 {
412 // buffer first record
414 {
417 }
418
419 return {*this};
420 }
421
435 sentinel end() noexcept
436 {
437 return {};
438 }
439
463 reference front() noexcept
464 {
465 return *begin();
466 }
468
473
474protected:
476
486
498 {
499 delete ptr;
500 }
501
506
510 bool at_end{false};
512
513private:
516 {
517 // clear the record
519
520 // at end if we could not read further
523 {
524 at_end = true;
525 return;
526 }
527
529 }
530
570
582 template <typename format_t>
584 {
595
601 {
602 // read new record
603 {
605 options,
607 detail::get_or_ignore<field::seq>(record_buffer),
608 detail::get_or_ignore<field::id>(record_buffer),
609 detail::get_or_ignore<field::qual>(record_buffer));
610 }
611 }
612
615 };
616
619
621 friend iterator;
622};
623
630template <input_stream stream_type, sequence_file_input_format file_format>
631sequence_file_input(stream_type & stream,
632 file_format const &)
634 typename sequence_file_input<>::selected_field_ids, // default field ids.
636
638template <input_stream stream_type, sequence_file_input_format file_format>
639sequence_file_input(stream_type && stream,
640 file_format const &)
642 typename sequence_file_input<>::selected_field_ids, // default field ids.
644
646template <input_stream stream_type,
647 sequence_file_input_format file_format,
649sequence_file_input(stream_type && stream,
650 file_format const &,
651 selected_field_ids const &)
655
657template <input_stream stream_type,
658 sequence_file_input_format file_format,
660sequence_file_input(stream_type & stream,
661 file_format const &,
662 selected_field_ids const &)
667
668} // namespace seqan3
Provides seqan3::aa27, container aliases and string literals.
Provides alphabet adaptations for standard char types.
The twenty-seven letter amino acid alphabet.
Definition aa27.hpp:43
Input iterator necessary for providing a range-like interface in input file.
Definition in_file_iterator.hpp:39
The 15 letter DNA alphabet, containing all IUPAC smybols minus the gap.
Definition dna15.hpp:48
The five letter DNA alphabet of A,C,G,T and the unknown character N.
Definition dna5.hpp:48
Quality type for traditional Sanger and modern Illumina Phred scores.
Definition phred42.hpp:44
The generic concept for sequence file in formats.
Definition sequence_file/input_format_concept.hpp:91
A class for reading sequence files, e.g. FASTA, FASTQ ...
Definition sequence_file/input.hpp:206
static void stream_deleter_default(std::basic_istream< stream_char_type > *ptr)
Stream deleter with default behaviour (ownership assumed).
Definition sequence_file/input.hpp:497
void const_reference
The const_reference type is void, because files are not const-iterable.
Definition sequence_file/input.hpp:266
std::default_sentinel_t sentinel
The type returned by end().
Definition sequence_file/input.hpp:276
sequence_file_input(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition sequence_file/input.hpp:311
reference front() noexcept
Return the record we are currently at in the file.
Definition sequence_file/input.hpp:463
iterator begin()
Returns an iterator to current position in the file.
Definition sequence_file/input.hpp:410
sequence_file_input_options_type options
The options are public and its members can be set directly.
Definition sequence_file/input.hpp:472
typename traits_type::template sequence_container< typename traits_type::sequence_alphabet > sequence_type
The type of field::seq (std::vector <seqan3::dna5> by default).
Definition sequence_file/input.hpp:244
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition sequence_file/input.hpp:435
record_type record_buffer
Buffer for a single record.
Definition sequence_file/input.hpp:480
char stream_char_type
Character type of the stream(s).
Definition sequence_file/input.hpp:219
sequence_file_input(stream_type &stream, file_format const &, selected_field_ids const &) -> sequence_file_input< typename sequence_file_input<>::traits_type, selected_field_ids, type_list< file_format > >
This is an overloaded member function, provided for convenience. It differs from the above function o...
sequence_file_input(stream_type &&stream, file_format const &) -> sequence_file_input< typename sequence_file_input<>::traits_type, typename sequence_file_input<>::selected_field_ids, type_list< file_format > >
This is an overloaded member function, provided for convenience. It differs from the above function o...
size_t size_type
An unsigned integer type, usually std::size_t.
Definition sequence_file/input.hpp:268
stream_ptr_t primary_stream
The primary stream is the user provided stream or the file stream if constructed from filename.
Definition sequence_file/input.hpp:503
sequence_file_input(sequence_file_input const &)=delete
Copy construction is explicitly deleted, because you can't have multiple access to the same file.
typename traits_type::template id_container< typename traits_type::id_alphabet > id_type
The type of field::id (std::string by defaul).
Definition sequence_file/input.hpp:246
~sequence_file_input()=default
Destructor is defaulted.
sequence_file_input & operator=(sequence_file_input &&)=default
Move assignment is defaulted.
sequence_file_input(sequence_file_input &&)=default
Move construction is defaulted.
void const_iterator
The const iterator type is void, because files are not const-iterable.
Definition sequence_file/input.hpp:274
sequence_file_input & operator=(sequence_file_input const &)=delete
Copy assignment is explicitly deleted, because you can't have multiple access to the same file.
sequence_file_input(stream_type &stream, file_format const &) -> sequence_file_input< typename sequence_file_input<>::traits_type, typename sequence_file_input<>::selected_field_ids, type_list< file_format > >
Deduces the sequence input file type from the stream and the format.
sequence_file_input(stream_t &&stream, file_format const &format_tag, selected_field_ids const &fields_tag=selected_field_ids{})
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition sequence_file/input.hpp:378
sequence_file_input()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
std::streampos position_buffer
Buffer for the previous record position.
Definition sequence_file/input.hpp:484
traits_type_ traits_type
A traits type that defines aliases and template for storage of the fields.
Definition sequence_file/input.hpp:213
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition sequence_file/input.hpp:215
static void stream_deleter_noop(std::basic_istream< stream_char_type > *)
Stream deleter that does nothing (no ownership assumed).
Definition sequence_file/input.hpp:494
bool first_record_was_read
Tracks whether the very first record is buffered when calling begin().
Definition sequence_file/input.hpp:508
sequence_file_input(stream_type &&stream, file_format const &, selected_field_ids const &) -> sequence_file_input< typename sequence_file_input<>::traits_type, selected_field_ids, type_list< file_format > >
Deduces the sequence input file type from the stream, the format and the field ids.
typename traits_type::template quality_container< typename traits_type::quality_alphabet > quality_type
The type of field::qual (std::vector <seqan3::phred42> by default).
Definition sequence_file/input.hpp:248
sequence_file_input(stream_t &stream, file_format const &format_tag, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from an existing stream and with specified format.
Definition sequence_file/input.hpp:362
friend iterator
Befriend iterator so it can access the buffers.
Definition sequence_file/input.hpp:621
std::unique_ptr< sequence_format_base > format
An instance of the detected/selected format.
Definition sequence_file/input.hpp:618
std::vector< char > stream_buffer
A larger (compared to stl default) stream buffer to use when reading from a file.
Definition sequence_file/input.hpp:482
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition sequence_file/input.hpp:217
bool at_end
File is at position 1 behind the last record.
Definition sequence_file/input.hpp:510
stream_ptr_t secondary_stream
The secondary stream is a compression layer on the primary or just points to the primary (no compress...
Definition sequence_file/input.hpp:505
void read_next_record()
Tell the format to move to the next record and update the buffer.
Definition sequence_file/input.hpp:515
sequence_record< detail::select_types_with_ids_t< field_types, field_ids, selected_field_ids >, selected_field_ids > record_type
The type of the record, a specialisation of seqan3::record; acts as a tuple of the selected field typ...
Definition sequence_file/input.hpp:254
Auxiliary concept that checks whether a type is a specialisation of seqan3::fields.
Definition detail/record.hpp:32
Auxiliary concept that checks whether a type is a seqan3::type_list and all types meet seqan3::sequen...
Definition sequence_file/input_format_concept.hpp:163
T data(T... args)
Provides auxiliary data structures and functions for seqan3::record and seqan3::fields.
Provides seqan3::dna15, container aliases and string literals.
Provides seqan3::dna5, container aliases and string literals.
Provides the seqan3::sequence_file_format_genbank class.
Provides the seqan3::format_sam.
T get(T... args)
field
An enumerator for the fields used in file formats.
Definition record.hpp:60
void set_format(format_variant_type &format, std::filesystem::path const &file_name)
Sets the file format according to the file name extension.
Definition io/detail/misc.hpp:65
auto make_secondary_istream(std::basic_istream< char_t > &primary_stream, std::filesystem::path &filename) -> std::unique_ptr< std::basic_istream< char_t >, std::function< void(std::basic_istream< char_t > *)> >
Depending on the magic bytes of the given stream, return a decompression stream or forward the primar...
Definition misc_input.hpp:73
Provides the seqan3::detail::in_file_iterator class template.
Checks whether from can be explicitly converted to to.
A more refined container concept than seqan3::container.
The requirements a traits_type for seqan3::sequence_file_input must meet.
Refines seqan3::alphabet and adds assignability.
A concept that indicates whether a writable alphabet represents quality scores.
Provides exceptions used in the I/O module.
Stream concepts.
Provides various utility functions required only for input.
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
Provides seqan3::phred42 quality scores.
Provides quality alphabet composites.
Provides seqan3::sequence_file_input_format and auxiliary classes.
Provides seqan3::sequence_record.
T size(T... args)
Internal class used to expose the actual format interface to read sequence records from the file.
Definition sequence_file/input_format_concept.hpp:38
void read_sequence_record(ts &&... args)
Forwards to the seqan3::sequence_file_input_format::read_sequence_record interface.
Definition sequence_file/input_format_concept.hpp:44
A class template that holds a choice of seqan3::field.
Definition record.hpp:125
static constexpr bool contains(field f)
Whether a field is contained in the parameter pack.
Definition record.hpp:146
Thrown if there is an unspecified filesystem or stream error while opening, e.g. permission problem.
Definition io/exception.hpp:36
void clear() noexcept(noexcept(std::apply(expander, std::declval< record & >().as_base())))
Clears containers that provide .clear() and (re-)initialises all other elements with = {}.
Definition record.hpp:242
The specific selected format to read the records from.
Definition sequence_file/input.hpp:584
void read_sequence_record(std::istream &instream, record_type &record_buffer, std::streampos &position_buffer, sequence_file_input_options_type const &options) override
Reads the next format specific record from the given istream.
Definition sequence_file/input.hpp:597
selected_sequence_format(selected_sequence_format const &)=default
Default.
selected_sequence_format & operator=(selected_sequence_format &&)=default
Default.
selected_sequence_format(selected_sequence_format &&)=default
Default.
selected_sequence_format & operator=(selected_sequence_format const &)=default
Default.
detail::sequence_file_input_format_exposer< format_t > _format
The selected format stored as a format exposer object.
Definition sequence_file/input.hpp:614
An abstract base class to store the selected input format.
Definition sequence_file/input.hpp:542
sequence_format_base & operator=(sequence_format_base &&)=default
Default.
sequence_format_base(sequence_format_base const &)=default
Default.
sequence_format_base & operator=(sequence_format_base const &)=default
Default.
sequence_format_base(sequence_format_base &&)=default
Default.
virtual void read_sequence_record(std::istream &instream, record_type &record_buffer, std::streampos &position_buffer, sequence_file_input_options_type const &options)=0
Reads the next format specific record from the given istream.
A traits type that specifies input as amino acids.
Definition sequence_file/input.hpp:166
The default traits for seqan3::sequence_file_input.
Definition sequence_file/input.hpp:130
char id_alphabet
The alphabet for an identifier string is char.
Definition sequence_file/input.hpp:147
Type that contains multiple types.
Definition type_list.hpp:26
Provides traits for seqan3::type_list.
T visit(T... args)
Hide me