SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
policy_affine_gap_recursion.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 <tuple>
17 
24 
25 namespace seqan3::detail
26 {
27 
43 template <typename alignment_configuration_t>
44 class policy_affine_gap_recursion
45 {
46 protected:
48  using traits_type = alignment_configuration_traits<alignment_configuration_t>;
50  using original_score_type = typename traits_type::original_score_type;
52  using score_type = typename traits_type::score_type;
54  using affine_score_tuple_t = std::tuple<score_type, score_type, score_type>;
56  using affine_cell_type = affine_cell_proxy<affine_score_tuple_t>;
57 
59  score_type gap_extension_score{};
61  score_type gap_open_score{};
62 
64  bool first_row_is_free{};
66  bool first_column_is_free{};
67 
71  policy_affine_gap_recursion() = default;
72  policy_affine_gap_recursion(policy_affine_gap_recursion const &) = default;
73  policy_affine_gap_recursion(policy_affine_gap_recursion &&) = default;
74  policy_affine_gap_recursion & operator=(policy_affine_gap_recursion const &) = default;
75  policy_affine_gap_recursion & operator=(policy_affine_gap_recursion &&) = default;
76  ~policy_affine_gap_recursion() = default;
77 
87  explicit policy_affine_gap_recursion(alignment_configuration_t const & config)
88  {
89  // Get the gap scheme from the config or choose -1 and -10 as default.
90  auto const & selected_gap_scheme = config.get_or(align_cfg::gap_cost_affine{align_cfg::open_score{-10},
91  align_cfg::extension_score{-1}});
92 
93  gap_extension_score = maybe_convert_to_simd(selected_gap_scheme.extension_score);
94  gap_open_score = maybe_convert_to_simd(selected_gap_scheme.open_score) + gap_extension_score;
95 
96  auto method_global_config = config.get_or(align_cfg::method_global{});
97  first_row_is_free = method_global_config.free_end_gaps_sequence1_leading;
98  first_column_is_free = method_global_config.free_end_gaps_sequence2_leading;
99  }
101 
119  template <typename affine_cell_t>
120  affine_cell_type compute_inner_cell(score_type diagonal_score,
121  affine_cell_t previous_cell,
122  score_type const sequence_score) const noexcept
123  {
124  diagonal_score += sequence_score;
125  score_type horizontal_score = previous_cell.horizontal_score();
126  score_type vertical_score = previous_cell.vertical_score();
127 
128  diagonal_score = (diagonal_score < vertical_score) ? vertical_score : diagonal_score;
129  diagonal_score = (diagonal_score < horizontal_score) ? horizontal_score : diagonal_score;
130 
131  score_type tmp = diagonal_score + gap_open_score;
132  vertical_score += gap_extension_score;
133  horizontal_score += gap_extension_score;
134 
135  // store the vertical_score and horizontal_score value in the next path
136  vertical_score = (vertical_score < tmp) ? tmp : vertical_score;
137  horizontal_score = (horizontal_score < tmp) ? tmp : horizontal_score;
138 
139  return {diagonal_score, horizontal_score, vertical_score};
140  }
141 
152  affine_cell_type initialise_origin_cell() const noexcept
153  {
154  return {score_type{},
155  first_row_is_free ? score_type{} : gap_open_score,
156  first_column_is_free ? score_type{} : gap_open_score};
157  }
158 
173  template <typename affine_cell_t>
174  affine_cell_type initialise_first_column_cell(affine_cell_t previous_cell) const noexcept
175  {
176  score_type new_vertical = previous_cell.vertical_score() + gap_extension_score;
177  return {previous_cell.vertical_score(),
178  previous_cell.vertical_score() + gap_open_score,
179  first_column_is_free ? previous_cell.vertical_score() : new_vertical};
180  }
181 
196  template <typename affine_cell_t>
197  affine_cell_type initialise_first_row_cell(affine_cell_t previous_cell) const noexcept
198  {
199  score_type new_horizontal_score = previous_cell.horizontal_score() + gap_extension_score;
200  return {previous_cell.horizontal_score(),
201  first_row_is_free ? previous_cell.horizontal_score() : new_horizontal_score,
202  previous_cell.horizontal_score() + gap_open_score};
203  }
204 
215  score_type lowest_viable_score() const noexcept
216  {
217  if constexpr (simd_concept<score_type>)
218  assert(gap_open_score[0] <= 0 && gap_extension_score[0] <= 0);
219  else
220  assert(gap_open_score <= 0 && gap_extension_score <= 0);
221 
222  return maybe_convert_to_simd(std::numeric_limits<original_score_type>::lowest()) -
223  (gap_open_score + gap_extension_score);
224  }
225 
233  template <typename score_t>
237  constexpr auto maybe_convert_to_simd(score_t && score) const noexcept
238  {
239  if constexpr (simd_concept<score_type>)
240  return simd::fill<score_type>(std::forward<score_t>(score));
241  else // Return unmodified.
242  return std::forward<score_t>(score);
243  }
244 };
245 } // namespace seqan3::detail
Provides seqan3::detail::affine_cell_proxy.
Provides algorithms to modify seqan3::simd::simd_type.
Provides seqan3::align_config::gap_cost_affine.
Provides helper type traits for the configuration and execution of the alignment algorithm.
Provides type traits for working with templates.
A type that satisfies std::is_arithmetic_v<t>.
Provides seqan3::simd::simd_concept.