SeqAn3 3.4.0-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-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 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
27#include <seqan3/io/detail/record.hpp>
38
39namespace seqan3
40{
41
42// ----------------------------------------------------------------------------
43// sequence_file_input_traits
44// ----------------------------------------------------------------------------
45
94template <typename t>
96 requires (t v) {
99 requires detail::is_char_adaptation_v<typename t::sequence_alphabet>
102
105
108 };
110
111// ----------------------------------------------------------------------------
112// sequence_file_input_default_traits
113// ----------------------------------------------------------------------------
114
131{
139
142
144 template <typename _sequence_alphabet>
146
148 using id_alphabet = char;
149
151 template <typename _id_alphabet>
153
156
158 template <typename _quality_alphabet>
160
162};
163
180
181// ----------------------------------------------------------------------------
182// sequence_file_input
183// ----------------------------------------------------------------------------
184
203 detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>,
204 detail::type_list_of_sequence_file_input_formats valid_formats_ =
207{
208public:
214 using traits_type = traits_type_;
216 using selected_field_ids = selected_field_ids_;
218 using valid_formats = valid_formats_;
220 using stream_char_type = char;
222
227
228 static_assert(
229 []() constexpr
230 {
231 for (field f : selected_field_ids::as_array)
232 if (!field_ids::contains(f))
233 return false;
234 return true;
235 }(),
236 "You selected a field that is not valid for sequence files, please refer to the documentation "
237 "of sequence_file_input::field_ids for the accepted values.");
238
247 using id_type = typename traits_type::template id_container<typename traits_type::id_alphabet>;
249 using quality_type = typename traits_type::template quality_container<typename traits_type::quality_alphabet>;
252
257
267 using const_reference = void;
269 using size_type = size_t;
273 using iterator = detail::in_file_iterator<sequence_file_input>;
275 using const_iterator = void;
277 using sentinel = std::default_sentinel_t;
279
295
313 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
314 primary_stream{new std::ifstream{}, stream_deleter_default}
315 {
316 primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
317 static_cast<std::basic_ifstream<char> *>(primary_stream.get())
318 ->open(filename, std::ios_base::in | std::ios::binary);
319
320 if (!primary_stream->good())
321 throw file_open_error{"Could not open file " + filename.string() + " for reading."};
322
323 // possibly add intermediate compression stream
324 secondary_stream = detail::make_secondary_istream(*primary_stream, filename);
325
326 // initialise format handler or throw if format is not found
327 using format_variant_t =
328 typename detail::variant_from_tags<valid_formats, detail::sequence_file_input_format_exposer>::type;
329 format_variant_t format_variant{};
330 detail::set_format(format_variant, filename);
331
333 [&](auto && selected_format)
334 {
335 using format_t = std::remove_cvref_t<decltype(selected_format)>;
336 format = std::make_unique<selected_sequence_format<format_t>>();
337 },
338 format_variant);
339 }
340 /* NOTE(h-2): Curiously we do not need a user-defined deduction guide for the above constructor.
341 * A combination of default template parameters and auto-deduction guides works as expected,
342 * independent of whether the second/optional parameter is specified or not, i.e. it is possible
343 * to auto-deduct and overwrite a single template parameter out of the four if the optional parameter
344 * is specified and use the default otherwise.
345 */
346
361 template <input_stream stream_t, sequence_file_input_format file_format>
362 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
363 sequence_file_input(stream_t & stream,
364 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
365 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
366 primary_stream{&stream, stream_deleter_noop},
367 format{std::make_unique<selected_sequence_format<file_format>>()}
368 {
369 static_assert(list_traits::contains<file_format, valid_formats>,
370 "You selected a format that is not in the valid_formats of this file.");
371
372 // possibly add intermediate compression stream
373 secondary_stream = detail::make_secondary_istream(*primary_stream);
374 }
375
377 template <input_stream stream_t, sequence_file_input_format file_format>
378 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
379 sequence_file_input(stream_t && stream,
380 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
381 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
382 primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
383 format{std::make_unique<selected_sequence_format<file_format>>()}
384 {
385 static_assert(list_traits::contains<file_format, valid_formats>,
386 "You selected a format that is not in the valid_formats of this file.");
387
388 // possibly add intermediate compression stream
389 secondary_stream = detail::make_secondary_istream(*primary_stream);
390 }
392
412 {
413 // buffer first record
414 if (!first_record_was_read)
415 {
416 read_next_record();
417 first_record_was_read = true;
418 }
419
420 return {*this};
421 }
422
436 sentinel end() noexcept
437 {
438 return {};
439 }
440
464 reference front() noexcept
465 {
466 return *begin();
467 }
469
474
475protected:
477
481 record_type record_buffer;
483 std::vector<char> stream_buffer{std::vector<char>(1'000'000)};
485 std::streampos position_buffer{};
487
495 static void stream_deleter_noop(std::basic_istream<stream_char_type> *)
496 {}
498 static void stream_deleter_default(std::basic_istream<stream_char_type> * ptr)
499 {
500 delete ptr;
501 }
502
504 stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
506 stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
507
509 bool first_record_was_read{false};
511 bool at_end{false};
513
514private:
516 void read_next_record()
517 {
518 // clear the record
519 record_buffer.clear();
520
521 // at end if we could not read further
522 if ((std::istreambuf_iterator<stream_char_type>{*secondary_stream}
524 {
525 at_end = true;
526 return;
527 }
528
529 format->read_sequence_record(*secondary_stream, record_buffer, position_buffer, options);
530 }
531
542 struct sequence_format_base
543 {
547 sequence_format_base() = default;
548 sequence_format_base(sequence_format_base const &) = default;
549 sequence_format_base(sequence_format_base &&) = default;
550 sequence_format_base & operator=(sequence_format_base const &) = default;
551 sequence_format_base & operator=(sequence_format_base &&) = default;
552 virtual ~sequence_format_base() = default;
554
566 virtual void read_sequence_record(std::istream & instream,
567 record_type & record_buffer,
568 std::streampos & position_buffer,
570 };
571
583 template <typename format_t>
584 struct selected_sequence_format final : public sequence_format_base
585 {
589 selected_sequence_format() = default;
590 selected_sequence_format(selected_sequence_format const &) = default;
591 selected_sequence_format(selected_sequence_format &&) = default;
592 selected_sequence_format & operator=(selected_sequence_format const &) = default;
593 selected_sequence_format & operator=(selected_sequence_format &&) = default;
594 ~selected_sequence_format() = default;
596
598 void read_sequence_record(std::istream & instream,
599 record_type & record_buffer,
600 std::streampos & position_buffer,
602 {
603 // read new record
604 {
605 _format.read_sequence_record(instream,
606 options,
607 position_buffer,
608 detail::get_or_ignore<field::seq>(record_buffer),
609 detail::get_or_ignore<field::id>(record_buffer),
610 detail::get_or_ignore<field::qual>(record_buffer));
611 }
612 }
613
615 detail::sequence_file_input_format_exposer<format_t> _format{};
616 };
617
620
622 friend iterator;
623};
624
631template <input_stream stream_type, sequence_file_input_format file_format>
632sequence_file_input(stream_type & stream,
633 file_format const &)
635 typename sequence_file_input<>::selected_field_ids, // default field ids.
637
639template <input_stream stream_type, sequence_file_input_format file_format>
640sequence_file_input(stream_type && stream,
641 file_format const &)
643 typename sequence_file_input<>::selected_field_ids, // default field ids.
645
647template <input_stream stream_type,
648 sequence_file_input_format file_format,
649 detail::fields_specialisation selected_field_ids>
650sequence_file_input(stream_type && stream,
651 file_format const &,
652 selected_field_ids const &)
656
658template <input_stream stream_type,
659 sequence_file_input_format file_format,
660 detail::fields_specialisation selected_field_ids>
661sequence_file_input(stream_type & stream,
662 file_format const &,
663 selected_field_ids const &)
668
669} // 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
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:96
A class for reading sequence files, e.g. FASTA, FASTQ ...
Definition sequence_file/input.hpp:207
void const_reference
The const_reference type is void, because files are not const-iterable.
Definition sequence_file/input.hpp:267
std::default_sentinel_t sentinel
The type returned by end().
Definition sequence_file/input.hpp:277
sequence_file_input(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition sequence_file/input.hpp:312
reference front() noexcept
Return the record we are currently at in the file.
Definition sequence_file/input.hpp:464
iterator begin()
Returns an iterator to current position in the file.
Definition sequence_file/input.hpp:411
sequence_file_input_options_type options
The options are public and its members can be set directly.
Definition sequence_file/input.hpp:473
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:245
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition sequence_file/input.hpp:436
char stream_char_type
Character type of the stream(s).
Definition sequence_file/input.hpp:220
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:269
sequence_file_input(sequence_file_input const &)=delete
Copy construction is explicitly deleted, because you can't have multiple access to the same file.
detail::in_file_iterator< sequence_file_input > iterator
The iterator type of this view (an input iterator).
Definition sequence_file/input.hpp:273
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:247
~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:275
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:379
sequence_file_input()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
traits_type_ traits_type
A traits type that defines aliases and template for storage of the fields.
Definition sequence_file/input.hpp:214
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition sequence_file/input.hpp:216
sequence_file_input_options< typename traits_type::sequence_legal_alphabet > sequence_file_input_options_type
The input file options type.
Definition sequence_file/input.hpp:471
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:249
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:363
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition sequence_file/input.hpp:218
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:255
T data(T... args)
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
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)
A class template that holds a choice of seqan3::field.
Definition record.hpp:125
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
A traits type that specifies input as amino acids.
Definition sequence_file/input.hpp:167
The default traits for seqan3::sequence_file_input.
Definition sequence_file/input.hpp:131
char id_alphabet
The alphabet for an identifier string is char.
Definition sequence_file/input.hpp:148
Type that contains multiple types.
Definition type_list.hpp:26
Provides traits for seqan3::type_list.
T visit(T... args)
Hide me