SeqAn3 3.3.0-rc.1
The Modern C++ library for sequence analysis.
dna4.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
15#include <vector>
16
18
19// ------------------------------------------------------------------
20// dna4
21// ------------------------------------------------------------------
22
23namespace seqan3
24{
25
26class rna4;
27
52class dna4 : public nucleotide_base<dna4, 4>
53{
54private:
57
59 friend base_t;
62 friend base_t::base_t;
65 friend rna4;
66
67public:
71 constexpr dna4() noexcept = default;
72 constexpr dna4(dna4 const &) noexcept = default;
73 constexpr dna4(dna4 &&) noexcept = default;
74 constexpr dna4 & operator=(dna4 const &) noexcept = default;
75 constexpr dna4 & operator=(dna4 &&) noexcept = default;
76 ~dna4() noexcept = default;
77
78 using base_t::base_t;
79
87 template <std::same_as<rna4> t> // template parameter t to accept incomplete type
88 constexpr dna4(t const & r) noexcept
89 {
90 assign_rank(r.to_rank());
91 }
93
95 constexpr dna4 complement() const noexcept
96 {
97 return dna4{}.assign_rank(to_rank() ^ 0b11);
98 }
99
100private:
124 static constexpr char_type rank_to_char_table[alphabet_size]{'A', 'C', 'G', 'T'};
125
127 static constexpr rank_type rank_complement_table[alphabet_size]{
128 3, // T is complement of 'A'_dna4
129 2, // G is complement of 'C'_dna4
130 1, // C is complement of 'G'_dna4
131 0 // A is complement of 'T'_dna4
132 };
133
138 static constexpr rank_type rank_complement(rank_type const rank)
139 {
140 return rank_complement_table[rank];
141 }
142
147 static constexpr char_type rank_to_char(rank_type const rank)
148 {
149 return rank_to_char_table[rank];
150 }
151
156 static constexpr rank_type char_to_rank(char_type const chr)
157 {
158 using index_t = std::make_unsigned_t<char_type>;
159 return char_to_rank_table[static_cast<index_t>(chr)];
160 }
161
162 // clang-format off
166 static constexpr std::array<rank_type, 256> char_to_rank_table
167 {
168 []() constexpr {
170
171 // reverse mapping for characters and their lowercase
172 for (size_t rnk = 0u; rnk < alphabet_size; ++rnk)
173 {
174 ret[rank_to_char_table[rnk]] = rnk;
175 ret[to_lower(rank_to_char_table[rnk])] = rnk;
176 }
177
178 // set U equal to T
179 ret['U'] = ret['T'];
180 ret['u'] = ret['t'];
181
182 // iupac characters get special treatment, because there is no N
183 ret['R'] = ret['A'];
184 ret['r'] = ret['A']; // A or G
185 ret['Y'] = ret['C'];
186 ret['y'] = ret['C']; // C or T
187 ret['S'] = ret['C'];
188 ret['s'] = ret['C']; // C or G
189 ret['W'] = ret['A'];
190 ret['w'] = ret['A']; // A or T
191 ret['K'] = ret['G'];
192 ret['k'] = ret['G']; // G or T
193 ret['M'] = ret['A'];
194 ret['m'] = ret['A']; // A or T
195 ret['B'] = ret['C'];
196 ret['b'] = ret['C']; // C or G or T
197 ret['D'] = ret['A'];
198 ret['d'] = ret['A']; // A or G or T
199 ret['H'] = ret['A'];
200 ret['h'] = ret['A']; // A or C or T
201 ret['V'] = ret['A'];
202 ret['v'] = ret['A']; // A or C or G
203
204 return ret;
205 }()
206 };
207};
208// clang-format on
209
210// ------------------------------------------------------------------
211// containers
212// ------------------------------------------------------------------
213
220
221inline namespace literals
222{
223
224// ------------------------------------------------------------------
225// literals
226// ------------------------------------------------------------------
227
243constexpr dna4 operator""_dna4(char const c) noexcept
244{
245 return dna4{}.assign_char(c);
246}
247
258inline dna4_vector operator""_dna4(char const * s, std::size_t n)
259{
260 dna4_vector r;
261 r.resize(n);
262
263 for (size_t i = 0; i < n; ++i)
264 r[i].assign_char(s[i]);
265
266 return r;
267}
269
270} // namespace literals
271
272} // namespace seqan3
constexpr derived_type & assign_char(char_type const chr) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:163
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:137
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:80
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
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:72
constexpr dna4 & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:187
The four letter DNA alphabet of A,C,G,T..
Definition: dna4.hpp:53
constexpr dna4 complement() const noexcept
Returns the complement of the current nucleotide.
Definition: dna4.hpp:95
constexpr dna4() noexcept=default
Defaulted.
A CRTP-base that refines seqan3::alphabet_base and is used by the nucleotides.
Definition: nucleotide_base.hpp:43
The four letter RNA alphabet of A,C,G,U..
Definition: rna4.hpp:49
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
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:83
SeqAn specific customisations in the standard namespace.
Provides seqan3::nucleotide_base.
T resize(T... args)