SeqAn3 3.4.0-rc.4
The Modern C++ library for sequence analysis.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
dna4.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <vector>
13
15
16// ------------------------------------------------------------------
17// dna4
18// ------------------------------------------------------------------
19
20namespace seqan3
21{
22
23class rna4;
24
49class dna4 : public nucleotide_base<dna4, 4>
50{
51private:
54
56 friend base_t;
59 friend base_t::base_t;
62 friend rna4;
63
64public:
68 constexpr dna4() noexcept = default;
74
76
84 template <std::same_as<rna4> t> // template parameter t to accept incomplete type
86 {
87 assign_rank(r.to_rank());
88 }
90
93 {
94 return dna4{}.assign_rank(to_rank() ^ 0b11);
95 }
96
97private:
121 static constexpr char_type rank_to_char_table[alphabet_size]{'A', 'C', 'G', 'T'};
122
124 static constexpr rank_type rank_complement_table[alphabet_size]{
125 3, // T is complement of 'A'_dna4
126 2, // G is complement of 'C'_dna4
127 1, // C is complement of 'G'_dna4
128 0 // A is complement of 'T'_dna4
129 };
130
135 static constexpr rank_type rank_complement(rank_type const rank)
136 {
137 return rank_complement_table[rank];
138 }
139
144 static constexpr char_type rank_to_char(rank_type const rank)
145 {
146 return rank_to_char_table[rank];
147 }
148
153 static constexpr rank_type char_to_rank(char_type const chr)
154 {
155 using index_t = std::make_unsigned_t<char_type>;
156 return char_to_rank_table[static_cast<index_t>(chr)];
157 }
158
162 static constexpr std::array<rank_type, 256> char_to_rank_table{[]() constexpr
163 {
165
166 // reverse mapping for characters and their lowercase
167 for (size_t rnk = 0u; rnk < alphabet_size; ++rnk)
168 {
169 ret[rank_to_char_table[rnk]] = rnk;
170 ret[to_lower(rank_to_char_table[rnk])] = rnk;
171 }
172
173 // set U equal to T
174 ret['U'] = ret['T'];
175 ret['u'] = ret['t'];
176
177 // iupac characters get special treatment, because there is no N
178 ret['R'] = ret['A'];
179 ret['r'] = ret['A']; // A or G
180 ret['Y'] = ret['C'];
181 ret['y'] = ret['C']; // C or T
182 ret['S'] = ret['C'];
183 ret['s'] = ret['C']; // C or G
184 ret['W'] = ret['A'];
185 ret['w'] = ret['A']; // A or T
186 ret['K'] = ret['G'];
187 ret['k'] = ret['G']; // G or T
188 ret['M'] = ret['A'];
189 ret['m'] = ret['A']; // A or T
190 ret['B'] = ret['C'];
191 ret['b'] = ret['C']; // C or G or T
192 ret['D'] = ret['A'];
193 ret['d'] = ret['A']; // A or G or T
194 ret['H'] = ret['A'];
195 ret['h'] = ret['A']; // A or C or T
196 ret['V'] = ret['A'];
197 ret['v'] = ret['A']; // A or C or G
198
199 return ret;
200 }()};
201};
202
203// ------------------------------------------------------------------
204// containers
205// ------------------------------------------------------------------
206
213
214inline namespace literals
215{
216
217// ------------------------------------------------------------------
218// literals
219// ------------------------------------------------------------------
220
236constexpr dna4 operator""_dna4(char const c) noexcept
237{
238 return dna4{}.assign_char(c);
239}
240
251SEQAN3_WORKAROUND_LITERAL dna4_vector operator""_dna4(char const * s, std::size_t n)
252{
254 r.resize(n);
255
256 for (size_t i = 0; i < n; ++i)
257 r[i].assign_char(s[i]);
258
259 return r;
260}
262
263} // namespace literals
264
265} // namespace seqan3
constexpr derived_type & assign_char(char_type const chr) noexcept
Assign from a character, implicitly converts invalid characters.
Definition alphabet_base.hpp:160
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_t, void >, char, char_t > 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 "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
The four letter DNA alphabet of A,C,G,T.
Definition dna4.hpp:50
constexpr dna4 complement() const noexcept
Returns the complement of the current nucleotide.
Definition dna4.hpp:92
constexpr dna4() noexcept=default
Defaulted.
A CRTP-base that refines seqan3::alphabet_base and is used by the nucleotides.
Definition nucleotide_base.hpp:41
The four letter RNA alphabet of A,C,G,U.
Definition rna4.hpp:46
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition alphabet/concept.hpp:152
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
constexpr char_type to_lower(char_type const c) noexcept
Converts 'A'-'Z' to 'a'-'z' respectively; other characters are returned as is.
Definition transform.hpp:74
SeqAn specific customisations in the standard namespace.
Provides seqan3::nucleotide_base.
#define SEQAN3_WORKAROUND_LITERAL
Our char literals returning std::vector should be constexpr if constexpr std::vector is supported.
Definition platform.hpp:310
Hide me