SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
policy_optimum_tracker.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, 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 
14 #pragma once
15 
16 #include <limits>
17 
24 
25 namespace seqan3::detail
26 {
27 
38 struct max_score_updater
39 {
55  template <typename score_t, typename coordinate_t>
57  requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &> &&
58  std::assignable_from<coordinate_t &, coordinate_t const &>)
60  void operator()(score_t & optimal_score,
61  coordinate_t & optimal_coordinate,
62  score_t current_score,
63  coordinate_t current_coordinate) const noexcept
64  {
65  bool const is_better_score = current_score >= optimal_score;
66  optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
67  optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
68  }
69 };
70 
111 struct max_score_banded_updater
112 {
113 private:
115  size_t target_row_index{};
117  size_t target_col_index{};
118 
119 public:
137  template <typename score_t, typename coordinate_t>
139  requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &> &&
140  std::assignable_from<coordinate_t &, coordinate_t const &>)
142  void operator()(score_t & optimal_score,
143  coordinate_t & optimal_coordinate,
144  score_t current_score,
145  coordinate_t current_coordinate) const noexcept
146  {
147  bool const is_better_score = (target_row_index == current_coordinate.row ||
148  target_col_index == current_coordinate.col) &&
149  (current_score >= optimal_score);
150  optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
151  optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
152  }
153 
158  void set_target_indices(row_index_type<size_t> row_index, column_index_type<size_t> col_index) noexcept
159  {
160  target_row_index = row_index.get();
161  target_col_index = col_index.get();
162  }
163 };
164 
184 template <typename alignment_configuration_t, std::semiregular optimum_updater_t>
186  requires is_type_specialisation_of_v<alignment_configuration_t, configuration> &&
187  std::invocable<optimum_updater_t,
188  typename alignment_configuration_traits<alignment_configuration_t>::score_type &,
189  typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type &,
190  typename alignment_configuration_traits<alignment_configuration_t>::score_type,
191  typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type>
193 class policy_optimum_tracker
194 {
195 protected:
197  using traits_type = alignment_configuration_traits<alignment_configuration_t>;
199  using score_type = typename traits_type::score_type;
201  using matrix_coordinate_type = typename traits_type::matrix_coordinate_type;
202 
204  score_type optimal_score{};
206  matrix_coordinate_type optimal_coordinate{};
208  optimum_updater_t compare_and_set_optimum{};
209 
211  bool test_every_cell{false};
213  bool test_last_row_cell{false};
215  bool test_last_column_cell{false};
216 
220  policy_optimum_tracker() = default;
221  policy_optimum_tracker(policy_optimum_tracker const &) = default;
222  policy_optimum_tracker(policy_optimum_tracker &&) = default;
223  policy_optimum_tracker & operator=(policy_optimum_tracker const &) = default;
224  policy_optimum_tracker & operator=(policy_optimum_tracker &&) = default;
225  ~policy_optimum_tracker() = default;
226 
235  policy_optimum_tracker(alignment_configuration_t const & config)
236  {
237  auto method_global_config = config.get_or(align_cfg::method_global{});
238  test_last_row_cell = method_global_config.free_end_gaps_sequence1_trailing;
239  test_last_column_cell = method_global_config.free_end_gaps_sequence2_trailing;
240  }
242 
257  template <typename cell_t>
258  decltype(auto) track_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
259  {
260  if (test_every_cell)
261  invoke_comparator(cell, std::move(coordinate));
262 
263  return std::forward<cell_t>(cell);
264  }
265 
280  template <typename cell_t>
281  decltype(auto) track_last_row_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
282  {
283  if (test_last_row_cell && !test_every_cell)
284  invoke_comparator(cell, std::move(coordinate));
285  }
300  template <typename cell_t>
301  decltype(auto) track_last_column_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
302  {
303  if (test_last_column_cell && !test_every_cell)
304  invoke_comparator(cell, std::move(coordinate));
305 
306  return std::forward<cell_t>(cell);
307  }
308 
323  template <typename cell_t>
324  decltype(auto) track_final_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
325  {
326  if (!(test_every_cell || test_last_row_cell || test_last_column_cell))
327  invoke_comparator(cell, std::move(coordinate));
328  }
330  void reset_optimum() noexcept
331  {
332  optimal_score = std::numeric_limits<score_type>::lowest();
333  optimal_coordinate = {};
334  }
335 
347  template <typename cell_t>
348  void invoke_comparator(cell_t && cell, matrix_coordinate_type coordinate) noexcept
349  {
350  compare_and_set_optimum(optimal_score, optimal_coordinate, cell.best_score(), std::move(coordinate));
351  }
352 };
353 } // 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.
Provides type traits for working with templates.
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:74
T lowest(T... args)
Provides seqan3::detail::matrix_index, seqan3::detail::matrix_coordinate and associated strong types.
Provides seqan3::tuple_like.