SeqAn3  3.0.1
The Modern C++ library for sequence analysis.
input.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 <fstream>
17 #include <string>
18 #include <variant>
19 #include <vector>
20 
21 // remove the following after range-v3 is updated to 1.0
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
24 
33 #include <seqan3/io/exception.hpp>
34 #include <seqan3/std/filesystem>
35 #include <seqan3/io/record.hpp>
38 #include <seqan3/io/detail/record.hpp>
46 
47 namespace seqan3
48 {
49 
50 // ----------------------------------------------------------------------------
51 // sequence_file_input_traits
52 // ----------------------------------------------------------------------------
53 
102 template <typename t>
105 SEQAN3_CONCEPT sequence_file_input_traits = requires (t v)
106 {
111 
114 
117 };
119 
120 // ----------------------------------------------------------------------------
121 // sequence_file_input_default_traits
122 // ----------------------------------------------------------------------------
123 
138 {
144  using sequence_alphabet = dna5;
146 
149 
151  template <typename _sequence_alphabet>
153 
155  using id_alphabet = char;
156 
158  template <typename _id_alphabet>
160 
163 
165  template <typename _quality_alphabet>
167 
169 };
170 
174 {
180  using sequence_alphabet = aa27;
182 
186 };
187 
188 // ----------------------------------------------------------------------------
189 // sequence_file_input
190 // ----------------------------------------------------------------------------
191 
312 template <
314  detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>,
315  detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl,
316  format_fasta,
317  format_fastq,
319  format_sam>>
321 {
322 public:
327  using traits_type = traits_type_;
330  using selected_field_ids = selected_field_ids_;
332  using valid_formats = valid_formats_;
334  using stream_char_type = char;
336 
341 
342  static_assert([] () constexpr
343  {
344  for (field f : selected_field_ids::as_array)
345  if (!field_ids::contains(f))
346  return false;
347  return true;
348  }(),
349  "You selected a field that is not valid for sequence files, please refer to the documentation "
350  "of sequence_file_input::field_ids for the accepted values.");
351 
352  static_assert([] () constexpr
353  {
357  }(),
358  "You may not select field::seq_qual and either of field::seq and field::qual at the same time.");
359 
365  using sequence_type = typename traits_type::template sequence_container<
367  typename traits_type::sequence_alphabet>;
369  using id_type = typename traits_type::template id_container<
370  typename traits_type::id_alphabet>;
372  using quality_type = typename traits_type::template quality_container<
373  typename traits_type::quality_alphabet>;
375  using sequence_quality_type = typename traits_type::
376  template sequence_container<qualified<typename traits_type::sequence_alphabet,
377  typename traits_type::quality_alphabet>>;
378 
381 
386 
391  using value_type = record_type;
396  using const_reference = void;
398  using size_type = size_t;
402  using iterator = detail::in_file_iterator<sequence_file_input>;
404  using const_iterator = void;
406  using sentinel = std::ranges::default_sentinel_t;
408 
412  sequence_file_input() = delete;
415  sequence_file_input(sequence_file_input const &) = delete;
423  ~sequence_file_input() = default;
424 
442  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
443  primary_stream{new std::ifstream{filename, std::ios_base::in | std::ios::binary}, stream_deleter_default}
444  {
445  if (!primary_stream->good())
446  throw file_open_error{"Could not open file " + filename.string() + " for reading."};
447 
448  // possibly add intermediate compression stream
449  secondary_stream = detail::make_secondary_istream(*primary_stream, filename);
450 
451  // initialise format handler or throw if format is not found
452  detail::set_format(format, filename);
453  }
454  /* NOTE(h-2): Curiously we do not need a user-defined deduction guide for the above constructor.
455  * A combination of default template parameters and auto-deduction guides works as expected,
456  * independent of whether the second/optional parameter is specified or not, i.e. it is possible
457  * to auto-deduct and overwrite a single template parameter out of the four if the optional parameter
458  * is specified and use the default otherwise.
459  */
460 
475  template <input_stream stream_t,
476  sequence_file_input_format file_format>
480  sequence_file_input(stream_t & stream,
481  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
482  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
483  primary_stream{&stream, stream_deleter_noop},
484  format{detail::sequence_file_input_format_exposer<file_format>{}}
485  {
486  static_assert(list_traits::contains<file_format, valid_formats>,
487  "You selected a format that is not in the valid_formats of this file.");
488 
489  // possibly add intermediate compression stream
490  secondary_stream = detail::make_secondary_istream(*primary_stream);
491  }
492 
494  template <input_stream stream_t,
495  sequence_file_input_format file_format>
499  sequence_file_input(stream_t && stream,
500  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
501  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
502  primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
503  format{detail::sequence_file_input_format_exposer<file_format>{}}
504  {
505  static_assert(list_traits::contains<file_format, valid_formats>,
506  "You selected a format that is not in the valid_formats of this file.");
507 
508  // possibly add intermediate compression stream
509  secondary_stream = detail::make_secondary_istream(*primary_stream);
510  }
512 
532  {
533  // buffer first record
534  if (!first_record_was_read)
535  {
536  read_next_record();
537  first_record_was_read = true;
538  }
539 
540  return {*this};
541  }
542 
556  sentinel end() noexcept
557  {
558  return {};
559  }
560 
584  reference front() noexcept
585  {
586  return *begin();
587  }
589 
591  sequence_file_input_options<typename traits_type::sequence_legal_alphabet,
593 
594 protected:
596 
599  record_type record_buffer;
602 
610  static void stream_deleter_noop(std::basic_istream<stream_char_type> *) {}
612  static void stream_deleter_default(std::basic_istream<stream_char_type> * ptr) { delete ptr; }
613 
615  stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
617  stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
618 
620  bool first_record_was_read{false};
622  bool at_end{false};
623 
625  using format_type = typename detail::variant_from_tags<valid_formats,
626  detail::sequence_file_input_format_exposer>::type;
628  format_type format;
630 
632  void read_next_record()
633  {
634  // clear the record
635  record_buffer.clear();
636 
637  // at end if we could not read further
638  if ((std::istreambuf_iterator<stream_char_type>{*secondary_stream} ==
640  {
641  at_end = true;
642  return;
643  }
644 
645  assert(!format.valueless_by_exception());
646  std::visit([&] (auto & f)
647  {
648  // read new record
650  {
651  f.read_sequence_record(*secondary_stream,
652  options,
653  detail::get_or_ignore<field::seq_qual>(record_buffer),
654  detail::get_or_ignore<field::id>(record_buffer),
655  detail::get_or_ignore<field::seq_qual>(record_buffer));
656  }
657  else
658  {
659  f.read_sequence_record(*secondary_stream,
660  options,
661  detail::get_or_ignore<field::seq>(record_buffer),
662  detail::get_or_ignore<field::id>(record_buffer),
663  detail::get_or_ignore<field::qual>(record_buffer));
664  }
665  }, format);
666  }
667 
669  friend iterator;
670 };
671 
677 template <input_stream stream_type,
679  sequence_file_input_format file_format>
680 sequence_file_input(stream_type & stream,
681  file_format const &)
683  typename sequence_file_input<>::selected_field_ids, // default field ids.
684  type_list<file_format>>;
685 
687 template <input_stream stream_type,
688  sequence_file_input_format file_format>
689 sequence_file_input(stream_type && stream,
690  file_format const &)
692  typename sequence_file_input<>::selected_field_ids, // default field ids.
693  type_list<file_format>>;
694 
696 template <input_stream stream_type,
697  sequence_file_input_format file_format,
698  detail::fields_specialisation selected_field_ids>
699 sequence_file_input(stream_type && stream,
700  file_format const &,
701  selected_field_ids const &)
703  selected_field_ids,
704  type_list<file_format>>;
705 
707 template <input_stream stream_type,
708  sequence_file_input_format file_format,
709  detail::fields_specialisation selected_field_ids>
710 sequence_file_input(stream_type & stream,
711  file_format const &,
712  selected_field_ids const &)
714  selected_field_ids,
715  type_list<file_format>>;
717 
718 } // namespace seqan3
719 #pragma GCC diagnostic pop
seqan3::field::seq_qual
Sequence and qualities combined in one range.
seqan3::sequence_file_input::end
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition: input.hpp:556
seqan3::sequence_file_input::id_type
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:370
seqan3::field::seq
The "sequence", usually a range of nucleotides or amino acids.
qualified.hpp
Provides quality alphabet composites.
input_format_concept.hpp
Provides seqan3::sequence_file_input_format and auxiliary classes.
fstream
std::basic_string
pack_algorithm.hpp
Provides algorithms for meta programming, parameter packs and seqan3::type_list.
sequence_container
A more refined container concept than seqan3::container.
seqan3::type_list
meta::list< types... > type_list
Type that contains multiple types, an alias for meta::list.
Definition: type_list.hpp:31
concept.hpp
Stream concepts.
seqan3::sequence_file_input::const_reference
void const_reference
The const_reference type is void, because files are not const-iterable.
Definition: input.hpp:396
vector
seqan3::sequence_file_input::options
sequence_file_input_options< typename traits_type::sequence_legal_alphabet, selected_field_ids::contains(field::seq_qual)> options
The options are public and its members can be set directly.
Definition: input.hpp:592
explicitly_convertible_to
Resolves to std::ranges::explicitly_convertible_to<type1, type2>().
seqan3::format_genbank
The GenBank format.
Definition: format_genbank.hpp:72
format_sam.hpp
Provides the seqan3::format_sam.
seqan3::views::move
const auto move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
seqan3::dna15
The 15 letter DNA alphabet, containing all IUPAC smybols minus the gap.
Definition: dna15.hpp:48
seqan3::sequence_file_input::sentinel
std::ranges::default_sentinel_t sentinel
The type returned by end().
Definition: input.hpp:406
seqan3::sequence_file_input::sequence_file_input
sequence_file_input()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
record.hpp
Provides the seqan3::record template and the seqan3::field enum.
std::function
seqan3::sequence_file_input::record_type
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:384
filesystem
This header includes C++17 filesystem support and imports it into namespace seqan3::filesystem (indep...
seqan3::format_fasta
The FastA format.
Definition: format_fasta.hpp:80
seqan3::sequence_file_input::field_types
type_list< sequence_type, id_type, quality_type, sequence_quality_type > field_types
The previously defined types aggregated in a seqan3::type_list.
Definition: input.hpp:380
seqan3::sequence_file_input::quality_type
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:373
std::filesystem::path
seqan3::sequence_file_input::traits_type
traits_type_ traits_type
A traits type that defines aliases and template for storage of the fields.
Definition: input.hpp:328
seqan3::pack_traits::contains
constexpr bool contains
Whether a type occurs in a pack or not.
Definition: traits.hpp:193
seqan3::fields
A class template that holds a choice of seqan3::field.
Definition: record.hpp:165
seqan3::sequence_file_input_options
The options type defines various option members that influence the behaviour of all or some formats.
Definition: input_options.hpp:25
seqan3::sequence_file_input::valid_formats
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition: input.hpp:332
seqan3::sequence_file_input::sequence_file_input
sequence_file_input(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition: input.hpp:441
same_as
The concept std::same_as<T, U> is satisfied if and only if T and U denote the same type.
format_fastq.hpp
all.hpp
Meta-header for the nucleotide submodule; includes all headers from alphabet/nucleotide/.
aa27.hpp
Provides seqan3::aa27, container aliases and string literals.
seqan3::sequence_file_input::begin
iterator begin()
Returns an iterator to current position in the file.
Definition: input.hpp:531
seqan3::sequence_file_input::operator=
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.
seqan3::sequence_file_input::sequence_type
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:367
exception.hpp
Provides exceptions used in the I/O module.
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:36
seqan3::sequence_file_input::size_type
size_t size_type
An unsigned integer type, usually std::size_t.
Definition: input.hpp:398
seqan3::sequence_file_input::selected_field_ids
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition: input.hpp:330
std::istreambuf_iterator
std::make_signed_t
seqan3::phred42
Quality type for traditional Sanger and modern Illumina Phred scores (typical range).
Definition: phred42.hpp:43
format_fasta.hpp
seqan3::sequence_file_input::front
reference front() noexcept
Return the record we are currently at in the file.
Definition: input.hpp:584
misc_input.hpp
Provides various utility functions required only for input.
seqan3::sequence_file_input::iterator
detail::in_file_iterator< sequence_file_input > iterator
The iterator type of this view (an input iterator).
Definition: input.hpp:402
char.hpp
Provides alphabet adaptations for standard char types.
seqan3::sequence_file_input_default_traits_aa
A traits type that specifies input as amino acids.
Definition: input.hpp:173
concatenated_sequences.hpp
Provides seqan3::concatenated_sequences.
seqan3::sequence_file_input::const_iterator
void const_iterator
The const iterator type is void, because files are not const-iterable.
Definition: input.hpp:404
seqan3::sequence_file_input::stream_char_type
char stream_char_type
Character type of the stream(s).
Definition: input.hpp:334
seqan3::sequence_file_input
A class for reading sequence files, e.g. FASTA, FASTQ ...
Definition: input.hpp:320
seqan3::sequence_file_input::sequence_file_input
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:499
seqan3::format_embl
The EMBL format.
Definition: format_embl.hpp:76
seqan3::format_fastq
The FastQ format.
Definition: format_fastq.hpp:78
cassert
seqan3::field
field
An enumerator for the fields used in file formats.
Definition: record.hpp:64
seqan3::sequence_file_input_default_traits_dna::id_alphabet
char id_alphabet
The alphabet for an identifier string is char.
Definition: input.hpp:155
format_embl.hpp
seqan3::record< detail::select_types_with_ids_t< field_types, field_ids, selected_field_ids >, selected_field_ids >
sequence_file_input_format
The generic concept for sequence file in formats.
seqan3::qualified
Joins an arbitrary alphabet with a quality alphabet.
Definition: qualified.hpp:55
seqan3::format_sam
The SAM format (tag).
Definition: format_sam.hpp:124
std::visit
T visit(T... args)
seqan3::field::qual
The qualities, usually in phred-score notation.
seqan3::sequence_file_input::sequence_file_input
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:480
seqan3::dna5
The five letter DNA alphabet of A,C,G,T and the unknown character N.
Definition: dna5.hpp:48
in_file_iterator.hpp
Provides the seqan3::detail::in_file_iterator class template.
sequence_file_input_traits
The requirements a traits_type for seqan3::sequence_file_input must meet.
seqan3::sequence_file_input::~sequence_file_input
~sequence_file_input()=default
Destructor is defaulted.
traits.hpp
Provides traits for seqan3::type_list.
std::basic_istream
writable_alphabet
Refines seqan3::alphabet and adds assignability.
std::unique_ptr< std::basic_istream< stream_char_type >, std::function< void(std::basic_istream< stream_char_type > *)> >
writable_quality_alphabet
A concept that indicates whether a writable alphabet represents quality scores.
seqan3::aa27
The twenty-seven letter amino acid alphabet.
Definition: aa27.hpp:43
phred42.hpp
Provides seqan3::phred42 quality scores.
seqan3::sequence_file_input_default_traits_dna
The default traits for seqan3::sequence_file_input.
Definition: input.hpp:137
seqan3::sequence_file_input::sequence_quality_type
typename traits_type::template sequence_container< qualified< typename traits_type::sequence_alphabet, typename traits_type::quality_alphabet > > sequence_quality_type
The type of field::seq_qual (std::vector <seqan3::dna5q> by default).
Definition: input.hpp:377
format_genbank.hpp
Provides the seqan3::sequence_file_format_genbank class.
variant
std::ifstream
string