SeqAn3  3.0.1
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 <range/v3/algorithm/copy.hpp>
17 
23 #include <seqan3/std/algorithm>
24 
25 #if SEQAN3_WITH_CEREAL
26 #include <cereal/types/array.hpp>
27 #endif // SEQAN3_WITH_CEREAL
28 
29 namespace seqan3
30 {
31 
32 // ------------------------------------------------------------------
33 // seqan3::match_score
34 // ------------------------------------------------------------------
35 
41 template <arithmetic score_type>
42 struct match_score : detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>
43 {
44  using detail::strong_type<score_type, match_score<score_type>, detail::strong_type_skill::convert>::strong_type;
45 };
46 
52 template <arithmetic score_type>
56 
57 // ------------------------------------------------------------------
58 // seqan3::mismatch_score
59 // ------------------------------------------------------------------
60 
66 template <arithmetic score_type>
67 struct mismatch_score : detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>
68 {
69  using detail::strong_type<score_type, mismatch_score<score_type>, detail::strong_type_skill::convert>::strong_type;
70 };
71 
77 template <arithmetic score_type>
81 
82 // ------------------------------------------------------------------
83 // seqan3::scoring_scheme_base
84 // ------------------------------------------------------------------
85 
99 template <typename derived_t, alphabet alphabet_t, arithmetic score_t>
101 {
102 public:
106  using score_type = score_t;
109  using alphabet_type = alphabet_t;
113 
115  static constexpr matrix_size_type matrix_size = alphabet_size<alphabet_t>;
116 
123 
124 private:
126  friend derived_t;
127 
129 
132  constexpr scoring_scheme_base(scoring_scheme_base const &) noexcept = default;
133  constexpr scoring_scheme_base(scoring_scheme_base &&) noexcept = default;
134  constexpr scoring_scheme_base & operator=(scoring_scheme_base const &) noexcept = default;
135  constexpr scoring_scheme_base & operator=(scoring_scheme_base &&) noexcept = default;
136  ~scoring_scheme_base() noexcept = default;
137 
139  constexpr scoring_scheme_base() noexcept
140  {
141  set_hamming_distance();
142  }
143 
147  template <arithmetic score_arg_t>
149  {
150  set_simple_scheme(ms, mms);
151  }
152 
156  constexpr scoring_scheme_base(matrix_type const & matrix) noexcept
157  {
158  set_custom_matrix(matrix);
159  }
161 
162 public:
166  constexpr void set_hamming_distance() noexcept
168  {
169  set_simple_scheme(match_score<score_t>{0}, mismatch_score<score_t>{-1});
170  }
171 
178  template <arithmetic score_arg_t>
180  {
181  std::conditional_t<std::integral<score_t>, int64_t, double> i_ms = static_cast<score_arg_t>(ms);
182  std::conditional_t<std::integral<score_t>, int64_t, double> i_mms = static_cast<score_arg_t>(mms);
185  {
186  throw std::invalid_argument{"You passed a score value to set_simple_scheme that is out of range of the "
187  "scoring scheme's underlying type. Define your scoring scheme with a larger "
188  "template parameter or down-cast you score value beforehand to prevent "
189  "this exception."};
190  }
191 
192  for (matrix_size_type i = 0; i < matrix_size; ++i)
193  for (matrix_size_type j = 0; j < matrix_size; ++j)
194  matrix[i][j] = (i == j) ? static_cast<score_t>(i_ms) : static_cast<score_t>(i_mms);
195  }
196 
200  constexpr void set_custom_matrix(matrix_type const & matrix) noexcept
201  {
202  std::ranges::copy(matrix, this->matrix.begin());
203  }
205 
216  template <typename alph1_t, typename alph2_t>
220  constexpr score_t & score(alph1_t const alph1, alph2_t const alph2) noexcept
221  {
222  return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
223  }
224 
226  template <typename alph1_t, typename alph2_t>
230  constexpr score_t score(alph1_t const alph1, alph2_t const alph2) const noexcept
231  {
232  return matrix[to_rank(static_cast<alphabet_t>(alph1))][to_rank(static_cast<alphabet_t>(alph2))];
233  }
235 
238 
240  constexpr bool operator==(derived_t const & rhs) const noexcept
241  {
242  return matrix == rhs.matrix;
243  }
244 
246  constexpr bool operator!=(derived_t const & rhs) const noexcept
247  {
248  return matrix != rhs.matrix;
249  }
251 
259  template <cereal_archive archive_t>
260  void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
261  {
262  archive(matrix);
263  }
265 
266 private:
268  matrix_type matrix{};
269 };
270 
271 } // namespace seqan3
Provides basic data structure for strong types.
Provides concepts for core language types and relations that don&#39;t have concepts in C++20 (yet)...
A CRTP base class for scoring schemes.
Definition: scoring_scheme_base.hpp:100
Provides various shortcuts for common std::ranges functions.
score_t score_type
Type of the score values.
Definition: scoring_scheme_base.hpp:107
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:142
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:148
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:220
The main SeqAn3 namespace.
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:230
constexpr scoring_scheme_base(matrix_type const &matrix) noexcept
Constructor for a custom scheme (delegates to set_custom_matrix()).
Definition: scoring_scheme_base.hpp:156
A strong type of underlying type score_type that represents the score of two matching characters...
Definition: scoring_scheme_base.hpp:42
A strong type of underlying type score_type that represents the score two different characters...
Definition: scoring_scheme_base.hpp:67
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:179
Adaptions of concepts from the Cereal library.
T max(T... args)
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:200
Adaptations of algorithms from the Ranges TS.
Core alphabet concept and free function/type trait wrappers.
alphabet_t alphabet_type
Type of the underlying alphabet.
Definition: scoring_scheme_base.hpp:109
constexpr bool operator==(derived_t const &rhs) const noexcept
Checks whether *this is equal to rhs.
Definition: scoring_scheme_base.hpp:240
match_score(score_type) -> match_score< score_type >
Deduce the score type from the provided argument.
constexpr bool operator!=(derived_t const &rhs) const noexcept
Checks whether *this is not equal to rhs.
Definition: scoring_scheme_base.hpp:246
Resolves to std::ranges::explicitly_convertible_to<type1, type2>().