SeqAn3  3.1.0-rc.1
The Modern C++ library for sequence analysis.
input.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <cassert>
16 #include <seqan3/std/filesystem>
17 #include <fstream>
18 #include <string>
19 #include <variant>
20 #include <vector>
21 
31 #include <seqan3/io/detail/record.hpp>
32 #include <seqan3/io/exception.hpp>
42 
43 namespace seqan3
44 {
45 
46 // ----------------------------------------------------------------------------
47 // sequence_file_input_traits
48 // ----------------------------------------------------------------------------
49 
96 template <typename t>
97 SEQAN3_CONCEPT sequence_file_input_traits = requires (t v)
98 {
103 
106 
109 };
111 
112 // ----------------------------------------------------------------------------
113 // sequence_file_input_default_traits
114 // ----------------------------------------------------------------------------
115 
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 
166 {
174 
178 };
179 
180 // ----------------------------------------------------------------------------
181 // sequence_file_input
182 // ----------------------------------------------------------------------------
183 
300 template <
302  detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>,
303  detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl,
304  format_fasta,
305  format_fastq,
307  format_sam>>
309 {
310 public:
316  using traits_type = traits_type_;
318  using selected_field_ids = selected_field_ids_;
320  using valid_formats = valid_formats_;
322  using stream_char_type = char;
324 
329 
330  static_assert([] () constexpr
331  {
332  for (field f : selected_field_ids::as_array)
333  if (!field_ids::contains(f))
334  return false;
335  return true;
336  }(),
337  "You selected a field that is not valid for sequence files, please refer to the documentation "
338  "of sequence_file_input::field_ids for the accepted values.");
339 
346  using sequence_type = typename traits_type::template sequence_container<
347  typename traits_type::sequence_alphabet>;
349  using id_type = typename traits_type::template id_container<
350  typename traits_type::id_alphabet>;
352  using quality_type = typename traits_type::template quality_container<
353  typename traits_type::quality_alphabet>;
356 
358  using record_type = sequence_record<detail::select_types_with_ids_t<field_types,
359  field_ids,
363 
373  using const_reference = void;
375  using size_type = size_t;
379  using iterator = detail::in_file_iterator<sequence_file_input>;
381  using const_iterator = void;
383  using sentinel = std::default_sentinel_t;
385 
400  ~sequence_file_input() = default;
401 
419  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
420  primary_stream{new std::ifstream{}, stream_deleter_default}
421  {
422  primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
423  static_cast<std::basic_ifstream<char> *>(primary_stream.get())->open(filename,
424  std::ios_base::in | std::ios::binary);
425 
426  if (!primary_stream->good())
427  throw file_open_error{"Could not open file " + filename.string() + " for reading."};
428 
429  // possibly add intermediate compression stream
430  secondary_stream = detail::make_secondary_istream(*primary_stream, filename);
431 
432  // initialise format handler or throw if format is not found
433  using format_variant_t = typename detail::variant_from_tags<valid_formats,
434  detail::sequence_file_input_format_exposer>::type;
435  format_variant_t format_variant{};
436  detail::set_format(format_variant, filename);
437 
438  std::visit([&] (auto && selected_format)
439  {
440  using format_t = std::remove_cvref_t<decltype(selected_format)>;
441  format = std::make_unique<selected_sequence_format<format_t>>();
442  }, format_variant);
443  }
444  /* NOTE(h-2): Curiously we do not need a user-defined deduction guide for the above constructor.
445  * A combination of default template parameters and auto-deduction guides works as expected,
446  * independent of whether the second/optional parameter is specified or not, i.e. it is possible
447  * to auto-deduct and overwrite a single template parameter out of the four if the optional parameter
448  * is specified and use the default otherwise.
449  */
450 
465  template <input_stream stream_t,
466  sequence_file_input_format file_format>
468  requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
470  sequence_file_input(stream_t & stream,
471  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
472  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
473  primary_stream{&stream, stream_deleter_noop},
474  format{std::make_unique<selected_sequence_format<file_format>>()}
475  {
476  static_assert(list_traits::contains<file_format, valid_formats>,
477  "You selected a format that is not in the valid_formats of this file.");
478 
479  // possibly add intermediate compression stream
480  secondary_stream = detail::make_secondary_istream(*primary_stream);
481  }
482 
484  template <input_stream stream_t,
485  sequence_file_input_format file_format>
487  requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
489  sequence_file_input(stream_t && stream,
490  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
491  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
492  primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
493  format{std::make_unique<selected_sequence_format<file_format>>()}
494  {
495  static_assert(list_traits::contains<file_format, valid_formats>,
496  "You selected a format that is not in the valid_formats of this file.");
497 
498  // possibly add intermediate compression stream
499  secondary_stream = detail::make_secondary_istream(*primary_stream);
500  }
502 
522  {
523  // buffer first record
524  if (!first_record_was_read)
525  {
526  read_next_record();
527  first_record_was_read = true;
528  }
529 
530  return {*this};
531  }
532 
546  sentinel end() noexcept
547  {
548  return {};
549  }
550 
574  reference front() noexcept
575  {
576  return *begin();
577  }
579 
584 
585 protected:
587 
591  record_type record_buffer;
593  std::vector<char> stream_buffer{std::vector<char>(1'000'000)};
595 
603  static void stream_deleter_noop(std::basic_istream<stream_char_type> *) {}
605  static void stream_deleter_default(std::basic_istream<stream_char_type> * ptr) { delete ptr; }
606 
608  stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
610  stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
611 
613  bool first_record_was_read{false};
615  bool at_end{false};
617 
618 private:
620  void read_next_record()
621  {
622  // clear the record
623  record_buffer.clear();
624 
625  // at end if we could not read further
626  if ((std::istreambuf_iterator<stream_char_type>{*secondary_stream} ==
628  {
629  at_end = true;
630  return;
631  }
632 
633  format->read_sequence_record(*secondary_stream, record_buffer, options);
634  }
635 
646  struct sequence_format_base
647  {
651  sequence_format_base() = default;
652  sequence_format_base(sequence_format_base const &) = default;
653  sequence_format_base(sequence_format_base &&) = default;
654  sequence_format_base & operator=(sequence_format_base const &) = default;
655  sequence_format_base & operator=(sequence_format_base &&) = default;
656  virtual ~sequence_format_base() = default;
658 
669  virtual void read_sequence_record(std::istream & instream,
670  record_type & record_buffer,
672  };
673 
685  template <typename format_t>
686  struct selected_sequence_format final : public sequence_format_base
687  {
691  selected_sequence_format() = default;
692  selected_sequence_format(selected_sequence_format const &) = default;
693  selected_sequence_format(selected_sequence_format &&) = default;
694  selected_sequence_format & operator=(selected_sequence_format const &) = default;
695  selected_sequence_format & operator=(selected_sequence_format &&) = default;
696  ~selected_sequence_format() = default;
698 
700  void read_sequence_record(std::istream & instream,
701  record_type & record_buffer,
702  sequence_file_input_options_type const & options) override
703  {
704  // read new record
705  {
706  _format.read_sequence_record(instream,
707  options,
708  detail::get_or_ignore<field::seq>(record_buffer),
709  detail::get_or_ignore<field::id>(record_buffer),
710  detail::get_or_ignore<field::qual>(record_buffer));
711  }
712  };
713 
715  detail::sequence_file_input_format_exposer<format_t> _format{};
716  };
717 
720 
722  friend iterator;
723 };
724 
731 template <input_stream stream_type,
732  sequence_file_input_format file_format>
733 sequence_file_input(stream_type & stream,
734  file_format const &)
736  typename sequence_file_input<>::selected_field_ids, // default field ids.
738 
740 template <input_stream stream_type,
741  sequence_file_input_format file_format>
742 sequence_file_input(stream_type && stream,
743  file_format const &)
745  typename sequence_file_input<>::selected_field_ids, // default field ids.
747 
749 template <input_stream stream_type,
750  sequence_file_input_format file_format,
751  detail::fields_specialisation selected_field_ids>
752 sequence_file_input(stream_type && stream,
753  file_format const &,
754  selected_field_ids const &)
758 
760 template <input_stream stream_type,
761  sequence_file_input_format file_format,
762  detail::fields_specialisation selected_field_ids>
763 sequence_file_input(stream_type & stream,
764  file_format const &,
765  selected_field_ids const &)
770 
771 } // 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:46
The 15 letter DNA alphabet, containing all IUPAC smybols minus the gap.
Definition: dna15.hpp:51
The five letter DNA alphabet of A,C,G,T and the unknown character N.
Definition: dna5.hpp:51
The EMBL format.
Definition: format_embl.hpp:73
The FastA format.
Definition: format_fasta.hpp:79
The FastQ format.
Definition: format_fastq.hpp:78
The GenBank format.
Definition: format_genbank.hpp:72
The SAM format (tag).
Definition: format_sam.hpp:115
Quality type for traditional Sanger and modern Illumina Phred scores.
Definition: phred42.hpp:47
A class for reading sequence files, e.g. FASTA, FASTQ ...
Definition: input.hpp:309
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.
typename traits_type::template id_container< typename traits_type::id_alphabet > id_type
The type of field::id (std::string by defaul).
Definition: input.hpp:350
void const_reference
The const_reference type is void, because files are not const-iterable.
Definition: input.hpp:373
std::default_sentinel_t sentinel
The type returned by end().
Definition: input.hpp:383
sequence_file_input(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition: input.hpp:418
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.
reference front() noexcept
Return the record we are currently at in the file.
Definition: input.hpp:574
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...
type_list< sequence_type, id_type, quality_type > field_types
The previously defined types aggregated in a seqan3::type_list.
Definition: input.hpp:355
fields< field::seq, field::id, field::qual > field_ids
The subset of seqan3::field IDs that are valid for this file; order corresponds to the types in field...
Definition: input.hpp:328
iterator begin()
Returns an iterator to current position in the file.
Definition: input.hpp:521
sequence_file_input_options_type options
The options are public and its members can be set directly.
Definition: input.hpp:583
sequence_file_input & operator=(sequence_file_input &&)=default
Move assignment is defaulted.
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition: input.hpp:546
char stream_char_type
Character type of the stream(s).
Definition: input.hpp:322
size_t size_type
An unsigned integer type, usually std::size_t.
Definition: input.hpp:375
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: input.hpp:470
sequence_file_input(sequence_file_input const &)=delete
Copy construction 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 >>
This is an overloaded member function, provided for convenience. It differs from the above function o...
detail::in_file_iterator< sequence_file_input > iterator
The iterator type of this view (an input iterator).
Definition: input.hpp:379
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: input.hpp:489
~sequence_file_input()=default
Destructor 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: input.hpp:381
sequence_file_input()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
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: input.hpp:353
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: input.hpp:347
traits_type_ traits_type
A traits type that defines aliases and template for storage of the fields.
Definition: input.hpp:316
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition: input.hpp:318
sequence_file_input_options< typename traits_type::sequence_legal_alphabet > sequence_file_input_options_type
The input file options type.
Definition: input.hpp:581
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition: input.hpp:320
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.
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: input.hpp:361
T data(T... args)
Provides seqan3::dna15, container aliases and string literals.
Provides seqan3::dna5, container aliases and string literals.
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
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:63
Provides the seqan3::detail::in_file_iterator class template.
Resolves to std::ranges::explicitly_convertible_to<type1, type2>(). <dl class="no-api">This entity i...
A more refined container concept than seqan3::container.
The generic concept for sequence file in formats.
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:29
Provides algorithms for meta programming, parameter packs and seqan3::type_list.
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:128
void clear() noexcept(noexcept(std::apply(expander, std::declval< record & >())))
Clears containers that provide .clear() and (re-)initialises all other elements with = {}.
Definition: record.hpp:235
A traits type that specifies input as amino acids.
Definition: input.hpp:166
The default traits for seqan3::sequence_file_input.
Definition: input.hpp:130
char id_alphabet
The alphabet for an identifier string is char.
Definition: input.hpp:147
Type that contains multiple types.
Definition: type_list.hpp:29
Provides traits for seqan3::type_list.
T visit(T... args)