SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
quality_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 
13 #pragma once
14 
16 #include <seqan3/alphabet/detail/convert.hpp>
18 
19 namespace seqan3
20 {
21 
27 template <typename derived_type, auto size>
28 class quality_base : public alphabet_base<derived_type, size, char>
29 {
30 public:
34  using phred_type = int8_t;
37 
38 private:
41 
45  constexpr quality_base() noexcept = default;
46  constexpr quality_base(quality_base const &) noexcept = default;
47  constexpr quality_base(quality_base &&) noexcept = default;
48  constexpr quality_base & operator=(quality_base const &) noexcept = default;
49  constexpr quality_base & operator=(quality_base &&) noexcept = default;
50  ~quality_base() noexcept = default;
51 
53  constexpr quality_base(phred_type const p) noexcept
54  {
55  static_cast<derived_type *>(this)->assign_phred(p);
56  }
58 
60  friend derived_type;
61 
62 public:
63  // Import from base type:
65  using base_t::to_rank;
66  using base_t::assign_rank;
67  using typename base_t::char_type;
68  using typename base_t::rank_type;
69 
73  // This constructor needs to be public, because constructor templates are not inherited otherwise
75  template <typename other_qual_type>
81  explicit constexpr quality_base(other_qual_type const & other) noexcept
82  {
83  assign_phred_to(seqan3::to_phred(other), static_cast<derived_type &>(*this));
84  }
86 
90  constexpr char_type to_char() const noexcept
92  {
93  return static_cast<char_type>(to_rank()) + derived_type::offset_char;
94  }
95 
97  constexpr phred_type to_phred() const noexcept
98  {
99  return static_cast<phred_type>(to_rank()) + derived_type::offset_phred;
100  }
102 
117  constexpr derived_type & assign_phred(phred_type const p) noexcept
118  {
119  return assign_rank(phred_to_rank[static_cast<rank_type>(p)]);
120  }
122 
123 protected:
126  {
127  [] () constexpr
128  {
130 
131  for (int64_t i = std::numeric_limits<phred_type>::lowest(); i <= std::numeric_limits<phred_type>::max(); ++i)
132  {
133  if (i < derived_type::offset_phred) // map too-small to smallest possible
134  ret[static_cast<rank_type>(i)] = 0;
135  else if (i >= derived_type::offset_phred + alphabet_size) // map too-large to highest possible
136  ret[static_cast<rank_type>(i)] = alphabet_size - 1;
137  else // map valid range to identity
138  ret[static_cast<rank_type>(i)] = i - derived_type::offset_phred;
139  }
140  return ret;
141  }()
142  };
143 
146  {
147  [] () constexpr
148  {
150 
151  for (int64_t i = std::numeric_limits<char_type>::lowest(); i <= std::numeric_limits<char_type>::max(); ++i)
152  {
153  if (i < derived_type::offset_char) // map too-small to smallest possible
154  ret[static_cast<rank_type>(i)] = 0;
155  else if (i >= derived_type::offset_char + alphabet_size) // map too-large to highest possible
156  ret[static_cast<rank_type>(i)] = alphabet_size - 1;
157  else // map valid range to identity
158  ret[static_cast<rank_type>(i)] = i - derived_type::offset_char;
159  }
160 
161  return ret;
162  }()
163  };
164 };
165 
166 } // namespace seqan3
static std::array< rank_type, 256 > constexpr phred_to_rank
Phred to rank conversion table.
Definition: quality_base.hpp:126
detail::min_viable_uint_t< size - 1 > rank_type
The type of the alphabet when represented as a number (e.g. via to_rank()).
Definition: alphabet_base.hpp:63
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: quality_base.hpp:91
A concept that indicates whether an alphabet represents quality scores.
constexpr derived_type & assign_phred(phred_type const p) noexcept
Assign from the numeric phred value.
Definition: quality_base.hpp:117
int8_t phred_type
The integer representation of a quality score assignable with =operator.
Definition: quality_base.hpp:35
The main SeqAn3 namespace.
static std::array< rank_type, 256 > constexpr char_to_rank
Char to rank conversion table.
Definition: quality_base.hpp:146
A CRTP-base that refines seqan3::alphabet_base and is used by the quality alphabets.
Definition: quality_base.hpp:28
constexpr phred_type to_phred() const noexcept
Return the alphabet&#39;s value in phred representation.
Definition: quality_base.hpp:97
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:166
constexpr auto assign_phred_to
Assign a phred score to a quality alphabet object.
Definition: concept.hpp:201
constexpr quality_base(other_qual_type const &other) noexcept
Allow explicit construction from any other quality type by means of the phred representation.
Definition: quality_base.hpp:81
Provides seqan3::alphabet_base.
constexpr auto to_phred
The public getter function for the phred representation of a quality score.
Definition: concept.hpp:104
static detail::min_viable_uint_t< size > constexpr alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: alphabet_base.hpp:175
constexpr rank_type to_rank() const noexcept
Return the letter&#39;s numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:117
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:52
Quality alphabet concept.
The concept std::Same<T, U> is satisfied if and only if T and U denote the same type.
std::conditional_t< std::Same< char, void >, char, char > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: alphabet_base.hpp:61