26 #include <seqan3/io/detail/record.hpp>
43 #ifdef SEQAN3_DEPRECATED_310
45 template <
typename tuple_t>
46 static constexpr
bool fourth_tuple_element_is_sequence =
false;
48 template <
typename tuple_t>
50 static constexpr
bool fourth_tuple_element_is_sequence<tuple_t>
156 template <detail::fields_specialisation selected_field_ids_ =
170 detail::type_list_of_sam_file_output_formats valid_formats_ = type_list<format_sam, format_bam>,
171 typename ref_ids_type = ref_info_not_given>
183 #ifdef SEQAN3_DEPRECATED_310
186 using ref_ids_type_t = ref_ids_type;
190 using stream_char_type = char;
208 #ifdef SEQAN3_DEPRECATED_310
213 "You selected the deprecated seqan3::field::ref_seq. It will not be available in the record.");
215 "You selected the deprecated seqan3::field::evalue. It will not be available in the record.");
217 "You selected the deprecated seqan3::field::bit_score. It will not be available in the record.");
220 static_assert([] () constexpr
222 for (
field f : selected_field_ids::as_array)
223 if (!field_ids::contains(f))
227 "You selected a field that is not valid for alignment files, "
228 "please refer to the documentation of "
229 "seqan3::sam_file_output::field_ids for the accepted values.");
247 using iterator = detail::out_file_iterator<sam_file_output>;
299 primary_stream->rdbuf()->pubsetbuf(stream_buffer.
data(), stream_buffer.
size());
301 std::ios_base::out | std::ios::binary);
304 if (!primary_stream->good())
305 throw file_open_error{
"Could not open file " + filename.
string() +
" for writing."};
308 secondary_stream = detail::make_secondary_ostream(*primary_stream, filename);
311 detail::set_format(format, filename);
330 template <output_stream stream_type, sam_file_output_format file_format>
332 requires std::same_as<typename std::remove_reference_t<stream_type>::char_type,
stream_char_type>
335 file_format
const & SEQAN3_DOXYGEN_ONLY(format_tag),
337 primary_stream{&stream, stream_deleter_noop},
338 secondary_stream{&stream, stream_deleter_noop},
339 format{detail::sam_file_output_format_exposer<file_format>{}}
341 static_assert(list_traits::contains<file_format, valid_formats>,
342 "You selected a format that is not in the valid_formats of this file.");
346 template <output_stream stream_type, sam_file_output_format file_format>
348 requires std::same_as<typename std::remove_reference_t<stream_type>::char_type,
stream_char_type>
351 file_format
const & SEQAN3_DOXYGEN_ONLY(format_tag),
353 primary_stream{
new stream_type{
std::move(stream)}, stream_deleter_default},
354 secondary_stream{&*primary_stream, stream_deleter_noop},
355 format{detail::sam_file_output_format_exposer<file_format>{}}
357 static_assert(list_traits::contains<file_format, valid_formats>,
358 "You selected a format that is not in the valid_formats of this file.");
391 template <
typename ref_
ids_type_, std::ranges::forward_range ref_lengths_type>
393 requires std::same_as<std::remove_reference_t<ref_ids_type_>, ref_ids_type>
396 ref_ids_type_ && ref_ids,
397 ref_lengths_type && ref_lengths,
402 initialise_header_information(ref_ids, ref_lengths);
426 template <output_stream stream_type,
428 typename ref_ids_type_,
429 std::ranges::forward_range ref_lengths_type>
431 requires std::same_as<std::remove_reference_t<ref_ids_type_>, ref_ids_type>
434 ref_ids_type_ && ref_ids,
435 ref_lengths_type && ref_lengths,
436 file_format
const & SEQAN3_DOXYGEN_ONLY(format_tag),
440 initialise_header_information(ref_ids, ref_lengths);
507 template <
typename record_t>
510 requires detail::record_like<record_t>
516 write_record(detail::get_or<field::header_ptr>(r,
nullptr),
520 detail::get_or<field::offset>(r, 0u),
522 detail::get_or<field::ref_id>(r, std::ignore),
524 detail::get_or<field::alignment>(r, default_align_t{}),
527 detail::get_or<field::mapq>(r, 0u),
528 detail::get_or<field::mate>(r, default_mate_t{}),
530 detail::get_or<field::evalue>(r, 0u),
531 detail::get_or<field::bit_score>(r, 0u));
555 #ifdef SEQAN3_DEPRECATED_310
556 template <
typename tuple_t>
563 push_back_tuple(std::forward<tuple_t>(t));
598 template <
typename tuple_t>
601 && (std::tuple_size_v<std::remove_cvref_t<tuple_t>> <= 3)
603 push_back_tuple(std::forward<tuple_t>(t));
610 template <
typename tuple_t>
613 && (std::tuple_size_v<std::remove_cvref_t<tuple_t>> > 3)
616 push_back_tuple(std::forward<tuple_t>(t));
623 template <
typename tuple_t>
626 && (std::tuple_size_v<std::remove_cvref_t<tuple_t>> > 3)
636 detail::get_or<2>(t, 0u),
638 detail::get_or<4>(t, std::ignore),
640 detail::get_or<6>(t, default_align_t{}),
642 detail::get_or<8>(t, 0u),
645 detail::get_or<11>(t, default_mate_t{}),
646 detail::get_or<12>(t, sam_tag_dictionary{}),
649 detail::get_or<15>(t,
nullptr)
656 template <
typename tuple_t>
657 void push_back_tuple(tuple_t && t)
659 template <
typename tuple_t>
670 write_record(detail::get_or<selected_field_ids::index_of(
field::header_ptr)>(t,
nullptr),
674 detail::get_or<selected_field_ids::index_of(
field::offset)>(t, 0u),
676 detail::get_or<selected_field_ids::index_of(
field::ref_id)>(t, std::ignore),
678 detail::get_or<selected_field_ids::index_of(
field::alignment)>(t, default_align_t{}),
681 detail::get_or<selected_field_ids::index_of(
field::mapq)>(t, 0u),
682 detail::get_or<selected_field_ids::index_of(
field::mate)>(t, default_mate_t{}),
683 detail::get_or<selected_field_ids::index_of(
field::tags)>(t, sam_tag_dictionary{}),
684 detail::get_or<selected_field_ids::index_of(
field::evalue)>(t, 0u),
711 template <
typename arg_t,
typename ...arg_types>
716 #ifdef SEQAN3_DEPRECATED_310
724 #ifdef SEQAN3_DEPRECATED_310
727 template <
typename arg_t,
typename ...arg_types>
728 requires (
sizeof...(arg_types) + 1 <= 3)
739 template <
typename seq_t,
typename id_t,
typename offset_t,
typename ref_id_t,
typename ...arg_types>
741 void
emplace_back(seq_t && seq, id_t &&
id, offset_t && offset, ref_id_t && ref_id, arg_types && ... args)
751 template <
typename seq_t,
typename id_t,
typename offset_t,
typename ref_seq_t,
typename ...arg_types>
756 [[maybe_unused]] ref_seq_t && ref_seq,
757 arg_types && ... args)
760 #pragma GCC diagnostic push
761 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
764 #pragma GCC diagnostic pop
790 template <
typename rng_t>
796 for (
auto &&
record : range)
829 template <
typename rng_t>
840 template <
typename rng_t>
859 return *secondary_stream;
875 if constexpr (std::same_as<ref_ids_type, ref_info_not_given>)
876 throw std::logic_error{
"Please construct your file with reference id and length information in order "
877 "to properly initialise the header before accessing it."};
899 stream_ptr_t primary_stream{
nullptr, stream_deleter_noop};
901 stream_ptr_t secondary_stream{
nullptr, stream_deleter_noop};
904 using format_type =
typename detail::variant_from_tags<
valid_formats,
905 detail::sam_file_output_format_exposer>::type;
912 using header_type = sam_file_header<std::conditional_t<std::same_as<ref_ids_type, ref_info_not_given>,
920 template <
typename ref_
ids_type_,
typename ref_lengths_type>
921 void initialise_header_information(ref_ids_type_ && ref_ids, ref_lengths_type && ref_lengths)
925 header_ptr = std::make_unique<sam_file_header<ref_ids_type>>(std::forward<ref_ids_type_>(ref_ids));
927 for (int32_t idx = 0; idx < std::ranges::distance(ref_ids); ++idx)
929 header_ptr->ref_id_info.emplace_back(ref_lengths[idx],
"");
931 if constexpr (std::ranges::contiguous_range<std::ranges::range_reference_t<ref_ids_type_>> &&
932 std::ranges::sized_range<std::ranges::range_reference_t<ref_ids_type_>> &&
933 std::ranges::borrowed_range<std::ranges::range_reference_t<ref_ids_type_>>)
946 template <
typename record_header_ptr_t,
typename ...pack_type>
947 void write_record(record_header_ptr_t && record_header_ptr, pack_type && ...remainder)
949 static_assert((
sizeof...(pack_type) == 15),
"Wrong parameter list passed to write_record.");
951 assert(!
format.valueless_by_exception());
956 if constexpr (!std::same_as<record_header_ptr_t, std::nullptr_t>)
958 f.write_alignment_record(*secondary_stream,
961 std::forward<pack_type>(remainder)...);
963 else if constexpr (std::same_as<ref_ids_type, ref_info_not_given>)
965 f.write_alignment_record(*secondary_stream,
968 std::forward<pack_type>(remainder)...);
972 f.write_alignment_record(*secondary_stream,
975 std::forward<pack_type>(remainder)...);
992 template <detail::fields_specialisation selected_field_
ids>
999 template <output_stream stream_type,
1008 template <output_stream stream_type,
1017 template <output_stream stream_type,
1025 template <output_stream stream_type,
1032 std::ranges::forward_range ref_ids_type,
1033 std::ranges::forward_range ref_lengths_type>
1040 template <std::ranges::forward_range ref_ids_type,
1041 std::ranges::forward_range ref_lengths_type>
1048 template <output_stream stream_type,
1049 std::ranges::forward_range ref_ids_type,
1050 std::ranges::forward_range ref_lengths_type,
1057 template <output_stream stream_type,
1058 std::ranges::forward_range ref_ids_type,
1059 std::ranges::forward_range ref_lengths_type,
1066 template <output_stream stream_type,
1067 std::ranges::forward_range ref_ids_type,
1068 std::ranges::forward_range ref_lengths_type,
1070 sam_file_output(stream_type &&, ref_ids_type &&, ref_lengths_type &&, file_format
const &)
1076 template <output_stream stream_type,
1077 std::ranges::forward_range ref_ids_type,
1078 std::ranges::forward_range ref_lengths_type,
A class for writing alignment files, e.g. SAM, BAL, BLAST, ...
Definition: output.hpp:173
void const_reference
The const reference type (void).
Definition: output.hpp:241
sam_file_output(stream_type &&stream, ref_ids_type_ &&ref_ids, ref_lengths_type &&ref_lengths, file_format const &format_tag, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from an existing stream and with specified format.
Definition: output.hpp:433
sam_file_output(stream_type &, ref_ids_type &&, ref_lengths_type &&, file_format const &, selected_field_ids const &) -> sam_file_output< selected_field_ids, type_list< file_format >, std::remove_reference_t< ref_ids_type >>
Deduces selected_field_ids, the valid format, and the ref_ids_type from input.
void size_type
The size type (void).
Definition: output.hpp:243
sam_file_output(stream_type &&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: output.hpp:350
sam_file_output(sam_file_output const &)=delete
Copy construction is explicitly deleted, because you can't have multiple access to the same file.
friend sam_file_output & operator|(rng_t &&range, sam_file_output &f)
Write a range of records (or tuples) to the file.
Definition: output.hpp:830
sam_file_output()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
detail::out_file_iterator< sam_file_output > iterator
The iterator type of this view (an output iterator).
Definition: output.hpp:247
sam_file_output & operator=(rng_t &&range)
Write a range of records (or tuples) to the file.
Definition: output.hpp:791
sam_file_output & operator=(sam_file_output const &)=delete
Copy assignment is explicitly deleted, because you can't have multiple access to the same file.
sam_file_output(std::filesystem::path const &, ref_ids_type &&, ref_lengths_type &&) -> sam_file_output< typename sam_file_output<>::selected_field_ids, typename sam_file_output<>::valid_formats, std::remove_reference_t< ref_ids_type >>
Deduces ref_ids_type from input. Valid formats, and selected_field_ids are set to the default.
auto & header()
Access the file's header.
Definition: output.hpp:873
std::default_sentinel_t sentinel
The type returned by end().
Definition: output.hpp:251
char stream_char_type
Character type of the stream(s).
Definition: output.hpp:190
sam_file_output(stream_type &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: output.hpp:334
iterator begin() noexcept
Returns an iterator to current position in the file.
Definition: output.hpp:465
sam_file_output(std::filesystem::path const &, ref_ids_type &&, ref_lengths_type &&, selected_field_ids const &) -> sam_file_output< selected_field_ids, typename sam_file_output<>::valid_formats, std::remove_reference_t< ref_ids_type >>
Deduces selected_field_ids and ref_ids_type from input. valid_formats is set to the default.
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition: output.hpp:484
static constexpr bool is_default_selected_field_ids
brief Does selected_field_ids contain all fields like in the default case?
Definition: output.hpp:210
sam_file_output(std::filesystem::path, selected_field_ids const &) -> sam_file_output< selected_field_ids, typename sam_file_output<>::valid_formats, ref_info_not_given >
Deduces selected_field_ids from input and sets sam_file_output::ref_ids_type to seqan3::detail::ref_i...
void push_back(record_t &&r)
Write a seqan3::record to the file.
Definition: output.hpp:508
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition: output.hpp:180
sam_file_output & operator=(sam_file_output &&)=default
Move assignment is defaulted.
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition: output.hpp:182
void reference
The reference type (void).
Definition: output.hpp:239
sam_file_output(stream_type &, ref_ids_type &&, ref_lengths_type &&, file_format const &) -> sam_file_output< typename sam_file_output<>::selected_field_ids, type_list< file_format >, std::remove_reference_t< ref_ids_type >>
Deduces the valid format, and the ref_ids_type from input. selected_field_ids set to the default.
sam_file_output(stream_type &&, file_format const &) -> sam_file_output< typename sam_file_output<>::selected_field_ids, type_list< file_format >, ref_info_not_given >
Deduces the valid format from input and sets sam_file_output::ref_ids_type to seqan3::detail::ref_inf...
sam_file_output(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition: output.hpp:295
sam_file_output(stream_type &&, ref_ids_type &&, ref_lengths_type &&, file_format const &) -> sam_file_output< typename sam_file_output<>::selected_field_ids, type_list< file_format >, std::remove_reference_t< ref_ids_type >>
Deduces the valid format, and the ref_ids_type from input. selected_field_ids set to the default.
void push_back(tuple_t &&t)
Write a record in form of a std::tuple to the file.
Definition: output.hpp:557
void value_type
The value type (void).
Definition: output.hpp:237
~sam_file_output()=default
Destructor is defaulted.
sam_file_output_options options
The options are public and its members can be set directly.
Definition: output.hpp:852
sam_file_output(stream_type &&, ref_ids_type &&, ref_lengths_type &&, file_format const &, selected_field_ids const &) -> sam_file_output< selected_field_ids, type_list< file_format >, std::remove_reference_t< ref_ids_type >>
Deduces selected_field_ids, the valid format, and the ref_ids_type from input.
sam_file_output(sam_file_output &&)=default
Move construction is defaulted.
sam_file_output(stream_type &, file_format const &, selected_field_ids const &) -> sam_file_output< selected_field_ids, type_list< file_format >, ref_info_not_given >
Deduces selected_field_ids, and the valid format from input and sets sam_file_output::ref_ids_type to...
void emplace_back(arg_t &&arg, arg_types &&... args) requires(!is_default_selected_field_ids)
Write a record to the file by passing individual fields.
Definition: output.hpp:715
sam_file_output(stream_type &&, file_format const &, selected_field_ids const &) -> sam_file_output< selected_field_ids, type_list< file_format >, ref_info_not_given >
Deduces selected_field_ids, and the valid format from input and sets sam_file_output::ref_ids_type to...
sam_file_output(std::filesystem::path const &filename, ref_ids_type_ &&ref_ids, ref_lengths_type &&ref_lengths, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition: output.hpp:395
friend sam_file_output operator|(rng_t &&range, sam_file_output &&f)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: output.hpp:841
void const_iterator
The const iterator type is void, because files are not const-iterable.
Definition: output.hpp:249
sam_file_output(stream_type &, file_format const &) -> sam_file_output< typename sam_file_output<>::selected_field_ids, type_list< file_format >, ref_info_not_given >
Deduces the valid format from input and sets sam_file_output::ref_ids_type to seqan3::detail::ref_inf...
The SAM tag dictionary class that stores all optional SAM fields.
Definition: sam_tag_dictionary.hpp:332
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
@ none
None of the flags below are set.
field
An enumerator for the fields used in file formats.
Definition: record.hpp:63
@ flag
The alignment flag (bit information), uint16_t value.
@ ref_offset
Sequence (seqan3::field::ref_seq) relative start position (0-based), unsigned value.
@ ref_seq
The (reference) "sequence" information, usually a range of nucleotides or amino acids.
@ alignment
The (pairwise) alignment stored in an object that models seqan3::detail::pairwise_alignment.
@ cigar
The cigar vector (std::vector<seqan3::cigar>) representing the alignment in SAM/BAM format.
@ mapq
The mapping quality of the seqan3::field::seq alignment, usually a Phred-scaled score.
@ bit_score
The bit score (statistical significance indicator), unsigned value.
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
@ mate
The mate pair information given as a std::tuple of reference name, offset and template length.
@ header_ptr
A pointer to the seqan3::sam_file_header object storing header information.
@ ref_id
The identifier of the (reference) sequence that seqan3::field::seq was aligned to.
@ evalue
The e-value (length normalized bit score), double value.
@ id
The identifier, usually a string.
@ tags
The optional tags in the SAM format, stored in a dictionary.
@ seq
The "sequence", usually a range of nucleotides or amino acids.
@ qual
The qualities, usually in Phred score notation.
constexpr bool contains
Whether a type occurs in a type list or not.
Definition: traits.hpp:231
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
std::remove_cv_t< std::remove_reference_t< t > > remove_cvref_t
Return the input type with const, volatile and references removed (type trait).
Definition: basic.hpp:45
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:74
The generic concept for a (biological) sequence.
Whether a type behaves like a tuple.
Provides exceptions used in the I/O module.
Provides various utility functions required only for output.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Provides the seqan3::detail::out_file_iterator class template.
Adaptations of concepts from the Ranges TS.
Provides the seqan3::record template and the seqan3::field enum.
Provides seqan3::detail::record_like.
Provides seqan3::sam_file_output_options.
Provides helper data structures for the seqan3::sam_file_output.
A class template that holds a choice of seqan3::field.
Definition: record.hpp:172
The class template that file records are based on; behaves like an std::tuple.
Definition: record.hpp:235
Type tag which indicates that no reference information has been passed to the alignment file on const...
Definition: sam_flag.hpp:23
The options type defines various option members that influence the behavior of all or some formats.
Definition: output_options.hpp:23
Type that contains multiple types.
Definition: type_list.hpp:29
Provides seqan3::tuple_like.
Provides traits for seqan3::type_list.