BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
dna5.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
16#include <vector>
17
18#include <bio/alphabet/detail/to_lower.hpp>
20
21// ------------------------------------------------------------------
22// dna5
23// ------------------------------------------------------------------
24
25namespace bio::alphabet
26{
27
28class rna5;
29
49class dna5 : public nucleotide_base<dna5, 5>
50{
51private:
54
56 friend base_t;
58 friend base_t::base_t;
61 friend rna5;
62
63public:
67 constexpr dna5() noexcept = default;
68 constexpr dna5(dna5 const &) noexcept = default;
69 constexpr dna5(dna5 &&) noexcept = default;
70 constexpr dna5 & operator=(dna5 const &) noexcept = default;
71 constexpr dna5 & operator=(dna5 &&) noexcept = default;
72 ~dna5() noexcept = default;
73
74 using base_t::base_t;
75
77 template <std::same_as<rna5> t> // Accept incomplete type
78 constexpr dna5(t const & r) noexcept
79 {
80 assign_rank(r.to_rank());
81 }
83
84protected:
86
88 static constexpr std::array<char_type, alphabet_size> rank_to_char{'A', 'C', 'G', 'N', 'T'};
89
91 static constexpr std::array<rank_type, 256> char_to_rank = []() constexpr
92 {
94
95 // initialize with UNKNOWN
96 ret.fill(3); // == 'N'
97
98 // reverse mapping for characters and their lowercase
99 for (size_t rnk = 0u; rnk < alphabet_size; ++rnk)
100 {
101 ret[rank_to_char[rnk]] = rnk;
102 ret[detail::to_lower(rank_to_char[rnk])] = rnk;
103 }
104
105 // set U equal to T
106 ret['U'] = ret['T'];
107 ret['u'] = ret['t'];
108
109 // iupac characters are implicitly "UNKNOWN"
110 return ret;
111 }();
112
114 static const std::array<dna5, alphabet_size> complement_table;
115};
116
117} // namespace bio::alphabet
118
119// ------------------------------------------------------------------
120// literals
121// ------------------------------------------------------------------
122
123namespace bio::alphabet
124{
125
126inline namespace literals
127{
128
137consteval dna5 operator""_dna5(char const c)
138{
139 if (!char_is_valid_for<dna5>(c))
140 throw std::invalid_argument{"Illegal character in character literal."};
141
142 return dna5{}.assign_char(c);
143}
144
154template <meta::detail::literal_buffer_string str>
155constexpr std::vector<dna5> operator""_dna5()
156{
157 return detail::string_literal<str, dna5>();
158}
160
161} // namespace literals
162
163} // namespace bio::alphabet
164
165// ------------------------------------------------------------------
166// dna5 (deferred definition)
167// ------------------------------------------------------------------
168
169namespace bio::alphabet
170{
171
172constexpr std::array<dna5, dna5::alphabet_size> dna5::complement_table{
173 'T'_dna5, // complement of 'A'_dna5
174 'G'_dna5, // complement of 'C'_dna5
175 'C'_dna5, // complement of 'G'_dna5
176 'N'_dna5, // complement of 'N'_dna5
177 'A'_dna5 // complement of 'T'_dna5
178};
179
180} // namespace bio::alphabet
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: base.hpp:168
static constexpr size_t alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: base.hpp:177
constexpr derived_type & assign_char(char_type const c) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: base.hpp:145
The five letter DNA alphabet of A,C,G,T and the unknown character N..
Definition: dna5.hpp:50
constexpr dna5() noexcept=default
Defaulted.
A CRTP-base that refines bio::alphabet::base and is used by the nucleotides.
Definition: nucleotide_base.hpp:42
The five letter RNA alphabet of A,C,G,U and the unknown character N..
Definition: rna5.hpp:48
T fill(T... args)
The alphabet module's namespace.
Definition: aa10li.hpp:23
Provides bio::alphabet::nucleotide_base.