SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
phred_base.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm>
13
15#include <seqan3/alphabet/detail/convert.hpp>
17
18namespace seqan3
19{
20
28template <typename derived_type, size_t size>
29class phred_base : public alphabet_base<derived_type, size, char>
30{
31public:
39 using phred_type = int8_t;
41
42private:
45
49 friend base_t;
50
54 constexpr phred_base() noexcept = default;
55 constexpr phred_base(phred_base const &) noexcept = default;
56 constexpr phred_base(phred_base &&) noexcept = default;
57 constexpr phred_base & operator=(phred_base const &) noexcept = default;
58 constexpr phred_base & operator=(phred_base &&) noexcept = default;
59 ~phred_base() noexcept = default;
60
62 friend derived_type;
63
64public:
65 // Import from base type:
66 using base_t::alphabet_size;
67 using base_t::assign_rank;
68 using base_t::to_rank;
69 using typename base_t::char_type;
70 using typename base_t::rank_type;
71
75 // This constructor needs to be public, because constructor templates are not inherited otherwise
80 template <typename other_qual_type>
81 requires (!std::same_as<phred_base, other_qual_type>)
82 && (!std::same_as<derived_type, other_qual_type>) && quality_alphabet<other_qual_type>
83 explicit constexpr phred_base(other_qual_type const & other) noexcept
84 {
85 assign_phred_to(seqan3::to_phred(other), static_cast<derived_type &>(*this));
86 }
88
98 constexpr phred_type to_phred() const noexcept
99 {
100 return rank_to_phred[to_rank()];
101 }
103
123 constexpr derived_type & assign_phred(phred_type const p) noexcept
124 {
125 return assign_rank(phred_to_rank[static_cast<rank_type>(p)]);
126 }
128
129private:
131 static constexpr rank_type char_to_rank(char_type const chr)
132 {
133 int64_t difference = static_cast<int64_t>(chr) - static_cast<int64_t>(derived_type::offset_char);
134 return std::clamp<int64_t>(difference, 0, alphabet_size - 1);
135 }
136
138 static constexpr char_type rank_to_char(rank_type const rank)
139 {
140 return rank + derived_type::offset_char;
141 }
142
143 // clang-format off
145 static constexpr std::array<rank_type, 256> phred_to_rank
146 {
147 []() constexpr {
149
151 i <= std::numeric_limits<phred_type>::max();
152 ++i)
153 {
154 if (i < derived_type::offset_phred) // map too-small to smallest possible
155 ret[static_cast<rank_type>(i)] = 0;
156 else if (i >= derived_type::offset_phred + alphabet_size) // map too-large to highest possible
157 ret[static_cast<rank_type>(i)] = alphabet_size - 1;
158 else // map valid range to identity
159 ret[static_cast<rank_type>(i)] = i - derived_type::offset_phred;
160 }
161
162 return ret;
163 }()
164 };
165
167 static constexpr std::array<phred_type, alphabet_size> rank_to_phred
168 {
169 []() constexpr {
171
172 for (size_t i = 0; i < alphabet_size; ++i)
173 ret[i] = i + derived_type::offset_phred;
174
175 return ret;
176 }()
177 };
178};
179// clang-format on
180
181} // namespace seqan3
Quality alphabet concept.
Provides seqan3::alphabet_base.
A CRTP-base that makes defining a custom alphabet easier.
Definition alphabet_base.hpp:54
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition alphabet_base.hpp:134
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:77
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:196
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:69
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition alphabet_base.hpp:184
A CRTP-base that refines seqan3::alphabet_base and is used by the quality alphabets.
Definition phred_base.hpp:30
constexpr phred_type to_phred() const noexcept
Return the alphabet's value in Phred score representation.
Definition phred_base.hpp:98
constexpr derived_type & assign_phred(phred_type const p) noexcept
Assign from the numeric Phred score value.
Definition phred_base.hpp:123
int8_t phred_type
The integer representation of the quality score.
Definition phred_base.hpp:39
constexpr auto to_phred
The public getter function for the Phred representation of a quality score.
Definition alphabet/quality/concept.hpp:97
constexpr auto assign_phred_to
Assign a Phred score to a quality alphabet object.
Definition alphabet/quality/concept.hpp:227
A concept that indicates whether an alphabet represents quality scores.
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
Hide me