SeqAn3  3.0.3
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 <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 
303 template <
305  detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>,
306  detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl,
307  format_fasta,
308  format_fastq,
310  format_sam>>
312 {
313 public:
319  using traits_type = traits_type_;
321  using selected_field_ids = selected_field_ids_;
323  using valid_formats = valid_formats_;
325  using stream_char_type = char;
327 
332 
333  static_assert([] () constexpr
334  {
335  for (field f : selected_field_ids::as_array)
336  if (!field_ids::contains(f))
337  return false;
338  return true;
339  }(),
340  "You selected a field that is not valid for sequence files, please refer to the documentation "
341  "of sequence_file_input::field_ids for the accepted values.");
342 
343  static_assert([] () constexpr
344  {
348  }(),
349  "You may not select field::seq_qual and either of field::seq and field::qual at the same time.");
350 
357  using sequence_type = typename traits_type::template sequence_container<
358  typename traits_type::sequence_alphabet>;
360  using id_type = typename traits_type::template id_container<
361  typename traits_type::id_alphabet>;
363  using quality_type = typename traits_type::template quality_container<
364  typename traits_type::quality_alphabet>;
365 #ifdef SEQAN3_DEPRECATED_310
369  using sequence_quality_type = typename traits_type::
370  template sequence_container<qualified<typename traits_type::sequence_alphabet,
371  typename traits_type::quality_alphabet>>;
372 #endif // SEQAN3_DEPRECATED_310
373 
376 
378  using record_type = sequence_record<detail::select_types_with_ids_t<field_types,
379  field_ids,
383 
393  using const_reference = void;
395  using size_type = size_t;
399  using iterator = detail::in_file_iterator<sequence_file_input>;
401  using const_iterator = void;
403  using sentinel = std::default_sentinel_t;
405 
420  ~sequence_file_input() = default;
421 
439  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
440  primary_stream{new std::ifstream{}, stream_deleter_default}
441  {
442  primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
443  static_cast<std::basic_ifstream<char> *>(primary_stream.get())->open(filename,
444  std::ios_base::in | std::ios::binary);
445 
446  if (!primary_stream->good())
447  throw file_open_error{"Could not open file " + filename.string() + " for reading."};
448 
449  // possibly add intermediate compression stream
450  secondary_stream = detail::make_secondary_istream(*primary_stream, filename);
451 
452  // initialise format handler or throw if format is not found
453  detail::set_format(format, filename);
454  }
455  /* NOTE(h-2): Curiously we do not need a user-defined deduction guide for the above constructor.
456  * A combination of default template parameters and auto-deduction guides works as expected,
457  * independent of whether the second/optional parameter is specified or not, i.e. it is possible
458  * to auto-deduct and overwrite a single template parameter out of the four if the optional parameter
459  * is specified and use the default otherwise.
460  */
461 
476  template <input_stream stream_t,
477  sequence_file_input_format file_format>
479  requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
481  sequence_file_input(stream_t & stream,
482  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
483  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
484  primary_stream{&stream, stream_deleter_noop},
485  format{detail::sequence_file_input_format_exposer<file_format>{}}
486  {
487  static_assert(list_traits::contains<file_format, valid_formats>,
488  "You selected a format that is not in the valid_formats of this file.");
489 
490  // possibly add intermediate compression stream
491  secondary_stream = detail::make_secondary_istream(*primary_stream);
492  }
493 
495  template <input_stream stream_t,
496  sequence_file_input_format file_format>
498  requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
500  sequence_file_input(stream_t && stream,
501  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
502  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
503  primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
504  format{detail::sequence_file_input_format_exposer<file_format>{}}
505  {
506  static_assert(list_traits::contains<file_format, valid_formats>,
507  "You selected a format that is not in the valid_formats of this file.");
508 
509  // possibly add intermediate compression stream
510  secondary_stream = detail::make_secondary_istream(*primary_stream);
511  }
513 
533  {
534  // buffer first record
535  if (!first_record_was_read)
536  {
537  read_next_record();
538  first_record_was_read = true;
539  }
540 
541  return {*this};
542  }
543 
557  sentinel end() noexcept
558  {
559  return {};
560  }
561 
585  reference front() noexcept
586  {
587  return *begin();
588  }
590 
592  sequence_file_input_options<typename traits_type::sequence_legal_alphabet,
594 
595 protected:
597 
601  record_type record_buffer;
603  std::vector<char> stream_buffer{std::vector<char>(1'000'000)};
605 
613  static void stream_deleter_noop(std::basic_istream<stream_char_type> *) {}
615  static void stream_deleter_default(std::basic_istream<stream_char_type> * ptr) { delete ptr; }
616 
618  stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
620  stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
621 
623  bool first_record_was_read{false};
625  bool at_end{false};
626 
628  using format_type = typename detail::variant_from_tags<valid_formats,
629  detail::sequence_file_input_format_exposer>::type;
631  format_type format;
633 
635  void read_next_record()
636  {
637  // clear the record
638  record_buffer.clear();
639 
640  // at end if we could not read further
641  if ((std::istreambuf_iterator<stream_char_type>{*secondary_stream} ==
643  {
644  at_end = true;
645  return;
646  }
647 
648  assert(!format.valueless_by_exception());
649  std::visit([&] (auto & f)
650  {
651  // read new record
653  {
654  f.read_sequence_record(*secondary_stream,
655  options,
656  detail::get_or_ignore<field::_seq_qual_deprecated>(record_buffer),
657  detail::get_or_ignore<field::id>(record_buffer),
658  detail::get_or_ignore<field::_seq_qual_deprecated>(record_buffer));
659  }
660  else
661  {
662  f.read_sequence_record(*secondary_stream,
663  options,
664  detail::get_or_ignore<field::seq>(record_buffer),
665  detail::get_or_ignore<field::id>(record_buffer),
666  detail::get_or_ignore<field::qual>(record_buffer));
667  }
668  }, format);
669  }
670 
672  friend iterator;
673 };
674 
681 template <input_stream stream_type,
682  sequence_file_input_format file_format>
683 sequence_file_input(stream_type & stream,
684  file_format const &)
686  typename sequence_file_input<>::selected_field_ids, // default field ids.
688 
690 template <input_stream stream_type,
691  sequence_file_input_format file_format>
692 sequence_file_input(stream_type && stream,
693  file_format const &)
695  typename sequence_file_input<>::selected_field_ids, // default field ids.
697 
699 template <input_stream stream_type,
700  sequence_file_input_format file_format,
701  detail::fields_specialisation selected_field_ids>
702 sequence_file_input(stream_type && stream,
703  file_format const &,
704  selected_field_ids const &)
708 
710 template <input_stream stream_type,
711  sequence_file_input_format file_format,
712  detail::fields_specialisation selected_field_ids>
713 sequence_file_input(stream_type & stream,
714  file_format const &,
715  selected_field_ids const &)
720 
721 } // 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:82
The FastQ format.
Definition: format_fastq.hpp:79
The GenBank format.
Definition: format_genbank.hpp:74
The SAM format (tag).
Definition: format_sam.hpp:128
Quality type for traditional Sanger and modern Illumina Phred scores.
Definition: phred42.hpp:47
Joins an arbitrary alphabet with a quality alphabet.
Definition: qualified.hpp:61
A class for reading sequence files, e.g. FASTA, FASTQ ...
Definition: input.hpp:312
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:361
void const_reference
The const_reference type is void, because files are not const-iterable.
Definition: input.hpp:393
std::default_sentinel_t sentinel
The type returned by end().
Definition: input.hpp:403
sequence_file_input(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition: input.hpp:438
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:375
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_options< typename traits_type::sequence_legal_alphabet, selected_field_ids::contains(field::_seq_qual_deprecated)> options
The options are public and its members can be set directly.
Definition: input.hpp:593
reference front() noexcept
Return the record we are currently at in the file.
Definition: input.hpp:585
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...
iterator begin()
Returns an iterator to current position in the file.
Definition: input.hpp:532
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:557
char stream_char_type
Character type of the stream(s).
Definition: input.hpp:325
size_t size_type
An unsigned integer type, usually std::size_t.
Definition: input.hpp:395
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:481
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:399
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:500
~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:401
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:364
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:358
traits_type_ traits_type
A traits type that defines aliases and template for storage of the fields.
Definition: input.hpp:319
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition: input.hpp:321
fields< field::seq, field::id, field::qual, field::_seq_qual_deprecated > field_ids
The subset of seqan3::field IDs that are valid for this file; order corresponds to the types in field...
Definition: input.hpp:331
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition: input.hpp:323
typename traits_type::template sequence_container< qualified< typename traits_type::sequence_alphabet, typename traits_type::quality_alphabet > > sequence_quality_type
[DEPRECATED] The type of field::seq_qual (std::vector <seqan3::dna5q> by default).
Definition: input.hpp:371
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:381
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.
T format(T... args)
T get(T... args)
field
An enumerator for the fields used in file formats.
Definition: record.hpp:62
@ _seq_qual_deprecated
[DEPRECATED] Sequence and qualities combined in one range. Use field::seq and field::qual instead.
@ seq
The "sequence", usually a range of nucleotides or amino acids.
@ qual
The qualities, usually in Phred score notation.
meta::list< types... > type_list
Type that contains multiple types, an alias for meta::list.
Definition: type_list.hpp:31
constexpr bool contains
Whether a type occurs in a type list or not.
Definition: traits.hpp:194
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:70
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 the seqan3::format_sam.
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:167
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:271
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
The options type defines various option members that influence the behaviour of all or some formats.
Definition: input_options.hpp:30
Provides traits for seqan3::type_list.
T visit(T... args)