SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
edit_distance_score_matrix_full.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 <bitset>
13
16
17namespace seqan3::detail
18{
19
28template <typename word_t, typename score_t, bool is_semi_global, bool use_max_errors>
29class edit_distance_score_matrix_full
30{
31public:
33 template <std::ranges::viewable_range database_t,
34 std::ranges::viewable_range query_t,
35 typename align_config_t,
36 typename edit_traits>
37 friend class edit_distance_unbanded;
38
42 edit_distance_score_matrix_full() = default;
43 edit_distance_score_matrix_full(edit_distance_score_matrix_full const &) = default;
44 edit_distance_score_matrix_full(edit_distance_score_matrix_full &&) = default;
45 edit_distance_score_matrix_full & operator=(edit_distance_score_matrix_full const &) = default;
46 edit_distance_score_matrix_full & operator=(edit_distance_score_matrix_full &&) = default;
47 ~edit_distance_score_matrix_full() = default;
48
49protected:
51 template <typename derived_t, typename edit_traits>
52 friend class edit_distance_unbanded_score_matrix_policy;
53
57 edit_distance_score_matrix_full(size_t const rows_size) : rows_size{rows_size}, columns{}
58 {}
60
61public:
63 using word_type = word_t;
64
66 static constexpr auto word_size = bits_of<word_type>;
67
69 using score_type = score_t;
70
73
75 using reference = value_type;
76
78 using size_type = size_t;
79
81 static constexpr std::optional<score_type> inf = std::nullopt;
82
91 void reserve(size_t const new_capacity)
92 {
93 columns.reserve(new_capacity);
94 }
95
104 template <typename score_type>
105 static size_t max_rows(word_type const score_mask,
106 unsigned const last_block,
107 score_type const score,
108 score_type const max_errors) noexcept
109 {
110 size_t const offset = std::bit_width(score_mask);
111 size_t const active_row = word_size * last_block + offset;
112 return active_row + (score <= max_errors);
113 }
114
120 static score_type score_delta_of_word(word_type const & vp, word_type const & vn) noexcept
121 {
122 score_type const p = std::bitset<word_size>{vp}.count();
123 score_type const n = std::bitset<word_size>{vn}.count();
124 return p - n;
125 }
126
127public:
129 reference at(matrix_coordinate const & coordinate) const noexcept
130 {
131 size_t col = coordinate.col;
132 size_t row = coordinate.row;
133
134 assert(row < rows());
135 assert(col < cols());
136
137 column_type const & column = columns[col];
138 if constexpr (use_max_errors)
139 if (!(row < column.max_rows))
140 return inf;
141
142 score_type score = is_semi_global ? 0u : static_cast<score_type>(col);
143
144 size_t current_row = 1u;
145 size_t word_idx = 0u;
146
147 for (; current_row + word_size <= row; ++word_idx, current_row += word_size)
148 score += score_delta_of_word(column.vp[word_idx], column.vn[word_idx]);
149
150 if (row >= current_row)
151 {
152 word_type const mask = (1u << (row - current_row + 1u)) - 1u;
153 score += score_delta_of_word(column.vp[word_idx] & mask, column.vn[word_idx] & mask);
154 }
155
156 return -score;
157 }
158
160 size_t rows() const noexcept
161 {
162 return rows_size;
163 }
164
166 size_t cols() const noexcept
167 {
168 return columns.size();
169 }
170
171protected:
173 struct max_errors_state
174 {
177 size_t max_rows;
178 };
179
181 struct score_matrix_state
182 {
187 };
188
190 struct column_type : enable_state_t<true, score_matrix_state>, enable_state_t<use_max_errors, max_errors_state>
191 {};
192
197 void add_column(std::vector<word_type> vp, std::vector<word_type> vn)
198 requires (!use_max_errors)
199 {
200 column_type column{};
201 column.vp = std::move(vp);
202 column.vn = std::move(vn);
203
204 columns.push_back(std::move(column));
205 }
206
212 void add_column(std::vector<word_type> vp, std::vector<word_type> vn, size_t const max_rows)
213 requires use_max_errors
214 {
215 column_type column{};
216 column.vp = std::move(vp);
217 column.vn = std::move(vn);
218 column.max_rows = max_rows;
219
220 columns.push_back(std::move(column));
221 }
222
223private:
225 size_t rows_size{};
227 std::vector<column_type> columns{};
228};
229
230} // namespace seqan3::detail
T bit_width(T... args)
Provides utility functions for bit twiddling.
T count(T... args)
Forwards for seqan3::edit_distance_unbanded related types.
@ 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
Hide me