Antkeeper  0.0.1
base.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <engine/genetics/base.hpp>
21 
22 namespace genetics {
23 namespace base {
24 
31 static inline unsigned char decode(char symbol)
32 {
33  static constexpr unsigned char bases[25] =
34  {
35  0b0001, // A
36  0b1110, // B
37  0b0010, // C
38  0b1101, // D
39  0, // E
40  0, // F
41  0b0100, // G
42  0b1011, // H
43  0, // I
44  0, // J
45  0b1100, // K
46  0, // L
47  0b0011, // M
48  0b1111, // N
49  0, // O
50  0, // P
51  0, // Q
52  0b0101, // R
53  0b0110, // S
54  0b1000, // T
55  0b1000, // U
56  0b0111, // V
57  0b1001, // W
58  0, // X
59  0b1010, // Y
60  };
61 
62  return (symbol < 'A' || symbol >= 'Z') ? 0 : bases[symbol - 'A'];
63 }
64 
65 int compare(char a, char b)
66 {
67  static constexpr int popcount[16] =
68  {
69  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
70  };
71 
72  return popcount[decode(a) & decode(b)];
73 }
74 
75 char transcribe(char symbol)
76 {
77  return (symbol == 'T') ? 'U' : (symbol == 'U') ? 'T' : symbol;
78 }
79 
80 namespace dna
81 {
82  char complement(char symbol)
83  {
84  constexpr const char* complements = "TVGHZZCDZZMZKNZZZYSAABWZR";
85  return (symbol < 'A' || symbol >= 'Z') ? 'Z' : complements[symbol - 'A'];
86  }
87 }
88 
89 namespace rna
90 {
91  char complement(char symbol)
92  {
93  constexpr const char* complements = "UVGHZZCDZZMZKNZZZYSAABWZR";
94  return (symbol < 'A' || symbol >= 'Z') ? 'Z' : complements[symbol - 'A'];
95  }
96 }
97 
98 } // namespace base
99 } // namespace genetics
char complement(char symbol)
Returns the DNA complement of an IUPAC degenerate base symbol.
Definition: base.cpp:82
char complement(char symbol)
Returns the RNA complement of an IUPAC degenerate base symbol.
Definition: base.cpp:91
int compare(char a, char b)
Returns the number of bases that are represented by both IUPAC degenerate base symbols.
Definition: base.cpp:65
char transcribe(char symbol)
Transcribes an IUPAC degenerate base symbol between DNA and RNA, swapping T for U or U for T.
Definition: base.cpp:75
Genetic algorithms.
Definition: amino-acid.hpp:25