SeqAn3 3.3.0-rc.1
The Modern C++ library for sequence analysis.
edit_distance_score_matrix_full.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6// -----------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <bitset>
16
19
20namespace seqan3::detail
21{
22
31template <typename word_t, typename score_t, bool is_semi_global, bool use_max_errors>
33{
34public:
36 template <std::ranges::viewable_range database_t,
37 std::ranges::viewable_range query_t,
38 typename align_config_t,
39 typename edit_traits>
41
51
52protected:
54 template <typename derived_t, typename edit_traits>
56
61 {}
63
64public:
66 using word_type = word_t;
67
69 static constexpr auto word_size = bits_of<word_type>;
70
72 using score_type = score_t;
73
76
79
81 using size_type = size_t;
82
84 static constexpr std::optional<score_type> inf = std::nullopt;
85
94 void reserve(size_t const new_capacity)
95 {
96 columns.reserve(new_capacity);
97 }
98
107 template <typename score_type>
108 static size_t max_rows(word_type const score_mask,
109 unsigned const last_block,
110 score_type const score,
111 score_type const max_errors) noexcept
112 {
113 size_t const offset = std::bit_width(score_mask);
114 size_t const active_row = word_size * last_block + offset;
115 return active_row + (score <= max_errors);
116 }
117
123 static score_type score_delta_of_word(word_type const & vp, word_type const & vn) noexcept
124 {
127 return p - n;
128 }
129
130public:
132 reference at(matrix_coordinate const & coordinate) const noexcept
133 {
134 size_t col = coordinate.col;
135 size_t row = coordinate.row;
136
137 assert(row < rows());
138 assert(col < cols());
139
140 column_type const & column = columns[col];
141 if constexpr (use_max_errors)
142 if (!(row < column.max_rows))
143 return inf;
144
145 score_type score = is_semi_global ? 0u : static_cast<score_type>(col);
146
147 size_t current_row = 1u;
148 size_t word_idx = 0u;
149
150 for (; current_row + word_size <= row; ++word_idx, current_row += word_size)
151 score += score_delta_of_word(column.vp[word_idx], column.vn[word_idx]);
152
153 if (row >= current_row)
154 {
155 word_type const mask = (1u << (row - current_row + 1u)) - 1u;
156 score += score_delta_of_word(column.vp[word_idx] & mask, column.vn[word_idx] & mask);
157 }
158
159 return -score;
160 }
161
163 size_t rows() const noexcept
164 {
165 return rows_size;
166 }
167
169 size_t cols() const noexcept
170 {
171 return columns.size();
172 }
173
174protected:
177 {
180 size_t max_rows;
181 };
182
185 {
190 };
191
193 struct column_type : enable_state_t<true, score_matrix_state>, enable_state_t<use_max_errors, max_errors_state>
194 {};
195
201 requires (!use_max_errors)
202 {
204 column.vp = std::move(vp);
205 column.vn = std::move(vn);
206
207 columns.push_back(std::move(column));
208 }
209
216 requires use_max_errors
217 {
219 column.vp = std::move(vp);
220 column.vn = std::move(vn);
221 column.max_rows = max_rows;
222
223 columns.push_back(std::move(column));
224 }
225
226private:
228 size_t rows_size{};
231};
232
233} // namespace seqan3::detail
T bit_width(T... args)
Provides utility functions for bit twiddling.
The underlying data structure of seqan3::detail::edit_distance_unbanded that represents the score mat...
Definition: edit_distance_score_matrix_full.hpp:33
score_t score_type
The type of the score.
Definition: edit_distance_score_matrix_full.hpp:72
size_t size_type
The size type of the matrix.
Definition: edit_distance_score_matrix_full.hpp:81
edit_distance_score_matrix_full & operator=(edit_distance_score_matrix_full &&)=default
Defaulted.
static size_t max_rows(word_type const score_mask, unsigned const last_block, score_type const score, score_type const max_errors) noexcept
Computes the number of max rows in the current column.
Definition: edit_distance_score_matrix_full.hpp:108
size_t rows_size
The number of rows in the matrix.
Definition: edit_distance_score_matrix_full.hpp:228
reference at(matrix_coordinate const &coordinate) const noexcept
A reference to the entry of the matrix at the given coordinate.
Definition: edit_distance_score_matrix_full.hpp:132
edit_distance_score_matrix_full(size_t const rows_size)
Construct the score_matrix by giving the number of rows within the matrix.
Definition: edit_distance_score_matrix_full.hpp:60
std::vector< column_type > columns
The columns of the score matrix.
Definition: edit_distance_score_matrix_full.hpp:230
static constexpr std::optional< score_type > inf
A special score which represents infinity.
Definition: edit_distance_score_matrix_full.hpp:84
edit_distance_score_matrix_full(edit_distance_score_matrix_full &&)=default
Defaulted.
size_t rows() const noexcept
The number of rows in the matrix.
Definition: edit_distance_score_matrix_full.hpp:163
static constexpr auto word_size
The size of one machine word.
Definition: edit_distance_score_matrix_full.hpp:69
void add_column(std::vector< word_type > vp, std::vector< word_type > vn, size_t const max_rows)
Adds a column to the score matrix.
Definition: edit_distance_score_matrix_full.hpp:215
size_t cols() const noexcept
The number of columns in the matrix.
Definition: edit_distance_score_matrix_full.hpp:169
std::conditional_t< use_max_errors, std::optional< score_type >, score_type > value_type
The type of an entry in the matrix.
Definition: edit_distance_score_matrix_full.hpp:75
word_t word_type
The type of one machine word.
Definition: edit_distance_score_matrix_full.hpp:66
edit_distance_score_matrix_full(edit_distance_score_matrix_full const &)=default
Defaulted.
void reserve(size_t const new_capacity)
Increase the capacity of the columns to a value that is greater or equal to new_capacity.
Definition: edit_distance_score_matrix_full.hpp:94
edit_distance_score_matrix_full & operator=(edit_distance_score_matrix_full const &)=default
Defaulted.
void add_column(std::vector< word_type > vp, std::vector< word_type > vn)
Adds a column to the score matrix.
Definition: edit_distance_score_matrix_full.hpp:200
static score_type score_delta_of_word(word_type const &vp, word_type const &vn) noexcept
Computes delta score between vp and vn.
Definition: edit_distance_score_matrix_full.hpp:123
Only available when default_edit_distance_trait_type::compute_score_matrix is true.
Definition: edit_distance_unbanded.hpp:421
This calculates an alignment using the edit distance and without a band.
Definition: edit_distance_unbanded.hpp:714
Implementation of a masked alphabet to be used for tuple composites..
Definition: mask.hpp:38
T count(T... args)
Forwards for seqan3::edit_distance_unbanded related types.
@ column
The corresponding alignment coordinate will be incrementable/decrementable in the column index.
@ row
The corresponding alignment coordinate will be incrementable/decrementable in the row index.
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
The state of one computation step.
Definition: edit_distance_score_matrix_full.hpp:194
If use_max_errors is true store these additional state information in state_type.
Definition: edit_distance_score_matrix_full.hpp:177
size_t max_rows
The number of max_rows within the current column. Computed by seqan3::detail::edit_distance_score_mat...
Definition: edit_distance_score_matrix_full.hpp:180
This information is needed to infer the score matrix.
Definition: edit_distance_score_matrix_full.hpp:185
std::vector< word_type > vn
The machine word which stores the negative vertical differences.
Definition: edit_distance_score_matrix_full.hpp:189
std::vector< word_type > vp
The machine word which stores the positive vertical differences.
Definition: edit_distance_score_matrix_full.hpp:187