SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
simd_find_optimum_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
12#include <type_traits>
13
20
21namespace seqan3::detail
22{
23
35template <simd::simd_concept simd_t>
47
74template <typename alignment_algorithm_t, simd::simd_concept simd_t>
76{
77private:
80
84 bool test_every_cell{false};
86 bool test_last_row_cell{false};
89
93 constexpr simd_find_optimum_policy() = default;
99
101 template <typename configuration_t>
102 simd_find_optimum_policy(configuration_t const & config)
103 {
104 if constexpr (configuration_t::template exists<align_cfg::method_local>())
105 test_every_cell = true;
106
107 is_global_alignment = configuration_t::template exists<align_cfg::method_global>();
108
109 auto method_global_config = config.get_or(align_cfg::method_global{});
110
111 test_last_row_cell = method_global_config.free_end_gaps_sequence1_trailing || is_global_alignment;
112 test_last_column_cell = method_global_config.free_end_gaps_sequence2_trailing || is_global_alignment;
113 }
115
116protected:
118 template <typename cell_t, typename score_t>
119 constexpr void check_score_of_cell([[maybe_unused]] cell_t const & current_cell,
120 [[maybe_unused]] alignment_algorithm_state<score_t> & state) const noexcept
121 {
122 if (test_every_cell)
123 check_and_update(current_cell, state);
124 }
125
127 template <typename other_alignment_algorithm_t, simd_concept score_t, typename is_local_t>
129
131 template <typename other_alignment_algorithm_t>
133
135 template <typename cell_t, typename score_t>
136 constexpr void
137 check_score_of_last_row_cell([[maybe_unused]] cell_t const & last_row_cell,
138 [[maybe_unused]] alignment_algorithm_state<score_t> & state) const noexcept
139 {
140 // Only search in last row if requested and not done already.}
142 {
144 check_and_update<true>(last_row_cell, state);
145 else
146 check_and_update(last_row_cell, state);
147 }
148 }
149
151 template <typename alignment_column_t, typename score_t>
152 constexpr void
153 check_score_of_cells_in_last_column([[maybe_unused]] alignment_column_t && last_column,
154 [[maybe_unused]] alignment_algorithm_state<score_t> & state) const noexcept
155 {
156 // Only check last cell if not done before.
158 {
159 for (auto && cell : last_column)
160 {
162 check_and_update<false>(cell, state);
163 else
164 check_and_update(cell, state);
165 }
166 }
167 }
168
170 template <typename cell_t, typename score_t>
171 constexpr void check_score_of_last_cell([[maybe_unused]] cell_t const & last_cell,
172 [[maybe_unused]] alignment_algorithm_state<score_t> & state) const noexcept
173 {
174 // Only check last cell if not done before.
176 check_and_update(last_cell, state);
177 }
178
191 template <std::ranges::forward_range sequence1_collection_t,
192 std::ranges::forward_range sequence2_collection_t,
193 arithmetic score_t>
194 void initialise_find_optimum_policy([[maybe_unused]] sequence1_collection_t && sequence1_collection,
195 [[maybe_unused]] sequence2_collection_t && sequence2_collection,
196 [[maybe_unused]] score_t const padding_score)
197 {
199 {
200 assert(std::ranges::distance(sequence1_collection) == std::ranges::distance(sequence2_collection));
201
202 constexpr size_t simd_size = simd_traits<simd_t>::length;
203 // First get global size.
204 std::array<size_t, simd_size> sequence1_sizes{};
205 std::array<size_t, simd_size> sequence2_sizes{};
206
207 std::ptrdiff_t max_sequence1_size{};
208 std::ptrdiff_t max_sequence2_size{};
209
210 size_t array_index{};
211 for (auto && [sequence1, sequence2] : views::zip(sequence1_collection, sequence2_collection))
212 {
213 sequence1_sizes[array_index] = std::ranges::distance(sequence1);
214 sequence2_sizes[array_index] = std::ranges::distance(sequence2);
215 max_sequence1_size = std::max<std::ptrdiff_t>(sequence1_sizes[array_index], max_sequence1_size);
216 max_sequence2_size = std::max<std::ptrdiff_t>(sequence2_sizes[array_index], max_sequence2_size);
217 ++array_index;
218 }
219
220 // The global diagonal ending in the sink of the outer alignment matrix.
221 std::ptrdiff_t global_diagonal = max_sequence1_size - max_sequence2_size;
222
223 for (size_t simd_index = 0; simd_index < simd_size; ++simd_index)
224 {
225 if (std::ptrdiff_t local_diagonal = sequence1_sizes[simd_index] - sequence2_sizes[simd_index];
226 local_diagonal < global_diagonal)
227 { // optimum is stored in last row.
228 this->last_row_mask[simd_index] = max_sequence1_size - (global_diagonal - local_diagonal);
229 this->last_column_mask[simd_index] = max_sequence2_size + 1;
230 this->coordinate_offset[simd_index] = max_sequence2_size - sequence2_sizes[simd_index];
231 this->score_offset[simd_index] = padding_score * this->coordinate_offset[simd_index];
232 }
233 else // optimum is stored in last column
234 {
235 this->last_column_mask[simd_index] = max_sequence2_size - (local_diagonal - global_diagonal);
236 this->last_row_mask[simd_index] = max_sequence1_size + 1;
237 this->coordinate_offset[simd_index] = max_sequence1_size - sequence1_sizes[simd_index];
238 this->score_offset[simd_index] = padding_score * this->coordinate_offset[simd_index];
239 }
240 }
241 }
242 // else no-op
243 }
244
245private:
254 template <typename cell_t, typename score_t>
255 constexpr void check_and_update(cell_t const & cell, alignment_algorithm_state<score_t> & state) const noexcept
256 {
257 assert(!is_global_alignment); // This function should not be called for the global alignment.
258
259 auto const & [score_cell, trace_cell] = cell;
260 state.optimum.update_if_new_optimal_score(score_cell.current,
261 column_index_type{trace_cell.coordinate.first},
262 row_index_type{trace_cell.coordinate.second});
263 }
264
281 template <bool in_last_row, typename cell_t, typename score_t>
282 constexpr void check_and_update(cell_t const & cell, alignment_algorithm_state<score_t> & state) const noexcept
283 {
284 assert(is_global_alignment); // This function should only be called for the global alignment.
285
286 using simd_mask_t = typename simd_traits<simd_t>::mask_type;
287 auto const & [score_cell, trace_cell] = cell;
288 simd_t column_positions = simd::fill<simd_t>(trace_cell.coordinate.first);
289 simd_t row_positions = simd::fill<simd_t>(trace_cell.coordinate.second);
290
291 simd_mask_t mask{};
292
293 if constexpr (in_last_row) // Check if column was masked
294 mask = (column_positions == this->last_row_mask);
295 else // Check if row was masked
296 mask = (row_positions == this->last_column_mask);
297
298 // In global alignment we are only interested in the position not the max of the scores.
299 // In addition, the scores need to be corrected in order to track the right score.
300 state.optimum.score = mask ? score_cell.current - this->score_offset : state.optimum.score;
301 state.optimum.column_index = mask ? column_positions - this->coordinate_offset : state.optimum.column_index;
302 state.optimum.row_index = mask ? row_positions - this->coordinate_offset : state.optimum.row_index;
303 }
304};
305
306} // namespace seqan3::detail
Provides seqan3::detail::alignment_algorithm_state.
Provides seqan3::detail::alignment_optimum.
Sets the global alignment method.
Definition align_config_method.hpp:119
The CRTP-policy that implements the initialisation of the dynamic programming matrix with affine gaps...
Definition affine_gap_init_policy.hpp:34
The CRTP-policy that computes a batch of cells in the alignment matrix using simd instructions.
Definition simd_affine_gap_policy.hpp:51
The CRTP-policy to determine the optimum of the dynamic programming matrix.
Definition simd_find_optimum_policy.hpp:76
constexpr void check_score_of_cells_in_last_column(alignment_column_t &&last_column, alignment_algorithm_state< score_t > &state) const noexcept
Checks all cells of the last alignment column for a new alignment optimum.
Definition simd_find_optimum_policy.hpp:153
~simd_find_optimum_policy()=default
Defaulted.
constexpr simd_find_optimum_policy & operator=(simd_find_optimum_policy &&)=default
Defaulted.
constexpr void check_and_update(cell_t const &cell, alignment_algorithm_state< score_t > &state) const noexcept
Tests if the score in the current cell is greater than the current alignment optimum.
Definition simd_find_optimum_policy.hpp:255
constexpr simd_find_optimum_policy & operator=(simd_find_optimum_policy const &)=default
Defaulted.
constexpr void check_score_of_last_cell(cell_t const &last_cell, alignment_algorithm_state< score_t > &state) const noexcept
Checks if the last cell of the alignment matrix is a new optimum in the alignment.
Definition simd_find_optimum_policy.hpp:171
constexpr simd_find_optimum_policy(simd_find_optimum_policy &&)=default
Defaulted.
constexpr simd_find_optimum_policy(simd_find_optimum_policy const &)=default
Defaulted.
void initialise_find_optimum_policy(sequence1_collection_t &&sequence1_collection, sequence2_collection_t &&sequence2_collection, score_t const padding_score)
Initialises the global alignment state for the current batch of sequences.
Definition simd_find_optimum_policy.hpp:194
friend alignment_algorithm_t
Befriends the derived class to grant it access to the private members.
Definition simd_find_optimum_policy.hpp:79
constexpr void check_score_of_cell(cell_t const &current_cell, alignment_algorithm_state< score_t > &state) const noexcept
Checks if a given cell is a new optimum in the alignment.
Definition simd_find_optimum_policy.hpp:119
bool test_every_cell
Whether every cell of the alignment matrix shall be tracked.
Definition simd_find_optimum_policy.hpp:84
constexpr void check_and_update(cell_t const &cell, alignment_algorithm_state< score_t > &state) const noexcept
Tests if the current row, respectively column, is part of a global alignment to track.
Definition simd_find_optimum_policy.hpp:282
bool test_last_column_cell
Whether cells of the last column shall be tracked.
Definition simd_find_optimum_policy.hpp:88
simd_find_optimum_policy(configuration_t const &config)
Initialise the policy.
Definition simd_find_optimum_policy.hpp:102
constexpr void check_score_of_last_row_cell(cell_t const &last_row_cell, alignment_algorithm_state< score_t > &state) const noexcept
Checks if a cell in the last row of the alignment matrix is a new optimum in the alignment.
Definition simd_find_optimum_policy.hpp:137
constexpr simd_find_optimum_policy()=default
Defaulted.
bool test_last_row_cell
Whether cells of the last row shall be tracked.
Definition simd_find_optimum_policy.hpp:86
bool is_global_alignment
A flag to check if global alignment is computed.
Definition simd_find_optimum_policy.hpp:82
Implementation of a masked alphabet to be used for tuple composites.
Definition mask.hpp:35
Provides seqan3::detail::empty_type.
Provides seqan3::detail::find_optimum_policy.
seqan::stl::views::zip zip
A view adaptor that takes several views and returns tuple-like values from every i-th element of each...
Definition zip.hpp:24
A type that satisfies std::is_arithmetic_v<t>.
Refines the seqan3::simd::simd_concept requiring the underlying scalar type to model std::integral.
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Local state for the standard alignment algorithm.
Definition alignment_algorithm_state.hpp:32
A strong type for designated initialisation of the column index of a matrix.
Definition matrix_coordinate.hpp:29
A strong type for designated initialisation of the row index of a matrix.
Definition matrix_coordinate.hpp:58
A state that is only used for global alignments.
Definition simd_find_optimum_policy.hpp:37
simd_t coordinate_offset
A coordinate offset that needs to be subtracted for every alignment to get the correct end position.
Definition simd_find_optimum_policy.hpp:41
simd_t score_offset
The score offset that needs to be subtracted for every alignment to get the correct result.
Definition simd_find_optimum_policy.hpp:39
simd_t last_row_mask
A mask vector storing the column indices for alignments that end in the last row of the global matrix...
Definition simd_find_optimum_policy.hpp:45
simd_t last_column_mask
A mask vector storing the row indices for alignments that end in the last column of the global matrix...
Definition simd_find_optimum_policy.hpp:43
seqan3::simd::simd_traits is the trait class that provides uniform interface to the properties of sim...
Definition simd_traits.hpp:38
Provides seqan3::simd::simd_concept.
Provides seqan3::views::zip.
Hide me