SeqAn3  3.0.1
The Modern C++ library for sequence analysis.
quality_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 
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 phred_type to_phred() const noexcept
92  {
93  return rank_to_phred[to_rank()];
94  }
96 
111  constexpr derived_type & assign_phred(phred_type const p) noexcept
112  {
113  return assign_rank(phred_to_rank[static_cast<rank_type>(p)]);
114  }
116 
117 protected:
120  {
121  [] () constexpr
122  {
124 
125  for (int64_t i = std::numeric_limits<phred_type>::lowest(); i <= std::numeric_limits<phred_type>::max(); ++i)
126  {
127  if (i < derived_type::offset_phred) // map too-small to smallest possible
128  ret[static_cast<rank_type>(i)] = 0;
129  else if (i >= derived_type::offset_phred + alphabet_size) // map too-large to highest possible
130  ret[static_cast<rank_type>(i)] = alphabet_size - 1;
131  else // map valid range to identity
132  ret[static_cast<rank_type>(i)] = i - derived_type::offset_phred;
133  }
134  return ret;
135  }()
136  };
137 
140  {
141  [] () constexpr
142  {
144 
145  for (int64_t i = std::numeric_limits<char_type>::lowest(); i <= std::numeric_limits<char_type>::max(); ++i)
146  {
147  if (i < derived_type::offset_char) // map too-small to smallest possible
148  ret[static_cast<rank_type>(i)] = 0;
149  else if (i >= derived_type::offset_char + alphabet_size) // map too-large to highest possible
150  ret[static_cast<rank_type>(i)] = alphabet_size - 1;
151  else // map valid range to identity
152  ret[static_cast<rank_type>(i)] = i - derived_type::offset_char;
153  }
154 
155  return ret;
156  }()
157  };
158 
161  {
162  [] () constexpr
163  {
165 
166  for (size_t i = 0; i < alphabet_size; ++i)
167  ret[i] = i + derived_type::offset_phred;
168 
169  return ret;
170  }()
171  };
172 
175  {
176  [] () constexpr
177  {
179 
180  for (size_t i = 0; i < alphabet_size; ++i)
181  ret[i] = i + derived_type::offset_char;
182 
183  return ret;
184  }()
185  };
186 };
187 
188 } // namespace seqan3
seqan3::quality_base::assign_phred
constexpr derived_type & assign_phred(phred_type const p) noexcept
Assign from the numeric phred value.
Definition: quality_base.hpp:111
alphabet_base.hpp
Provides seqan3::alphabet_base.
seqan3::quality_base::rank_to_phred
static constexpr std::array< phred_type, alphabet_size > rank_to_phred
Rank to phred conversion table.
Definition: quality_base.hpp:161
seqan3::quality_base::rank_to_char
static constexpr std::array< char_type, alphabet_size > rank_to_char
Rank to char conversion table.
Definition: quality_base.hpp:175
seqan3::alphabet_base< derived_type, size, char >::alphabet_size
static constexpr detail::min_viable_uint_t< size > alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: alphabet_base.hpp:174
seqan3::quality_base::phred_to_rank
static constexpr std::array< rank_type, 256 > phred_to_rank
Phred to rank conversion table.
Definition: quality_base.hpp:120
quality_alphabet
A concept that indicates whether an alphabet represents quality scores.
seqan3::alphabet_base
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:51
seqan3::quality_base::phred_type
int8_t phred_type
The integer representation of a quality score assignable with =operator.
Definition: quality_base.hpp:35
same_as
The concept std::same_as<T, U> is satisfied if and only if T and U denote the same type.
concept.hpp
Quality alphabet concept.
seqan3::quality_base::to_phred
constexpr phred_type to_phred() const noexcept
Return the alphabet's value in phred representation.
Definition: quality_base.hpp:91
seqan3::quality_base::quality_base
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
seqan3::alphabet_base< derived_type, size, char >::rank_type
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:62
std::array< rank_type, 256 >
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:36
seqan3::quality_base::char_to_rank
static constexpr std::array< rank_type, 256 > char_to_rank
Char to rank conversion table.
Definition: quality_base.hpp:140
seqan3::to_phred
constexpr auto to_phred
The public getter function for the phred representation of a quality score.
Definition: concept.hpp:89
seqan3::alphabet_base< derived_type, size, char >::char_type
std::conditional_t< std::same_as< char, void >, char, char > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: alphabet_base.hpp:60
seqan3::assign_phred_to
constexpr auto assign_phred_to
Assign a phred score to a quality alphabet object.
Definition: concept.hpp:189
seqan3::quality_base
A CRTP-base that refines seqan3::alphabet_base and is used by the quality alphabets.
Definition: quality_base.hpp:28
seqan3::alphabet_base< derived_type, size, char >::assign_rank
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:165
seqan3::alphabet_base< derived_type, size, char >::to_rank
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:116
std::numeric_limits