SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
|
A class for reading SAM files, both SAM and its binary representation BAM are supported. More...
#include <seqan3/io/sam_file/input.hpp>
Public 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 (default std::vector<seqan3::dna5>). | |
using | id_type = typename traits_type::template id_container< char > |
The type of field::id (default std::string by default). | |
using | ref_sequence_type = std::conditional_t< std::same_as< typename traits_type::ref_sequences, ref_info_not_given >, dummy_ref_type, ref_sequence_sliced_type > |
The type of field::ref_seq (default depends on construction). | |
using | ref_id_type = std::optional< int32_t > |
The type of field::ref_id is fixed to std::optional<int32_t>. | |
using | ref_offset_type = std::optional< int32_t > |
The type of field::ref_offset is fixed to a std::optional<int32_t>. | |
using | mapq_type = uint8_t |
The type of field::mapq is fixed to uint8_t. | |
using | quality_type = typename traits_type::template quality_container< typename traits_type::quality_alphabet > |
The type of field::qual (default std::vector<seqan3::phred42>). | |
using | flag_type = sam_flag |
The type of field::flag is fixed to seqan3::sam_flag. | |
using | cigar_type = std::vector< cigar > |
The type of field::cigar is fixed to std::vector<cigar>. | |
using | mate_type = std::tuple< ref_id_type, ref_offset_type, int32_t > |
The type of field::mate is fixed to std::tuple<ref_id_type, ref_offset_type, int32_t>). | |
using | header_type = sam_file_header< typename traits_type::ref_ids > |
The type of field::header_ptr (default: sam_file_header<typename traits_type::ref_ids>). | |
using | field_types = type_list< sequence_type, id_type, ref_id_type, ref_offset_type, std::vector< cigar >, mapq_type, quality_type, flag_type, mate_type, sam_tag_dictionary, header_type * > |
The previously defined types aggregated in a seqan3::type_list. | |
using | field_ids = fields< field::seq, field::id, field::ref_id, field::ref_offset, field::cigar, field::mapq, field::qual, field::flag, field::mate, field::tags, field::header_ptr > |
The subset of seqan3::field tags valid for this file; order corresponds to the types in field_types. | |
using | record_type = sam_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< sam_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 | |
header_type & | header () |
Access the file's header. | |
Constructors, destructor and assignment | |
sam_file_input ()=delete | |
Default constructor is explicitly deleted, you need to give a stream or file name. | |
sam_file_input (sam_file_input const &)=delete | |
Copy construction is explicitly deleted because you cannot have multiple access to the same file. | |
sam_file_input & | operator= (sam_file_input const &)=delete |
Copy assignment is explicitly deleted because you cannot have multiple access to the same file. | |
sam_file_input (sam_file_input &&)=default | |
Move construction is defaulted. | |
sam_file_input & | operator= (sam_file_input &&)=default |
Move assignment is defaulted. | |
~sam_file_input ()=default | |
Destructor is defaulted. | |
sam_file_input (std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{}) | |
Construct from filename. | |
template<input_stream stream_t, sam_file_input_format file_format> requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type> | |
sam_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. | |
template<input_stream stream_t, sam_file_input_format file_format> requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type> | |
sam_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. | |
sam_file_input (std::filesystem::path filename, typename traits_type::ref_ids &ref_ids, typename traits_type::ref_sequences &ref_sequences, selected_field_ids const &fields_tag=selected_field_ids{}) | |
Construct from filename and given additional reference information. | |
template<input_stream stream_t, sam_file_input_format file_format> | |
sam_file_input (stream_t &stream, typename traits_type::ref_ids &ref_ids, typename traits_type::ref_sequences &ref_sequences, file_format const &format_tag, selected_field_ids const &fields_tag=selected_field_ids{}) | |
Construct from an existing stream and with specified format. | |
template<input_stream stream_t, sam_file_input_format file_format> | |
sam_file_input (stream_t &&stream, typename traits_type::ref_ids &ref_ids, typename traits_type::ref_sequences &ref_sequences, 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. | |
sentinel | end () noexcept |
Returns a sentinel for comparison with iterator. | |
reference | front () noexcept |
Return the record we are currently at in the file. | |
Public Attributes | |
sam_file_input_options< typename traits_type::sequence_legal_alphabet > | options |
The options are public and its members can be set directly. | |
Related Symbols | |
(Note that these are not member symbols.) | |
Type deduction guides | |
template<input_stream stream_type, sam_file_input_format file_format, detail::fields_specialisation selected_field_ids> | |
sam_file_input (stream_type &&stream, file_format const &, selected_field_ids const &) -> sam_file_input< typename sam_file_input<>::traits_type, selected_field_ids, type_list< file_format > > | |
Deduce selected fields, file_format, and default the rest. | |
template<input_stream stream_type, sam_file_input_format file_format, detail::fields_specialisation selected_field_ids> | |
sam_file_input (stream_type &stream, file_format const &, selected_field_ids const &) -> sam_file_input< typename sam_file_input<>::traits_type, selected_field_ids, type_list< file_format > > | |
Deduce selected fields, file_format, and default the rest. | |
template<input_stream stream_type, sam_file_input_format file_format> | |
sam_file_input (stream_type &&stream, file_format const &) -> sam_file_input< typename sam_file_input<>::traits_type, typename sam_file_input<>::selected_field_ids, type_list< file_format > > | |
Deduce file_format, and default the rest. | |
template<input_stream stream_type, sam_file_input_format file_format> | |
sam_file_input (stream_type &stream, file_format const &) -> sam_file_input< typename sam_file_input<>::traits_type, typename sam_file_input<>::selected_field_ids, type_list< file_format > > | |
Deduce file_format, and default the rest. | |
template<std::ranges::forward_range ref_ids_t, std::ranges::forward_range ref_sequences_t, detail::fields_specialisation selected_field_ids> | |
sam_file_input (std::filesystem::path path, ref_ids_t &, ref_sequences_t &, selected_field_ids const &) -> sam_file_input< sam_file_input_default_traits< std::remove_reference_t< ref_sequences_t >, std::remove_reference_t< ref_ids_t > >, selected_field_ids, typename sam_file_input<>::valid_formats > | |
Deduce selected fields, ref_sequences_t and ref_ids_t, default the rest. | |
template<std::ranges::forward_range ref_ids_t, std::ranges::forward_range ref_sequences_t> | |
sam_file_input (std::filesystem::path path, ref_ids_t &, ref_sequences_t &) -> sam_file_input< sam_file_input_default_traits< std::remove_reference_t< ref_sequences_t >, std::remove_reference_t< ref_ids_t > >, typename sam_file_input<>::selected_field_ids, typename sam_file_input<>::valid_formats > | |
Deduce ref_sequences_t and ref_ids_t, default the rest. | |
template<input_stream stream_type, std::ranges::forward_range ref_ids_t, std::ranges::forward_range ref_sequences_t, sam_file_input_format file_format, detail::fields_specialisation selected_field_ids> | |
sam_file_input (stream_type &&stream, ref_ids_t &, ref_sequences_t &, file_format const &, selected_field_ids const &) -> sam_file_input< sam_file_input_default_traits< std::remove_reference_t< ref_sequences_t >, std::remove_reference_t< ref_ids_t > >, selected_field_ids, type_list< file_format > > | |
Deduce selected fields, ref_sequences_t and ref_ids_t, and file format. | |
template<input_stream stream_type, std::ranges::forward_range ref_ids_t, std::ranges::forward_range ref_sequences_t, sam_file_input_format file_format, detail::fields_specialisation selected_field_ids> | |
sam_file_input (stream_type &stream, ref_ids_t &, ref_sequences_t &, file_format const &, selected_field_ids const &) -> sam_file_input< sam_file_input_default_traits< std::remove_reference_t< ref_sequences_t >, std::remove_reference_t< ref_ids_t > >, selected_field_ids, type_list< file_format > > | |
Deduce selected fields, ref_sequences_t and ref_ids_t, and file format. | |
template<input_stream stream_type, std::ranges::forward_range ref_ids_t, std::ranges::forward_range ref_sequences_t, sam_file_input_format file_format> | |
sam_file_input (stream_type &&stream, ref_ids_t &, ref_sequences_t &, file_format const &) -> sam_file_input< sam_file_input_default_traits< std::remove_reference_t< ref_sequences_t >, std::remove_reference_t< ref_ids_t > >, typename sam_file_input<>::selected_field_ids, type_list< file_format > > | |
Deduce ref_sequences_t and ref_ids_t, and file format. | |
template<input_stream stream_type, std::ranges::forward_range ref_ids_t, std::ranges::forward_range ref_sequences_t, sam_file_input_format file_format> | |
sam_file_input (stream_type &stream, ref_ids_t &, ref_sequences_t &, file_format const &) -> sam_file_input< sam_file_input_default_traits< std::remove_reference_t< ref_sequences_t >, std::remove_reference_t< ref_ids_t > >, typename sam_file_input<>::selected_field_ids, type_list< file_format > > | |
Deduce selected fields, ref_sequences_t and ref_ids_t, and file format. | |
A class for reading SAM files, both SAM and its binary representation BAM are supported.
traits_type | An auxiliary type that defines certain member types and constants, must model seqan3::sam_file_input_traits. |
selected_field_ids | A seqan3::fields type with the list and order of desired record entries; all fields must be in seqan3::sam_file_input::field_ids. |
valid_formats | A seqan3::type_list of the selectable formats (each must meet seqan3::sam_file_input_format). |
sam_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, sam_file_input<>
would have also worked, but for opening from stream it would not have. rec
has the type seqan3::sam_file_input::record_type which is a specialisation of seqan3::record and behaves like a 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.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:selected_field_ids
parameter) are ignored and the respective value in the record stays empty.get
on the record, you can also use structured bindings to decompose the record into its elements. Considering the example of reading only the flag and mapping quality like before you can also write:flag
of seqan3::sam_file_input::flag_type and mapq
of seqan3::sam_file_input::mapq_type.seqan3::aligned_sequence
s.The conversion from a CIGAR string to an alignment can be done with the function seqan3::alignment_from_cigar
. You need to pass the reference sequence with the position the read was aligned to and the read sequence. All of it is already in the record
when reading a SAM file:begin()
and end()
(if they are the same, the file is at its end).using seqan3::sam_file_input< traits_type_, selected_field_ids_, valid_formats_ >::field_ids = fields<field::seq, field::id, field::ref_id, field::ref_offset, field::cigar, field::mapq, field::qual, field::flag, field::mate, field::tags, field::header_ptr> |
The subset of seqan3::field tags valid for this file; order corresponds to the types in field_types.
The SAM file abstraction supports reading 10 different fields:
There exists one more field for SAM files, the seqan3::field::header_ptr, but this field is mostly used internally. Please see the seqan3::sam_file_output::header member function for details on how to access the seqan3::sam_file_header of the file.
using seqan3::sam_file_input< traits_type_, selected_field_ids_, valid_formats_ >::ref_id_type = std::optional<int32_t> |
The type of field::ref_id is fixed to std::optional<int32_t>.
To be consistent with the BAM format, the field::ref_id will hold the index to the actual reference information stored in the header. If a read is unmapped, the optional will remain valueless.
using seqan3::sam_file_input< traits_type_, selected_field_ids_, valid_formats_ >::ref_offset_type = std::optional<int32_t> |
The type of field::ref_offset is fixed to a std::optional<int32_t>.
The SAM format is 1-based and a 0 in the ref_offset field indicated an unmapped read. Since we convert 1-based positions to 0-based positions when reading the SAM format, we model the ref_offset_type as a std::optional. If the input value is 0, the std::optional will remain valueless.
using seqan3::sam_file_input< traits_type_, selected_field_ids_, valid_formats_ >::ref_sequence_type = std::conditional_t<std::same_as<typename traits_type::ref_sequences, ref_info_not_given>, dummy_ref_type, ref_sequence_sliced_type> |
The type of field::ref_seq (default depends on construction).
If no reference information are given on construction, this type deduces to a sized view that throws on access (since there is nothing to access anyway). If the reference information are given, the type is deduced to a view over the given input reference sequence type such that no sequence information is copied.
|
inline |
Construct from filename.
[in] | filename | Path to the file you wish to open. |
[in] | fields_tag | A seqan3::fields tag. [optional] |
seqan3::file_open_error | If the file could not be opened, e.g. non-existent, non-readable, unknown format. |
In addition to the file name, you may specify a custom seqan3::fields object (e.g. seqan3::fields<seqan3::field::seq>{}
) which may be easier than defining all the template parameters.
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.
|
inline |
Construct from an existing stream and with specified format.
stream_t | The stream type; must model seqan3::input_stream. |
file_format | The format of the file in the stream, must model seqan3::sam_file_input_format. |
[in] | stream | The stream to operate on; must be derived of std::basic_istream. |
[in] | format_tag | The file format tag. |
[in] | fields_tag | A seqan3::fields tag. [optional] |
In addition to the stream and the format, you may specify a custom seqan3::fields object (e.g. seqan3::fields<seqan3::field::seq>{}
) which may be easier than defining all the template parameters.
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.
|
inline |
Construct from filename and given additional reference information.
[in] | filename | Path to the file you wish to open. |
[in] | ref_ids | A range containing the reference ids that correspond to the SAM/BAM file. |
[in] | ref_sequences | A range containing the reference sequences that correspond to the SAM/BAM file. |
[in] | fields_tag | A seqan3::fields tag. [optional] |
seqan3::file_open_error | If the file could not be opened, e.g. non-existent, non-readable, unknown format. |
The reference information given by the IDs (names) and sequences will be used to keep the record entry seqan3::sam_file_input::record_type::reference_id()
consistent with the order imposed by ref_ids
. This way, you can use the value of seqan3::sam_file_input::record_type::reference_id()
to access the lists ref_ids
and ref_sequences
to retrieve the correct information for the current record.
In addition to the file name and reference information, you may specify a custom seqan3::fields object (e.g. seqan3::fields<seqan3::field::seq>{}
) which may be easier than defining all the template parameters.
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.
|
inline |
Construct from an existing stream and with specified format.
stream_t | The stream type; must model seqan3::input_stream. |
file_format | The format of the file in the stream; must model seqan3::sam_file_input_format. |
[in] | stream | The stream to operate on; must be derived of std::basic_istream. |
[in] | ref_ids | A range containing the reference ids that correspond to the SAM/BAM file. |
[in] | ref_sequences | A range containing the reference sequences that correspond to the SAM/BAM file. |
[in] | format_tag | The file format tag. |
[in] | fields_tag | A seqan3::fields tag. [optional] |
The reference information given by the IDs (names) and sequences will be used to keep the record entry seqan3::sam_file_input::record_type::reference_id()
consistent with the order imposed by ref_ids
. This way, you can use the value of seqan3::sam_file_input::record_type::reference_id()
to access the lists ref_ids
and ref_sequences
to retrieve the correct information for the current record.
In addition to the stream, reference information and format, you may specify a custom seqan3::fields object (e.g. seqan3::fields<seqan3::field::seq>{}
) which may be easier than defining all the template parameters.
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.
|
inline |
Returns an iterator to current position in the file.
seqan3::format_error |
Equals end() if the file is at end.
Constant.
Throws seqan3::format_error if the first record could not be read into the buffer.
|
inlinenoexcept |
Returns a sentinel for comparison with iterator.
This element acts as a placeholder; attempting to dereference it results in undefined behaviour.
Constant.
No-throw guarantee.
|
inlinenoexcept |
Return the record we are currently at in the file.
This function returns a reference to the currently buffered record, it is identical to dereferencing begin(), and begin also always points to the current record on single pass input ranges:
In 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:
Constant.
No-throw guarantee.
|
inline |
Access the file's header.
You can access the header directly after the construction with reference information of the file object.