SeqAn3 3.3.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// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2022, 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
18
19namespace seqan3
20{
21
29template <typename derived_type, size_t size>
30class phred_base : public alphabet_base<derived_type, size, char>
31{
32public:
40 using phred_type = int8_t;
42
43private:
46
50 friend base_t;
51
55 constexpr phred_base() noexcept = default;
56 constexpr phred_base(phred_base const &) noexcept = default;
57 constexpr phred_base(phred_base &&) noexcept = default;
58 constexpr phred_base & operator=(phred_base const &) noexcept = default;
59 constexpr phred_base & operator=(phred_base &&) noexcept = default;
60 ~phred_base() noexcept = default;
61
64
65public:
66 // Import from base type:
67 using base_t::alphabet_size;
68 using base_t::assign_rank;
69 using base_t::to_rank;
70 using typename base_t::char_type;
71 using typename base_t::rank_type;
72
76 // This constructor needs to be public, because constructor templates are not inherited otherwise
81 template <typename other_qual_type>
82 requires (!std::same_as<phred_base, other_qual_type>)
83 && (!std::same_as<derived_type, other_qual_type>) && quality_alphabet<other_qual_type>
84 explicit constexpr phred_base(other_qual_type const & other) noexcept
85 {
86 assign_phred_to(seqan3::to_phred(other), static_cast<derived_type &>(*this));
87 }
89
99 constexpr phred_type to_phred() const noexcept
100 {
101 return rank_to_phred[to_rank()];
102 }
104
124 constexpr derived_type & assign_phred(phred_type const p) noexcept
125 {
126 return assign_rank(phred_to_rank[static_cast<rank_type>(p)]);
127 }
129
130private:
132 static constexpr rank_type char_to_rank(char_type const chr)
133 {
134 int64_t difference = static_cast<int64_t>(chr) - static_cast<int64_t>(derived_type::offset_char);
135 return std::clamp<int64_t>(difference, 0, alphabet_size - 1);
136 }
137
139 static constexpr char_type rank_to_char(rank_type const rank)
140 {
141 return rank + derived_type::offset_char;
142 }
143
144 // clang-format off
147 {
148 []() constexpr {
150
152 i <= std::numeric_limits<phred_type>::max();
153 ++i)
154 {
155 if (i < derived_type::offset_phred) // map too-small to smallest possible
156 ret[static_cast<rank_type>(i)] = 0;
157 else if (i >= derived_type::offset_phred + alphabet_size) // map too-large to highest possible
158 ret[static_cast<rank_type>(i)] = alphabet_size - 1;
159 else // map valid range to identity
160 ret[static_cast<rank_type>(i)] = i - derived_type::offset_phred;
161 }
162
163 return ret;
164 }()
165 };
166
169 {
170 []() constexpr {
172
173 for (size_t i = 0; i < alphabet_size; ++i)
174 ret[i] = i + derived_type::offset_phred;
175
176 return ret;
177 }()
178 };
179};
180// clang-format on
181
182} // namespace seqan3
Provides seqan3::detail::convert_through_char_representation.
Quality alphabet concept.
Provides seqan3::alphabet_base.
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:57
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:137
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:199
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:187
rank_type rank
The value of the alphabet letter is stored as the rank.
Definition: alphabet_base.hpp:261
A CRTP-base that refines seqan3::alphabet_base and is used by the quality alphabets.
Definition: phred_base.hpp:31
constexpr phred_type to_phred() const noexcept
Return the alphabet's value in Phred score representation.
Definition: phred_base.hpp:99
friend derived_type
Befriend the derived_type so it can instantiate.
Definition: phred_base.hpp:63
static constexpr std::array< phred_type, alphabet_size > rank_to_phred
Rank to phred conversion table.
Definition: phred_base.hpp:169
static constexpr char_type rank_to_char(rank_type const rank)
Returns the character representation of rank.
Definition: phred_base.hpp:139
static constexpr rank_type char_to_rank(char_type const chr)
Returns the rank representation of character.
Definition: phred_base.hpp:132
constexpr phred_base() noexcept=default
Defaulted.
friend base_t
Befriend the base type so it can access seqan3::alphabet_base::char_to_rank and seqan3::alphabet_base...
Definition: phred_base.hpp:50
constexpr derived_type & assign_phred(phred_type const p) noexcept
Assign from the numeric Phred score value.
Definition: phred_base.hpp:124
static constexpr std::array< rank_type, 256 > phred_to_rank
Phred to rank conversion table.
Definition: phred_base.hpp:147
int8_t phred_type
The integer representation of the quality score.
Definition: phred_base.hpp:40
constexpr auto to_phred
The public getter function for the Phred representation of a quality score.
Definition: alphabet/quality/concept.hpp:100
constexpr auto assign_phred_to
Assign a Phred score to a quality alphabet object.
Definition: alphabet/quality/concept.hpp:230
A concept that indicates whether an alphabet represents quality scores.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.