SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
affine_gap_policy.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
10#pragma once
11
16
17namespace seqan3::detail
18{
19
20// ----------------------------------------------------------------------------
21// affine_gap_policy
22// ----------------------------------------------------------------------------
23
42template <typename alignment_algorithm_t, typename score_t, typename align_local_t = std::false_type>
44{
45private:
48
51
56 constexpr affine_gap_policy() noexcept = default;
57 constexpr affine_gap_policy(affine_gap_policy const &) noexcept = default;
58 constexpr affine_gap_policy(affine_gap_policy &&) noexcept = default;
59 constexpr affine_gap_policy & operator=(affine_gap_policy const &) noexcept = default;
60 constexpr affine_gap_policy & operator=(affine_gap_policy &&) noexcept = default;
61 ~affine_gap_policy() noexcept = default;
62
64 template <typename configuration_t>
65 affine_gap_policy(configuration_t const & /*config*/)
66 {}
68
82 template <typename cell_t>
83 constexpr void
84 compute_cell(cell_t && current_cell, alignment_algorithm_state<score_t> & cache, score_t const score) const noexcept
85 {
86 // score_cell = seqan3::detail::alignment_score_matrix_proxy
87 // trace_cell = seqan3::detail::alignment_trace_matrix_proxy
88 auto & [score_cell, trace_cell] = current_cell;
89 constexpr bool with_trace = !decays_to_ignore_v<std::remove_reference_t<decltype(trace_cell.current)>>;
90 // Precompute the diagonal score.
91 score_t tmp = score_cell.diagonal + score;
92
93 if constexpr (with_trace)
94 {
95 tmp = (tmp < score_cell.up) ? (trace_cell.current = trace_cell.up, score_cell.up)
96 : (trace_cell.current = trace_directions::diagonal | trace_cell.up, tmp);
97 tmp = (tmp < score_cell.r_left)
98 ? (trace_cell.current = trace_cell.r_left | (trace_cell.current & trace_directions::carry_up_open),
99 score_cell.r_left)
100 : (trace_cell.current |= trace_cell.r_left, tmp);
101 }
102 else
103 {
104 tmp = (tmp < score_cell.up) ? score_cell.up : tmp;
105 tmp = (tmp < score_cell.r_left) ? score_cell.r_left : tmp;
106 }
107
108 if constexpr (align_local_t::value)
109 tmp = (tmp < 0) ? (trace_cell.current = trace_directions::none, 0) : tmp;
110
111 // Store the current max score.
112 score_cell.current = tmp;
113 // Check if this was the optimum. Possibly a noop.
114 static_cast<alignment_algorithm_t const &>(*this).check_score_of_cell(current_cell, cache);
115
116 // Prepare horizontal and vertical score for next column.
117 tmp += cache.gap_open_score;
118 score_cell.up += cache.gap_extension_score;
119 score_cell.w_left = score_cell.r_left + cache.gap_extension_score;
120
121 score_cell.up = (score_cell.up < tmp) ? (trace_cell.up = trace_directions::up_open, tmp)
122 : (trace_cell.up = trace_directions::up, score_cell.up);
123 score_cell.w_left = (score_cell.w_left < tmp) ? (trace_cell.w_left = trace_directions::left_open, tmp)
124 : (trace_cell.w_left = trace_directions::left, score_cell.w_left);
125 }
126
140 template <typename cell_t>
141 constexpr void compute_first_band_cell(cell_t && current_cell,
143 score_t const score) const noexcept
144 {
145 // Compute the diagonal score and the compare with the previous horizontal value.
146 // score_cell = seqan3::detail::alignment_score_matrix_proxy
147 // trace_cell = seqan3::detail::alignment_trace_matrix_proxy
148 auto & [score_cell, trace_cell] = current_cell;
149 score_cell.current = score_cell.diagonal + score;
150
151 score_cell.current = (score_cell.current < score_cell.r_left)
152 ? (trace_cell.current = trace_cell.r_left, score_cell.r_left)
153 : (trace_cell.current = trace_directions::diagonal, score_cell.current);
154
155 if constexpr (align_local_t::value)
156 {
157 score_cell.current =
158 (score_cell.current < 0) ? (trace_cell.current = trace_directions::none, 0) : score_cell.current;
159 }
160 // Check if this was the optimum. Possibly a noop.
161 static_cast<alignment_algorithm_t const &>(*this).check_score_of_cell(current_cell, cache);
162
163 // At the top of the band we can not come from up but only diagonal or left, so the next vertical must be a
164 // gap open.
165 score_cell.up = score_cell.current + cache.gap_open_score; // add gap open cost
166 trace_cell.up = trace_directions::up_open;
167 }
168
181 template <typename alignment_configuration_t>
182 constexpr void initialise_alignment_state(alignment_configuration_t const & config) noexcept
183 {
184 auto scheme =
186
187 alignment_state.gap_extension_score = static_cast<score_t>(scheme.extension_score);
188 alignment_state.gap_open_score = static_cast<score_t>(scheme.extension_score + scheme.open_score);
189 }
190
192};
193
194} // namespace seqan3::detail
Provides seqan3::align_config::gap_cost_affine.
Provides seqan3::detail::alignment_algorithm_state.
A configuration element for the affine gap cost scheme.
Definition align_config_gap_cost_affine.hpp:72
The CRTP-policy that computes a single cell in the alignment matrix.
Definition affine_gap_policy.hpp:44
constexpr void compute_first_band_cell(cell_t &&current_cell, alignment_algorithm_state< score_t > &cache, score_t const score) const noexcept
Computes the score of the first cell within the band.
Definition affine_gap_policy.hpp:141
friend alignment_algorithm_t
Befriends the derived class to grant it access to the private members.
Definition affine_gap_policy.hpp:47
constexpr void compute_cell(cell_t &&current_cell, alignment_algorithm_state< score_t > &cache, score_t const score) const noexcept
Computes the score of the current cell.
Definition affine_gap_policy.hpp:84
constexpr void initialise_alignment_state(alignment_configuration_t const &config) noexcept
Initialise the alignment state for affine gap computation.
Definition affine_gap_policy.hpp:182
alignment_state_t alignment_state
The internal alignment state tracking the current alignment optimum.
Definition affine_gap_policy.hpp:191
constexpr affine_gap_policy() noexcept=default
Defaulted.
@ up
Trace comes from the above entry.
@ left
Trace comes from the left entry.
@ diagonal
Trace comes from the diagonal entry.
@ carry_up_open
Carry bit for the last up open even if it is not the maximum value.
@ left_open
Trace comes from the left entry, while opening the gap.
@ up_open
Trace comes from the above entry, while opening the gap.
constexpr bool decays_to_ignore_v
Return whether the input type with const, volatile and references removed is std::ignore's type....
Definition basic.hpp:135
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
A strong type of underlying type int32_t that represents the score (usually negative) of any characte...
Definition align_config_gap_cost_affine.hpp:48
A strong type of underlying type int32_t that represents a score (usually negative) that is incurred ...
Definition align_config_gap_cost_affine.hpp:31
score_type gap_extension_score
The cached gap extension score.
Definition alignment_algorithm_state.hpp:34
score_type gap_open_score
The cached gap open score.
Definition alignment_algorithm_state.hpp:36
Provides the declaration of seqan3::detail::trace_directions.
Provides concepts that do not have equivalents in C++20.
Hide me