Loading...
Searching...
No Matches
policy_optimum_tracker.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <limits>
#include <seqan3/alignment/matrix/detail/coordinate_matrix.hpp>
#include <seqan3/alignment/matrix/detail/matrix_coordinate.hpp>
#include <seqan3/alignment/pairwise/detail/type_traits.hpp>
#include <seqan3/core/configuration/configuration.hpp>
#include <seqan3/core/detail/template_inspection.hpp>
#include <seqan3/utility/tuple/concept.hpp>
namespace seqan3::detail
{
struct max_score_updater
{
template <typename score_t, typename coordinate_t>
requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &>
&& std::assignable_from<coordinate_t &, coordinate_t const &>)
void operator()(score_t & optimal_score,
coordinate_t & optimal_coordinate,
score_t current_score,
coordinate_t current_coordinate) const noexcept
{
bool const is_better_score = current_score >= optimal_score;
optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
}
};
struct max_score_banded_updater
{
private:
size_t target_row_index{};
size_t target_col_index{};
public:
template <typename score_t, typename coordinate_t>
requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &>
&& std::assignable_from<coordinate_t &, coordinate_t const &>)
void operator()(score_t & optimal_score,
coordinate_t & optimal_coordinate,
score_t current_score,
coordinate_t current_coordinate) const noexcept
{
bool const is_better_score =
(target_row_index == current_coordinate.row || target_col_index == current_coordinate.col)
&& (current_score >= optimal_score);
optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
}
void set_target_indices(row_index_type<size_t> row_index, column_index_type<size_t> col_index) noexcept
{
target_row_index = row_index.get();
target_col_index = col_index.get();
}
};
template <typename alignment_configuration_t, std::semiregular optimum_updater_t>
requires is_type_specialisation_of_v<alignment_configuration_t, configuration>
&& std::invocable<
optimum_updater_t,
typename alignment_configuration_traits<alignment_configuration_t>::score_type &,
typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type &,
typename alignment_configuration_traits<alignment_configuration_t>::score_type,
typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type>
class policy_optimum_tracker
{
protected:
using traits_type = alignment_configuration_traits<alignment_configuration_t>;
using score_type = typename traits_type::score_type;
using matrix_coordinate_type = typename traits_type::matrix_coordinate_type;
score_type optimal_score{};
matrix_coordinate_type optimal_coordinate{};
optimum_updater_t compare_and_set_optimum{};
bool test_every_cell{false};
bool test_last_row_cell{false};
bool test_last_column_cell{false};
policy_optimum_tracker() = default;
policy_optimum_tracker(policy_optimum_tracker const &) = default;
policy_optimum_tracker(policy_optimum_tracker &&) = default;
policy_optimum_tracker & operator=(policy_optimum_tracker const &) = default;
policy_optimum_tracker & operator=(policy_optimum_tracker &&) = default;
~policy_optimum_tracker() = default;
policy_optimum_tracker(alignment_configuration_t const & config)
{
auto method_global_config = config.get_or(align_cfg::method_global{});
test_last_row_cell = method_global_config.free_end_gaps_sequence1_trailing;
test_last_column_cell = method_global_config.free_end_gaps_sequence2_trailing;
}
template <typename cell_t>
decltype(auto) track_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
{
if (test_every_cell)
invoke_comparator(cell, std::move(coordinate));
return std::forward<cell_t>(cell);
}
template <typename cell_t>
decltype(auto) track_last_row_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
{
if (test_last_row_cell && !test_every_cell)
invoke_comparator(cell, std::move(coordinate));
}
template <typename cell_t>
decltype(auto) track_last_column_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
{
if (test_last_column_cell && !test_every_cell)
invoke_comparator(cell, std::move(coordinate));
return std::forward<cell_t>(cell);
}
template <typename cell_t>
decltype(auto) track_final_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
{
if (!(test_every_cell || test_last_row_cell || test_last_column_cell))
invoke_comparator(cell, std::move(coordinate));
}
void reset_optimum() noexcept
{
optimal_score = std::numeric_limits<score_type>::lowest();
optimal_coordinate = {};
}
template <typename cell_t>
void invoke_comparator(cell_t && cell, matrix_coordinate_type coordinate) noexcept
{
compare_and_set_optimum(optimal_score, optimal_coordinate, cell.best_score(), std::move(coordinate));
}
};
} // namespace seqan3::detail
type_traits.hpp
Provides helper type traits for the configuration and execution of the alignment algorithm.
configuration.hpp
Provides seqan3::configuration and utility functions.
coordinate_matrix.hpp
Provides seqan3::detail::coordinate_matrix.
std::forward
T forward(T... args)
limits
std::numeric_limits::lowest
T lowest(T... args)
matrix_coordinate.hpp
Provides seqan3::detail::matrix_index, seqan3::detail::matrix_coordinate and associated strong types.
template_inspection.hpp
Provides type traits for working with templates.
concept.hpp
Provides seqan3::tuple_like.
Hide me