-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevcomp.h
142 lines (106 loc) · 2.56 KB
/
revcomp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
static char *revcomp_simple(const char *forward, size_t len, char *reverse)
{
reverse[len] = '\0';
for (size_t k = 0; k < len; k++) {
unsigned char d;
switch (forward[k]) {
case 'A': d = 'T'; break;
case 'C': d = 'G'; break;
case 'G': d = 'C'; break;
case 'T': d = 'A'; break;
}
reverse[len - k - 1] = d;
}
return reverse;
}
static char *revcomp_table(const char *forward, size_t len, char *reverse)
{
static /*constexpr*/ char table[127];
table['A'] = 'T';
table['T'] = 'A';
table['G'] = 'C';
table['C'] = 'G';
reverse[len] = '\0';
for (size_t k = 0; k < len; k++) {
reverse[len - k - 1] = table[forward[k]];
}
return reverse;
}
static char *revcomp_reverse(const char *forward, size_t len, char *reverse)
{
reverse[len] = '\0';
for (size_t k = 0; k < len; k++) {
unsigned char d;
switch (forward[len - k - 1]) {
case 'A': d = 'T'; break;
case 'C': d = 'G'; break;
case 'G': d = 'C'; break;
case 'T': d = 'A'; break;
}
reverse[k] = d;
}
return reverse;
}
static char *revcomp_twocase(const char *forward, size_t len, char *reverse)
{
reverse[len] = '\0';
for (size_t k = 0; k < len; k++) {
unsigned char c = forward[len - k - 1], d;
if (c == 'G' || c == 'C') {
d = 'C' + 'G' - c;
} else {
d = 'A' + 'T' - c;
}
reverse[k] = d;
}
return reverse;
}
static char *revcomp_twocase_switch(const char *forward, size_t len,
char *reverse)
{
reverse[len] = '\0';
for (size_t k = 0; k < len; k++) {
unsigned char c = forward[len - k - 1], d;
// the following switch statement is sematically identical, but much
// slower than the if above
switch (c) {
default:
case 'A':
case 'T': d = 'A' + 'T' - c; break;
case 'C':
case 'G': d = 'C' + 'G' - c; break;
}
reverse[k] = d;
}
return reverse;
}
static char *revcomp_twiddle(const char *forward, size_t len, char *reverse)
{
reverse[len] = '\0';
for (size_t k = 0; k < len; k++) {
char c = forward[len - k - 1];
reverse[k] = c ^= c & 2 ? 4 : 21;
}
return reverse;
}
static char *revcomp_subtraction(const char *forward, size_t len, char *reverse)
{
reverse[len] = '\0';
for (size_t k = 0; k < len; k++) {
char c = forward[len - k - 1];
reverse[k] = (c & 2 ? 'G' + 'C' : 'A' + 'T') - c;
}
return reverse;
}
static char *revcomp_twostep(const char *forward, size_t len, char *reverse)
{
reverse[len] = '\0';
for (size_t k = 0; k < len; k++) {
char c = forward[k];
reverse[k] = c ^= c & 2 ? 4 : 21;
}
for (size_t k = 0; k < len / 2; k++) {
std::swap(reverse[k], reverse[len - k - 1]);
}
return reverse;
}