Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseq_utils.cpp
More file actions
Latest commit
251 lines (228 loc) · 8.29 KB
/
Copy pathseq_utils.cpp
File metadata and controls
251 lines (228 loc) · 8.29 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include"seq_utils.h"
#include"qgenlib/qgen_error.h"
#include<cstdio>
#include<cstring>
/* constant table - from Heng Li at
https://github.com/lh3/seqtk/blob/master/seqtk.c
https://github.com/samtools/htslib/blob/develop/hts.c */
// ASCII to IUPAC mapper
constunsignedchar seq_nt16_table[256] = {
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15/*'-'*/, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 1, 14, 2, 13, 15, 15, 4, 11, 15, 15, 12, 15, 3, 15, 15,
15, 15, 5, 6, 8, 15, 7, 9, 0, 10, 15, 15, 15, 15, 15, 15,
15, 1, 14, 2, 13, 15, 15, 4, 11, 15, 15, 12, 15, 3, 15, 15,
15, 15, 5, 6, 8, 15, 7, 9, 0, 10, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15};
// ASCII to XACGTN representation
constunsignedchar seq_nt6_table[256] = {
0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 1, 5, 2, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 1, 5, 2, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5,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, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
// IUPAC16 to letter mapping
constchar *seq_nt16_rev_table = "XACMGRSVTWYHKDBN";
// ACGNT to ASCII conversion
constchar *seq_nt5_rev_table = "ACGNT";
// Force-convert IUPAC16 to AGCT(01234)
constunsignedchar seq_nt16to4_table[] = {4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4};
// 4-bit comparison string between IUPAC16 to ACGT
// A -> 1000, C -> 0100 M=AC -> 1010
constunsignedchar seq_nt16comp_table[] = {0, 8, 4, 12, 2, 10, 9, 14, 1, 6, 5, 13, 3, 11, 7, 15};
// How many letters does a IUPACT code represent?
constint bitcnt_table[] = {4, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
// reverse complement table
constchar comp_tab[] = {
0, 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, 'T', 'V', 'G', 'H', 'E', 'F', 'C', 'D', 'I', 'J', 'M', 'L', 'K', 'N', 'O',
'P', 'Q', 'Y', 'S', 'A', 'A', 'B', 'W', 'X', 'R', 'Z', 91, 92, 93, 94, 95,
64, 't', 'v', 'g', 'h', 'e', 'f', 'c', 'd', 'i', 'j', 'm', 'l', 'k', 'n', 'o',
'p', 'q', 'y', 's', 'a', 'a', 'b', 'w', 'x', 'r', 'z', 123, 124, 125, 126, 127};
// reverse complement of string sequences
voidseq_revcomp(char *seq, int32_t l)
{
int32_t i, c0, c1;
for (i = 0; i < l >> 1; ++i)
{ // reverse complement sequence
c0 = comp_tab[(int32_t)seq[i]];
c1 = comp_tab[(int32_t)seq[l - 1 - i]];
seq[i] = c1;
seq[l - 1 - i] = c0;
}
if (l & 1) // complement the remaining base
seq[l >> 1] = comp_tab[(int)seq[l >> 1]];
}
// reverse the sequence without taking complement (useful for quality strings)
voidseq_revonly(char *seq, int32_t l)
{
int32_t i;
char c0, c1;
for (i = 0; i < l >> 1; ++i)
{ // reverse the characters
c0 = seq[i];
c1 = seq[l - 1 - i];
seq[i] = c1;
seq[l - 1 - i] = c0;
}
}
// count the number of mismatches compared to IUPAC pattern
int32_tseq_iupac_mismatch(constchar *seq, constchar *pattern, int32_t len)
{
int32_t mismatches = 0;
unsignedchar iupac, nt6;
for (int32_t i = 0; i < len; ++i)
{
if (pattern[i] != 'N')
{
iupac = seq_nt16_table[pattern[i]];
nt6 = seq_nt6_table[seq[i]];
if (nt6 > 0 && nt6 < 5)
{
if ((seq_nt16comp_table[iupac] & (0x01 << (4 - nt6))) == 0)
++mismatches;
}
else
{
++mismatches;
}
}
}
return mismatches;
}
// convert sequences into 2bit strings (len <= 32)
uint64_tseq2bits(constchar *seq, int32_t len, uint8_t nonACGTs)
{
uint64_t bits = 0;
uint8_t c;
for (int32_t i = 0; i < len; ++i)
{
c = seq_nt6_table[seq[i]];
if (c == 0 || c == 5)
c = nonACGTs + 1;
bits = ((bits << 2) | ((c - 1) & 0x03));
}
return bits;
}
// convert sequences into 2bit strings (len <= 64)
uint128_tseq2bits_128(constchar *seq, int32_t len, uint8_t nonACGTs)
{
uint128_t bits = 0;
uint8_t c;
for (int32_t i = 0; i < len; ++i)
{
c = seq_nt6_table[seq[i]];
if (c == 0 || c == 5)
c = nonACGTs + 1;
bits = ((bits << 2) | ((c - 1) & 0x03));
}
return bits;
}
// convert sequences into base-5 binaries (len <= 27)
uint64_tseq2nt5(constchar *seq, int32_t len)
{
uint64_t bits = 0;
for (int32_t i = 0; i < len; ++i)
{
bits = bits * 5 + (int32_t)seq_nt5_table[seq[i]];
}
// error("%s %llu", seq, bits);
return bits;
}
// convert sequences into base-5 binaries (len <= 55)
uint128_tseq2nt5_128(constchar *seq, int32_t len)
{
uint128_t bits = 0;
for (int32_t i = 0; i < len; ++i)
{
bits = bits * 5 + (int32_t)seq_nt5_table[seq[i]];
}
// error("%s %llu", seq, bits);
return bits;
}
// convert base-5 binaries into sequences (len <= 27)
boolnt52seq(uint64_t nt5, int32_t len, char *seq) {
int32_t i;
for (i = len - 1; i >= 0; --i) {
seq[i] = seq_nt5_rev_table[nt5 % 5];
nt5 /= 5;
}
seq[len] = '\0';
return nt5 == 0; // if nt5 is not zero, then the sequence is too long
}
// convert base-5 binaries into sequences (len <= 55)
boolnt52seq_128(uint128_t nt5, int32_t len, char *seq) {
int32_t i;
for (i = len - 1; i >= 0; --i) {
seq[i] = seq_nt5_rev_table[nt5 % 5];
nt5 /= 5;
}
seq[len] = '\0';
return nt5 == 0; // if nt5 is not zero, then the sequence is too long
}
// convert long sequences into multiple chunks of base-5 binaries (len <= 27)
int32_tseq2nt5multi(constchar *seq, int32_t lseq, uint64_t *nt5s, int32_t nt5unit) {
// determine the number of chunks
int32_t nchunks = (lseq + nt5unit - 1) / nt5unit;
int32_t offset = 0;
int32_t i;
for (i = 0; i < nchunks; ++i) {
nt5s[i] = seq2nt5(seq + i * nt5unit, offset + nt5unit > lseq ? lseq - offset : nt5unit);
offset += nt5unit;
}
return nchunks;
}
// convert multiple chunks of base-5 binaries into sequences
boolnt5multi2seq(uint64_t *nt5s, int32_t lseq, char *seq, int32_t nt5unit) {
int32_t nchunks = (lseq + nt5unit - 1) / nt5unit;
int32_t offset = 0;
int32_t i;
for (i = 0; i < nchunks; ++i) {
if (!nt52seq(nt5s[i], offset + nt5unit > lseq ? lseq - offset : nt5unit, seq + i * nt5unit))
returnfalse;
offset += nt5unit;
}
seq[lseq] = '\0';
returntrue;
}