SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
scoring_scheme_base.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
14 #pragma once
15 
16 #include <seqan3/std/algorithm>
17 
22 
23 #if SEQAN3_WITH_CEREAL
24 #include <cereal/types/array.hpp>
25 #endif // SEQAN3_WITH_CEREAL
26 
27 namespace seqan3
28 {
29 
30 // ------------------------------------------------------------------
31 // seqan3::match_score
32 // ------------------------------------------------------------------
33 
39 template <arithmetic score_type>
40 struct match_score : detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>
41 {
42  using detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>::strong_type;
43 };
44 
50 template <arithmetic score_type>
54 
55 // ------------------------------------------------------------------
56 // seqan3::mismatch_score
57 // ------------------------------------------------------------------
58 
64 template <arithmetic score_type>
65 struct mismatch_score : detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>
66 {
67  using detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>::strong_type;
68 };
69 
75 template <arithmetic score_type>
79 
80 // ------------------------------------------------------------------
81 // seqan3::scoring_scheme_base
82 // ------------------------------------------------------------------
83 
98 template <typename derived_t, alphabet alphabet_t, arithmetic score_t>
100 {
101 public:
105  using score_type = score_t;
108  using alphabet_type = alphabet_t;
110  using matrix_size_type = std::remove_const_t<decltype(alphabet_size<alphabet_t>)>;
112 
114  static constexpr matrix_size_type matrix_size = alphabet_size<alphabet_t>;
115 
122 
123 private:
125  friend derived_t;
126 
128 
131  constexpr scoring_scheme_base(scoring_scheme_base const &) noexcept = default;
132  constexpr scoring_scheme_base(scoring_scheme_base &&) noexcept = default;
133  constexpr scoring_scheme_base & operator=(scoring_scheme_base const &) noexcept = default;
134  constexpr scoring_scheme_base & operator=(scoring_scheme_base &&) noexcept = default;
135  ~scoring_scheme_base() noexcept = default;
136 
138  constexpr scoring_scheme_base() noexcept
139  {
141  }
142 
146  template <arithmetic score_arg_t>
148  {
149  set_simple_scheme(ms, mms);
150  }
151 
155  constexpr scoring_scheme_base(matrix_type const & matrix) noexcept
156  {
157  set_custom_matrix(matrix);
158  }
160 
161 public:
165  constexpr void set_hamming_distance() noexcept
167  {
168  set_simple_scheme(match_score<score_t>{0}, mismatch_score<score_t>{-1});
169  }
170 
177  template <arithmetic score_arg_t>
179  {
180  std::conditional_t<std::integral<score_t>, int64_t, double> i_ms = static_cast<score_arg_t>(ms);
181  std::conditional_t<std::integral<score_t>, int64_t, double> i_mms = static_cast<score_arg_t>(mms);
184  {
185  throw std::invalid_argument{"You passed a score value to set_simple_scheme that is out of range of the "
186  "scoring scheme's underlying type. Define your scoring scheme with a larger "
187  "template parameter or down-cast you score value beforehand to prevent "
188  "this exception."};
189  }
190 
191  for (matrix_size_type i = 0; i < matrix_size; ++i)
192  for (matrix_size_type j = 0; j < matrix_size; ++j)
193  matrix[i][j] = (i == j) ? static_cast<score_t>(i_ms) : static_cast<score_t>(i_mms);
194  }
195 
199  constexpr void set_custom_matrix(matrix_type const & matrix) noexcept
200  {
201  std::ranges::copy(matrix, this->matrix.begin());
202  }
204 
215  template <typename alph1_t, typename alph2_t>
219  constexpr score_t & score(alph1_t const alph1, alph2_t const alph2) noexcept
220  {
221  return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
222  }
223 
225  template <typename alph1_t, typename alph2_t>
229  constexpr score_t score(alph1_t const alph1, alph2_t const alph2) const noexcept
230  {
231  return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
232  }
234 
237 
239  constexpr bool operator==(derived_t const & rhs) const noexcept
240  {
241  return matrix == rhs.matrix;
242  }
243 
245  constexpr bool operator!=(derived_t const & rhs) const noexcept
246  {
247  return matrix != rhs.matrix;
248  }
250 
258  template <cereal_archive archive_t>
259  void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
260  {
261  archive(matrix);
262  }
264 
265 private:
267  matrix_type matrix{};
268 };
269 
270 } // namespace seqan3
seqan3::scoring_scheme_base::matrix_size
static constexpr matrix_size_type matrix_size
Size of the matrix dimensions (i.e. size of the alphabet).
Definition: scoring_scheme_base.hpp:114
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:245
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:166
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:229
seqan3::scoring_scheme_base::scoring_scheme_base
constexpr scoring_scheme_base(scoring_scheme_base const &) noexcept=default
Defaulted.
seqan3::scoring_scheme_base
A CRTP base class for scoring schemes.
Definition: scoring_scheme_base.hpp:100
explicitly_convertible_to
Resolves to std::ranges::explicitly_convertible_to<type1, type2>().
strong_type.hpp
Provides basic data structure for strong types.
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:199
seqan3::to_rank
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:143
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:219
algorithm
Adaptations of algorithms from the Ranges TS.
seqan3::mismatch_score::mismatch_score
mismatch_score(score_type) -> mismatch_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:66
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:147
core_language.hpp
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
seqan3::scoring_scheme_base::score_type
score_t score_type
Type of the score values.
Definition: scoring_scheme_base.hpp:106
std::array< std::array< score_type, matrix_size >, matrix_size >
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
std::invalid_argument
seqan3::scoring_scheme_base< nucleotide_scoring_scheme< int8_t >, dna15, int8_t >::alphabet_type
alphabet_t alphabet_type
Type of the underlying alphabet.
Definition: scoring_scheme_base.hpp:108
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:155
seqan3::scoring_scheme_base::scoring_scheme_base
constexpr scoring_scheme_base(scoring_scheme_base &&) noexcept=default
Defaulted.
std::array::begin
T begin(T... args)
std::remove_const_t
cereal.hpp
Adaptions of concepts from the Cereal library.
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:239
std::conditional_t
std::numeric_limits::max
T max(T... args)
seqan3::scoring_scheme_base::matrix_type
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:120
seqan3::match_score::match_score
match_score(score_type) -> match_score< score_type >
Deduce the score type from the provided argument.
seqan3::match_score
A strong type of underlying type score_type that represents the score of two matching characters.
Definition: scoring_scheme_base.hpp:41
seqan3::views::convert
auto const convert
A view that converts each element in the input range (implicitly or via static_cast).
Definition: convert.hpp:71
std::numeric_limits
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:178
concept.hpp
Core alphabet concept and free function/type trait wrappers.