SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
seqan3::sequence_file_input< traits_type_, selected_field_ids_, valid_formats_ > Class Template Reference

A class for reading sequence files, e.g. FASTA, FASTQ ... More...

#include <seqan3/io/sequence_file/input.hpp>

Public Types

using field_ids = fields< field::seq, field::id, field::qual, field::seq_qual >
 The subset of seqan3::field IDs that are valid for this file; order corresponds to the types in field_types.
 
Template arguments

Exposed as member types for public access.

using traits_type = traits_type_
 A traits type that defines aliases and template for storage of the fields.
 
using selected_field_ids = selected_field_ids_
 A seqan3::fields list with the fields selected for the record.
 
using valid_formats = valid_formats_
 A seqan3::type_list with the possible formats.
 
using stream_char_type = char
 Character type of the stream(s).
 
Field types and record type

These types are relevant for record/row-based reading; they may be manipulated via the traits_type to achieve different storage behaviour.

using sequence_type = typename traits_type::template sequence_container< typename traits_type::sequence_alphabet >
 The type of field::seq (std::vector <seqan3::dna5> by default).
 
using id_type = typename traits_type::template id_container< typename traits_type::id_alphabet >
 The type of field::id (std::string by defaul).
 
using quality_type = typename traits_type::template quality_container< typename traits_type::quality_alphabet >
 The type of field::qual (std::vector <seqan3::phred42> by default).
 
using sequence_quality_type = typename traits_type::template sequence_container< qualified< typename traits_type::sequence_alphabet, typename traits_type::quality_alphabet > >
 The type of field::seq_qual (std::vector <seqan3::dna5q> by default).
 
using field_types = type_list< sequence_type, id_type, quality_type, sequence_quality_type >
 The previously defined types aggregated in a seqan3::type_list.
 
using record_type = record< detail::select_types_with_ids_t< field_types, field_ids, selected_field_ids >, selected_field_ids >
 The type of the record, a specialisation of seqan3::record; acts as a tuple of the selected field types.
 
Range associated types

The types necessary to facilitate the behaviour of an input range (used in record-wise reading).

using value_type = record_type
 The value_type is the record_type.
 
using reference = record_type &
 The reference type.
 
using const_reference = void
 The const_reference type is void, because files are not const-iterable.
 
using size_type = size_t
 An unsigned integer type, usually std::size_t.
 
using difference_type = std::make_signed_t< size_t >
 A signed integer type, usually std::ptrdiff_t.
 
using iterator = detail::in_file_iterator< sequence_file_input >
 The iterator type of this view (an input iterator).
 
using const_iterator = void
 The const iterator type is void, because files are not const-iterable.
 
using sentinel = std::default_sentinel_t
 The type returned by end().
 

Public Member Functions

Constructors, destructor and assignment
 sequence_file_input ()=delete
 Default constructor is explicitly deleted, you need to give a stream or file name.
 
 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_inputoperator= (sequence_file_input const &)=delete
 Copy assignment is explicitly deleted, because you can't have multiple access to the same file.
 
 sequence_file_input (sequence_file_input &&)=default
 Move construction is defaulted.
 
sequence_file_inputoperator= (sequence_file_input &&)=default
 Move assignment is defaulted.
 
 ~sequence_file_input ()=default
 Destructor is defaulted.
 
 sequence_file_input (std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
 Construct from filename. More...
 
template<input_stream stream_t, sequence_file_input_format file_format>
 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. More...
 
template<input_stream stream_t, sequence_file_input_format file_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 only in what argument(s) it accepts.
 
Range interface

Provides functions for record based reading of the file.

iterator begin ()
 Returns an iterator to current position in the file. More...
 
sentinel end () noexcept
 Returns a sentinel for comparison with iterator. More...
 
reference front () noexcept
 Return the record we are currently at in the file. More...
 

Public Attributes

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.
 

Related Functions

(Note that these are not member functions.)

Type deduction guides
template<input_stream stream_type, sequence_file_input_format file_format>
 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.
 
template<input_stream stream_type, sequence_file_input_format file_format>
 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 only in what argument(s) it accepts.
 
template<input_stream stream_type, sequence_file_input_format file_format, detail::fields_specialisation selected_field_ids>
 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.
 
template<input_stream stream_type, sequence_file_input_format file_format, detail::fields_specialisation selected_field_ids>
 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 only in what argument(s) it accepts.
 

Detailed Description

template<sequence_file_input_traits traits_type_ = sequence_file_input_default_traits_dna, detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>, detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl, format_fasta, format_fastq, format_genbank, format_sam>>
class seqan3::sequence_file_input< traits_type_, selected_field_ids_, valid_formats_ >

A class for reading sequence files, e.g. FASTA, FASTQ ...

Template Parameters
traits_typeAn auxiliary type that defines certain member types and constants, must satisfy seqan3::sequence_file_input_traits.
selected_field_idsA seqan3::fields type with the list and order of desired record entries; all fields must be in seqan3::sequence_file_input::field_ids.
valid_formatsA seqan3::type_list of the selectable formats (each must meet seqan3::sequence_file_input_format).

Introduction

Sequence files are the most generic and common biological files. Well-known formats include FastA and FastQ, but some may also be interested in treating SAM or BAM files as sequence files, discarding the alignment.

The Sequence file abstraction supports reading four different fields:

  1. seqan3::field::seq
  2. seqan3::field::id
  3. seqan3::field::qual
  4. seqan3::field::seq_qual (sequence and qualities in one range)

The first three fields are retrieved by default (and in that order). The last field may be selected to have sequence and qualities directly stored in a more memory-efficient combined container. If you select the last field you may not select seqan3::field::seq or seqan3::field::qual.

Construction and specialisation

This class comes with two constructors, one for construction from a file name and one for construction from an existing stream and a known format. The first one automatically picks the format based on the extension of the file name. The second can be used if you have a non-file stream, like std::cin or std::istringstream, that you want to read from and/or if you cannot use file-extension based detection, but know that your input file has a certain format.

In most cases the template parameters are deduced completely automatically:

int main()
{
using seqan3::operator""_dna4;
auto tmp_file = std::filesystem::temp_directory_path() / "my.fasta";
{
// Create a /tmp/my.fasta file.
fout.emplace_back("ACGT"_dna4, "TEST1");
fout.emplace_back("AGGCTGA"_dna4, "Test2");
fout.emplace_back("GGAGTATAATATATATATATATAT"_dna4, "Test3");
}
// FastA with DNA sequences assumed, regular std::ifstream taken as stream
}

Reading from an std::istringstream:

#include <sstream>
#include <utility>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
// ^ no need to specify the template arguments
}

Note that this is not the same as writing sequence_file_input<> (with angle brackets). In the latter case they are explicitly set to their default values, in the former case automatic deduction happens which chooses different parameters depending on the constructor arguments. For opening from file, sequence_file_input<> would have also worked, but for opening from stream it would not have.

In some cases, you do need to specify the arguments, e.g. if you want to read amino acids:

#include <sstream>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
}

You can define your own traits type to further customise the types used by and returned by this class, see seqan3::sequence_file_default_traits_dna for more details. As mentioned above, specifying at least one template parameter yourself means that you loose automatic deduction so if you want to read amino acids and want to read from a string stream you need to give all types yourself:

#include <sstream>
// ... input had amino acid sequences
auto input = R"(> TEST1
FQTWE
> Test2
KYRTW
> Test3
EEYQTWEEFARAAEKLYLTDPMKV)";
int main()
{ // Use amino acid traits below
seqan3::field::qual>,
sequence_file_input_type fin{std::istringstream{input}, seqan3::format_fasta{}};
}

Reading record-wise

You can iterate over this file record-wise:

#include <sstream>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
for (auto & rec : fin)
{
seqan3::debug_stream << "ID: " << seqan3::get<seqan3::field::id>(rec) << '\n';
seqan3::debug_stream << "SEQ: " << seqan3::get<seqan3::field::seq>(rec) << '\n';
// a quality field also exists, but is not printed, because we know it's empty for FastA files.
}
}

In the above example, rec has the type record_type which is a specialisation of seqan3::record and behaves like an std::tuple (that's why we can access it via get). Instead of using the seqan3::field based interface on the record, you could also use std::get<0> or even std::get<dna4_vector> to retrieve the sequence, but it is not recommended, because it is more error-prone.

Note: It is important to write auto & and not just auto, otherwise you will copy the record on every iteration. Since the buffer gets "refilled" on every iteration, you can also move the data out of the record if you want to store it somewhere without copying:

#include <sstream>
#include <utility>
#include <vector>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
using record_type = typename decltype(fin)::record_type;
for (auto & rec : fin)
records.push_back(std::move(rec));
}

Reading record-wise (decomposed records)

Instead of using get on the record, you can also use structured bindings to decompose the record into its elements:

auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
for (auto & [seq, id, qual] : fin)
{
seqan3::debug_stream << "ID: " << id << '\n';
seqan3::debug_stream << "SEQ: " << seq << '\n';
seqan3::debug_stream << "EMPTY QUAL." << qual << '\n'; // qual is empty for FastA files
}
}

In this case you immediately get the two elements of the tuple: seq of sequence_type and id of id_type. But beware: with structured bindings you do need to get the order of elements correctly!

Reading record-wise (custom fields)

If you want to skip specific fields from the record you can pass a non-empty fields trait object to the sequence_file_input constructor to select the fields that should be read from the input. For example to choose a combined field for SEQ and QUAL (see above). Or to never actually read the QUAL, if you don't need it. The following snippets demonstrate the usage of such a fields trait object.

#include <sstream>
#include <vector>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
using seqan3::get;
for (auto & [id, seq_qual] : fin) // the order is now different, "id" comes first, because it was specified first
{
seqan3::debug_stream << "ID: " << id << '\n';
// sequence and qualities are part of the same vector, of type std::vector<dna5q>
seqan3::debug_stream << "SEQ: " << (seq_qual | seqan3::views::get<0>) << '\n'; // sequence string is extracted
seqan3::debug_stream << "QUAL: " << (seq_qual | seqan3::views::get<1>) << '\n'; // quality string is extracted
}
}

When reading a file, all fields not present in the file (but requested implicitly or via the selected_field_ids parameter) are ignored.

Views on files

Since SeqAn files are ranges, you can also create views over files. A useful example is to filter the records based on certain criteria, e.g. minimum length of the sequence field:

#include <sstream>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
using seqan3::get;
#if !SEQAN3_WORKAROUND_GCC_93983
auto minimum_length5_filter = std::views::filter([] (auto const & rec)
{
return std::ranges::size(get<seqan3::field::seq>(rec)) >= 5;
});
#endif // !SEQAN3_WORKAROUND_GCC_93983
#if SEQAN3_WORKAROUND_GCC_93983
for (auto & rec : fin /*| minimum_length5_filter*/) // only record with sequence length >= 5 will "appear"
#else // ^^^ workaround / no workaround vvv
for (auto & rec : fin | minimum_length5_filter) // only record with sequence length >= 5 will "appear"
#endif // SEQAN3_WORKAROUND_GCC_93983
{
seqan3::debug_stream << "IDs of seq_length >= 5: " << get<seqan3::field::id>(rec) << '\n';
// ...
}
}

End of file

You can check whether a file is at end by comparing begin() and end() (if they are the same, the file is at end).

Formats

We currently support reading the following formats:

Constructor & Destructor Documentation

◆ sequence_file_input() [1/2]

template<sequence_file_input_traits traits_type_ = sequence_file_input_default_traits_dna, detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>, detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl, format_fasta, format_fastq, format_genbank, format_sam>>
seqan3::sequence_file_input< traits_type_, selected_field_ids_, valid_formats_ >::sequence_file_input ( std::filesystem::path  filename,
selected_field_ids const &  fields_tag = selected_field_ids{} 
)
inline

Construct from filename.

Parameters
[in]filenamePath to the file you wish to open.
[in]fields_tagA seqan3::fields tag. [optional]
Exceptions
seqan3::file_open_errorIf the file could not be opened, e.g. non-existant, non-readable, unknown format.

In addition to the file name, you may specify a custom seqan3::fields type which may be easier than defining all the template parameters.

Decompression

This constructor transparently applies a decompression stream on top of the file stream in case the file is detected as being compressed. See the section on compression and decompression for more information.

◆ sequence_file_input() [2/2]

template<sequence_file_input_traits traits_type_ = sequence_file_input_default_traits_dna, detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>, detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl, format_fasta, format_fastq, format_genbank, format_sam>>
template<input_stream stream_t, sequence_file_input_format file_format>
seqan3::sequence_file_input< traits_type_, selected_field_ids_, valid_formats_ >::sequence_file_input ( stream_t &  stream,
file_format const &  format_tag,
selected_field_ids const &  fields_tag = selected_field_ids{} 
)
inline

Construct from an existing stream and with specified format.

Template Parameters
file_formatThe format of the file in the stream, must satisfy seqan3::sequence_file_input_format.
Parameters
[in]streamThe stream to operate on; must be derived of std::basic_istream.
[in]format_tagThe file format tag.
[in]fields_tagA seqan3::fields tag. [optional]

Decompression

This constructor transparently applies a decompression stream on top of the stream in case it is detected as being compressed. See the section on compression and decompression for more information.

Member Function Documentation

◆ begin()

template<sequence_file_input_traits traits_type_ = sequence_file_input_default_traits_dna, detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>, detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl, format_fasta, format_fastq, format_genbank, format_sam>>
iterator seqan3::sequence_file_input< traits_type_, selected_field_ids_, valid_formats_ >::begin ( )
inline

Returns an iterator to current position in the file.

Returns
An iterator pointing to the current position in the file.
Exceptions
seqan3::format_errorEquals end() if the file is at end.

Complexity

Constant.

Exceptions

Throws seqan3::format_error if the first record could not be read into the buffer.

◆ end()

template<sequence_file_input_traits traits_type_ = sequence_file_input_default_traits_dna, detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>, detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl, format_fasta, format_fastq, format_genbank, format_sam>>
sentinel seqan3::sequence_file_input< traits_type_, selected_field_ids_, valid_formats_ >::end ( )
inlinenoexcept

Returns a sentinel for comparison with iterator.

Returns
Iterator to the first element.

This element acts as a placeholder; attempting to dereference it results in undefined behaviour.

Complexity

Constant.

Exceptions

No-throw guarantee.

◆ front()

template<sequence_file_input_traits traits_type_ = sequence_file_input_default_traits_dna, detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>, detail::type_list_of_sequence_file_input_formats valid_formats_ = type_list<format_embl, format_fasta, format_fastq, format_genbank, format_sam>>
reference seqan3::sequence_file_input< traits_type_, selected_field_ids_, valid_formats_ >::front ( )
inlinenoexcept

Return the record we are currently at in the file.

Returns
A reference to the currently buffered record.

This function returns a reference to the currently buffered record, it is identical to dereferencing begin(), but begin also always points to the current record on single pass input ranges:

#include <sstream>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
auto it = std::ranges::begin(fin);
// the following are equivalent:
auto & rec0 = *it;
auto & rec1 = fin.front();
// Note: rec0 and rec1 are references and become invalid after incrementing "it"!
}

It most situations using the iterator interface or a range-based for-loop are preferable to using front(), because you can only move to the next record via the iterator.

In any case, don't forget the reference! If you want to save the data from the record elsewhere, use move:

#include <sstream>
#include <utility>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
auto rec0 = std::move(fin.front());
}

Complexity

Constant.

Exceptions

No-throw guarantee.


The documentation for this class was generated from the following file:
debug_stream.hpp
Provides seqan3::debug_stream and related types.
sstream
dna4.hpp
Provides seqan3::dna4, container aliases and string literals.
utility
seqan3::type_list
meta::list< types... > type_list
Type that contains multiple types, an alias for meta::list.
Definition: type_list.hpp:31
std::filesystem::temp_directory_path
T temp_directory_path(T... args)
vector
seqan3::get
constexpr auto const & get(configuration< configs_t... > const &config) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: configuration.hpp:627
seqan3::field::id
@ id
The identifier, usually a string.
std::istringstream
input.hpp
Provides seqan3::sequence_file_input and corresponding traits classes.
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:379
filesystem
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
seqan3::format_fasta
The FastA format.
Definition: format_fasta.hpp:80
seqan3::fields
A class template that holds a choice of seqan3::field.
Definition: record.hpp:166
std::vector::push_back
T push_back(T... args)
seqan3::seq
constexpr sequenced_policy seq
Global execution policy object for sequenced execution policy.
Definition: execution.hpp:54
output.hpp
Provides seqan3::sequence_file_output and corresponding traits classes.
seqan3::views::move
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
seqan3::sequence_file_output
A class for writing sequence files, e.g. FASTA, FASTQ ...
Definition: output.hpp:168
std::filesystem::remove
T remove(T... args)
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
seqan3::sequence_file_input_default_traits_aa
A traits type that specifies input as amino acids.
Definition: input.hpp:169
ranges
Adaptations of concepts from the Ranges TS.
seqan3::sequence_file_output::emplace_back
void emplace_back(arg_t &&arg, arg_types &&... args)
Write a record to the file by passing individual fields.
Definition: output.hpp:459
seqan3::sequence_file_input
A class for reading sequence files, e.g. FASTA, FASTQ ...
Definition: input.hpp:316
std::ranges::begin
T begin(T... args)
seqan3::debug_stream
debug_stream_type debug_stream
A global instance of seqan3::debug_stream_type.
Definition: debug_stream.hpp:42
get.hpp
Provides seqan3::views::get.
type_list.hpp
Provides seqan3::type_list.