SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
scoring_scheme_base.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, 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 <range/v3/algorithm/copy.hpp>
17 
22 #include <seqan3/std/algorithm>
23 
24 #if SEQAN3_WITH_CEREAL
25 #include <cereal/types/array.hpp>
26 #endif // SEQAN3_WITH_CEREAL
27 
28 namespace seqan3
29 {
30 
31 // ------------------------------------------------------------------
32 // seqan3::match_score
33 // ------------------------------------------------------------------
34 
40 template <Arithmetic score_type>
41 struct match_score : detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>
42 {
43  using detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>::strong_type;
44 };
45 
51 template <Arithmetic score_type>
53 match_score(score_type &&) -> match_score<score_type>;
55 
56 // ------------------------------------------------------------------
57 // seqan3::mismatch_score
58 // ------------------------------------------------------------------
59 
65 template <Arithmetic score_type>
66 struct mismatch_score : detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>
67 {
68  using detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>::strong_type;
69 };
70 
76 template <Arithmetic score_type>
80 
81 // ------------------------------------------------------------------
82 // seqan3::scoring_scheme_base
83 // ------------------------------------------------------------------
84 
98 template <typename derived_t, Alphabet alphabet_t, Arithmetic score_t>
100 {
101 public:
105  using score_type = score_t;
110 
112  static constexpr matrix_size_type matrix_size = alphabet_size<alphabet_t>;
113 
120 
121 private:
123  friend derived_t;
124 
126 
129  constexpr scoring_scheme_base(scoring_scheme_base const &) noexcept = default;
130  constexpr scoring_scheme_base(scoring_scheme_base &&) noexcept = default;
131  constexpr scoring_scheme_base & operator=(scoring_scheme_base const &) noexcept = default;
132  constexpr scoring_scheme_base & operator=(scoring_scheme_base &&) noexcept = default;
133  ~scoring_scheme_base() noexcept = default;
134 
136  constexpr scoring_scheme_base() noexcept
137  {
139  }
140 
144  template <Arithmetic score_arg_t>
146  {
147  set_simple_scheme(ms, mms);
148  }
149 
153  constexpr scoring_scheme_base(matrix_type const & _matrix) noexcept
154  {
155  set_custom_matrix(_matrix);
156  }
158 
159 public:
163  constexpr void set_hamming_distance() noexcept
165  {
166  set_simple_scheme(match_score<score_t>{0}, mismatch_score<score_t>{-1});
167  }
168 
175  template <Arithmetic score_arg_t>
177  {
178  std::conditional_t<std::Integral<score_t>, int64_t, double> i_ms = static_cast<score_arg_t>(ms);
179  std::conditional_t<std::Integral<score_t>, int64_t, double> i_mms = static_cast<score_arg_t>(mms);
182  {
183  throw std::invalid_argument{"You passed a score value to set_simple_scheme that is out of range of the "
184  "scoring scheme's underlying type. Define your scoring scheme with a larger "
185  "template parameter or down-cast you score value beforehand to prevent "
186  "this exception."};
187  }
188 
189  for (matrix_size_type i = 0; i < matrix_size; ++i)
190  for (matrix_size_type j = 0; j < matrix_size; ++j)
191  matrix[i][j] = (i == j) ? static_cast<score_t>(i_ms) : static_cast<score_t>(i_mms);
192  }
193 
197  constexpr void set_custom_matrix(matrix_type const & _matrix) noexcept
198  {
199  std::ranges::copy(_matrix, begin(matrix));
200  }
202 
213  template <ExplicitlyConvertibleTo<alphabet_t> alph1_t,
214  ExplicitlyConvertibleTo<alphabet_t> alph2_t>
215  constexpr score_t & score(alph1_t const alph1, alph2_t const alph2) noexcept
216  {
217  return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
218  }
219 
221  template <ExplicitlyConvertibleTo<alphabet_t> alph1_t, ExplicitlyConvertibleTo<alphabet_t> 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 <CerealArchive archive_t>
252  void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
253  {
254  archive(matrix);
255  }
257 
258 private:
260  matrix_type matrix{};
261 };
262 
263 } // namespace seqan3
Provides basic data structure for strong types.
A CRTP base class for scoring schemes.
Definition: scoring_scheme_base.hpp:99
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:164
Provides various shortcuts for common std::ranges functions.
score_t score_type
Type of the score values.
Definition: scoring_scheme_base.hpp:106
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:103
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:145
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:215
static constexpr matrix_size_type matrix_size
Size of the matrix dimensions (i.e. size of the alphabet).
Definition: scoring_scheme_base.hpp:112
The main SeqAn3 namespace.
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:197
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
A strong type of underlying type score_type that represents the score of two matching characters...
Definition: scoring_scheme_base.hpp:41
A strong type of underlying type score_type that represents the score two different characters...
Definition: scoring_scheme_base.hpp:66
::ranges::begin begin
Alias for ranges::begin. Returns an iterator to the beginning of a range.
Definition: ranges:174
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:176
Adaptions of concepts from the Cereal library.
constexpr scoring_scheme_base(matrix_type const &_matrix) noexcept
Constructor for a custom scheme (delegates to set_custom_matrix()).
Definition: scoring_scheme_base.hpp:153
::ranges::copy copy
Alias for ranges::copy. Copies a range of elements to a new location.
Definition: algorithm:44
T max(T... args)
constexpr scoring_scheme_base() noexcept
The default constructor (delegates to set_hamming_distance()).
Definition: scoring_scheme_base.hpp:136
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:118
Adaptations of algorithms from the Ranges TS.
Core alphabet concept and free function/type trait wrappers.
constexpr bool operator==(derived_t const &rhs) const noexcept
Checks whether *this is equal to rhs.
Definition: scoring_scheme_base.hpp:232
constexpr bool operator!=(derived_t const &rhs) const noexcept
Checks whether *this is not equal to rhs.
Definition: scoring_scheme_base.hpp:238