SeqAn3 3.3.0-rc.1
The Modern C++ library for sequence analysis.
dna5.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
19
20// ------------------------------------------------------------------
21// dna5
22// ------------------------------------------------------------------
23
24namespace seqan3
25{
26
27class rna5;
28
50class dna5 : public nucleotide_base<dna5, 5>
51{
52private:
55
57 friend base_t;
60 friend base_t::base_t;
63 friend rna5;
64
65public:
69 constexpr dna5() noexcept = default;
70 constexpr dna5(dna5 const &) noexcept = default;
71 constexpr dna5(dna5 &&) noexcept = default;
72 constexpr dna5 & operator=(dna5 const &) noexcept = default;
73 constexpr dna5 & operator=(dna5 &&) noexcept = default;
74 ~dna5() noexcept = default;
75
76 using base_t::base_t;
77
85 template <std::same_as<rna5> t> // Accept incomplete type
86 constexpr dna5(t const & r) noexcept
87 {
88 assign_rank(r.to_rank());
89 }
91
92private:
94 static constexpr char_type rank_to_char_table[alphabet_size]{'A', 'C', 'G', 'N', 'T'};
95
98 4, // T is complement of 'A'_dna5
99 2, // G is complement of 'C'_dna5
100 1, // C is complement of 'G'_dna5
101 3, // N is complement of 'N'_dna5
102 0 // A is complement of 'T'_dna5
103 };
104
106 static constexpr rank_type rank_complement(rank_type const rank)
107 {
109 }
110
112 static constexpr char_type rank_to_char(rank_type const rank)
113 {
114 return rank_to_char_table[rank];
115 }
116
118 static constexpr rank_type char_to_rank(char_type const chr)
119 {
120 using index_t = std::make_unsigned_t<char_type>;
121 return char_to_rank_table[static_cast<index_t>(chr)];
122 }
123
124 // clang-format off
127 {
128 []() constexpr {
130
131 ret.fill(3u); // initialize with UNKNOWN ('N')
132
133 // reverse mapping for characters and their lowercase
134 for (size_t rnk = 0u; rnk < alphabet_size; ++rnk)
135 {
136 ret[rank_to_char_table[rnk]] = rnk;
137 ret[to_lower(rank_to_char_table[rnk])] = rnk;
138 }
139
140 // set U equal to T
141 ret['U'] = ret['T'];
142 ret['u'] = ret['t'];
143
144 // iupac characters are implicitly "UNKNOWN"
145 return ret;
146 }()
147 };
148};
149// clang-format on
150
151// ------------------------------------------------------------------
152// containers
153// ------------------------------------------------------------------
154
161
162// ------------------------------------------------------------------
163// literals
164// ------------------------------------------------------------------
165inline namespace literals
166{
167
182constexpr dna5 operator""_dna5(char const c) noexcept
183{
184 return dna5{}.assign_char(c);
185}
186
196SEQAN3_WORKAROUND_LITERAL dna5_vector operator""_dna5(char const * s, std::size_t n)
197{
198 dna5_vector r;
199 r.resize(n);
200
201 for (size_t i = 0; i < n; ++i)
202 r[i].assign_char(s[i]);
203
204 return r;
205}
207
208} // namespace literals
209
210} // 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
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 dna5 & 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
The five letter DNA alphabet of A,C,G,T and the unknown character N..
Definition: dna5.hpp:51
static constexpr std::array< rank_type, 256 > char_to_rank_table
The lookup table used in char_to_rank.
Definition: dna5.hpp:127
constexpr dna5() noexcept=default
Defaulted.
static constexpr rank_type rank_complement_table[alphabet_size]
The rank complement table.
Definition: dna5.hpp:97
static constexpr char_type rank_to_char(rank_type const rank)
Returns the character representation of rank.
Definition: dna5.hpp:112
static constexpr char_type rank_to_char_table[alphabet_size]
The lookup table used in rank_to_char.
Definition: dna5.hpp:94
static constexpr rank_type rank_complement(rank_type const rank)
Returns the complement by rank.
Definition: dna5.hpp:106
friend base_t
Befriend seqan3::nucleotide_base.
Definition: dna5.hpp:57
static constexpr rank_type char_to_rank(char_type const chr)
Returns the rank representation of character.
Definition: dna5.hpp:118
friend rna5
Befriend seqan3::rna5 so it can copy char_to_rank.
Definition: dna5.hpp:63
A CRTP-base that refines seqan3::alphabet_base and is used by the nucleotides.
Definition: nucleotide_base.hpp:43
alphabet_base< dna5, size, char > base_t
Type of the base class.
Definition: nucleotide_base.hpp:46
The five letter RNA alphabet of A,C,G,U and the unknown character N..
Definition: rna5.hpp:49
T fill(T... args)
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.
#define SEQAN3_WORKAROUND_LITERAL
Our char literals returning std::vector should be constexpr if constexpr std::vector is supported.
Definition: platform.hpp:248
T resize(T... args)
Provides utilities for modifying characters.