forked from jfmrod/MAPseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmerseqtables.cpp
469 lines (418 loc) · 12 KB
/
kmerseqtables.cpp
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <eutils/emain.h>
#include <eutils/efile.h>
#define KMERSIZE 10ul
#define KMERBITS (KMERSIZE*2ul)
#define MAXSIZE (1ul<<KMERBITS)
#define KMERMAX (1ul<<KMERBITS)
#define KMERMASK (MAXSIZE-1ul)
#define KMERSIZE2 8ul
#define KMERBITS2 (KMERSIZE2*2ul)
#define MAXSIZE2 (1ul<<KMERBITS2)
#define KMERMAX2 (1ul<<KMERBITS2)
#define KMERMASK2 (MAXSIZE2-1ul)
#define PKMERSIZE 9ul
#define PKMERBITS (PKMERSIZE*2ul)
#define PMAXSIZE (1ul<<PKMERBITS)
#define PKMERMAX (1ul<<PKMERBITS)
#define PKMERMASK (PMAXSIZE-1ul)
#define IKMERSIZE 8ul
#define IKMERBITS (IKMERSIZE*2ul)
#define IKMERMAX (1ul<<IKMERBITS)
#define IKMERMASK (IKMERMAX-1ul)
#include "data/prot_trans_table.h"
uint32_t nuccount=12u;
uint32_t nuc[]={'a','t','g','c','A','T','u','U','G','C','n','N'};
uint32_t compnuc[]={0x0u,0x1u,0x2u,0x3u,0x0u,0x1u,0x1u,0x1u,0x2u,0x3u,0x0u,0x0u};
uint32_t revnuc[]={0x1u,0x0u,0x3u,0x2u};
uint32_t consnuc[]={0x0001u,0x0010u,0x0100u,0x1000u};
unsigned char seq_match_table[IKMERMAX];
unsigned char seq_comp_table[IKMERMAX];
unsigned char prot_comp_table[1u<<8u];
unsigned char seq_ident_table[IKMERMAX];
unsigned int kmer_prot_lt[PKMERMAX];
unsigned int kmer_protrev_lt[PKMERMAX];
unsigned long kmer_rev_lt[KMERMAX];
unsigned long kmer_rev_lt2[KMERMAX2];
uint64_t seq_alignment_lt[IKMERMAX];
char oct2hex(unsigned char o)
{
switch (o){
case 0x00: return('0');
case 0x01: return('1');
case 0x02: return('2');
case 0x03: return('3');
case 0x04: return('4');
case 0x05: return('5');
case 0x06: return('6');
case 0x07: return('7');
case 0x08: return('8');
case 0x09: return('9');
case 0x0a: return('a');
case 0x0b: return('b');
case 0x0c: return('c');
case 0x0d: return('d');
case 0x0e: return('e');
case 0x0f: return('f');
}
ldie("wrong octet value");
return('-');
}
void chr2hex(char *tmpstr,unsigned char c)
{
tmpstr[0]='0';
tmpstr[1]='x';
tmpstr[2]=oct2hex((c>>4u)&0xf);
tmpstr[3]=oct2hex(c&0xf);
tmpstr[4]=0x00;
}
void int2hex(char *tmpstr,unsigned int c)
{
tmpstr[0]='0';
tmpstr[1]='x';
for (unsigned int k=0; k<sizeof(c)*8u/4u; ++k)
tmpstr[2+k]=oct2hex((c>>(sizeof(c)*8u-4u-k*4u))&0xf);
// tmpstr[2]=oct2hex((c>>12u)&0xf);
// tmpstr[3]=oct2hex((c>>8u)&0xf);
// tmpstr[4]=oct2hex((c>>4u)&0xf);
// tmpstr[5]=oct2hex(c&0xf);
tmpstr[2+sizeof(c)*8u/4u]=0x00;
}
void uint2hex(char *tmpstr,unsigned int c)
{
tmpstr[0u]='0';
tmpstr[1u]='x';
for (unsigned int k=0; k<sizeof(c)*8u/4u; ++k)
tmpstr[2u+k]=oct2hex((c>>(sizeof(c)*8u-4u-k*4u))&0xf);
tmpstr[2u+sizeof(c)*8u/4u]='u';
tmpstr[2u+sizeof(c)*8u/4u+1u]=0x00;
}
void long2hex(char *tmpstr,unsigned long c)
{
tmpstr[0u]='0';
tmpstr[1u]='x';
for (unsigned int k=0; k<sizeof(c)*8u/4u; ++k)
tmpstr[2u+k]=oct2hex((c>>(sizeof(c)*8u-4u-k*4u))&0xf);
tmpstr[2u+sizeof(c)*8u/4u]='l';
tmpstr[2u+sizeof(c)*8u/4u+1u]=0x00;
}
void ulong2hex(char *tmpstr,unsigned long c)
{
tmpstr[0u]='0';
tmpstr[1u]='x';
for (unsigned int k=0; k<sizeof(c)*8u/4u; ++k)
tmpstr[2u+k]=oct2hex((c>>(sizeof(c)*8u-4u-k*4u))&0xf);
tmpstr[2u+sizeof(c)*8u/4u]='u';
tmpstr[2u+sizeof(c)*8u/4u+1u]='l';
tmpstr[2u+sizeof(c)*8u/4u+2u]=0x00;
}
void initMatchTable()
{
unsigned char tmp[4];
tmp[0x00u]=1u;
tmp[0x01u]=0u;
tmp[0x02u]=0u;
tmp[0x03u]=0u;
for (uint32_t i=0u; i<IKMERMAX; ++i){
seq_match_table[i]=0u;
for (unsigned int k=0u; k<16u; k+=2u)
seq_match_table[i]+=tmp[0x03u&(i>>k)];
}
}
void initProtCompressionTable()
{
uint32_t i;
for (i=0; i<(1u<<8u); ++i)
prot_comp_table[i]=0x00u;
prot_comp_table['*']=0x09u;
prot_comp_table['A']=prot_comp_table['a']=0x0Eu;
prot_comp_table['N']=prot_comp_table['n']=0x10u;
prot_comp_table['C']=prot_comp_table['c']=0x19u;
prot_comp_table['P']=prot_comp_table['p']=0x0Fu;
prot_comp_table['D']=prot_comp_table['d']=0x12u;
prot_comp_table['Q']=prot_comp_table['q']=0x03u;
prot_comp_table['E']=prot_comp_table['e']=0x02u;
prot_comp_table['R']=prot_comp_table['r']=0x0Bu;
prot_comp_table['F']=prot_comp_table['f']=0x15u;
prot_comp_table['S']=prot_comp_table['s']=0x0Du;
prot_comp_table['G']=prot_comp_table['g']=0x0Au;
prot_comp_table['T']=prot_comp_table['t']=0x0Cu;
prot_comp_table['H']=prot_comp_table['h']=0x13u;
prot_comp_table['V']=prot_comp_table['v']=0x06u;
prot_comp_table['I']=prot_comp_table['i']=0x04u;
prot_comp_table['W']=prot_comp_table['w']=0x29u;
prot_comp_table['K']=prot_comp_table['k']=0x00u;
prot_comp_table['Y']=prot_comp_table['y']=0x11u;
prot_comp_table['L']=prot_comp_table['l']=0x07u;
prot_comp_table['M']=prot_comp_table['m']=0x24u;
}
void initCompressionTable()
{
uint32_t i;
for (i=0; i<IKMERMAX; ++i)
seq_comp_table[i]=0x00u;
for (i=0x0000u; i<=0xff00u; i+=0x0100u){
for (int j=0; j<nuccount; ++j)
seq_comp_table[i | nuc[j]]|=compnuc[j];
}
for (i=0; i<nuccount; ++i){
for (uint32_t j=0x0u; j<=0xffu; ++j)
seq_comp_table[(nuc[i]<<8u) | j]|=(compnuc[i]<<2u);
}
}
void initCompressionTableStatus()
{
uint32_t i;
for (i=0; i<IKMERMAX; ++i)
seq_comp_table[i]=0x10u; // left 4bits are status code 0x1X=error by default, right 4bits are compressed 2 nucleotides
for (i=0x0000u; i<=0xff00u; i+=0x0100u){
for (int j=0; j<nuccount; ++j)
seq_comp_table[i | nuc[j]]|=compnuc[j];
}
for (i=0; i<nuccount; ++i){
for (uint32_t j=0x0u; j<=0xffu; ++j)
seq_comp_table[(nuc[i]<<8u) | j]|=(compnuc[i]<<2u);
}
for (int j=0; j<nuccount; ++j){
seq_comp_table[(uint32_t(' ')<<8u) | nuc[j]]=0x20u|compnuc[j];
seq_comp_table[(uint32_t('\t')<<8u) | nuc[j]]=0x20u|compnuc[j];
}
for (int i=0; i<nuccount; ++i){
seq_comp_table[(nuc[i]<<8u)|' ']=0x20u|compnuc[i];
seq_comp_table[(nuc[i]<<8u)|'\t']=0x20u|compnuc[i];
}
seq_comp_table[(' '<<8u)|' ']=0x30u; // 0x30: both missing nucleotides, skip both spaces
seq_comp_table[(' '<<8u)|'\t']=0x30u; // 0x30: both missing nucleotides, skip both spaces
seq_comp_table[('\t'<<8u)|' ']=0x30u; // 0x30: both missing nucleotides, skip both spaces
seq_comp_table[('\t'<<8u)|'\t']=0x30u; // 0x30: both missing nucleotides, skip both spaces
for (i=0; i<nuccount; ++i){
for (int j=0; j<nuccount; ++j)
seq_comp_table[(nuc[i]<<8u) | nuc[j]]=0x00|(compnuc[i]<<2u)|compnuc[j]; // when both characters are nucleotides reset the status code to 0x0X = noerror
}
}
void initProtTable()
{
const unsigned long codonmask=((0x1u<<6u) - 1u);
for (unsigned long i=0; i<PKMERMAX; ++i){
unsigned long tmp=i;
unsigned long res=0x0ul;
kmer_prot_lt[i]=nuc2prot[i&codonmask] | ((unsigned long)(nuc2prot[(i>>6u)&codonmask])<<6u) | ((unsigned long)(nuc2prot[(i>>12u)&codonmask])<<12u);
}
for (unsigned long i=0; i<PKMERMAX; ++i){
unsigned long tmp=i;
unsigned long res=0x0ul;
for (int j=0; j<PKMERSIZE; ++j){
res=(res<<2u)|(unsigned long)(revnuc[0x3ul&tmp]);
tmp>>=2u;
}
kmer_protrev_lt[i]=kmer_prot_lt[res];
}
}
void initRevComplementTable()
{
for (unsigned long i=0; i<KMERMAX; ++i){
unsigned long tmp=i;
unsigned long res=0x0ul;
for (int j=0; j<KMERSIZE; ++j){
res=(res<<2u)|(unsigned long)(revnuc[0x3ul&tmp]);
tmp>>=2u;
}
kmer_rev_lt[i]=res;
}
for (unsigned long i=0; i<KMERMAX2; ++i){
unsigned long tmp=i;
unsigned long res=0x0ul;
for (int j=0; j<KMERSIZE2; ++j){
res=(res<<2u)|(unsigned long)(revnuc[0x3ul&tmp]);
tmp>>=2u;
}
kmer_rev_lt2[i]=res;
}
}
void initSeqIdent()
{
for (uint32_t i=0; i<IKMERMAX; ++i){
for (uint32_t j=0; j<IKMERMAX; ++j){
seq_ident_table[i^j]=0u;
uint32_t ti=i; uint32_t tj=j;
for (int k=0; k<IKMERSIZE; ++k,tj>>=2u,ti>>=2u){
if ((0x03u&ti)==(0x03u&tj))
++seq_ident_table[i^j];
}
}
}
}
uint32_t seqatgc_count_lt[1u<<16u];
uint64_t seqatgc_conv_lt[1u<<8u];
void initATGCCount()
{
uint32_t ti;
uint32_t a=0u,t=0u,g=0u,c=0u;
for (uint32_t i=0; i<(1u<<16u); ++i){
a=0u; t=0u; g=0u; c=0u;
ti=i;
for (int k=0; k<8u; ++k,ti>>=2u){
a>>=1u; t>>=1u; g>>=1u; c>>=1u;
switch (0x03u&ti) {
case 0x0u: a|=0x80u; break;
case 0x1u: t|=0x80u; break;
case 0x2u: g|=0x80u; break;
case 0x3u: c|=0x80u; break;
}
}
seqatgc_count_lt[i]=(a<<24u)|(t<<16u)|(g<<8u)|c;
}
uint64_t r;
for (uint32_t i=0; i<(1u<<8u); ++i){
ti=i;
r=0ul;
for (int k=0u; k<8u; ++k,ti>>=1u){
r>>=8u;
r|=(ti&0x1u?0x0100000000000000ul:0ul);
}
seqatgc_conv_lt[i]=r;
}
}
void initAlignment()
{
uint64_t id;
for (uint32_t i=0; i<IKMERMAX; ++i){
for (uint32_t j=0; j<IKMERMAX; ++j){
uint32_t ti=i; uint32_t tj=j;
id=0u;
for (int k=0; k<IKMERSIZE; ++k,tj>>=2u,ti>>=2u){
id>>=8u;
if ((0x03u&ti)==(0x03u&tj))
id|=0x0100000000000000ul;
else
id|=0x0200000000000000ul;
}
seq_alignment_lt[i^j]=id;
}
}
}
int emain()
{
char tmpstr[3+sizeof(long)*2u];
efile f;
f.open("kmerseqtables-data.h","w");
initMatchTable();
f.write("unsigned char seq_match_table[]={\n");
chr2hex(tmpstr,seq_match_table[0]);
f.write(tmpstr);
for (int i=1; i<(1u<<16u); ++i){
f.write(",");
chr2hex(tmpstr,seq_match_table[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
initCompressionTable();
f.write("unsigned char seq_comp_table[]={\n");
chr2hex(tmpstr,seq_comp_table[0]);
f.write(tmpstr);
for (int i=1; i<(1u<<16u); ++i){
f.write(",");
chr2hex(tmpstr,seq_comp_table[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
initProtCompressionTable();
f.write("unsigned char prot_comp_table[]={\n");
chr2hex(tmpstr,prot_comp_table[0]);
f.write(tmpstr);
for (int i=1; i<(1u<<8u); ++i){
f.write(",");
chr2hex(tmpstr,prot_comp_table[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
initSeqIdent();
f.write("unsigned char seq_ident_table[]={\n");
chr2hex(tmpstr,seq_ident_table[0]);
f.write(tmpstr);
for (int i=1; i<IKMERMAX; ++i){
f.write(",");
chr2hex(tmpstr,seq_ident_table[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
initProtTable();
f.write("unsigned int kmer_prot_lt[]={\n");
int2hex(tmpstr,kmer_prot_lt[0]);
f.write(tmpstr);
for (int i=1; i<PKMERMAX; ++i){
f.write(",");
int2hex(tmpstr,kmer_prot_lt[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
f.write("unsigned int kmer_protrev_lt[]={\n");
int2hex(tmpstr,kmer_protrev_lt[0]);
f.write(tmpstr);
for (int i=1; i<PKMERMAX; ++i){
f.write(",");
int2hex(tmpstr,kmer_protrev_lt[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
initRevComplementTable();
f.write("unsigned int kmer_rev_lt[]={\n");
int2hex(tmpstr,kmer_rev_lt[0]);
f.write(tmpstr);
for (int i=1; i<KMERMAX; ++i){
f.write(",");
int2hex(tmpstr,kmer_rev_lt[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
f.write("unsigned int kmer_rev_lt2[]={\n");
int2hex(tmpstr,kmer_rev_lt2[0]);
f.write(tmpstr);
for (int i=1; i<KMERMAX2; ++i){
f.write(",");
int2hex(tmpstr,kmer_rev_lt2[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
initATGCCount();
f.write("uint32_t seqatgc_count_lt[]={\n");
uint2hex(tmpstr,seqatgc_count_lt[0]);
f.write(tmpstr);
for (int i=1; i<(1u<<16u); ++i){
f.write(",");
uint2hex(tmpstr,seqatgc_count_lt[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
f.write("uint64_t seqatgc_conv_lt[]={\n");
ulong2hex(tmpstr,seqatgc_conv_lt[0]);
f.write(tmpstr);
for (int i=1; i<(1u<<8u); ++i){
f.write(",");
ulong2hex(tmpstr,seqatgc_conv_lt[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
initAlignment();
f.write("uint64_t seq_alignment_lt[]={\n");
ulong2hex(tmpstr,seq_alignment_lt[0]);
f.write(tmpstr);
for (int i=1; i<IKMERMAX; ++i){
f.write(",");
ulong2hex(tmpstr,seq_alignment_lt[i]);
f.write(tmpstr);
if (i%20==0) f.write("\n");
}
f.write("\n};\n\n");
f.close();
return(0);
}