32 #include <seqan3/io/detail/record.hpp>
168 template <detail::fields_specialisation selected_field_ids_ =
184 detail::type_list_of_alignment_file_output_formats valid_formats_ = type_list<format_sam, format_bam>,
185 typename ref_ids_type = ref_info_not_given>
219 static_assert([] () constexpr
221 for (
field f : selected_field_ids::as_array)
222 if (!field_ids::contains(f))
226 "You selected a field that is not valid for alignment files, "
227 "please refer to the documentation of "
228 "seqan3::alignment_file_output::field_ids for the accepted values.");
230 static_assert([] () constexpr
235 "You may not select field::alignment and field::cigar at the same time.");
253 using iterator = detail::out_file_iterator<alignment_file_output>;
303 primary_stream{
new std::ofstream{filename, std::ios_base::out | std::ios::binary}, stream_deleter_default}
306 if (!primary_stream->good())
307 throw file_open_error{
"Could not open file " + filename.
string() +
" for writing."};
310 secondary_stream = detail::make_secondary_ostream(*primary_stream, filename);
313 detail::set_format(format, filename);
332 template <output_stream stream_type, alignment_file_output_format file_format>
337 file_format
const & SEQAN3_DOXYGEN_ONLY(format_tag),
339 primary_stream{&stream, stream_deleter_noop},
340 secondary_stream{&stream, stream_deleter_noop},
341 format{detail::alignment_file_output_format_exposer<file_format>{}}
343 static_assert(list_traits::contains<file_format, valid_formats>,
344 "You selected a format that is not in the valid_formats of this file.");
348 template <output_stream stream_type, alignment_file_output_format file_format>
353 file_format
const & SEQAN3_DOXYGEN_ONLY(format_tag),
355 primary_stream{
new stream_type{
std::move(stream)}, stream_deleter_default},
356 secondary_stream{&*primary_stream, stream_deleter_noop},
357 format{detail::alignment_file_output_format_exposer<file_format>{}}
359 static_assert(list_traits::contains<file_format, valid_formats>,
360 "You selected a format that is not in the valid_formats of this file.");
393 template <
typename ref_
ids_type_, std::ranges::forward_range ref_lengths_type>
398 ref_ids_type_ && ref_ids,
399 ref_lengths_type && ref_lengths,
404 initialise_header_information(ref_ids, ref_lengths);
428 template <output_stream stream_type,
430 typename ref_ids_type_,
431 std::ranges::forward_range ref_lengths_type>
436 ref_ids_type_ && ref_ids,
437 ref_lengths_type && ref_lengths,
438 file_format
const & SEQAN3_DOXYGEN_ONLY(format_tag),
442 initialise_header_information(ref_ids, ref_lengths);
509 template <
typename record_t>
513 requires { requires detail::is_type_specialisation_of_v<remove_cvref_t<record_t>,
record>; }
519 write_record(detail::get_or<field::header_ptr>(r,
nullptr),
523 detail::get_or<field::offset>(r, 0u),
525 detail::get_or<field::ref_id>(r, std::ignore),
527 detail::get_or<field::alignment>(r, default_align_t{}),
530 detail::get_or<field::mapq>(r, 0u),
531 detail::get_or<field::mate>(r, default_mate_t{}),
532 detail::get_or<field::tags>(r, sam_tag_dictionary{}),
533 detail::get_or<field::evalue>(r, 0u),
534 detail::get_or<field::bit_score>(r, 0u));
558 template <
typename tuple_t>
568 write_record(detail::get_or<selected_field_ids::index_of(
field::header_ptr)>(t,
nullptr),
572 detail::get_or<selected_field_ids::index_of(
field::offset)>(t, 0u),
574 detail::get_or<selected_field_ids::index_of(
field::ref_id)>(t, std::ignore),
576 detail::get_or<selected_field_ids::index_of(
field::alignment)>(t, default_align_t{}),
579 detail::get_or<selected_field_ids::index_of(
field::mapq)>(t, 0u),
580 detail::get_or<selected_field_ids::index_of(
field::mate)>(t, default_mate_t{}),
582 detail::get_or<selected_field_ids::index_of(
field::evalue)>(t, 0u),
609 template <
typename arg_t,
typename ...arg_types>
636 template <
typename rng_t>
642 for (
auto &&
record : range)
675 template <
typename rng_t>
686 template <
typename rng_t>
705 return *secondary_stream;
722 throw std::logic_error{
"Please construct your file with reference id and length information in order "
723 "to properly initialise the header before accessing it."};
743 stream_ptr_t primary_stream{
nullptr, stream_deleter_noop};
745 stream_ptr_t secondary_stream{
nullptr, stream_deleter_noop};
748 using format_type =
typename detail::variant_from_tags<
valid_formats,
749 detail::alignment_file_output_format_exposer>::type;
756 using header_type = alignment_file_header<std::conditional_t<std::same_as<ref_ids_type, ref_info_not_given>,
764 template <
typename ref_
ids_type_,
typename ref_lengths_type>
765 void initialise_header_information(ref_ids_type_ && ref_ids, ref_lengths_type && ref_lengths)
769 header_ptr = std::make_unique<alignment_file_header<ref_ids_type>>(std::forward<ref_ids_type_>(ref_ids));
771 for (int32_t idx = 0; idx < std::ranges::distance(ref_ids); ++idx)
773 header_ptr->ref_id_info.emplace_back(ref_lengths[idx],
"");
775 if constexpr (std::ranges::contiguous_range<reference_t<ref_ids_type_>> &&
776 std::ranges::sized_range<reference_t<ref_ids_type_>> &&
790 template <
typename record_header_ptr_t,
typename ...pack_type>
791 void write_record(record_header_ptr_t && record_header_ptr, pack_type && ...remainder)
793 static_assert((
sizeof...(pack_type) == 15),
"Wrong parameter list passed to write_record.");
795 assert(!format.valueless_by_exception());
802 f.write_alignment_record(*secondary_stream,
805 std::forward<pack_type>(remainder)...);
809 f.write_alignment_record(*secondary_stream,
812 std::forward<pack_type>(remainder)...);
816 f.write_alignment_record(*secondary_stream,
819 std::forward<pack_type>(remainder)...);
836 template <detail::fields_specialisation selected_field_
ids>
838 -> alignment_file_output<selected_field_ids,
845 template <output_stream stream_type,
847 detail::fields_specialisation selected_field_ids>
848 alignment_file_output(stream_type &&, file_format
const &, selected_field_ids
const &)
849 -> alignment_file_output<selected_field_ids,
850 type_list<file_format>,
856 template <output_stream stream_type,
858 detail::fields_specialisation selected_field_ids>
859 alignment_file_output(stream_type &, file_format
const &, selected_field_ids
const &)
860 -> alignment_file_output<selected_field_ids,
861 type_list<file_format>,
867 template <output_stream stream_type,
869 alignment_file_output(stream_type &&, file_format
const &)
871 type_list<file_format>,
877 template <output_stream stream_type,
879 alignment_file_output(stream_type &, file_format
const &)
881 type_list<file_format>,
885 template <detail::fields_specialisation selected_field_ids,
886 std::ranges::forward_range ref_ids_type,
887 std::ranges::forward_range ref_lengths_type>
891 selected_field_ids
const &)
892 -> alignment_file_output<selected_field_ids,
897 template <std::ranges::forward_range ref_ids_type,
898 std::ranges::forward_range ref_lengths_type>
907 template <output_stream stream_type,
908 std::ranges::forward_range ref_ids_type,
909 std::ranges::forward_range ref_lengths_type,
911 detail::fields_specialisation selected_field_ids>
912 alignment_file_output(stream_type &&,
916 selected_field_ids
const &)
917 -> alignment_file_output<selected_field_ids,
918 type_list<file_format>,
922 template <output_stream stream_type,
923 std::ranges::forward_range ref_ids_type,
924 std::ranges::forward_range ref_lengths_type,
926 detail::fields_specialisation selected_field_ids>
927 alignment_file_output(stream_type &,
931 selected_field_ids
const &)
932 -> alignment_file_output<selected_field_ids,
933 type_list<file_format>,
937 template <output_stream stream_type,
938 std::ranges::forward_range ref_ids_type,
939 std::ranges::forward_range ref_lengths_type,
941 alignment_file_output(stream_type &&,
946 type_list<file_format>,
950 template <output_stream stream_type,
951 std::ranges::forward_range ref_ids_type,
952 std::ranges::forward_range ref_lengths_type,
954 alignment_file_output(stream_type &,
959 type_list<file_format>,