BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
quality_base.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
16#include <bio/alphabet/base.hpp>
17#include <bio/alphabet/detail/convert.hpp>
19
20namespace bio::alphabet
21{
22
28template <typename derived_type, auto size>
29class quality_base : public base<derived_type, size, char>
30{
31public:
36 using phred_type = int8_t;
38
39private:
42
46 constexpr quality_base() noexcept = default;
47 constexpr quality_base(quality_base const &) noexcept = default;
48 constexpr quality_base(quality_base &&) noexcept = default;
49 constexpr quality_base & operator=(quality_base const &) noexcept = default;
50 constexpr quality_base & operator=(quality_base &&) noexcept = default;
51 ~quality_base() noexcept = default;
52
54 constexpr quality_base(phred_type const p) noexcept { static_cast<derived_type *>(this)->assign_phred(p); }
56
58 friend derived_type;
59
60public:
61 // Import from base type:
64 using base_t::to_rank;
65 using typename base_t::char_type;
66 using typename base_t::rank_type;
67
71 // This constructor needs to be public, because constructor templates are not inherited otherwise
73 template <meta::different_from<derived_type> other_qual_type>
75 explicit constexpr quality_base(other_qual_type const & other) noexcept
76 {
77 assign_phred_to(bio::alphabet::to_phred(other), static_cast<derived_type &>(*this));
78 }
80
85 constexpr phred_type to_phred() const noexcept { return rank_to_phred[to_rank()]; }
87
102 constexpr derived_type & assign_phred(phred_type const p) noexcept
103 {
104 return assign_rank(phred_to_rank[static_cast<rank_type>(p)]);
105 }
107
108protected:
110 static constexpr std::array<rank_type, 256> phred_to_rank = []() constexpr
111 {
113
114 // NOLINTNEXTLINE(bugprone-signed-char-misuse)
115 for (int64_t i = std::numeric_limits<phred_type>::lowest(); i <= std::numeric_limits<phred_type>::max(); ++i)
116 {
117 if (i < derived_type::offset_phred) // map too-small to smallest possible
118 ret[static_cast<rank_type>(i)] = 0;
119 else if (i >= derived_type::offset_phred +
120 static_cast<int64_t>(alphabet_size)) // map too-large to highest possible
121 ret[static_cast<rank_type>(i)] = alphabet_size - 1;
122 else // map valid range to identity
123 ret[static_cast<rank_type>(i)] = i - derived_type::offset_phred;
124 }
125 return ret;
126 }();
127
129 static constexpr std::array<rank_type, 256> char_to_rank = []() constexpr
130 {
132
133 // NOLINTNEXTLINE(bugprone-signed-char-misuse)
134 for (int64_t i = std::numeric_limits<char_type>::lowest(); i <= std::numeric_limits<char_type>::max(); ++i)
135 {
136 if (i < derived_type::offset_char) // map too-small to smallest possible
137 ret[static_cast<rank_type>(i)] = 0;
138 else if (i >= derived_type::offset_char +
139 static_cast<int64_t>(alphabet_size)) // map too-large to highest possible
140 ret[static_cast<rank_type>(i)] = alphabet_size - 1;
141 else // map valid range to identity
142 ret[static_cast<rank_type>(i)] = i - derived_type::offset_char;
143 }
144
145 return ret;
146 }();
147
149 static constexpr std::array<phred_type, alphabet_size> rank_to_phred = []() constexpr
150 {
152
153 for (size_t i = 0; i < alphabet_size; ++i)
154 ret[i] = i + derived_type::offset_phred;
155
156 return ret;
157 }();
158
160 static constexpr std::array<char_type, alphabet_size> rank_to_char = []() constexpr
161 {
163
164 for (size_t i = 0; i < alphabet_size; ++i)
165 ret[i] = i + derived_type::offset_char;
166
167 return ret;
168 }();
169
171 friend constexpr phred_type tag_invoke(custom::to_phred, derived_type const alph) noexcept
172 {
173 return alph.to_phred();
174 }
175
177 friend constexpr derived_type & tag_invoke(custom::assign_phred_to,
178 phred_type const p,
179 derived_type & alph) noexcept
180 {
181 return alph.assign_phred(p);
182 }
183};
184
185} // namespace bio::alphabet
Quality alphabet concept.
Provides bio::alphabet::base.
A CRTP-base that makes defining a custom alphabet easier.
Definition: base.hpp:55
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: base.hpp:168
static constexpr size_t alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: base.hpp:177
meta::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: base.hpp:65
std::conditional_t< std::same_as< char, void >, char, char > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: base.hpp:63
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: base.hpp:124
A CRTP-base that refines bio::alphabet::base and is used by the quality alphabets.
Definition: quality_base.hpp:30
int8_t phred_type
The integer representation of a quality score assignable with =operator.
Definition: quality_base.hpp:36
friend constexpr phred_type tag_invoke(custom::to_phred, derived_type const alph) noexcept
tag_invoke() wrapper around member.
Definition: quality_base.hpp:171
static constexpr std::array< rank_type, 256 > char_to_rank
Char to rank conversion table.
Definition: quality_base.hpp:129
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:75
static constexpr std::array< char_type, alphabet_size > rank_to_char
Rank to char conversion table.
Definition: quality_base.hpp:160
static constexpr std::array< phred_type, alphabet_size > rank_to_phred
Rank to phred conversion table.
Definition: quality_base.hpp:149
static constexpr std::array< rank_type, 256 > phred_to_rank
Phred to rank conversion table.
Definition: quality_base.hpp:110
constexpr derived_type & assign_phred(phred_type const p) noexcept
Assign from the numeric phred value.
Definition: quality_base.hpp:102
friend constexpr derived_type & tag_invoke(custom::assign_phred_to, phred_type const p, derived_type &alph) noexcept
tag_invoke() wrapper around member.
Definition: quality_base.hpp:177
constexpr phred_type to_phred() const noexcept
Return the alphabet's value in phred representation.
Definition: quality_base.hpp:85
A concept that indicates whether an alphabet represents quality scores.
Definition: concept.hpp:171
constexpr auto assign_phred_to
Assign a phred score to a quality alphabet object.
Definition: concept.hpp:128
constexpr auto to_phred
The public getter function for the phred representation of a quality score.
Definition: concept.hpp:65
The alphabet module's namespace.
Definition: aa10li.hpp:23
Customisation tag for bio::alphabet::assign_char_to.
Definition: tag.hpp:57
Customisation tag for bio::alphabet::assign_char_to.
Definition: tag.hpp:53