SeqAn3
gap_scheme.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forge, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
45 #include <seqan3/std/concepts>
46 
47 namespace seqan3
48 {
49 
50 // ------------------------------------------------------------------
51 // seqan3::gap_score
52 // ------------------------------------------------------------------
53 
60 template <arithmetic_concept score_type>
61 struct gap_score : detail::strong_type<score_type, gap_score<score_type>, detail::strong_type_skill::convert>
62 {
63  using detail::strong_type<score_type, gap_score<score_type>, detail::strong_type_skill::convert>::strong_type;
64 };
65 
70 template <arithmetic_concept score_type>
71 gap_score(score_type &&) -> gap_score<score_type>;
73 
74 // ------------------------------------------------------------------
75 // seqan3::gap_open_score
76 // ------------------------------------------------------------------
77 
84 template <arithmetic_concept score_type>
85 struct gap_open_score : detail::strong_type<score_type, gap_open_score<score_type>, detail::strong_type_skill::convert>
86 {
87  using detail::strong_type<score_type, gap_open_score<score_type>, detail::strong_type_skill::convert>::strong_type;
88 };
89 
94 template <arithmetic_concept score_type>
97 
98 // ------------------------------------------------------------------
99 // seqan3::gap_scheme
100 // ------------------------------------------------------------------
101 
106 template <arithmetic_concept score_t = int8_t>
108 {
109 public:
110 
114  using score_type = score_t;
117 
121  constexpr gap_scheme() noexcept = default;
122  constexpr gap_scheme(gap_scheme const &) noexcept = default;
123  constexpr gap_scheme(gap_scheme &&) noexcept = default;
124  constexpr gap_scheme & operator=(gap_scheme const &) noexcept = default;
125  constexpr gap_scheme & operator=(gap_scheme &&) noexcept = default;
126  ~gap_scheme() noexcept = default;
127 
131  template <arithmetic_concept score_arg_t>
133  {
134  set_affine(g, go);
135  }
136 
140  template <arithmetic_concept score_arg_t>
142  {
143  set_linear(g);
144  }
146 
164  template <arithmetic_concept score_arg_t>
166  {
167  std::conditional_t<std::Integral<score_t>, int64_t, double> i_g = static_cast<score_arg_t>(g);
168  std::conditional_t<std::Integral<score_t>, int64_t, double> i_go = static_cast<score_arg_t>(go);
169  if ((i_g < std::numeric_limits<score_t>::lowest() || i_g > std::numeric_limits<score_t>::max()) ||
170  (i_go < std::numeric_limits<score_t>::lowest() || i_go > std::numeric_limits<score_t>::max()))
171  {
172  throw std::invalid_argument{"You passed a score value to set_affine/set_linear that is out of range of the "
173  "scoring scheme's underlying type. Define your scoring scheme with a larger "
174  "template parameter or down-cast your score value beforehand to prevent "
175  "this exception."};
176  }
177 
178  gap = static_cast<score_arg_t>(g);
179  gap_open = static_cast<score_arg_t>(go);
180  }
181 
192  template <arithmetic_concept score_arg_t>
193  constexpr void set_linear(gap_score<score_arg_t> const g)
194  {
195  set_affine(g, gap_open_score<score_arg_t>{0});
196  }
198 
204  constexpr score_t & get_gap_score() noexcept
205  {
206  return gap;
207  }
208 
210  constexpr score_t get_gap_score() const noexcept
211  {
212  return gap;
213  }
214 
217  constexpr score_t & get_gap_open_score() noexcept
218  {
219  return gap_open;
220  }
221 
223  constexpr score_t get_gap_open_score() const noexcept
224  {
225  return gap_open;
226  }
227 
232  constexpr ptrdiff_t score(size_t const number_of_consecutive_gaps) noexcept
233  {
234  return (gap_open * (number_of_consecutive_gaps ? 1 : 0)) + number_of_consecutive_gaps * gap;
235  }
237 
240  constexpr bool operator==(gap_scheme const & rhs) const noexcept
241  {
242  return std::tie(gap, gap_open) == std::tie(rhs.gap, rhs.gap_open);
243  }
244 
245  constexpr bool operator!=(gap_scheme const & rhs) const noexcept
246  {
247  return !(*this == rhs);
248  }
250 
258  template <cereal_archive_concept archive_t>
259  void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
260  {
261  archive(gap);
262  archive(gap_open);
263  }
265 
266 private:
268  score_t gap = 0;
270  score_t gap_open = 0;
271 };
272 
278 
283 template <floating_point_concept score_arg_type>
285 
290 template <floating_point_concept score_arg_type>
292 
297 template <arithmetic_concept score_arg_type>
299 
304 template <arithmetic_concept score_arg_type>
307 
308 } // namespace seqan3
Contains 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 strong type of underlying type score_type that represents an additional score (usually negative) th...
Definition: gap_scheme.hpp:85
A strong type of underlying type score_type that represents the score of any character against a gap ...
Definition: gap_scheme.hpp:61
The alphabet of a gap character &#39;-&#39;.
Definition: gap.hpp:62
A scheme for representing and computing scores against gap characters.
Definition: gap_scheme.hpp:107
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
constexpr void set_affine(gap_score< score_arg_t > const g, gap_open_score< score_arg_t > const go)
Set the Affine gap costs model.
Definition: gap_scheme.hpp:165
The Concepts library.
score_t score_type
The template parameter exposed as member type.
Definition: gap_scheme.hpp:115
constexpr void set_linear(gap_score< score_arg_t > const g)
Set the Linear gap costs model.
Definition: gap_scheme.hpp:193
Adaptions of concepts from the Cereal library.
constexpr gap_scheme(gap_score< score_arg_t > const g, gap_open_score< score_arg_t > const go)
Constructor for the Affine gap costs model (delegates to set_affine()).
Definition: gap_scheme.hpp:132
constexpr ptrdiff_t score(size_t const number_of_consecutive_gaps) noexcept
Compute the score of a stretch of gap characters.
Definition: gap_scheme.hpp:232
constexpr score_t & get_gap_open_score() noexcept
Return the gap open score.
Definition: gap_scheme.hpp:217
constexpr score_t & get_gap_score() noexcept
Return the gap score.
Definition: gap_scheme.hpp:204
constexpr score_t get_gap_score() const noexcept
A strong type of underlying type score_type that represents the score of any character against a gap ...
Definition: gap_scheme.hpp:210
constexpr gap_scheme(gap_score< score_arg_t > const g)
Constructor for the Linear gap costs model (delegates to set_linear()).
Definition: gap_scheme.hpp:141
constexpr score_t get_gap_open_score() const noexcept
A strong type of underlying type score_type that represents an additional score (usually negative) th...
Definition: gap_scheme.hpp:223