SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
simd_matrix_scoring_scheme.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 <concepts>
13
20
21namespace seqan3::detail
22{
23
53template <simd_concept simd_score_t, semialphabet alphabet_t, typename alignment_t>
54 requires (std::same_as<alignment_t, align_cfg::method_local> || std::same_as<alignment_t, align_cfg::method_global>)
56{
57private:
61 using simd_score_profile_type = simd_score_t;
63 using simd_alphabet_ranks_type = simd_score_t;
64
65 static_assert(seqan3::alphabet_size<alphabet_t> <= std::numeric_limits<scalar_type>::max(),
66 "The selected simd scalar type is not large enough to represent the given alphabet including an "
67 "additional padding symbol!");
68
69 static_assert(seqan3::alphabet_size<alphabet_t> < std::numeric_limits<scalar_type>::max(),
70 "The selected simd scalar type is not large enough to represent the given alphabet including an "
71 "additional padding symbol!");
72
74 static constexpr bool is_global = std::same_as<alignment_t, align_cfg::method_global>;
76 static constexpr size_t index_offset = seqan3::alphabet_size<alphabet_t> + 1; // scheme is extended by one.
78 static constexpr scalar_type score_for_padding_symbol = (is_global) ? 1 : -1;
79
81 std::vector<scalar_type> scoring_scheme_data{};
82
83public:
85 static constexpr scalar_type padding_symbol = static_cast<scalar_type>(seqan3::alphabet_size<alphabet_t>);
86
90 constexpr simd_matrix_scoring_scheme() = default;
96
98 template <typename scoring_scheme_t>
99 constexpr explicit simd_matrix_scoring_scheme(scoring_scheme_t const & scoring_scheme)
100 {
101 initialise_from_scalar_scoring_scheme(scoring_scheme);
102 }
103
105 template <typename scoring_scheme_t>
106 constexpr simd_matrix_scoring_scheme & operator=(scoring_scheme_t const & scoring_scheme)
107 {
108 initialise_from_scalar_scoring_scheme(scoring_scheme);
109 return *this;
110 }
112
137 constexpr simd_score_t score(simd_score_profile_type const & score_profile,
138 simd_alphabet_ranks_type const & ranks) const noexcept
139 {
140 simd_score_t const matrix_index = score_profile + ranks; // Compute the matrix indices for the lookup.
141 simd_score_t result{};
142
143 for (size_t idx = 0; idx < simd_traits<simd_score_t>::length; ++idx)
144 result[idx] = scoring_scheme_data.data()[matrix_index[idx]];
145
146 return result;
147 }
149
151 constexpr scalar_type padding_match_score() const noexcept
152 {
153 return score_for_padding_symbol;
154 }
155
167 {
168 return ranks * simd::fill<simd_score_t>(index_offset);
169 }
170
171private:
180 template <typename scoring_scheme_t>
182 constexpr void initialise_from_scalar_scoring_scheme(scoring_scheme_t const & scoring_scheme)
183 {
184 using score_t = decltype(std::declval<scoring_scheme_t const &>().score(alphabet_t{}, alphabet_t{}));
185
186 // Helper function to check if the matrix score is in the value range representable by the selected simd vector.
187 [[maybe_unused]] auto check_score_range = [&]([[maybe_unused]] score_t score)
188 {
189 // Note only if the size of the scalar type of the simd vector is smaller than the size of the score type
190 // of the original scoring scheme, the score might exceed the valid value range of the scalar type. In this
191 // case an exception will be thrown.
192 if constexpr (sizeof(scalar_type) < sizeof(score_t))
193 {
194 constexpr score_t max_score_value = static_cast<score_t>(std::numeric_limits<scalar_type>::max());
195 constexpr score_t min_score_value = static_cast<score_t>(std::numeric_limits<scalar_type>::lowest());
196
197 if (score > max_score_value || score < min_score_value)
198 throw std::invalid_argument{"The selected scoring scheme score overflows "
199 "for the selected scalar type of the simd type."};
200 }
201 };
202
203 // For the global alignment we extend the alphabet by one symbol to handle sequences with different size.
204 scoring_scheme_data.resize(index_offset * index_offset, score_for_padding_symbol);
205
206 // Convert the scoring matrix into a linear vector to allow gather operations later on.
207 using alphabet_size_t = std::remove_const_t<decltype(seqan3::alphabet_size<alphabet_t>)>;
208 auto data_it = scoring_scheme_data.begin();
209 for (alphabet_size_t lhs_rank = 0; lhs_rank < seqan3::alphabet_size<alphabet_t>; ++lhs_rank)
210 {
211 for (alphabet_size_t rhs_rank = 0; rhs_rank < seqan3::alphabet_size<alphabet_t>; ++rhs_rank, ++data_it)
212 {
213 score_t tmp_score = scoring_scheme.score(seqan3::assign_rank_to(lhs_rank, alphabet_t{}),
214 seqan3::assign_rank_to(rhs_rank, alphabet_t{}));
215
216 check_score_range(tmp_score);
217 *data_it = tmp_score;
218 }
219 ++data_it; // skip one for the padded symbol.
220 }
221 }
222};
223
224} // namespace seqan3::detail
Provides algorithms to modify seqan3::simd::simd_type.
Provides global and local alignment configurations.
Core alphabet concept and free function/type trait wrappers.
Adaptions of concepts from the Cereal library.
A vectorised scoring scheme to handle scoring matrices using gather strategy.
Definition simd_matrix_scoring_scheme.hpp:56
constexpr simd_matrix_scoring_scheme & operator=(scoring_scheme_t const &scoring_scheme)
Store the given scoring scheme matrix into a private member variable.
Definition simd_matrix_scoring_scheme.hpp:106
constexpr simd_score_t score(simd_score_profile_type const &score_profile, simd_alphabet_ranks_type const &ranks) const noexcept
Given two simd vectors, compute an element-wise score and return another simd vector.
Definition simd_matrix_scoring_scheme.hpp:137
simd_score_t simd_alphabet_ranks_type
The type of the simd vector representing the alphabet ranks of one sequence batch.
Definition simd_matrix_scoring_scheme.hpp:63
constexpr simd_matrix_scoring_scheme & operator=(simd_matrix_scoring_scheme const &)=default
Defaulted.
constexpr simd_matrix_scoring_scheme()=default
Defaulted.
constexpr simd_matrix_scoring_scheme(scoring_scheme_t const &scoring_scheme)
Store the given scoring scheme matrix into a private member variable.
Definition simd_matrix_scoring_scheme.hpp:99
constexpr scalar_type padding_match_score() const noexcept
Returns the score used when aligning a padding symbol.
Definition simd_matrix_scoring_scheme.hpp:151
constexpr void initialise_from_scalar_scoring_scheme(scoring_scheme_t const &scoring_scheme)
Store the given scoring scheme matrix into a private member variable.
Definition simd_matrix_scoring_scheme.hpp:182
constexpr simd_matrix_scoring_scheme(simd_matrix_scoring_scheme &&)=default
Defaulted.
constexpr simd_matrix_scoring_scheme & operator=(simd_matrix_scoring_scheme &&)=default
Defaulted.
constexpr simd_matrix_scoring_scheme(simd_matrix_scoring_scheme const &)=default
Defaulted.
simd_score_t simd_score_profile_type
The score profile type used for this scoring scheme, which is the same as the simd score type.
Definition simd_matrix_scoring_scheme.hpp:61
typename simd_traits< simd_score_t >::scalar_type scalar_type
The underlying scalar type of the simd vector.
Definition simd_matrix_scoring_scheme.hpp:59
constexpr simd_score_profile_type make_score_profile(simd_alphabet_ranks_type const &ranks) const noexcept
Converts the simd alphabet ranks into a score profile used for scoring it later with the alphabet ran...
Definition simd_matrix_scoring_scheme.hpp:166
constexpr auto assign_rank_to
Assign a rank to an alphabet object.
Definition alphabet/concept.hpp:290
A concept that requires that type be able to score two letters.
T lowest(T... args)
T max(T... args)
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides seqan3::scoring_scheme_for.
A representation of a location or offset within a two-dimensional matrix.
Definition matrix_coordinate.hpp:87
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.
Hide me