SeqAn3  3.2.0
The Modern C++ library for sequence analysis.
policy_optimum_tracker.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 
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>
56  requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &>
57  && std::assignable_from<coordinate_t &, coordinate_t const &>)
58  void operator()(score_t & optimal_score,
59  coordinate_t & optimal_coordinate,
60  score_t current_score,
61  coordinate_t current_coordinate) const noexcept
62  {
63  bool const is_better_score = current_score >= optimal_score;
64  optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
65  optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
66  }
67 };
68 
109 struct max_score_banded_updater
110 {
111 private:
113  size_t target_row_index{};
115  size_t target_col_index{};
116 
117 public:
135  template <typename score_t, typename coordinate_t>
136  requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &>
137  && std::assignable_from<coordinate_t &, coordinate_t const &>)
138  void operator()(score_t & optimal_score,
139  coordinate_t & optimal_coordinate,
140  score_t current_score,
141  coordinate_t current_coordinate) const noexcept
142  {
143  bool const is_better_score =
144  (target_row_index == current_coordinate.row || target_col_index == current_coordinate.col)
145  && (current_score >= optimal_score);
146  optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
147  optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
148  }
149 
154  void set_target_indices(row_index_type<size_t> row_index, column_index_type<size_t> col_index) noexcept
155  {
156  target_row_index = row_index.get();
157  target_col_index = col_index.get();
158  }
159 };
160 
180 template <typename alignment_configuration_t, std::semiregular optimum_updater_t>
181  requires is_type_specialisation_of_v<alignment_configuration_t, configuration>
182  && std::invocable<
183  optimum_updater_t,
184  typename alignment_configuration_traits<alignment_configuration_t>::score_type &,
185  typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type &,
186  typename alignment_configuration_traits<alignment_configuration_t>::score_type,
187  typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type>
188 class policy_optimum_tracker
189 {
190 protected:
192  using traits_type = alignment_configuration_traits<alignment_configuration_t>;
194  using score_type = typename traits_type::score_type;
196  using matrix_coordinate_type = typename traits_type::matrix_coordinate_type;
197 
199  score_type optimal_score{};
201  matrix_coordinate_type optimal_coordinate{};
203  optimum_updater_t compare_and_set_optimum{};
204 
206  bool test_every_cell{false};
208  bool test_last_row_cell{false};
210  bool test_last_column_cell{false};
211 
215  policy_optimum_tracker() = default;
216  policy_optimum_tracker(policy_optimum_tracker const &) = default;
217  policy_optimum_tracker(policy_optimum_tracker &&) = default;
218  policy_optimum_tracker & operator=(policy_optimum_tracker const &) = default;
219  policy_optimum_tracker & operator=(policy_optimum_tracker &&) = default;
220  ~policy_optimum_tracker() = default;
221 
230  policy_optimum_tracker(alignment_configuration_t const & config)
231  {
232  auto method_global_config = config.get_or(align_cfg::method_global{});
233  test_last_row_cell = method_global_config.free_end_gaps_sequence1_trailing;
234  test_last_column_cell = method_global_config.free_end_gaps_sequence2_trailing;
235  }
237 
252  template <typename cell_t>
253  decltype(auto) track_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
254  {
255  if (test_every_cell)
256  invoke_comparator(cell, std::move(coordinate));
257 
258  return std::forward<cell_t>(cell);
259  }
260 
275  template <typename cell_t>
276  decltype(auto) track_last_row_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
277  {
278  if (test_last_row_cell && !test_every_cell)
279  invoke_comparator(cell, std::move(coordinate));
280  }
295  template <typename cell_t>
296  decltype(auto) track_last_column_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
297  {
298  if (test_last_column_cell && !test_every_cell)
299  invoke_comparator(cell, std::move(coordinate));
300 
301  return std::forward<cell_t>(cell);
302  }
303 
318  template <typename cell_t>
319  decltype(auto) track_final_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
320  {
321  if (!(test_every_cell || test_last_row_cell || test_last_column_cell))
322  invoke_comparator(cell, std::move(coordinate));
323  }
325  void reset_optimum() noexcept
326  {
327  optimal_score = std::numeric_limits<score_type>::lowest();
328  optimal_coordinate = {};
329  }
330 
342  template <typename cell_t>
343  void invoke_comparator(cell_t && cell, matrix_coordinate_type coordinate) noexcept
344  {
345  compare_and_set_optimum(optimal_score, optimal_coordinate, cell.best_score(), std::move(coordinate));
346  }
347 };
348 } // 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.
requires requires
The rank_type of the semi-alphabet; defined as the return type of seqan3::to_rank....
Definition: alphabet/concept.hpp:164
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.