SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
debug_matrix.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <iomanip>
13
22
23namespace seqan3::detail
24{
25
57template <matrix matrix_t, typename first_sequence_t = std::nullopt_t, typename second_sequence_t = std::nullopt_t>
59{
60protected:
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>;
72
73public:
86
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;
95 ~debug_matrix() = default;
96
100 debug_matrix(matrix_t matrix) : debug_matrix(std::forward<matrix_t>(matrix), std::nullopt, std::nullopt)
101 {}
102
108 debug_matrix(matrix_t matrix, first_sequence_t first_sequence, second_sequence_t second_sequence) :
109 _matrix{std::forward<matrix_t>(matrix)},
110 _first_sequence{std::forward<first_sequence_t>(first_sequence)},
111 _second_sequence{std::forward<second_sequence_t>(second_sequence)}
112 {
113 if constexpr (has_first_sequence)
114 {
115 assert(_matrix.cols() <= _first_sequence.size() + 1u);
116 }
117
118 if constexpr (has_second_sequence)
119 {
120 assert(_matrix.rows() <= _second_sequence.size() + 1u);
121 }
122 }
124
126 size_t rows() const noexcept
127 {
128 if (!_transpose)
129 return _rows.value_or(_matrix.rows());
130 else
131 return _cols.value_or(_matrix.cols());
132 }
133
135 size_t cols() const noexcept
136 {
137 if (!_transpose)
138 return _cols.value_or(_matrix.cols());
139 else
140 return _rows.value_or(_matrix.rows());
141 }
142
144 first_sequence_t const & first_sequence() const noexcept
145 {
146 if (!_transpose)
147 return _first_sequence;
148 else
149 return _second_sequence;
150 }
151
153 second_sequence_t const & second_sequence() const noexcept
154 {
155 if (!_transpose)
156 return _second_sequence;
157 else
158 return _first_sequence;
159 }
160
162 const_reference at(matrix_coordinate const & coordinate) const noexcept
163 {
164 size_t row = coordinate.row;
165 size_t col = coordinate.col;
166
167 assert(row < rows() && col < cols());
168
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};
173
174 if (!_masking_matrix.has_value() || _masking_matrix.value().at({_mask_row, _mask_col}))
175 {
176 entry_t const & entry = _matrix.at({_row, _col});
177
179 return entry;
180
181 if constexpr (is_traceback_matrix)
182 {
183 trace_directions reverse{};
185 reverse |= trace_directions::up;
187 reverse |= trace_directions::left;
190 return reverse;
191 }
192 }
193
194 if constexpr (is_traceback_matrix)
196 else
197 return std::nullopt;
198 }
199
207 {
208 assert(masking_matrix.rows() == rows());
209 assert(masking_matrix.cols() == cols());
211 _masking_matrix = std::move(masking_matrix);
212 return *this;
213 }
214
219 debug_matrix & mask_matrix(std::vector<bool> masking_vector) noexcept
220 {
221 return mask_matrix(row_wise_matrix<bool>{number_rows{rows()}, number_cols{cols()}, std::move(masking_vector)});
222 }
223
229 debug_matrix & sub_matrix(size_t const new_rows, size_t const new_cols) noexcept
230 {
231 assert(new_rows <= rows());
232 assert(new_cols <= cols());
233 if (!_transpose)
234 {
235 _rows = new_rows;
236 _cols = new_cols;
237 }
238 else
239 {
240 _rows = new_cols;
241 _cols = new_rows;
242 }
243 return *this;
244 }
245
250 {
252 return *this;
253 }
254
255protected:
257 struct format_type; // forward declaration
259
260public:
270 template <typename ostream_t>
271 void stream_matrix(ostream_t & cout, fmtflags2 const flags) const noexcept
272 {
273 format_type const & symbols = (flags & fmtflags2::utf8) == fmtflags2::utf8 ? unicode : csv;
274 size_t const column_width =
275 this->column_width.has_value() ? this->column_width.value() : auto_column_width(flags);
276
277 auto char_first_sequence = [&]([[maybe_unused]] size_t const i) -> std::string
278 {
279 if constexpr (!has_first_sequence)
280 return " ";
281 else
282 return as_string(first_sequence()[i], flags);
283 };
284
285 auto char_second_sequence = [&]([[maybe_unused]] size_t const i) -> std::string
286 {
287 if constexpr (!has_second_sequence)
288 return " ";
289 else
290 return as_string(second_sequence()[i], flags);
291 };
292
293 auto print_cell = [&](std::string const & symbol)
294 {
295 // deal with unicode chars that mess up std::setw
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;
299
300 cout << std::left << std::setw(column_width + offset) << symbol << symbols.col_sep;
301 };
302
303 auto print_first_cell = [&](std::string const & symbol)
304 {
305 cout << symbol << symbols.col_sep;
306 };
307
308 // |_|d|a|t|a|b|a|s|e|
309 auto print_first_row = [&]
310 {
311 print_first_cell(" ");
312 print_cell(symbols.epsilon);
313
314 for (size_t col = 0; col < cols() - 1; ++col)
315 print_cell(char_first_sequence(col));
316
317 cout << "\n";
318 };
319
320 // |-|-|-|-|-|-|-|-|-|
321 auto print_divider = [&]
322 {
323 cout << " " << symbols.row_col_sep;
324 for (size_t col = 0; col < cols(); ++col)
325 {
326 for (size_t i = 0; i < column_width; ++i)
327 cout << symbols.row_sep;
328
329 cout << symbols.row_col_sep;
330 }
331 cout << "\n";
332 };
333
334 print_first_row();
335 for (size_t row = 0; row < rows(); ++row)
336 {
337 if (symbols.row_sep[0] != '\0')
338 print_divider();
339
340 // one query letter + one row of scores / traces
341 if (row == 0)
342 print_first_cell(symbols.epsilon);
343 else
344 print_first_cell(char_second_sequence(row - 1));
345
346 for (size_t col = 0; col < cols(); ++col)
347 print_cell(entry_at({row_index_type{row}, column_index_type{col}}, flags));
348
349 cout << "\n";
350 }
351 }
352
354 size_t auto_column_width(fmtflags2 const flags) const noexcept
355 {
356 size_t col_width = 1;
357 for (size_t row = 0; row < rows(); ++row)
358 for (size_t col = 0; col < cols(); ++col)
359 col_width =
360 std::max(col_width,
362
363 return col_width;
364 }
365
366protected:
368 std::string entry_at(matrix_coordinate const coordinate, fmtflags2 flags) const noexcept
369 {
370 format_type const & symbols = (flags & fmtflags2::utf8) == fmtflags2::utf8 ? unicode : csv;
371
372 value_type const & entry = at(coordinate);
373 if (!is_traceback_matrix && entry == matrix_inf<value_type>)
374 return symbols.inf;
375
376 return as_string(entry, flags);
377 }
378
380 template <typename value_type>
381 static std::string as_string(value_type && entry, fmtflags2 const flags) noexcept
382 {
383 std::stringstream strstream;
384 debug_stream_type stream{strstream};
385 stream << flags << entry;
386 return strstream.str();
387 }
388
391 static size_t unicode_str_length(std::string const & str) noexcept
392 {
393 size_t length = 0u;
394 for (auto it = str.cbegin(); it < str.cend(); ++it, ++length)
395 {
396 uint8_t v = *it;
397 if ((v & 0b11100000) == 0b11000000)
398 ++it;
399 else if ((v & 0b11110000) == 0b11100000)
400 it += 2;
401 else if ((v & 0b11111000) == 0b11110000)
402 it += 3;
403 }
404 return length;
405 }
406
409 {
411 char const * epsilon{};
413 char const * col_sep{};
415 char const * row_sep{};
417 char const * row_col_sep{};
419 char const * inf{};
420 };
421
423 static constexpr format_type csv{" ", ";", "", "", ""};
425 static constexpr format_type unicode{"ε", "║", "═", "╬", "∞"};
426
427public:
430
431protected:
433 matrix_t _matrix;
435 first_sequence_t _first_sequence;
437 second_sequence_t _second_sequence;
448};
449
455template <matrix matrix_t>
457
460template <matrix matrix_t, typename first_sequence_t, typename second_sequence_t>
461debug_matrix(matrix_t &&, first_sequence_t &&, second_sequence_t &&)
464
465} // namespace seqan3::detail
466
467namespace seqan3
468{
479template <typename char_t, detail::matrix alignment_matrix_t>
480inline debug_stream_type<char_t> & operator<<(debug_stream_type<char_t> & s, alignment_matrix_t && matrix)
481{
482 detail::debug_matrix debug{std::forward<alignment_matrix_t>(matrix)};
483
484 std::stringstream sstream{};
485 debug.stream_matrix(sstream, s.flags2());
486 s << sstream.str();
487 return s;
488}
489
491template <typename char_t, std::ranges::input_range alignment_matrix_t>
493inline debug_stream_type<char_t> & operator<<(debug_stream_type<char_t> & s, alignment_matrix_t && matrix)
494{
495 return s << detail::debug_matrix{std::forward<alignment_matrix_t>(matrix)};
496}
497
498} // namespace seqan3
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:75
fmtflags2 flags2() const
Retrieve the format flags from the stream.
Definition debug_stream_type.hpp:203
A debug matrix to wrap alignment matrices and sequences and make them printable together.
Definition debug_matrix.hpp:59
std::optional< size_t > _cols
The number of columns the debug matrix should have. Must be at most the size of the original matrix.
Definition debug_matrix.hpp:441
debug_matrix & mask_matrix(row_wise_matrix< bool > masking_matrix) noexcept
Masks entries out of the current matrix. This operations changes the way this.at(i,...
Definition debug_matrix.hpp:206
first_sequence_t const & first_sequence() const noexcept
The first sequence of the sequence alignment.
Definition debug_matrix.hpp:144
debug_matrix(matrix_t &&, first_sequence_t &&, second_sequence_t &&) -> debug_matrix< matrix_t, first_sequence_t, second_sequence_t >
The type deduction guide for the constructor seqan3::detail::debug_matrix(matrix_t,...
static constexpr format_type unicode
The format when printing to a unicode stream.
Definition debug_matrix.hpp:425
static std::string as_string(value_type &&entry, fmtflags2 const flags) noexcept
Convert a value into a std::string.
Definition debug_matrix.hpp:381
size_t rows() const noexcept
The number of rows in the matrix.
Definition debug_matrix.hpp:126
size_t cols() const noexcept
The number of columns in the matrix.
Definition debug_matrix.hpp:135
bool _transpose
Whether the current matrix should be transposed.
Definition debug_matrix.hpp:445
debug_matrix & operator=(debug_matrix &&)=default
Defaulted.
debug_matrix()=default
Defaulted.
debug_matrix(debug_matrix &&)=default
Defaulted.
bool _transpose_mask
Whether the masking matrix should be transposed.
Definition debug_matrix.hpp:447
size_t auto_column_width(fmtflags2 const flags) const noexcept
Determines the largest width of all entries in the matrix, e.g. -152 has width 4.
Definition debug_matrix.hpp:354
debug_matrix(debug_matrix const &)=default
Defaulted.
debug_matrix & sub_matrix(size_t const new_rows, size_t const new_cols) noexcept
Limits the view port of the current matrix.
Definition debug_matrix.hpp:229
static constexpr bool is_traceback_matrix
Whether the value_type is trace_directions.
Definition debug_matrix.hpp:68
debug_matrix & mask_matrix(std::vector< bool > masking_vector) noexcept
Creates the masking_matrix out of the given masking_vector and calls mask_matrix(row_wise_matrix<bool...
Definition debug_matrix.hpp:219
std::conditional_t< is_traceback_matrix||is_optional_score, entry_t, std::optional< entry_t > > value_type
The type of an entry in the matrix.
Definition debug_matrix.hpp:78
second_sequence_t const & second_sequence() const noexcept
The second sequence of the sequence alignment.
Definition debug_matrix.hpp:153
static constexpr bool is_optional_score
Whether a score matrix already returns std::optional scores. (Where std::nullopt means unset/invalid/...
Definition debug_matrix.hpp:71
~debug_matrix()=default
Defaulted.
static constexpr format_type csv
The format when printing to a ascii stream.
Definition debug_matrix.hpp:423
std::optional< row_wise_matrix< bool > > _masking_matrix
The masking matrix.
Definition debug_matrix.hpp:443
value_type reference
The type of a reference to an entry in the matrix.
Definition debug_matrix.hpp:80
static constexpr bool has_second_sequence
Whether the current debug_matrix was given a second_sequence.
Definition debug_matrix.hpp:64
std::optional< size_t > _rows
The number of rows the debug matrix should have. Must be at most the size of the original matrix.
Definition debug_matrix.hpp:439
matrix_t _matrix
The matrix.
Definition debug_matrix.hpp:433
first_sequence_t _first_sequence
The first sequence of the sequence alignment.
Definition debug_matrix.hpp:435
debug_matrix & transpose_matrix() noexcept
Transposes the current matrix.
Definition debug_matrix.hpp:249
debug_matrix(matrix_t matrix)
Construct the matrix out of an existing matrix.
Definition debug_matrix.hpp:100
second_sequence_t _second_sequence
The second sequence of the sequence alignment.
Definition debug_matrix.hpp:437
typename std::remove_reference_t< matrix_t >::value_type entry_t
The entry type.
Definition debug_matrix.hpp:66
void stream_matrix(ostream_t &cout, fmtflags2 const flags) const noexcept
Prints this matrix into the given stream.
Definition debug_matrix.hpp:271
debug_matrix & operator=(debug_matrix const &)=default
Defaulted.
debug_matrix(matrix_t &&) -> debug_matrix< matrix_t >
The type deduction guide for the constructor seqan3::detail::debug_matrix(matrix_t)
const_reference at(matrix_coordinate const &coordinate) const noexcept
A reference to the entry of the matrix at the given coordinate.
Definition debug_matrix.hpp:162
typename std::remove_reference_t< matrix_t >::size_type size_type
The size type.
Definition debug_matrix.hpp:84
std::string entry_at(matrix_coordinate const coordinate, fmtflags2 flags) const noexcept
Same as at(coordinate), but as string.
Definition debug_matrix.hpp:368
debug_matrix(matrix_t matrix, first_sequence_t first_sequence, second_sequence_t second_sequence)
Construct the matrix out of an existing matrix and two sequences.
Definition debug_matrix.hpp:108
static size_t unicode_str_length(std::string const &str) noexcept
The length of the str (traceback symbols are unicode aware).
Definition debug_matrix.hpp:391
std::optional< size_t > column_width
What is the width (number of chars) of an entry. Defaults to auto_column_width.
Definition debug_matrix.hpp:429
static constexpr bool has_first_sequence
Whether the current debug_matrix was given a first_sequence.
Definition debug_matrix.hpp:62
Defines the requirements of a matrix (e.g. score matrices, trace matrices).
Definition matrix_concept.hpp:58
A two dimensional matrix used inside of alignment algorithms.
Definition two_dimensional_matrix.hpp:62
A helper concept definition for ranges that can be streamed to the seqan3::debug_stream.
Definition range.hpp:36
Provides seqan3::debug_stream and related types.
Provides seqan3::debug_stream and related types.
debug_stream_type< char_t > & operator<<(debug_stream_type< char_t > &stream, alignment_t &&alignment)
Stream operator for alignments, which are represented as tuples of aligned sequences.
Definition debug_stream_alignment.hpp:107
trace_directions
The possible directions a trace can have. The values can be combined by the logical |-operator.
Definition trace_directions.hpp:26
@ row
The corresponding alignment coordinate will be incrementable/decrementable in the row index.
@ up
Trace comes from the above entry.
@ left
Trace comes from the left entry.
@ diagonal
Trace comes from the diagonal entry.
fmtflags2
Flags that change the behaviour of the seqan3::debug_stream.
Definition debug_stream_type.hpp:28
@ utf8
Enables use of non-ASCII UTF8 characters in formatted output.
Definition debug_stream_type.hpp:30
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
T left(T... args)
Provides seqan3::detail::matrix.
T max(T... args)
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
T has_value(T... args)
Provides seqan3::debug_stream and related types.
Provides seqan3::debug_stream and related types.
Provides seqan3::detail::row_wise_matrix.
T setw(T... args)
T str(T... args)
A strong type for designated initialisation of the column index of a matrix.
Definition matrix_coordinate.hpp:29
Format used by seqan3::detail::debug_matrix.
Definition debug_matrix.hpp:409
char const * inf
Infinity symbol (a single symbol)
Definition debug_matrix.hpp:419
char const * row_col_sep
Row separator symbol (a single symbol)
Definition debug_matrix.hpp:417
char const * col_sep
Column separator symbol (a single symbol)
Definition debug_matrix.hpp:413
char const * row_sep
Row separator symbol (a single symbol)
Definition debug_matrix.hpp:415
char const * epsilon
Epsilon symbol (a single symbol)
Definition debug_matrix.hpp:411
Strong type for setting the column dimension of a matrix.
Definition two_dimensional_matrix.hpp:29
Strong type for setting the row dimension of a matrix.
Definition two_dimensional_matrix.hpp:37
A strong type for designated initialisation of the row index of a matrix.
Definition matrix_coordinate.hpp:58
Provides type traits for working with templates.
Provides the declaration of seqan3::detail::trace_directions.
T value_or(T... args)
Hide me