Loading...
Searching...
No Matches
scoring_scheme_base.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <algorithm>
#include <seqan3/alphabet/concept.hpp>
#include <seqan3/core/concept/cereal.hpp>
#include <seqan3/core/detail/strong_type.hpp>
#include <seqan3/utility/concept.hpp>
#if SEQAN3_HAS_CEREAL
# include <cereal/types/array.hpp>
#endif // SEQAN3_HAS_CEREAL
namespace seqan3
{
// ------------------------------------------------------------------
// seqan3::match_score
// ------------------------------------------------------------------
template <arithmetic score_type>
struct match_score : detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>
{
using detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>::strong_type;
};
template <arithmetic score_type>
match_score(score_type) -> match_score<score_type>;
// ------------------------------------------------------------------
// seqan3::mismatch_score
// ------------------------------------------------------------------
template <arithmetic score_type>
struct mismatch_score : detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>
{
using detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>::strong_type;
};
template <arithmetic score_type>
mismatch_score(score_type) -> mismatch_score<score_type>;
// ------------------------------------------------------------------
// seqan3::scoring_scheme_base
// ------------------------------------------------------------------
template <typename derived_t, alphabet alphabet_t, arithmetic score_t>
class scoring_scheme_base
{
public:
using score_type = score_t;
using alphabet_type = alphabet_t;
using matrix_size_type = std::remove_const_t<decltype(alphabet_size<alphabet_t>)>;
static constexpr matrix_size_type matrix_size = alphabet_size<alphabet_t>;
using matrix_type = std::array<std::array<score_type, matrix_size>, matrix_size>;
private:
friend derived_t;
constexpr scoring_scheme_base(scoring_scheme_base const &) noexcept = default;
constexpr scoring_scheme_base(scoring_scheme_base &&) noexcept = default;
constexpr scoring_scheme_base & operator=(scoring_scheme_base const &) noexcept = default;
constexpr scoring_scheme_base & operator=(scoring_scheme_base &&) noexcept = default;
~scoring_scheme_base() noexcept = default;
constexpr scoring_scheme_base() noexcept
{
set_hamming_distance();
}
template <arithmetic score_arg_t>
constexpr scoring_scheme_base(match_score<score_arg_t> const ms, mismatch_score<score_arg_t> const mms)
{
set_simple_scheme(ms, mms);
}
constexpr scoring_scheme_base(matrix_type const & matrix) noexcept
{
set_custom_matrix(matrix);
}
public:
constexpr void set_hamming_distance() noexcept
{
set_simple_scheme(match_score<score_t>{0}, mismatch_score<score_t>{-1});
}
template <arithmetic score_arg_t>
constexpr void set_simple_scheme(match_score<score_arg_t> const ms, mismatch_score<score_arg_t> const mms)
{
std::conditional_t<std::integral<score_t>, int64_t, double> i_ms = static_cast<score_arg_t>(ms);
std::conditional_t<std::integral<score_t>, int64_t, double> i_mms = static_cast<score_arg_t>(mms);
if ((i_ms < std::numeric_limits<score_t>::lowest() || i_ms > std::numeric_limits<score_t>::max())
|| (i_mms < std::numeric_limits<score_t>::lowest() || i_mms > std::numeric_limits<score_t>::max()))
{
throw std::invalid_argument{"You passed a score value to set_simple_scheme that is out of range of the "
"scoring scheme's underlying type. Define your scoring scheme with a larger "
"template parameter or down-cast you score value beforehand to prevent "
"this exception."};
}
for (matrix_size_type i = 0; i < matrix_size; ++i)
for (matrix_size_type j = 0; j < matrix_size; ++j)
matrix[i][j] = (i == j) ? static_cast<score_t>(i_ms) : static_cast<score_t>(i_mms);
}
constexpr void set_custom_matrix(matrix_type const & matrix) noexcept
{
std::ranges::copy(matrix, this->matrix.begin());
}
template <typename alph1_t, typename alph2_t>
requires explicitly_convertible_to<alph1_t, alphabet_t> && explicitly_convertible_to<alph2_t, alphabet_t>
constexpr score_t & score(alph1_t const alph1, alph2_t const alph2) noexcept
{
return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
}
template <typename alph1_t, typename alph2_t>
requires explicitly_convertible_to<alph1_t, alphabet_t> && explicitly_convertible_to<alph2_t, alphabet_t>
constexpr score_t score(alph1_t const alph1, alph2_t const alph2) const noexcept
{
return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
}
constexpr bool operator==(derived_t const & rhs) const noexcept
{
return matrix == rhs.matrix;
}
constexpr bool operator!=(derived_t const & rhs) const noexcept
{
return matrix != rhs.matrix;
}
template <cereal_archive archive_t>
void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
{
archive(matrix);
}
private:
matrix_type matrix{};
};
} // namespace seqan3
algorithm
concept.hpp
Core alphabet concept and free function/type trait wrappers.
std::array
cereal.hpp
Adaptions of concepts from the Cereal library.
seqan3::scoring_scheme_base::set_simple_scheme
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
seqan3::scoring_scheme_base::set_hamming_distance
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
seqan3::scoring_scheme_base::scoring_scheme_base
constexpr scoring_scheme_base(scoring_scheme_base &&) noexcept=default
Defaulted.
seqan3::scoring_scheme_base::score
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
seqan3::scoring_scheme_base< aminoacid_scoring_scheme< score_type >, aa27, score_type >::matrix_size_type
std::remove_const_t< decltype(alphabet_size< alphabet_t >)> matrix_size_type
Definition scoring_scheme_base.hpp:107
seqan3::scoring_scheme_base< aminoacid_scoring_scheme< score_type >, aa27, score_type >::alphabet_type
alphabet_t alphabet_type
Definition scoring_scheme_base.hpp:105
seqan3::scoring_scheme_base::scoring_scheme_base
constexpr scoring_scheme_base(scoring_scheme_base const &) noexcept=default
Defaulted.
seqan3::scoring_scheme_base::operator!=
constexpr bool operator!=(derived_t const &rhs) const noexcept
Checks whether *this is not equal to rhs.
Definition scoring_scheme_base.hpp:238
seqan3::scoring_scheme_base::operator==
constexpr bool operator==(derived_t const &rhs) const noexcept
Checks whether *this is equal to rhs.
Definition scoring_scheme_base.hpp:232
seqan3::scoring_scheme_base< aminoacid_scoring_scheme< score_type >, aa27, score_type >::matrix_type
std::array< std::array< score_type, matrix_size >, matrix_size > matrix_type
Definition scoring_scheme_base.hpp:117
seqan3::scoring_scheme_base::scoring_scheme_base
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
seqan3::scoring_scheme_base::set_custom_matrix
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
seqan3::scoring_scheme_base::scoring_scheme_base
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
seqan3::scoring_scheme_base< aminoacid_scoring_scheme< score_type >, aa27, score_type >::score_type
score_t score_type
Definition scoring_scheme_base.hpp:103
seqan3::scoring_scheme_base::score
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
seqan3::scoring_scheme_base< aminoacid_scoring_scheme< score_type >, aa27, score_type >::matrix_size
static constexpr matrix_size_type matrix_size
Definition scoring_scheme_base.hpp:111
std::conditional_t
seqan3::alphabet_size
constexpr auto alphabet_size
A type trait that holds the size of a (semi-)alphabet.
Definition alphabet/concept.hpp:834
seqan3::to_rank
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition alphabet/concept.hpp:152
explicitly_convertible_to
Checks whether from can be explicitly converted to to.
std::invalid_argument
std::numeric_limits::lowest
T lowest(T... args)
std::numeric_limits::max
T max(T... args)
seqan3
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
std::remove_const_t
strong_type.hpp
Provides basic data structure for strong types.
seqan3::match_score
A strong type of underlying type score_type that represents the score of two matching characters.
Definition scoring_scheme_base.hpp:38
seqan3::match_score::match_score
match_score(score_type) -> match_score< score_type >
Deduce the score type from the provided argument.
seqan3::mismatch_score
A strong type of underlying type score_type that represents the score two different characters.
Definition scoring_scheme_base.hpp:63
seqan3::mismatch_score::mismatch_score
mismatch_score(score_type) -> mismatch_score< score_type >
Deduce the score type from the provided argument.
concept.hpp
Provides concepts that do not have equivalents in C++20.
Hide me