23namespace seqan3::detail
57template <matrix matrix_t,
typename first_sequence_t = std::nullopt_t,
typename second_sequence_t = std::nullopt_t>
62 static constexpr bool has_first_sequence = !std::is_same_v<std::decay_t<first_sequence_t>,
std::nullopt_t>;
64 static constexpr bool has_second_sequence = !std::is_same_v<std::decay_t<second_sequence_t>,
std::nullopt_t>;
68 static constexpr bool is_traceback_matrix = std::is_same_v<std::decay_t<entry_t>, trace_directions>;
71 static constexpr bool is_optional_score = is_type_specialisation_of_v<entry_t, std::optional>;
80 using reference = value_type;
82 using const_reference = reference;
90 debug_matrix() =
default;
91 debug_matrix(debug_matrix
const &) =
default;
92 debug_matrix(debug_matrix &&) =
default;
93 debug_matrix & operator=(debug_matrix
const &) =
default;
94 debug_matrix & operator=(debug_matrix &&) =
default;
95 ~debug_matrix() =
default;
100 debug_matrix(matrix_t matrix) : debug_matrix(
std::
forward<matrix_t>(matrix),
std::nullopt,
std::nullopt)
108 debug_matrix(matrix_t matrix, first_sequence_t first_sequence, second_sequence_t second_sequence) :
110 _first_sequence{
std::
forward<first_sequence_t>(first_sequence)},
111 _second_sequence{
std::
forward<second_sequence_t>(second_sequence)}
113 if constexpr (has_first_sequence)
115 assert(_matrix.cols() <= _first_sequence.size() + 1u);
118 if constexpr (has_second_sequence)
120 assert(_matrix.rows() <= _second_sequence.size() + 1u);
126 size_t rows() const noexcept
129 return _rows.value_or(_matrix.rows());
131 return _cols.value_or(_matrix.cols());
135 size_t cols() const noexcept
138 return _cols.value_or(_matrix.cols());
140 return _rows.value_or(_matrix.rows());
144 first_sequence_t
const & first_sequence() const noexcept
147 return _first_sequence;
149 return _second_sequence;
153 second_sequence_t
const & second_sequence() const noexcept
156 return _second_sequence;
158 return _first_sequence;
162 const_reference
at(matrix_coordinate
const & coordinate)
const noexcept
164 size_t row = coordinate.row;
165 size_t col = coordinate.col;
167 assert(row < rows() && col < cols());
169 row_index_type
const _row{!_transpose ? row : col};
170 column_index_type
const _col{!_transpose ? col : row};
171 row_index_type
const _mask_row{_transpose == _transpose_mask ? row : col};
172 column_index_type
const _mask_col{_transpose == _transpose_mask ? col : row};
174 if (!_masking_matrix.has_value() || _masking_matrix.value().at({_mask_row, _mask_col}))
176 entry_t
const & entry = _matrix.at({_row, _col});
178 if (!is_traceback_matrix || !_transpose)
181 if constexpr (is_traceback_matrix)
184 if ((entry & trace_directions::left) == trace_directions::left)
185 reverse |= trace_directions::up;
186 if ((entry & trace_directions::up) == trace_directions::up)
187 reverse |= trace_directions::left;
188 if ((entry & trace_directions::diagonal) == trace_directions::diagonal)
189 reverse |= trace_directions::diagonal;
194 if constexpr (is_traceback_matrix)
195 return trace_directions::none;
206 debug_matrix & mask_matrix(row_wise_matrix<bool> masking_matrix)
noexcept
208 assert(masking_matrix.rows() == rows());
209 assert(masking_matrix.cols() == cols());
210 _transpose_mask = _transpose;
211 _masking_matrix = std::move(masking_matrix);
221 return mask_matrix(row_wise_matrix<bool>{number_rows{rows()}, number_cols{cols()}, std::move(masking_vector)});
229 debug_matrix & sub_matrix(
size_t const new_rows,
size_t const new_cols)
noexcept
231 assert(new_rows <= rows());
232 assert(new_cols <= cols());
249 debug_matrix & transpose_matrix() noexcept
251 _transpose = !_transpose;
270 template <
typename ostream_t>
271 void stream_matrix(ostream_t & cout,
fmtflags2 const flags)
const noexcept
274 size_t const column_width =
275 this->column_width.has_value() ? this->column_width.value() : auto_column_width(flags);
277 auto char_first_sequence = [&]([[maybe_unused]]
size_t const i) ->
std::string
279 if constexpr (!has_first_sequence)
282 return as_string(first_sequence()[i], flags);
285 auto char_second_sequence = [&]([[maybe_unused]]
size_t const i) ->
std::string
287 if constexpr (!has_second_sequence)
290 return as_string(second_sequence()[i], flags);
296 size_t const length_bytes = symbol.size();
297 size_t const length = unicode_str_length(symbol);
298 size_t const offset = length_bytes - length;
303 auto print_first_cell = [&](
std::string const & symbol)
305 cout << symbol << symbols.col_sep;
309 auto print_first_row = [&]
311 print_first_cell(
" ");
312 print_cell(symbols.epsilon);
314 for (
size_t col = 0; col < cols() - 1; ++col)
315 print_cell(char_first_sequence(col));
321 auto print_divider = [&]
323 cout <<
" " << symbols.row_col_sep;
324 for (
size_t col = 0; col < cols(); ++col)
326 for (
size_t i = 0; i < column_width; ++i)
327 cout << symbols.row_sep;
329 cout << symbols.row_col_sep;
335 for (
size_t row = 0; row < rows(); ++row)
337 if (symbols.row_sep[0] !=
'\0')
342 print_first_cell(symbols.epsilon);
344 print_first_cell(char_second_sequence(row - 1));
346 for (
size_t col = 0; col < cols(); ++col)
347 print_cell(entry_at({row_index_type{row}, column_index_type{col}}, flags));
354 size_t auto_column_width(
fmtflags2 const flags)
const noexcept
356 size_t col_width = 1;
357 for (
size_t row = 0; row < rows(); ++row)
358 for (
size_t col = 0; col < cols(); ++col)
361 unicode_str_length(entry_at({row_index_type{row}, column_index_type{col}}, flags)));
372 value_type
const & entry =
at(coordinate);
373 if (!is_traceback_matrix && entry == matrix_inf<value_type>)
376 return as_string(entry, flags);
380 template <
typename value_type>
384 debug_stream_type stream{strstream};
385 stream << flags << entry;
386 return strstream.
str();
391 static size_t unicode_str_length(
std::string const & str)
noexcept
394 for (
auto it = str.cbegin(); it < str.cend(); ++it, ++length)
397 if ((v & 0b1110'0000) == 0b1100'0000)
399 else if ((v & 0b1111'0000) == 0b1110'0000)
401 else if ((v & 0b1111'1000) == 0b1111'0000)
411 char const * epsilon{};
413 char const * col_sep{};
415 char const * row_sep{};
417 char const * row_col_sep{};
423 static constexpr format_type csv{
" ",
";",
"",
"",
""};
425 static constexpr format_type unicode{
"ε",
"║",
"═",
"╬",
"∞"};
435 first_sequence_t _first_sequence;
437 second_sequence_t _second_sequence;
447 bool _transpose_mask{};
455template <matrix matrix_t>
456debug_matrix(matrix_t &&) -> debug_matrix<matrix_t>;
460template <matrix matrix_t,
typename first_sequence_t,
typename second_sequence_t>
461debug_matrix(matrix_t &&,
463 second_sequence_t &&) -> debug_matrix<matrix_t, first_sequence_t, second_sequence_t>;
478template <detail::matrix alignment_matrix_t>
493 template <
typename stream_t,
typename arg_t>
494 requires detail::is_type_specialisation_of_v<stream_t, debug_stream_type>
495 constexpr void operator()(stream_t & stream, arg_t && arg)
const
497 print_impl(stream.get_underlying_stream(), stream.flags2(), std::forward<arg_t>(arg));
510 template <
typename stream_t,
typename arg_t>
511 constexpr void operator()(stream_t & stream, arg_t && arg)
const
524 template <
typename stream_t,
typename arg_t>
525 void print_impl(stream_t & stream,
fmtflags2 const flags, arg_t && arg)
const
527 detail::debug_matrix debug{std::forward<arg_t>(arg)};
529 debug.stream_matrix(stream, flags);
Provides seqan3::debug_stream and related types.
Provides seqan3::debug_stream and related types.
fmtflags2
Flags that change the behaviour of the seqan3::debug_stream.
Definition debug_stream_type.hpp:32
@ utf8
Enables use of non-ASCII UTF8 characters in formatted output.
Definition debug_stream_type.hpp:34
@ none
No flag is set.
Definition debug_stream_type.hpp:33
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
typename decltype(detail::at< idx >(list_t{}))::type at
Return the type at given index from the type list.
Definition type_list/traits.hpp:276
Provides seqan3::detail::matrix.
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
Provides seqan3::debug_stream and related types.
Provides seqan3::debug_stream and related types.
Provides seqan3::detail::row_wise_matrix.
constexpr void operator()(stream_t &stream, arg_t &&arg) const
Prints the alignment matrix into the given stream using ascii formatting.
Definition debug_matrix.hpp:511
constexpr void operator()(stream_t &stream, arg_t &&arg) const
Prints the alignment matrix into the given stream using formatting specified by seqan3::fmtflags2.
Definition debug_matrix.hpp:495
Definition default_printer.hpp:28
Provides type traits for working with templates.
Provides the declaration of seqan3::detail::trace_directions.