SeqAn3 3.3.0-rc.1
The Modern C++ library for sequence analysis.
affine_gap_policy.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
13#pragma once
14
19
20namespace seqan3::detail
21{
22
23// ----------------------------------------------------------------------------
24// affine_gap_policy
25// ----------------------------------------------------------------------------
26
45template <typename alignment_algorithm_t, typename score_t, typename align_local_t = std::false_type>
47{
48private:
51
54
59 constexpr affine_gap_policy() noexcept = default;
60 constexpr affine_gap_policy(affine_gap_policy const &) noexcept = default;
61 constexpr affine_gap_policy(affine_gap_policy &&) noexcept = default;
62 constexpr affine_gap_policy & operator=(affine_gap_policy const &) noexcept = default;
63 constexpr affine_gap_policy & operator=(affine_gap_policy &&) noexcept = default;
64 ~affine_gap_policy() noexcept = default;
65
67 template <typename configuration_t>
68 affine_gap_policy(configuration_t const & /*config*/)
69 {}
71
85 template <typename cell_t>
86 constexpr void
87 compute_cell(cell_t && current_cell, alignment_algorithm_state<score_t> & cache, score_t const score) const noexcept
88 {
89 // score_cell = seqan3::detail::alignment_score_matrix_proxy
90 // trace_cell = seqan3::detail::alignment_trace_matrix_proxy
91 auto & [score_cell, trace_cell] = current_cell;
92 constexpr bool with_trace = !decays_to_ignore_v<std::remove_reference_t<decltype(trace_cell.current)>>;
93 // Precompute the diagonal score.
94 score_t tmp = score_cell.diagonal + score;
95
96 if constexpr (with_trace)
97 {
98 tmp = (tmp < score_cell.up) ? (trace_cell.current = trace_cell.up, score_cell.up)
99 : (trace_cell.current = trace_directions::diagonal | trace_cell.up, tmp);
100 tmp = (tmp < score_cell.r_left)
101 ? (trace_cell.current = trace_cell.r_left | (trace_cell.current & trace_directions::carry_up_open),
102 score_cell.r_left)
103 : (trace_cell.current |= trace_cell.r_left, tmp);
104 }
105 else
106 {
107 tmp = (tmp < score_cell.up) ? score_cell.up : tmp;
108 tmp = (tmp < score_cell.r_left) ? score_cell.r_left : tmp;
109 }
110
111 if constexpr (align_local_t::value)
112 tmp = (tmp < 0) ? (trace_cell.current = trace_directions::none, 0) : tmp;
113
114 // Store the current max score.
115 score_cell.current = tmp;
116 // Check if this was the optimum. Possibly a noop.
117 static_cast<alignment_algorithm_t const &>(*this).check_score_of_cell(current_cell, cache);
118
119 // Prepare horizontal and vertical score for next column.
120 tmp += cache.gap_open_score;
121 score_cell.up += cache.gap_extension_score;
122 score_cell.w_left = score_cell.r_left + cache.gap_extension_score;
123
124 score_cell.up = (score_cell.up < tmp) ? (trace_cell.up = trace_directions::up_open, tmp)
125 : (trace_cell.up = trace_directions::up, score_cell.up);
126 score_cell.w_left = (score_cell.w_left < tmp) ? (trace_cell.w_left = trace_directions::left_open, tmp)
127 : (trace_cell.w_left = trace_directions::left, score_cell.w_left);
128 }
129
143 template <typename cell_t>
144 constexpr void compute_first_band_cell(cell_t && current_cell,
146 score_t const score) const noexcept
147 {
148 // Compute the diagonal score and the compare with the previous horizontal value.
149 // score_cell = seqan3::detail::alignment_score_matrix_proxy
150 // trace_cell = seqan3::detail::alignment_trace_matrix_proxy
151 auto & [score_cell, trace_cell] = current_cell;
152 score_cell.current = score_cell.diagonal + score;
153
154 score_cell.current = (score_cell.current < score_cell.r_left)
155 ? (trace_cell.current = trace_cell.r_left, score_cell.r_left)
156 : (trace_cell.current = trace_directions::diagonal, score_cell.current);
157
158 if constexpr (align_local_t::value)
159 {
160 score_cell.current =
161 (score_cell.current < 0) ? (trace_cell.current = trace_directions::none, 0) : score_cell.current;
162 }
163 // Check if this was the optimum. Possibly a noop.
164 static_cast<alignment_algorithm_t const &>(*this).check_score_of_cell(current_cell, cache);
165
166 // At the top of the band we can not come from up but only diagonal or left, so the next vertical must be a
167 // gap open.
168 score_cell.up = score_cell.current + cache.gap_open_score; // add gap open cost
169 trace_cell.up = trace_directions::up_open;
170 }
171
184 template <typename alignment_configuration_t>
185 constexpr void initialise_alignment_state(alignment_configuration_t const & config) noexcept
186 {
187 auto scheme =
189
190 alignment_state.gap_extension_score = static_cast<score_t>(scheme.extension_score);
191 alignment_state.gap_open_score = static_cast<score_t>(scheme.extension_score + scheme.open_score);
192 }
193
195};
196
197} // 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:75
The CRTP-policy that computes a single cell in the alignment matrix.
Definition: affine_gap_policy.hpp:47
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:144
friend alignment_algorithm_t
Befriends the derived class to grant it access to the private members.
Definition: affine_gap_policy.hpp:50
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:87
constexpr void initialise_alignment_state(alignment_configuration_t const &config) noexcept
Initialise the alignment state for affine gap computation.
Definition: affine_gap_policy.hpp:185
alignment_state_t alignment_state
The internal alignment state tracking the current alignment optimum.
Definition: affine_gap_policy.hpp:194
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:134
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
A strong type of underlying type int32_t that represents the score (usually negative) of any characte...
Definition: align_config_gap_cost_affine.hpp:51
A strong type of underlying type int32_t that represents a score (usually negative) that is incurred ...
Definition: align_config_gap_cost_affine.hpp:34
score_type gap_extension_score
The cached gap extension score.
Definition: alignment_algorithm_state.hpp:37
score_type gap_open_score
The cached gap open score.
Definition: alignment_algorithm_state.hpp:39
Provides the declaration of seqan3::detail::trace_directions.
Provides concepts that do not have equivalents in C++20.