Skip to content

Commit

Permalink
[FEATURE] Allow setting warning stream for sam file
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Feb 29, 2024
1 parent c44d090 commit 835abba
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
19 changes: 13 additions & 6 deletions include/seqan3/io/sam_file/detail/format_sam_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <seqan3/io/detail/misc.hpp>
#include <seqan3/io/sam_file/detail/cigar.hpp>
#include <seqan3/io/sam_file/header.hpp>
#include <seqan3/io/sam_file/input_options.hpp>
#include <seqan3/io/sam_file/output_format_concept.hpp>
#include <seqan3/utility/detail/type_name_as_string.hpp>
#include <seqan3/utility/views/repeat_n.hpp>
Expand Down Expand Up @@ -82,10 +83,11 @@ class format_sam_base
template <arithmetic arithmetic_target_type>
void read_arithmetic_field(std::string_view const & str, arithmetic_target_type & arithmetic_target);

template <typename stream_view_type, typename ref_ids_type, typename ref_seqs_type>
template <typename stream_view_type, typename ref_ids_type, typename ref_seqs_type, typename seq_legal_alph_type>
void read_header(stream_view_type && stream_view,
sam_file_header<ref_ids_type> & hdr,
ref_seqs_type & /*ref_id_to_pos_map*/);
ref_seqs_type & /*ref_id_to_pos_map*/,
sam_file_input_options<seq_legal_alph_type> const & options);

template <typename stream_t, typename header_type>
void write_header(stream_t & stream, sam_file_output_options const & options, header_type & header);
Expand Down Expand Up @@ -270,10 +272,11 @@ inline void format_sam_base::read_arithmetic_field(std::string_view const & str,
* not in a correct state (e.g. required fields are not given), but throwing might occur downstream of the actual
* error.
*/
template <typename stream_view_type, typename ref_ids_type, typename ref_seqs_type>
template <typename stream_view_type, typename ref_ids_type, typename ref_seqs_type, typename seq_legal_alph_type>
inline void format_sam_base::read_header(stream_view_type && stream_view,
sam_file_header<ref_ids_type> & hdr,
ref_seqs_type & /*ref_id_to_pos_map*/)
ref_seqs_type & /*ref_id_to_pos_map*/,
sam_file_input_options<seq_legal_alph_type> const & options)
{
auto it = std::ranges::begin(stream_view);
auto end = std::ranges::end(stream_view);
Expand Down Expand Up @@ -335,9 +338,13 @@ inline void format_sam_base::read_header(stream_view_type && stream_view,
read_forward_range_field(string_buffer, value);
};

auto print_cerr_of_unspported_tag = [](char const * const header_tag, std::array<char, 2> raw_tag)
auto print_cerr_of_unspported_tag = [&options](char const * const header_tag, std::array<char, 2> raw_tag)
{
std::cerr << "Unsupported SAM header tag in @" << header_tag << ": " << raw_tag[0] << raw_tag[1] << '\n';
if (options.warning_stream == nullptr)
return;

*options.warning_stream << "Unsupported SAM header tag in @" << header_tag << ": " << raw_tag[0] << raw_tag[1]
<< '\n';
};

while (it != end && is_char<'@'>(*it))
Expand Down
4 changes: 2 additions & 2 deletions include/seqan3/io/sam_file/format_bam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ template <typename stream_type, // constraints checked by file
typename bit_score_type>
inline void
format_bam::read_alignment_record(stream_type & stream,
sam_file_input_options<seq_legal_alph_type> const & SEQAN3_DOXYGEN_ONLY(options),
sam_file_input_options<seq_legal_alph_type> const & options,
ref_seqs_type & ref_seqs,
sam_file_header<ref_ids_type> & header,
stream_pos_type & position_buffer,
Expand Down Expand Up @@ -308,7 +308,7 @@ format_bam::read_alignment_record(stream_type & stream,
read_integral_byte_field(stream_view, l_text);

if (l_text > 0) // header text is present
read_header(stream_view | detail::take_exactly_or_throw(l_text), header, ref_seqs);
read_header(stream_view | detail::take_exactly_or_throw(l_text), header, ref_seqs, options);

read_integral_byte_field(stream_view, n_ref);

Expand Down
4 changes: 2 additions & 2 deletions include/seqan3/io/sam_file/format_sam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ template <typename stream_type, // constraints checked by file
typename bit_score_type>
inline void
format_sam::read_alignment_record(stream_type & stream,
sam_file_input_options<seq_legal_alph_type> const & SEQAN3_DOXYGEN_ONLY(options),
sam_file_input_options<seq_legal_alph_type> const & options,
ref_seqs_type & ref_seqs,
sam_file_header<ref_ids_type> & header,
stream_pos_type & position_buffer,
Expand Down Expand Up @@ -389,7 +389,7 @@ format_sam::read_alignment_record(stream_type & stream,
// -------------------------------------------------------------------------------------------------------------
if (is_char<'@'>(*stream_it)) // we always read the header if present
{
read_header(stream_view, header, ref_seqs);
read_header(stream_view, header, ref_seqs, options);

if (std::ranges::begin(stream_view) == std::ranges::end(stream_view)) // file has no records
return;
Expand Down
4 changes: 3 additions & 1 deletion include/seqan3/io/sam_file/input_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#pragma once

#include <iostream>

#include <seqan3/core/platform.hpp>

namespace seqan3
Expand All @@ -22,7 +24,7 @@ namespace seqan3
template <typename sequence_legal_alphabet>
struct sam_file_input_options
{
// options to define
std::ostream * warning_stream{std::addressof(std::cerr)};
};

} // namespace seqan3

0 comments on commit 835abba

Please sign in to comment.