SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
policy_optimum_tracker.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
11#pragma once
12
13#include <limits>
14
21
22namespace seqan3::detail
23{
24
35struct max_score_updater
36{
52 template <typename score_t, typename coordinate_t>
53 requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &>
54 && std::assignable_from<coordinate_t &, coordinate_t const &>)
55 void operator()(score_t & optimal_score,
56 coordinate_t & optimal_coordinate,
57 score_t current_score,
58 coordinate_t current_coordinate) const noexcept
59 {
60 bool const is_better_score = current_score >= optimal_score;
61 optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
62 optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
63 }
64};
65
106struct max_score_banded_updater
107{
108private:
110 size_t target_row_index{};
112 size_t target_col_index{};
113
114public:
132 template <typename score_t, typename coordinate_t>
133 requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &>
134 && std::assignable_from<coordinate_t &, coordinate_t const &>)
135 void operator()(score_t & optimal_score,
136 coordinate_t & optimal_coordinate,
137 score_t current_score,
138 coordinate_t current_coordinate) const noexcept
139 {
140 bool const is_better_score =
141 (target_row_index == current_coordinate.row || target_col_index == current_coordinate.col)
142 && (current_score >= optimal_score);
143 optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
144 optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
145 }
146
151 void set_target_indices(row_index_type<size_t> row_index, column_index_type<size_t> col_index) noexcept
152 {
153 target_row_index = row_index.get();
154 target_col_index = col_index.get();
155 }
156};
157
177template <typename alignment_configuration_t, std::semiregular optimum_updater_t>
178 requires is_type_specialisation_of_v<alignment_configuration_t, configuration>
179 && std::invocable<
180 optimum_updater_t,
181 typename alignment_configuration_traits<alignment_configuration_t>::score_type &,
182 typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type &,
183 typename alignment_configuration_traits<alignment_configuration_t>::score_type,
184 typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type>
185class policy_optimum_tracker
186{
187protected:
189 using traits_type = alignment_configuration_traits<alignment_configuration_t>;
191 using score_type = typename traits_type::score_type;
193 using matrix_coordinate_type = typename traits_type::matrix_coordinate_type;
194
196 score_type optimal_score{};
198 matrix_coordinate_type optimal_coordinate{};
200 optimum_updater_t compare_and_set_optimum{};
201
203 bool test_every_cell{false};
205 bool test_last_row_cell{false};
207 bool test_last_column_cell{false};
208
212 policy_optimum_tracker() = default;
213 policy_optimum_tracker(policy_optimum_tracker const &) = default;
214 policy_optimum_tracker(policy_optimum_tracker &&) = default;
215 policy_optimum_tracker & operator=(policy_optimum_tracker const &) = default;
216 policy_optimum_tracker & operator=(policy_optimum_tracker &&) = default;
217 ~policy_optimum_tracker() = default;
218
227 policy_optimum_tracker(alignment_configuration_t const & config)
228 {
229 auto method_global_config = config.get_or(align_cfg::method_global{});
230 test_last_row_cell = method_global_config.free_end_gaps_sequence1_trailing;
231 test_last_column_cell = method_global_config.free_end_gaps_sequence2_trailing;
232 }
234
249 template <typename cell_t>
250 decltype(auto) track_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
251 {
252 if (test_every_cell)
253 invoke_comparator(cell, std::move(coordinate));
254
255 return std::forward<cell_t>(cell);
256 }
257
272 template <typename cell_t>
273 decltype(auto) track_last_row_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
274 {
275 if (test_last_row_cell && !test_every_cell)
276 invoke_comparator(cell, std::move(coordinate));
277 }
292 template <typename cell_t>
293 decltype(auto) track_last_column_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
294 {
295 if (test_last_column_cell && !test_every_cell)
296 invoke_comparator(cell, std::move(coordinate));
297
298 return std::forward<cell_t>(cell);
299 }
300
315 template <typename cell_t>
316 decltype(auto) track_final_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
317 {
318 if (!(test_every_cell || test_last_row_cell || test_last_column_cell))
319 invoke_comparator(cell, std::move(coordinate));
320 }
322 void reset_optimum() noexcept
323 {
325 optimal_coordinate = {};
326 }
327
339 template <typename cell_t>
340 void invoke_comparator(cell_t && cell, matrix_coordinate_type coordinate) noexcept
341 {
342 compare_and_set_optimum(optimal_score, optimal_coordinate, cell.best_score(), std::move(coordinate));
343 }
344};
345} // namespace seqan3::detail
Provides helper type traits for the configuration and execution of the alignment algorithm.
Provides seqan3::configuration and utility functions.
Provides seqan3::detail::coordinate_matrix.
T lowest(T... args)
Provides seqan3::detail::matrix_index, seqan3::detail::matrix_coordinate and associated strong types.
Provides type traits for working with templates.
Provides seqan3::tuple_like.
Hide me