SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
scoring_scheme_base.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
11#pragma once
12
13#include <algorithm>
14
19
20#if SEQAN3_WITH_CEREAL
21# include <cereal/types/array.hpp>
22#endif // SEQAN3_WITH_CEREAL
23
24namespace seqan3
25{
26
27// ------------------------------------------------------------------
28// seqan3::match_score
29// ------------------------------------------------------------------
30
36template <arithmetic score_type>
37struct match_score : detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>
38{
39 using detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>::strong_type;
40};
41
48template <arithmetic score_type>
51
52// ------------------------------------------------------------------
53// seqan3::mismatch_score
54// ------------------------------------------------------------------
55
61template <arithmetic score_type>
62struct mismatch_score : detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>
63{
64 using detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>::strong_type;
65};
66
73template <arithmetic score_type>
76
77// ------------------------------------------------------------------
78// seqan3::scoring_scheme_base
79// ------------------------------------------------------------------
80
95template <typename derived_t, alphabet alphabet_t, arithmetic score_t>
97{
98public:
103 using score_type = score_t;
105 using alphabet_type = alphabet_t;
107 using matrix_size_type = std::remove_const_t<decltype(alphabet_size<alphabet_t>)>;
109
111 static constexpr matrix_size_type matrix_size = alphabet_size<alphabet_t>;
112
119
120private:
122 friend derived_t;
123
125
128 constexpr scoring_scheme_base(scoring_scheme_base const &) noexcept = default;
129 constexpr scoring_scheme_base(scoring_scheme_base &&) noexcept = default;
130 constexpr scoring_scheme_base & operator=(scoring_scheme_base const &) noexcept = default;
131 constexpr scoring_scheme_base & operator=(scoring_scheme_base &&) noexcept = default;
132 ~scoring_scheme_base() noexcept = default;
133
135 constexpr scoring_scheme_base() noexcept
136 {
138 }
139
143 template <arithmetic score_arg_t>
145 {
146 set_simple_scheme(ms, mms);
147 }
148
152 constexpr scoring_scheme_base(matrix_type const & matrix) noexcept
153 {
154 set_custom_matrix(matrix);
155 }
157
158public:
163 constexpr void set_hamming_distance() noexcept
164 {
166 }
167
174 template <arithmetic score_arg_t>
176 {
177 std::conditional_t<std::integral<score_t>, int64_t, double> i_ms = static_cast<score_arg_t>(ms);
178 std::conditional_t<std::integral<score_t>, int64_t, double> i_mms = static_cast<score_arg_t>(mms);
181 {
182 throw std::invalid_argument{"You passed a score value to set_simple_scheme that is out of range of the "
183 "scoring scheme's underlying type. Define your scoring scheme with a larger "
184 "template parameter or down-cast you score value beforehand to prevent "
185 "this exception."};
186 }
187
188 for (matrix_size_type i = 0; i < matrix_size; ++i)
189 for (matrix_size_type j = 0; j < matrix_size; ++j)
190 matrix[i][j] = (i == j) ? static_cast<score_t>(i_ms) : static_cast<score_t>(i_mms);
191 }
192
196 constexpr void set_custom_matrix(matrix_type const & matrix) noexcept
197 {
198 std::ranges::copy(matrix, this->matrix.begin());
199 }
201
212 template <typename alph1_t, typename alph2_t>
214 constexpr score_t & score(alph1_t const alph1, alph2_t const alph2) noexcept
215 {
216 return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
217 }
218
220 template <typename alph1_t, typename alph2_t>
222 constexpr score_t score(alph1_t const alph1, alph2_t const alph2) const noexcept
223 {
224 return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
225 }
227
230
232 constexpr bool operator==(derived_t const & rhs) const noexcept
233 {
234 return matrix == rhs.matrix;
235 }
236
238 constexpr bool operator!=(derived_t const & rhs) const noexcept
239 {
240 return matrix != rhs.matrix;
241 }
243
251 template <cereal_archive archive_t>
252 void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
253 {
254 archive(matrix);
255 }
257
258private:
260 matrix_type matrix{};
261};
262
263} // namespace seqan3
Core alphabet concept and free function/type trait wrappers.
T begin(T... args)
Adaptions of concepts from the Cereal library.
A CRTP base class for scoring schemes.
Definition scoring_scheme_base.hpp:97
constexpr void set_simple_scheme(match_score< score_arg_t > const ms, mismatch_score< score_arg_t > const mms)
Set the simple scheme (everything is either match or mismatch).
Definition scoring_scheme_base.hpp:175
constexpr void set_hamming_distance() noexcept
Set the hamming scheme, a variant of the simple scheme where match is scored 0 and mismatch -1.
Definition scoring_scheme_base.hpp:163
constexpr scoring_scheme_base(scoring_scheme_base &&) noexcept=default
Defaulted.
constexpr score_t score(alph1_t const alph1, alph2_t const alph2) const noexcept
Score two letters (either two nucleotids or two amino acids).
Definition scoring_scheme_base.hpp:222
alphabet_t alphabet_type
Type of the underlying alphabet.
Definition scoring_scheme_base.hpp:105
constexpr scoring_scheme_base(scoring_scheme_base const &) noexcept=default
Defaulted.
constexpr bool operator!=(derived_t const &rhs) const noexcept
Checks whether *this is not equal to rhs.
Definition scoring_scheme_base.hpp:238
constexpr bool operator==(derived_t const &rhs) const noexcept
Checks whether *this is equal to rhs.
Definition scoring_scheme_base.hpp:232
std::array< std::array< score_type, matrix_size >, matrix_size > matrix_type
Type of the internal matrix (a two-dimensional array).
Definition scoring_scheme_base.hpp:117
constexpr scoring_scheme_base(matrix_type const &matrix) noexcept
Constructor for a custom scheme (delegates to set_custom_matrix()).
Definition scoring_scheme_base.hpp:152
constexpr void set_custom_matrix(matrix_type const &matrix) noexcept
Set a custom scheme by passing a full matrix with arbitrary content.
Definition scoring_scheme_base.hpp:196
constexpr scoring_scheme_base(match_score< score_arg_t > const ms, mismatch_score< score_arg_t > const mms)
Constructor for the simple scheme (delegates to set_simple_scheme()).
Definition scoring_scheme_base.hpp:144
score_t score_type
Type of the score values.
Definition scoring_scheme_base.hpp:103
constexpr score_t & score(alph1_t const alph1, alph2_t const alph2) noexcept
Score two letters (either two nucleotids or two amino acids).
Definition scoring_scheme_base.hpp:214
static constexpr matrix_size_type matrix_size
Size of the matrix dimensions (i.e. size of the alphabet).
Definition scoring_scheme_base.hpp:111
T copy(T... args)
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition alphabet/concept.hpp:152
Checks whether from can be explicitly converted to to.
T max(T... args)
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides basic data structure for strong types.
A strong type of underlying type score_type that represents the score of two matching characters.
Definition scoring_scheme_base.hpp:38
match_score(score_type) -> match_score< score_type >
Deduce the score type from the provided argument.
A strong type of underlying type score_type that represents the score two different characters.
Definition scoring_scheme_base.hpp:63
mismatch_score(score_type) -> mismatch_score< score_type >
Deduce the score type from the provided argument.
Provides concepts that do not have equivalents in C++20.
Hide me