23#include <seqan3/io/detail/record.hpp>
57template <detail::fields_specialisation selected_field_ids_ = fields<
field::seq,
68 detail::type_list_of_sam_file_output_formats valid_formats_ = type_list<format_sam, format_bam>,
69 typename ref_ids_type = ref_info_not_given>
99 "The field::offset is deprecated. It is already stored in the field::cigar as soft clipping (S) "
100 "at the front and not needed otherwise.");
103 "The seqan3::field::alignment was removed from the allowed fields for seqan3::sam_file_output. "
104 "Only seqan3::field::cigar is supported. seqan3::cigar_from_alignment on how to get a CIGAR string "
105 "from an alignment.");
110 for (
field f : selected_field_ids::as_array)
111 if (!field_ids::contains(
f))
115 "You selected a field that is not valid for SAM files, "
116 "please refer to the documentation of "
117 "seqan3::sam_file_output::field_ids for the accepted values.");
135 using iterator = detail::out_file_iterator<sam_file_output>;
161 if (header_has_been_written || !primary_stream)
164 assert(!format.valueless_by_exception());
169 if constexpr (std::same_as<ref_ids_type, ref_info_not_given>)
170 f.write_header(*secondary_stream,
options, std::ignore);
172 f.write_header(*secondary_stream,
options, *header_ptr);
204 primary_stream{new
std::ofstream{}, stream_deleter_default}
206 primary_stream->rdbuf()->pubsetbuf(stream_buffer.
data(), stream_buffer.
size());
208 ->open(filename, std::ios_base::out | std::ios::binary);
211 if (!primary_stream->good())
215 secondary_stream = detail::make_secondary_ostream(*primary_stream, filename);
218 detail::set_format(format, filename);
237 template <output_stream stream_type, sam_file_output_format file_format>
238 requires std::same_as<typename std::remove_reference_t<stream_type>::char_type,
stream_char_type>
242 primary_stream{&stream, stream_deleter_noop},
243 secondary_stream{&stream, stream_deleter_noop},
244 format{detail::sam_file_output_format_exposer<
file_format>{}}
246 static_assert(list_traits::contains<file_format, valid_formats>,
247 "You selected a format that is not in the valid_formats of this file.");
251 template <output_stream stream_type, sam_file_output_format file_format>
252 requires std::same_as<typename std::remove_reference_t<stream_type>::char_type, stream_char_type>
256 primary_stream{new
stream_type{
std::move(stream)}, stream_deleter_default},
257 secondary_stream{&*primary_stream, stream_deleter_noop},
258 format{detail::sam_file_output_format_exposer<
file_format>{}}
260 static_assert(list_traits::contains<file_format, valid_formats>,
261 "You selected a format that is not in the valid_formats of this file.");
294 template <
typename ref_
ids_type_, std::ranges::forward_range ref_lengths_type>
295 requires std::same_as<std::remove_reference_t<ref_ids_type_>, ref_ids_type>
303 initialise_header_information(std::forward<ref_ids_type_>(ref_ids),
328 template <output_stream stream_type,
329 sam_file_output_format file_format,
330 typename ref_ids_type_,
331 std::ranges::forward_range ref_lengths_type>
332 requires std::same_as<std::remove_reference_t<ref_ids_type_>, ref_ids_type>
340 initialise_header_information(std::forward<ref_ids_type_>(ref_ids),
408 template <
typename record_t>
410 requires detail::record_like<record_t>
414 write_record(detail::get_or<field::header_ptr>(
r,
nullptr),
419 detail::get_or<field::ref_id>(
r, std::ignore),
422 detail::get_or<field::flag>(
r, sam_flag::none),
423 detail::get_or<field::mapq>(
r, 0u),
426 detail::get_or<field::evalue>(
r, 0u),
427 detail::get_or<field::bit_score>(
r, 0u));
451 template <
typename tuple_t>
458 write_record(detail::get_or<selected_field_ids::index_of(field::header_ptr)>(t,
nullptr),
459 detail::get_or<selected_field_ids::index_of(field::seq)>(t,
std::string_view{}),
460 detail::get_or<selected_field_ids::index_of(field::qual)>(t,
std::string_view{}),
461 detail::get_or<selected_field_ids::index_of(field::id)>(t,
std::string_view{}),
462 detail::get_or<selected_field_ids::index_of(field::ref_seq)>(t,
std::string_view{}),
463 detail::get_or<selected_field_ids::index_of(field::ref_id)>(t, std::ignore),
466 detail::get_or<selected_field_ids::index_of(field::flag)>(t, sam_flag::none),
467 detail::get_or<selected_field_ids::index_of(field::mapq)>(t, 0u),
468 detail::get_or<selected_field_ids::index_of(field::mate)>(t,
default_mate_t{}),
470 detail::get_or<selected_field_ids::index_of(field::evalue)>(t, 0u),
471 detail::get_or<selected_field_ids::index_of(field::bit_score)>(t, 0u));
498 requires (
sizeof...(arg_types) + 1 <= selected_field_ids::size)
525 template <
typename rng_t>
562 template <
typename rng_t>
571 template <
typename rng_t>
588 return *secondary_stream;
604 if constexpr (std::same_as<ref_ids_type, ref_info_not_given>)
605 throw std::logic_error{
"Please construct your file with reference id and length information in order "
606 "to properly initialise the header before accessing it."};
614 bool header_has_been_written{
false};
635 stream_ptr_t primary_stream{
nullptr, stream_deleter_noop};
637 stream_ptr_t secondary_stream{
nullptr, stream_deleter_noop};
640 using format_type =
typename detail::variant_from_tags<valid_formats, detail::sam_file_output_format_exposer>::type;
647 using header_type = sam_file_header<
654 template <
typename ref_
ids_type_,
typename ref_lengths_type>
655 void initialise_header_information(ref_ids_type_ && ref_ids, ref_lengths_type && ref_lengths)
657 assert(std::ranges::size(ref_ids) == std::ranges::size(ref_lengths));
659 header_ptr = std::make_unique<sam_file_header<ref_ids_type>>(std::forward<ref_ids_type_>(ref_ids));
661 for (int32_t idx = 0; idx < std::ranges::distance(
header_ptr->ref_ids()); ++idx)
663 header_ptr->ref_id_info.emplace_back(ref_lengths[idx],
"");
665 if constexpr (std::ranges::contiguous_range<std::ranges::range_reference_t<ref_ids_type_>>
666 && std::ranges::sized_range<std::ranges::range_reference_t<ref_ids_type_>>
667 && std::ranges::borrowed_range<std::ranges::range_reference_t<ref_ids_type_>>)
680 template <
typename record_header_ptr_t,
typename... pack_type>
681 void write_record(record_header_ptr_t && record_header_ptr, pack_type &&... remainder)
683 static_assert((
sizeof...(pack_type) == 13),
"Wrong parameter list passed to write_record.");
685 assert(!
format.valueless_by_exception());
691 if constexpr (!std::same_as<record_header_ptr_t, std::nullptr_t>)
693 f.write_alignment_record(*secondary_stream,
696 std::forward<pack_type>(remainder)...);
698 else if constexpr (std::same_as<ref_ids_type, ref_info_not_given>)
700 f.write_alignment_record(*secondary_stream,
703 std::forward<pack_type>(remainder)...);
707 f.write_alignment_record(*secondary_stream,
710 std::forward<pack_type>(remainder)...);
715 header_has_been_written =
true;
730template <detail::fields_specialisation selected_field_
ids>
755template <output_stream stream_type, sam_file_output_format file_format>
762template <output_stream stream_type, sam_file_output_format file_format>
776template <std::ranges::forward_range ref_
ids_type, std::ranges::forward_range ref_lengths_type>
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
A class for writing SAM files, both SAM and its binary representation BAM are supported.
Definition io/sam_file/output.hpp:71
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.
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 io/sam_file/output.hpp:572
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(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.
sam_file_output & operator=(rng_t &&range)
Write a range of records (or tuples) to the file.
Definition io/sam_file/output.hpp:526
sam_file_output(sam_file_output const &)=delete
Copy construction is explicitly deleted, because you can't have multiple access to the same file.
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 io/sam_file/output.hpp:135
friend sam_file_output & operator|(rng_t &&range, sam_file_output &f)
Write a range of records (or tuples) to the file.
Definition io/sam_file/output.hpp:563
void emplace_back(arg_t &&arg, arg_types &&... args)
Write a record to the file by passing individual fields.
Definition io/sam_file/output.hpp:499
std::default_sentinel_t sentinel
The type returned by end().
Definition io/sam_file/output.hpp:139
char stream_char_type
Character type of the stream(s).
Definition io/sam_file/output.hpp:82
iterator begin() noexcept
Returns an iterator to current position in the file.
Definition io/sam_file/output.hpp:366
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition io/sam_file/output.hpp:385
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 io/sam_file/output.hpp:296
~sam_file_output()
The destructor will write the header if it has not been written before.
Definition io/sam_file/output.hpp:156
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...
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 & operator=(sam_file_output &&)=default
Move assignment is defaulted.
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(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(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 io/sam_file/output.hpp:239
void push_back(record_t &&r)
Write a seqan3::record to the file.
Definition io/sam_file/output.hpp:409
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 io/sam_file/output.hpp:202
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 io/sam_file/output.hpp:333
sam_file_output_options options
The options are public and its members can be set directly.
Definition io/sam_file/output.hpp:581
sam_file_output(sam_file_output &&)=default
Move construction is defaulted.
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 io/sam_file/output.hpp:253
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(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 push_back(tuple_t &&t)
Write a record in form of a std::tuple to the file.
Definition io/sam_file/output.hpp:452
auto & header()
Access the file's header.
Definition io/sam_file/output.hpp:602
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.
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:327
field
An enumerator for the fields used in file formats.
Definition record.hpp:60
@ flag
The alignment flag (bit information), uint16_t value.
@ ref_offset
Sequence (seqan3::field::ref_seq) relative start position (0-based), unsigned value.
@ 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.
@ 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.
@ 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.
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:26
SeqAn specific customisations in the standard namespace.
Provides the seqan3::detail::out_file_iterator class template.
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:125
Thrown if there is an unspecified filesystem or stream error while opening, e.g. permission problem.
Definition io/exception.hpp:36
The class template that file records are based on; behaves like a std::tuple.
Definition record.hpp:190
Type tag which indicates that no reference information has been passed to the SAM file on constructio...
Definition sam_flag.hpp:21
The options type defines various option members that influence the behavior of all or some formats.
Definition sam_file/output_options.hpp:23
Provides traits for seqan3::type_list.
Provides seqan3::tuple_like.