SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
policy_alignment_matrix.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 <tuple>
13
19
20namespace seqan3::detail
21{
22
37template <typename traits_t, typename alignment_matrix_t>
38 requires (is_type_specialisation_of_v<traits_t, alignment_configuration_traits> &&
39 requires (alignment_matrix_t & matrix, typename traits_t::score_type const initial_score)
40 {
41 { matrix.resize(column_index_type{size_t{}}, row_index_type{size_t{}}, initial_score) };
42 })
44{
45protected:
47 using score_type = typename traits_t::score_type;
49 using matrix_index_type = typename traits_t::matrix_index_type;
50
52 int32_t lower_diagonal{};
54 int32_t upper_diagonal{};
56 bool last_column_is_free{};
58 bool last_row_is_free{};
59
69
83 template <typename alignment_configuration_t>
84 requires (is_type_specialisation_of_v<alignment_configuration_t, configuration>)
85 policy_alignment_matrix(alignment_configuration_t const & config)
86 {
87 using seqan3::get;
88
89 auto band = config.get_or(seqan3::align_cfg::band_fixed_size{});
90
91 lower_diagonal = band.lower_diagonal;
92 upper_diagonal = band.upper_diagonal;
93
94 bool invalid_band = upper_diagonal < lower_diagonal;
95 std::string error_cause = (invalid_band) ? " The upper diagonal is smaller than the lower diagonal." : "";
96
97 if constexpr (traits_t::is_global)
98 {
99 auto method_global_config = get<seqan3::align_cfg::method_global>(config);
100
101 bool first_row_is_free = method_global_config.free_end_gaps_sequence1_leading;
102 bool first_column_is_free = method_global_config.free_end_gaps_sequence2_leading;
103
104 last_row_is_free = method_global_config.free_end_gaps_sequence1_trailing;
105 last_column_is_free = method_global_config.free_end_gaps_sequence2_trailing;
106 // band starts in first column without free gaps or band starts in first row without free gaps.
107 invalid_band |= (upper_diagonal < 0 && !first_column_is_free) || (lower_diagonal > 0 && !first_row_is_free);
108 error_cause += " The band starts in a region without free gaps.";
109 }
110
111 if (invalid_band)
112 throw invalid_alignment_configuration{"The selected band [" + std::to_string(lower_diagonal) + ":"
113 + std::to_string(upper_diagonal)
114 + "] cannot be used with the current "
115 "alignment configuration:"
116 + error_cause};
117 }
119
142 auto acquire_matrices(size_t const sequence1_size,
143 size_t const sequence2_size,
144 score_type initial_score = score_type{}) const
145 {
146 assert(sequence1_size < static_cast<uint64_t>(std::numeric_limits<int64_t>::max()));
147 assert(sequence2_size < static_cast<uint64_t>(std::numeric_limits<int64_t>::max()));
148
149 if constexpr (traits_t::is_banded)
150 check_valid_band_configuration(sequence1_size, sequence2_size);
151
152 static thread_local alignment_matrix_t alignment_matrix{};
153 static thread_local coordinate_matrix<matrix_index_type> index_matrix{};
154
155 // Increase dimension by one for the initialisation of the matrix.
156 size_t const column_count = sequence1_size + 1;
157 size_t row_count = sequence2_size + 1;
158
159 index_matrix.resize(column_index_type{column_count}, row_index_type{row_count});
160
161 if constexpr (traits_t::is_banded)
162 {
163 assert(upper_diagonal - lower_diagonal + 1 > 0); // Band size is a positive integer.
164 // Allocate one more cell to compute the last cell of the band with standard recursion function.
165 row_count = std::min<int64_t>(upper_diagonal - lower_diagonal + 2, row_count);
166 }
167
168 alignment_matrix.resize(column_index_type{column_count}, row_index_type{row_count}, initial_score);
169
170 return std::tie(alignment_matrix, index_matrix);
171 }
172
181 void check_valid_band_configuration(size_t const sequence1_size, size_t const sequence2_size) const
182 {
183 bool const upper_diagonal_ends_before_last_cell = (upper_diagonal + sequence2_size) < sequence1_size;
184 bool const lower_diagonal_ends_behind_last_cell = (-lower_diagonal + sequence1_size) < sequence2_size;
185
186 bool invalid_band = false;
187 std::string error_cause{};
188
189 if constexpr (traits_t::is_global)
190 {
191 // band ends in last column without free gaps or band ends in last row without free gaps.
192 invalid_band |= (lower_diagonal_ends_behind_last_cell && !last_column_is_free)
193 || (upper_diagonal_ends_before_last_cell && !last_row_is_free);
194 error_cause = "The band ends in a region without free gaps.";
195 }
196
197 if (invalid_band)
198 throw invalid_alignment_configuration{"The selected band [" + std::to_string(lower_diagonal) + ":"
199 + std::to_string(upper_diagonal)
200 + "] cannot be used with the current "
201 "alignment configuration: "
202 + error_cause};
203 }
204};
205} // namespace seqan3::detail
Includes customized exception types for the alignment module .
Provides helper type traits for the configuration and execution of the alignment algorithm.
Configuration element for setting a fixed size band.
Definition align_config_band.hpp:60
A policy that provides a common interface to acquire the correct alignment matrices.
Definition policy_alignment_matrix.hpp:44
auto acquire_matrices(size_t const sequence1_size, size_t const sequence2_size, score_type initial_score=score_type{}) const
Acquires a new thread local alignment and index matrix for the given sequence sizes.
Definition policy_alignment_matrix.hpp:142
typename traits_t::score_type score_type
The configured score type.
Definition policy_alignment_matrix.hpp:47
policy_alignment_matrix & operator=(policy_alignment_matrix const &)=default
Defaulted.
policy_alignment_matrix(policy_alignment_matrix &&)=default
Defaulted.
typename traits_t::matrix_index_type matrix_index_type
The configured matrix index type to store the coordinates.
Definition policy_alignment_matrix.hpp:49
policy_alignment_matrix()=default
Defaulted.
~policy_alignment_matrix()=default
Defaulted.
policy_alignment_matrix(alignment_configuration_t const &config)
Constructs and initialises the algorithm using the alignment configuration.
Definition policy_alignment_matrix.hpp:85
policy_alignment_matrix(policy_alignment_matrix const &)=default
Defaulted.
void check_valid_band_configuration(size_t const sequence1_size, size_t const sequence2_size) const
Checks whether the band is valid for the given sequence sizes.
Definition policy_alignment_matrix.hpp:181
policy_alignment_matrix & operator=(policy_alignment_matrix &&)=default
Defaulted.
Thrown if the configuration of the alignment algorithm is invalid.
Definition alignment/exception.hpp:32
Provides seqan3::configuration and utility functions.
Provides seqan3::detail::coordinate_matrix.
@ band
ID for the band option.
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
constexpr auto const & get(configuration< configs_t... > const &config) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition configuration.hpp:412
Provides type traits for working with templates.
T tie(T... args)
T to_string(T... args)
Hide me