SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
dna4.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 <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;
69 constexpr dna4(dna4 const &) noexcept = default;
70 constexpr dna4(dna4 &&) noexcept = default;
71 constexpr dna4 & operator=(dna4 const &) noexcept = default;
72 constexpr dna4 & operator=(dna4 &&) noexcept = default;
73 ~dna4() noexcept = default;
74
75 using base_t::base_t;
76
84 template <std::same_as<rna4> t> // template parameter t to accept incomplete type
85 constexpr dna4(t const & r) noexcept
86 {
87 assign_rank(r.to_rank());
88 }
90
92 constexpr dna4 complement() const noexcept
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
159 // clang-format off
163 static constexpr std::array<rank_type, 256> char_to_rank_table
164 {
165 []() constexpr {
167
168 // reverse mapping for characters and their lowercase
169 for (size_t rnk = 0u; rnk < alphabet_size; ++rnk)
170 {
171 ret[rank_to_char_table[rnk]] = rnk;
172 ret[to_lower(rank_to_char_table[rnk])] = rnk;
173 }
174
175 // set U equal to T
176 ret['U'] = ret['T'];
177 ret['u'] = ret['t'];
178
179 // iupac characters get special treatment, because there is no N
180 ret['R'] = ret['A'];
181 ret['r'] = ret['A']; // A or G
182 ret['Y'] = ret['C'];
183 ret['y'] = ret['C']; // C or T
184 ret['S'] = ret['C'];
185 ret['s'] = ret['C']; // C or G
186 ret['W'] = ret['A'];
187 ret['w'] = ret['A']; // A or T
188 ret['K'] = ret['G'];
189 ret['k'] = ret['G']; // G or T
190 ret['M'] = ret['A'];
191 ret['m'] = ret['A']; // A or T
192 ret['B'] = ret['C'];
193 ret['b'] = ret['C']; // C or G or T
194 ret['D'] = ret['A'];
195 ret['d'] = ret['A']; // A or G or T
196 ret['H'] = ret['A'];
197 ret['h'] = ret['A']; // A or C or T
198 ret['V'] = ret['A'];
199 ret['v'] = ret['A']; // A or C or G
200
201 return ret;
202 }()
203 };
204};
205// clang-format on
206
207// ------------------------------------------------------------------
208// containers
209// ------------------------------------------------------------------
210
217
218inline namespace literals
219{
220
221// ------------------------------------------------------------------
222// literals
223// ------------------------------------------------------------------
224
240constexpr dna4 operator""_dna4(char const c) noexcept
241{
242 return dna4{}.assign_char(c);
243}
244
255SEQAN3_WORKAROUND_LITERAL dna4_vector operator""_dna4(char const * s, std::size_t n)
256{
257 dna4_vector r;
258 r.resize(n);
259
260 for (size_t i = 0; i < n; ++i)
261 r[i].assign_char(s[i]);
262
263 return r;
264}
266
267} // namespace literals
268
269} // 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
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:40
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:80
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:269
T resize(T... args)
Hide me