-
Notifications
You must be signed in to change notification settings - Fork 7
/
mapseq.cpp
4737 lines (4277 loc) · 177 KB
/
mapseq.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <eutils/emain.h>
#include <eutils/ebasichashmap.h>
#include <eutils/estrhashof.h>
#include <eutils/einthashof.h>
#include <eutils/efile.h>
#include <eutils/etimer.h>
#include <eutils/eheap.h>
#include <eutils/ernd.h>
#include <eutils/ethread.h>
#include <eutils/esystem.h>
#include <eutils/eoption.h>
#include <deque>
#include <unistd.h>
#define LDEBUG(key,cmd) LDEBUG2(key,cmd)
#define LDEBUG2(key,cmd) LDEBUG_##key(cmd)
#define LDEBUG_(cmd) {}
#define LDEBUG_TRUE(cmd) cmd
#define D_SEQALIGNMENT
#define D_SEQIDENT
#define D_SEQSEARCH
#define D_PROFILE
//#define D_SEQIDENT TRUE
//#define D_SEQALIGNMENT TRUE
//#define D_SEQSEARCH TRUE
#include <math.h>
#include <map>
using namespace std;
#include "mapseq-config.h"
#include "ekmerhashmap.h"
#include "eseqali.h"
#include "kmerseqtables-data.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)
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
const uint32_t nuc[]={'a','t','g','c','u'};
const uint32_t compnuc[]={0x0u,0x1u,0x2u,0x3u,0x1u};
const uint32_t consnuc[]={0x0001u,0x0010u,0x0100u,0x1000u};
const unsigned long safe_shift[32]={0x0ul,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful,0xfffffffffffffffful};
unsigned int akmers[MAXSIZE]; // 6bases
int topotus=10;
int tophits=20;
int minscore=30;
int minid1=1;
int minid2=1;
int otulim=50;
float lambda=1.280;
float sweight=30.0;
/*
const float matchcost=2.0;
const float misscost=-1.0;
const float gapcost=-1.0;
const float gapopen=-5.0;
*/
//const ealignscore as(1.0,2.0,10.0,1.0);
//const ealignscore as(2.0,1.0,5.0,1.0,30.0);
const ealignscore as(1.0,1.0,5.0,1.0,20.0);
//const ealignscore as(2.0,1.0,5.0,1.0,30.0);
const int NCOUNT_MAXLEN=1600; // IMPORTANT: must be disivible by 8 to be 64bit aligned
float ti=0.0,ts=0.0,ti2=0.0,ts2=0.0,ta=0.0;
float tdp=0.0,tdp1=0.0,tdp2=0.0,tdpfl=0.0,tdpmd=0.0;
etimer t1,t2;
const uint32_t BMASK31=(1u<<31u)-1u;
const uint32_t BMASK16=(1u<<16u)-1u;
const uint32_t BMASK11=(1u<<11u)-1u;
const uint32_t BMASK10=(1u<<10u)-1u;
class etax
{
public:
estr name;
earray<estr> levels;
earray<estrarray> names;
earray<estrhashof<int> > ind;
ebasicarray<eseqtax*> seqs;
efloatarray cutoff;
efloatarray cutoffcoef;
};
typedef ebasicarray<unsigned int> euintarray;
class esearchws
{
public:
// eintarray best;
ernd rng;
ealignws alignws;
eintarray otukmerpos;
uint64_t *kmerbitmask;
uint64_t *bitmask;
eintarray idcount;
ebasicarray<uint32_t> idcount2;
unsigned int maskid;
euintarray kmermask;
unsigned int offset;
euintarray kmerpos;
euintarray kmerposrev;
unsigned int offset2;
euintarray kmerpos2;
ebasicarray<eintarray> taxcounts;
};
class ediag
{
public:
long i;
long j;
long i2;
long j2;
float V;
ediag *bestseg;
ediag(): i(-1),j(-1),i2(-1),j2(-1),V(-1),bestseg(0x00) {}
// ediag(int _i,int _j,int _i2,int _j2): i(_i),j(_j),i2(_i2),j2(_j2),V(-1),bestseg(0x00) {}
ediag(long _i,long _delta,long _len): i(_i),j(_i+_len),i2(_i+_delta),j2(_i+_delta+_len),V(-1),bestseg(0x00) {}
};
/*
class ekmer
{
public:
int i;
// eintarray pos;
int pos;
ekmer(): i(-1) {}
ekmer(int _i,int _pos): i(_i),pos(_pos) {}
// ekmer(int _i,eintarray& _pos): i(_i),pos(_pos) {}
};
*/
class emaxcount {
public:
int i;
int maxcount;
eintarray kwordaln;
emaxcount(): i(-1), maxcount(0) {}
};
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('-');
}
char* 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;
return(tmpstr);
}
uint32_t chr2kmer(const char *str)
{
uint32_t *tmp=(uint32_t*)str;
return(seq_comp_table[tmp[0]&0xffffu]|(seq_comp_table[(tmp[0]>>16u)&0xffffu]<<4u)|(seq_comp_table[tmp[1]&0xffffu]<<8u)|(seq_comp_table[(tmp[1]>>16u)&0xffffu]<<12u));
}
#define IKMERSIZE 8ul
#define IKMERBITS (IKMERSIZE*2ul)
#define IKMERMAX (1ul<<IKMERBITS)
#define IKMERMASK (IKMERMAX-1ul)
estr long2str(unsigned long kmer)
{
estr tmpstr;
for (int i=0; i<64u; i+=2u,kmer>>=2u){
switch (0x03u&kmer){
case 0x00u: tmpstr+='A'; break;
case 0x01u: tmpstr+='T'; break;
case 0x02u: tmpstr+='G'; break;
case 0x03u: tmpstr+='C'; break;
}
}
return(tmpstr);
}
estr kmer2str(uint32_t kmer)
{
estr tmpstr;
for (int i=0; i<IKMERSIZE; ++i,kmer>>=2u){
switch (0x03u&kmer){
case 0x00u: tmpstr+='A'; break;
case 0x01u: tmpstr+='T'; break;
case 0x02u: tmpstr+='G'; break;
case 0x03u: tmpstr+='C'; break;
}
}
return(tmpstr);
}
class eseqdb
{
public:
estrarrayof<eseq> seqs;
estrhashof<int> seqind;
eintarray seqotu;
earray<eintarray> otus;
ebasicarray<deque<uint32_t> > otukmers;
ebasicarray<etax> taxa;
};
unsigned long seqkmer(const eseq& s,long p1)
{
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s.seq._str);
return(((pstr1[p1/32u]>>(2u*(p1%32u)))|((pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u]))&KMERMASK);
}
unsigned long seqrevkmer(const eseq& s,long p1)
{
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s.seq._str);
p1=s.seqlen-p1;
return(kmer_rev_lt[((pstr1[p1/32u]>>(2u*(p1%32u)))|((pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u]))&KMERMASK]);
}
void print_tncount(unsigned char* tncounts,int p,int e)
{
uint64_t vid;
for (int i=p; i<e; ++i){
vid=tncounts[i];
switch (vid&0x3u){
case 0x0u: cout << "i: " << i << " -" << endl; break;
case 0x1u: cout << "i: " << i << " m" << endl; break;
case 0x2u: cout << "i: " << i << " x" << endl; break;
}
}
}
unsigned char d1_lt[1u<<16u];
unsigned char d2_lt[1u<<16u];
unsigned char id_lt[1u<<16u];
void initdlt()
{
for (uint32_t i=0u; i<(1u<<16u); ++i){
id_lt[i]=0u;
d1_lt[i]=8u;
d2_lt[i]=8u;
}
id_lt[0u]=1u;
d2_lt[0u]=0u;
for (uint32_t i=0u; i<8u; ++i)
d1_lt[i]=i;
}
void seqident_seg_left(const eseq& s1,int p1,const eseq& s2,int p2,ealigndata& adata,const ealignscore& as){
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s1.seq._str);
unsigned long *pstr2=reinterpret_cast<unsigned long*>(s2.seq._str);
unsigned long v1,v2;
unsigned char tmp[5];
int k;
int itmp;
if (p1==0 || p2==0) return;
// cout << "seg_left id: " << p1 << " :: " << p2 << " len1: " << p1 << " len2: " << p2 << endl;
for (; p1>=32 && p2>=32; p1+=k-32u+IKMERSIZE,p2+=k-32u+IKMERSIZE){
v1=pstr1[p1/32u-1u]>>(2u*(p1%32u));
v2=pstr2[p2/32u-1u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
for (k=32u-IKMERSIZE; k>=0; k-=IKMERSIZE,v1<<=IKMERSIZE*2u,v2<<=IKMERSIZE*2u){
// cout << p1 << "+" << k << " " << kmer2str((v1>>((32u-IKMERSIZE)*2u))&IKMERMASK) << endl;
// cout << p2 << "+" << k << " " << kmer2str((v2>>((32u-IKMERSIZE)*2u))&IKMERMASK) << endl;
tmp[0]=seq_ident_table[((v1^v2)>>((32u-IKMERSIZE)*2u))&IKMERMASK];
tmp[1]=seq_ident_table[((v1^(v2>>2ul))>>((32u-IKMERSIZE)*2u))&IKMERMASK];
tmp[2]=seq_ident_table[((v1^(v2>>4ul))>>((32u-IKMERSIZE)*2u))&IKMERMASK];
tmp[3]=seq_ident_table[((v2^(v1>>2ul))>>((32u-IKMERSIZE)*2u))&IKMERMASK];
tmp[4]=seq_ident_table[((v2^(v1>>4ul))>>((32u-IKMERSIZE)*2u))&IKMERMASK];
itmp=0;
for (int i=1; i<5; ++i){
if (tmp[i]>tmp[itmp]) itmp=i;
}
if (itmp!=0 && tmp[itmp]>tmp[0]+IKMERSIZE/4){
// matches+=tmp[0]; mismatches+=IKMERSIZE-tmp[0];
switch(itmp){
case 1: p1-=1; adata.gaps+=1; break;
case 2: p1-=2; adata.gaps+=2; break;
case 3: p2-=1; adata.gaps+=1; break;
case 4: p2-=2; adata.gaps+=2; break;
}
break;
}else{
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
adata._score+=tmp[0]*as.match-(IKMERSIZE-tmp[0])*as.mismatch;
}
}
}
// cout << "seg_left rest id: " << p1 << " :: " << p2 << " len1: " << p1 << " len2: " << p2 << endl;
for (;p1>=int(IKMERSIZE) && p2>=int(IKMERSIZE); p1+=k+int(IKMERSIZE)-32,p2+=k+int(IKMERSIZE)-32){ // needed to handle gap shifts gracefully
// cout << p1 << endl;
// cout << p2 << endl;
v1=(p1>=32?pstr1[p1/32u-1u]>>(2u*(p1%32u)):0ul);
v2=(p2>=32?pstr2[p2/32u-1u]>>(2u*(p2%32u)):0ul);
v1|=(pstr1[p1/32u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
for (k=32u-IKMERSIZE; k>=0 && p1+k>=int(IKMERSIZE) && p2+k>=int(IKMERSIZE); k-=IKMERSIZE,v1<<=IKMERSIZE*2u,v2<<=IKMERSIZE*2u){
// cout << p1 << "+" << k << " " << kmer2str((v1>>((32u-IKMERSIZE)*2u))&IKMERMASK) << endl;
// cout << p2 << "+" << k << " " << kmer2str((v2>>((32u-IKMERSIZE)*2u))&IKMERMASK) << endl;
tmp[0]=seq_ident_table[((v1^v2)>>((32u-IKMERSIZE)*2u))&IKMERMASK];
tmp[1]=seq_ident_table[((v1^(v2>>2ul))>>((32u-IKMERSIZE)*2u))&IKMERMASK];
tmp[2]=seq_ident_table[((v1^(v2>>4ul))>>((32u-IKMERSIZE)*2u))&IKMERMASK];
tmp[3]=seq_ident_table[((v2^(v1>>2ul))>>((32u-IKMERSIZE)*2u))&IKMERMASK];
tmp[4]=seq_ident_table[((v2^(v1>>4ul))>>((32u-IKMERSIZE)*2u))&IKMERMASK];
itmp=0;
for (int i=1; i<5; ++i){
if (tmp[i]>tmp[itmp]) itmp=i;
}
if (itmp!=0 && tmp[itmp]>tmp[0]+IKMERSIZE/4){
switch(itmp){
case 1: p1-=1; adata.gaps+=1; break;
case 2: p1-=2; adata.gaps+=2; break;
case 3: p2-=1; adata.gaps+=1; break;
case 4: p2-=2; adata.gaps+=2; break;
}
break;
}else{
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
adata._score+=tmp[0]*as.match-(IKMERSIZE-tmp[0])*as.mismatch;
}
}
}
int l=(p1<p2?p1:p2);
// cout << "missing: " << p1 << " l: " << l << endl;
if (l<=0) return;
p1-=l;
p2-=l;
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
tmp[0]=seq_ident_table[(v1^v2)&((0x1ul<<(l*2ul))-1ul)];
// cout << long2str(v1&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
// cout << long2str(v2&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
adata.matches+=tmp[0]-(IKMERSIZE-l);
adata.mismatches+=IKMERSIZE-tmp[0];
adata._score+=(tmp[0]-(IKMERSIZE-l))*as.match-(IKMERSIZE-tmp[0])*as.mismatch;
/*
tmp[0]=seq_ident_table[((v1^v2)>>((32u-IKMERSIZE)*2u))&~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul)&IKMERMASK]; // put mask at end of 64bit int, i.e.: FF000000
cout << long2str((v1>>((32u-IKMERSIZE)*2u))&(~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul))&IKMERMASK) << " " << long2str((~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul))&IKMERMASK) << endl;
cout << long2str((v2>>((32u-IKMERSIZE)*2u))&~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul)&IKMERMASK) << " " << long2str(~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul)&IKMERMASK) << endl;
matches+=tmp[0]-(IKMERSIZE-l);
mismatches+=IKMERSIZE-tmp[0];
*/
}
bool galign=true;
bool ignoreEmptyTax=false;
inline void sumcounts(unsigned char* tncount,int p1,uint64_t tmpnuc)
{
*reinterpret_cast<uint64_t*>(&tncount[p1])|=tmpnuc;
/*
for (int i=0; i<8; ++i,tmpnuc>>=8u)
tncount[p1+i]|=(tmpnuc&0xff);
*/
// cout << uint2hex(tmpstr,*p) << " " << uint2hex(tmpstr2,tmpnuc) << " " << uint2hex(tmpstr3,tncount[p1]) << endl;
}
void seqncount(const eseq& s1,long p1,long e1,const eseq& s2,long p2,long e2,ealigndata& adata,const ealignscore& as)
{
int k;
adata.matches+=e1-p1;
adata._score+=(e1-p1)*as.match;
adata.profile.add(AT_MATCH,e1-p1);
if (adata.aln==0x00) return;
/*
uint64_t *pstr1=reinterpret_cast<uint64_t*>(s1.seq._str);
uint64_t *pstr2=reinterpret_cast<uint64_t*>(s2.seq._str);
uint64_t v1,v2;
*/
// cout << "id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p2+IKMERSIZE<=e2; p2+=IKMERSIZE,p1+=IKMERSIZE){
// cout << "sumcounts: " << p1 << endl;
// v1=(pstr1[p1/32u]>>(2u*(p1%32u)))|((pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u]);
// v2=(pstr2[p2/32u]>>(2u*(p2%32u)))|((pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u]);
sumcounts(adata.aln,p1,seq_alignment_lt[0u]);
// tmpas1+=kmer2str(v1);
// tmpas2+=kmer2str(v2);
}
int l=e2-p2;
if (l<=0) return;
// if (l<=0) goto fend;
// cout << "missing: p1: " << p1 << " p2: " << p2 << " l: " << l << endl;
sumcounts(adata.aln,p1,seq_alignment_lt[0u]&((0x1ul<<(l*8u))-1ul));
// v1=(pstr1[p1/32u]>>(2u*(p1%32u)))|((pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u]);
// v2=(pstr2[p2/32u]>>(2u*(p2%32u)))|((pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u]);
// tmpas1+=kmer2str(v1).substr(0,l);
// tmpas2+=kmer2str(v2).substr(0,l);
// fend:
// pas1=tmpas1+pas1;
// pas2=tmpas2+pas2;
}
void seqident_seg_left_local(const eseq& s1,long p1,const eseq& s2,long p2,ealigndata& adata,const ealignscore& as){
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s1.seq._str);
unsigned long *pstr2=reinterpret_cast<unsigned long*>(s2.seq._str);
unsigned long v1,v2;
unsigned char tmp[5];
int k;
int itmp;
if (p1==0 || p2==0) {
adata.s1=p1;
adata.s2=p2;
return;
}
// cout << "seg_left id: " << p1 << " :: " << p2 << " len1: " << p1 << " len2: " << p2 << endl;
for (; p1>=32 && p2>=32; p1-=k,p2-=k){
v1=pstr1[p1/32u-1u]>>(2u*(p1%32u));
v2=pstr2[p2/32u-1u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
for (k=IKMERSIZE; k<32; k+=IKMERSIZE,v1<<=IKMERSIZE*2u,v2<<=IKMERSIZE*2u){
// cout << p1 << "+" << k << " " << kmer2str((v1>>((32u-IKMERSIZE)*2u))&IKMERMASK) << endl;
// cout << p2 << "+" << k << " " << kmer2str((v2>>((32u-IKMERSIZE)*2u))&IKMERMASK) << endl;
tmp[0]=seq_ident_table[((v1^v2)>>(64u-IKMERSIZE*2u))&IKMERMASK];
// if (!galign && tmp[0]<IKMERSIZE/2){
if (tmp[0]<IKMERSIZE/2){
adata.s1=p1-k;
adata.s2=p2-k;
return;
}
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
// sumcounts(tncount,p1-k,seq_alignment_lt[((v1^v2)>>(64u-IKMERSIZE*2u))&IKMERMASK]);
}
}
// cout << "seg_left rest id: " << p1 << " :: " << p2 << " len1: " << p1 << " len2: " << p2 << endl;
// cout << "2nd last p1: " << p1 << " p2: " << p2 << endl;
for (;p1>=int(IKMERSIZE) && p2>=int(IKMERSIZE); p1-=k,p2-=k){ // needed to handle gap shifts gracefully
// cout << "- 2nd last p1: " << p1 << " p2: " << p2 << endl;
// cout << p1 << endl;
// cout << p2 << endl;
v1=(p1>=32?pstr1[p1/32u-1u]>>(2u*(p1%32u)):0ul);
v2=(p2>=32?pstr2[p2/32u-1u]>>(2u*(p2%32u)):0ul);
v1|=(pstr1[p1/32u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
for (k=IKMERSIZE; p1>=k && p2>=k && k<=32; k+=IKMERSIZE,v1<<=IKMERSIZE*2u,v2<<=IKMERSIZE*2u){
// cout << p1 << "+" << k << " " << kmer2str((v1>>((32u-IKMERSIZE)*2u))&IKMERMASK) << endl;
// cout << p2 << "+" << k << " " << kmer2str((v2>>((32u-IKMERSIZE)*2u))&IKMERMASK) << endl;
tmp[0]=seq_ident_table[((v1^v2)>>(64u-IKMERSIZE*2u))&IKMERMASK];
// if (!galign && tmp[0]<=IKMERSIZE/2){
if (tmp[0]<=IKMERSIZE/2){
adata.s1=p1-k;
adata.s2=p2-k;
// cout << "break p1: " << adata.s1 << " p2: " << adata.s2 << endl;
return;
}
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
// sumcounts(tncount,p1-k,seq_alignment_lt[((v1^v2)>>(64u-IKMERSIZE*2u))&IKMERMASK]);
}
}
long l=(p1<p2?p1:p2);
// ldieif(p1<0 || p2<0,"negative sequence positions, p1: " + estr(p1) +" p2: "+p2);
// cout << "p1: " << p1 << " p2: " << p2 << " l: " << l << endl;
if (l<=0) {
adata.s1=p1-l;
adata.s2=p2-l;
return;
}
p1+=IKMERSIZE-l; p2+=IKMERSIZE-l; // adjust p1 and p2, such that we get a full kmer which includes the left end, we will mask the right side afterwards
v1=(p1>=32?pstr1[p1/32u-1u]>>(2u*(p1%32u)):0ul);
v2=(p2>=32?pstr2[p2/32u-1u]>>(2u*(p2%32u)):0ul);
v1|=(pstr1[p1/32u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
tmp[0]=seq_ident_table[((v1^v2)>>(62u-IKMERSIZE*2u)) & ((0x1ul<<(l*2ul))-1ul) & IKMERMASK];
// if (!galign && tmp[0]<l/2){
if (tmp[0]<l/2){
adata.s1=p1-IKMERSIZE+l;
adata.s2=p2-IKMERSIZE+l;
return;
}
// cout << long2str(v1&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
// cout << long2str(v2&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
adata.matches+=tmp[0]-(IKMERSIZE-l);
adata.mismatches+=IKMERSIZE-tmp[0];
adata.s1=p1-IKMERSIZE;
adata.s2=p2-IKMERSIZE;
// sumcounts(tncount,p1-IKMERSIZE,seq_alignment_lt[((v1^v2)>>(64u-IKMERSIZE*2u)) & IKMERMASK]&((0x1ul<<(l*8ul))-1ul));
// cout << "final p1: " << p1 << " p2: " << p2 << " l: " << l << endl;
/*
tmp[0]=seq_ident_table[((v1^v2)>>((32u-IKMERSIZE)*2u))&~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul)&IKMERMASK]; // put mask at end of 64bit int, i.e.: FF000000
cout << long2str((v1>>((32u-IKMERSIZE)*2u))&(~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul))&IKMERMASK) << " " << long2str((~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul))&IKMERMASK) << endl;
cout << long2str((v2>>((32u-IKMERSIZE)*2u))&~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul)&IKMERMASK) << " " << long2str(~((0x1ul<<(IKMERSIZE*2u-l*2ul))-1ul)&IKMERMASK) << endl;
matches+=tmp[0]-(IKMERSIZE-l);
mismatches+=IKMERSIZE-tmp[0];
*/
}
void seqident_seg_local_right(const eseq& s1,long p1,long e1,const eseq& s2,long p2,long e2,ealigndata& adata,ealignscore& as){
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s1.seq._str);
unsigned long *pstr2=reinterpret_cast<unsigned long*>(s2.seq._str);
unsigned long v1,v2;
unsigned char tmp[5];
int k;
int itmp;
// cout << "id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1<e1-32 && p2<e2-32; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
for (k=0; k<32u-IKMERSIZE; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
tmp[0]=seq_ident_table[(v1^v2)&IKMERMASK];
// if (!galign && tmp[0]<IKMERSIZE/2){
if (tmp[0]<IKMERSIZE/2){
adata.e1=p1+k;
adata.e2=p2+k;
return;
}
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
// sumcounts(tncount,p1+k,seq_alignment_lt[(v1^v2)&IKMERMASK]);
}
}
// cout << "rest id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1+IKMERSIZE<e1 && p2+IKMERSIZE<e2; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
for (k=0; p1+k+IKMERSIZE<e1 && p2+k+IKMERSIZE<e2; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
// cout << p1 << "+" << k << " " << kmer2str(v1) << endl;
// cout << p2 << "+" << k << " " << kmer2str(v2) << endl;
tmp[0]=seq_ident_table[(v1^v2)&IKMERMASK];
// if (!galign && tmp[0]<IKMERSIZE/2){
if (tmp[0]<IKMERSIZE/2){
adata.e1=p1+k;
adata.e2=p2+k;
return;
}
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
// sumcounts(tncount,p1+k,seq_alignment_lt[(v1^v2)&IKMERMASK]);
}
}
long l=(e1-p1<e2-p2?e1-p1:e2-p2);
// cout << "missing: " << p1 << " l: " << l << endl;
// ldieif(l<0,"error past end of sequence");
if (l<=0) {
adata.e1=p1+l;
adata.e2=p2+l;
return;
}
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
tmp[0]=seq_ident_table[(v1^v2)&((0x1ul<<(l*2ul))-1ul)];
// cout << long2str(v1&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
// cout << long2str(v2&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
// if (!galign && tmp[0]<(IKMERSIZE-l)/2) {
if (tmp[0]<(IKMERSIZE-l)/2) {
adata.e1=p1;
adata.e2=p2;
return;
}
adata.matches+=tmp[0]-(IKMERSIZE-l);
adata.mismatches+=IKMERSIZE-tmp[0];
// sumcounts(tncount,p1,seq_alignment_lt[(v1^v2)&IKMERMASK]&((0x1ul<<(l*8u))-1ul));
adata.e1=p1+l;
adata.e2=p2+l;
}
void seqident_seg_nogaps(const eseq& s1,long p1,long e1,const eseq& s2,long p2,long e2,ealigndata& adata,const ealignscore& as){
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s1.seq._str);
unsigned long *pstr2=reinterpret_cast<unsigned long*>(s2.seq._str);
unsigned long v1,v2;
unsigned char tmp;
int k;
int itmp;
ealignprofile tmpprof;
// cout << "id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1<e1-32 && p2<e2-32; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
for (k=0; k<32u-IKMERSIZE; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
tmp=seq_ident_table[(v1^v2)&IKMERMASK];
adata.matches+=tmp;
adata.mismatches+=IKMERSIZE-tmp;
tmpprof.add(AT_MATCH,tmp);
tmpprof.add(AT_MISS,IKMERSIZE-tmp);
adata._score+=tmp*as.match-(IKMERSIZE-tmp)*as.mismatch;
}
}
for (; p1+IKMERSIZE<e1 && p2+IKMERSIZE<e2; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
for (k=0; p1+k+IKMERSIZE<e1 && p2+k+IKMERSIZE<e2; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
tmp=seq_ident_table[(v1^v2)&IKMERMASK];
adata.matches+=tmp;
adata.mismatches+=IKMERSIZE-tmp;
tmpprof.add(AT_MATCH,tmp);
tmpprof.add(AT_MISS,IKMERSIZE-tmp);
adata._score+=tmp*as.match-(IKMERSIZE-tmp)*as.mismatch;
}
}
long l=(e1-p1<e2-p2?e1-p1:e2-p2);
// long ml=(e1-p1>e2-p2?e1-p1:e2-p2);
if (l<=0) return;
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
tmp=seq_ident_table[(v1^v2)&((0x1ul<<(l*2ul))-1ul)];
adata.matches+=tmp-(IKMERSIZE-l);
adata.mismatches+=IKMERSIZE-tmp;
adata._score+=(tmp-(IKMERSIZE-l))*as.match-(IKMERSIZE-tmp)*as.mismatch;
tmpprof.add(AT_MATCH,tmp-(IKMERSIZE-l));
tmpprof.add(AT_MISS,IKMERSIZE-tmp);
adata.profile.addinv(tmpprof);
}
void seqident_seg(const eseq& s1,long p1,long e1,const eseq& s2,long p2,long e2,ealigndata& adata,const ealignscore& as){
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s1.seq._str);
unsigned long *pstr2=reinterpret_cast<unsigned long*>(s2.seq._str);
unsigned long v1,v2;
unsigned char tmp[5];
int k;
int itmp;
// cout << "id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1<e1-32 && p2<e2-32; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
for (k=0; k<32u-IKMERSIZE; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
tmp[0]=seq_ident_table[(v1^v2)&IKMERMASK];
tmp[1]=seq_ident_table[(v1^(v2>>2ul))&IKMERMASK];
tmp[2]=seq_ident_table[(v1^(v2>>4ul))&IKMERMASK];
tmp[3]=seq_ident_table[(v2^(v1>>2ul))&IKMERMASK];
tmp[4]=seq_ident_table[(v2^(v1>>4ul))&IKMERMASK];
itmp=0;
for (int i=1; i<5; ++i){
if (tmp[i]>tmp[itmp]) itmp=i;
}
if (itmp!=0 && tmp[itmp]>tmp[0]+IKMERSIZE/4){
switch(itmp){
case 1: p2+=1; adata.gaps+=1; break;
case 2: p2+=2; adata.gaps+=2; break;
case 3: p1+=1; adata.gaps+=1; break;
case 4: p1+=2; adata.gaps+=2; break;
}
break;
}else{
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
adata._score+=tmp[0]*as.match-(IKMERSIZE-tmp[0])*as.mismatch;
}
}
}
// cout << "rest id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1+IKMERSIZE<e1 && p2+IKMERSIZE<e2; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
for (k=0; p1+k+IKMERSIZE<e1 && p2+k+IKMERSIZE<e2; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
// cout << p1 << "+" << k << " " << kmer2str(v1) << endl;
// cout << p2 << "+" << k << " " << kmer2str(v2) << endl;
tmp[0]=seq_ident_table[(v1^v2)&IKMERMASK];
tmp[1]=seq_ident_table[(v1^(v2>>2ul))&IKMERMASK];
tmp[2]=seq_ident_table[(v1^(v2>>4ul))&IKMERMASK];
tmp[3]=seq_ident_table[(v2^(v1>>2ul))&IKMERMASK];
tmp[4]=seq_ident_table[(v2^(v1>>4ul))&IKMERMASK];
itmp=0;
for (int i=1; i<5; ++i){
if (tmp[i]>tmp[itmp]) itmp=i;
}
if (itmp!=0 && tmp[itmp]>tmp[0]+IKMERSIZE/4){
switch(itmp){
case 1: p2+=1; adata.gaps+=1; break;
case 2: p2+=2; adata.gaps+=2; break;
case 3: p1+=1; adata.gaps+=1; break;
case 4: p1+=2; adata.gaps+=2; break;
}
break;
}else{
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
adata._score+=tmp[0]*as.match-(IKMERSIZE-tmp[0])*as.mismatch;
}
}
}
long l=(e1-p1<e2-p2?e1-p1:e2-p2);
long ml=(e1-p1>e2-p2?e1-p1:e2-p2);
if (e1!=s1.seqlen && e2!=s2.seqlen)
adata.gaps+=ml-l;
// cout << "missing: " << p1 << " l: " << l << endl;
if (l<=0) return;
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
tmp[0]=seq_ident_table[(v1^v2)&((0x1ul<<(l*2ul))-1ul)];
// cout << long2str(v1&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
// cout << long2str(v2&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
adata.matches+=tmp[0]-(IKMERSIZE-l);
adata.mismatches+=IKMERSIZE-tmp[0];
adata._score+=(tmp[0]-(IKMERSIZE-l))*as.match-(IKMERSIZE-tmp[0])*as.mismatch;
}
void seqident_seg(const eseq& s1,long p1,long e1,const eseq& s2,long p2,long e2,ealigndata& adata,unsigned char *tncount){
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s1.seq._str);
unsigned long *pstr2=reinterpret_cast<unsigned long*>(s2.seq._str);
unsigned long v1,v2;
unsigned char tmp[5];
int k;
int itmp;
// cout << "id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1<e1-32 && p2<e2-32; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
for (k=0; k<32u-IKMERSIZE; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
tmp[0]=seq_ident_table[(v1^v2)&IKMERMASK];
tmp[1]=seq_ident_table[(v1^(v2>>2ul))&IKMERMASK];
tmp[2]=seq_ident_table[(v1^(v2>>4ul))&IKMERMASK];
tmp[3]=seq_ident_table[(v2^(v1>>2ul))&IKMERMASK];
tmp[4]=seq_ident_table[(v2^(v1>>4ul))&IKMERMASK];
itmp=0;
for (int i=1; i<5; ++i){
if (tmp[i]>tmp[itmp]) itmp=i;
}
if (itmp!=0 && tmp[itmp]>tmp[0]+IKMERSIZE/4){
switch(itmp){
case 1: p2+=1; adata.gaps+=1; break;
case 2: p2+=2; adata.gaps+=2; break;
case 3: p1+=1; adata.gaps+=1; break;
case 4: p1+=2; adata.gaps+=2; break;
}
break;
}else{
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
sumcounts(tncount,p1+k,seq_alignment_lt[(v1^v2)&IKMERMASK]);
}
}
}
// cout << "rest id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1+IKMERSIZE<e1 && p2+IKMERSIZE<e2; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
for (k=0; p1+k+IKMERSIZE<e1 && p2+k+IKMERSIZE<e2; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
// cout << p1 << "+" << k << " " << kmer2str(v1) << endl;
// cout << p2 << "+" << k << " " << kmer2str(v2) << endl;
tmp[0]=seq_ident_table[(v1^v2)&IKMERMASK];
tmp[1]=seq_ident_table[(v1^(v2>>2ul))&IKMERMASK];
tmp[2]=seq_ident_table[(v1^(v2>>4ul))&IKMERMASK];
tmp[3]=seq_ident_table[(v2^(v1>>2ul))&IKMERMASK];
tmp[4]=seq_ident_table[(v2^(v1>>4ul))&IKMERMASK];
itmp=0;
for (int i=1; i<5; ++i){
if (tmp[i]>tmp[itmp]) itmp=i;
}
if (itmp!=0 && tmp[itmp]>tmp[0]+IKMERSIZE/4){
switch(itmp){
case 1: p2+=1; adata.gaps+=1; break;
case 2: p2+=2; adata.gaps+=2; break;
case 3: p1+=1; adata.gaps+=1; break;
case 4: p1+=2; adata.gaps+=2; break;
}
break;
}else{
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
sumcounts(tncount,p1+k,seq_alignment_lt[(v1^v2)&IKMERMASK]);
}
}
}
long l=(e1-p1<e2-p2?e1-p1:e2-p2);
long ml=(e1-p1>e2-p2?e1-p1:e2-p2);
if (e1!=s1.seqlen && e2!=s2.seqlen)
adata.gaps+=ml-l;
// cout << "missing: " << p1 << " l: " << l << endl;
if (l<=0) return;
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
tmp[0]=seq_ident_table[(v1^v2)&((0x1ul<<(l*2ul))-1ul)];
// cout << long2str(v1&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
// cout << long2str(v2&((0x1ul<<(l*2u))-1ul)) << " " << long2str(((0x1ul<<(l*2u))-1ul)) << endl;
adata.matches+=tmp[0]-(IKMERSIZE-l);
adata.mismatches+=IKMERSIZE-tmp[0];
sumcounts(tncount,p1,seq_alignment_lt[(v1^v2)&IKMERMASK]&((0x1ul<<(l*8u))-1ul));
}
void seqident_rev_seg(const eseq& s1,long p1,long e1,const eseq& s2,long p2,long e2,ealigndata& adata){
unsigned long *pstr1=reinterpret_cast<unsigned long*>(s1.seq._str);
unsigned long *pstr2=reinterpret_cast<unsigned long*>(s2.seq._str);
unsigned long v1,v2;
unsigned char tmp[5];
int k;
int itmp;
// cout << "id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1<e1-32 && p2<e2-32; p1+=k,p2+=k){
long rp2=s2.seqlen-p2-32;
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[rp2/32u]>>(2u*(rp2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[rp2/32u+1u]<<(64u-2u*(rp2%32u)))&safe_shift[rp2%32u];
for (k=0; k<32u-IKMERSIZE; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2<<=IKMERSIZE*2u){
tmp[0]=seq_ident_table[(v1^kmer_rev_lt[(v2>>((32u-IKMERSIZE)*2u))&IKMERMASK])&IKMERMASK];
tmp[1]=seq_ident_table[(v1^kmer_rev_lt[(v2>>((32u-IKMERSIZE)*2u-2u))&IKMERMASK])&IKMERMASK];
tmp[2]=seq_ident_table[(v1^kmer_rev_lt[(v2>>((32u-IKMERSIZE)*2u-4u))&IKMERMASK])&IKMERMASK];
tmp[3]=seq_ident_table[(kmer_rev_lt[(v2>>((32u-IKMERSIZE)*2u))&IKMERMASK]^(v1>>2ul))&IKMERMASK];
tmp[4]=seq_ident_table[(kmer_rev_lt[(v2>>((32u-IKMERSIZE)*2u))&IKMERMASK]^(v1>>4ul))&IKMERMASK];
itmp=0;
for (int i=1; i<5; ++i){
if (tmp[i]>tmp[itmp]) itmp=i;
}
if (itmp!=0 && tmp[itmp]>tmp[0]+IKMERSIZE/4){
switch(itmp){
case 1: p2+=1; adata.gaps+=1; break;
case 2: p2+=2; adata.gaps+=2; break;
case 3: p1+=1; adata.gaps+=1; break;
case 4: p1+=2; adata.gaps+=2; break;
}
break;
}else{
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
}
}
}
// cout << "rest id: " << p1 << "," << e1 << " :: " << p2 << "," <<e2 << " len1: " << e1-p1 << " len2: " << e2-p2 << endl;
for (; p1+IKMERSIZE<e1 && p2+IKMERSIZE<e2; p1+=k,p2+=k){
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
v1|=(pstr1[p1/32u+1u]<<(64u-2u*(p1%32u)))&safe_shift[p1%32u];
v2|=(pstr2[p2/32u+1u]<<(64u-2u*(p2%32u)))&safe_shift[p2%32u];
// cout << p1 << " " << long2str(v1) << endl;
// cout << p2 << " " << long2str(v2) << endl;
for (k=0; p1+k+IKMERSIZE<e1 && p2+k+IKMERSIZE<e2; k+=IKMERSIZE,v1>>=IKMERSIZE*2u,v2>>=IKMERSIZE*2u){
// cout << p1 << "+" << k << " " << kmer2str(v1) << endl;
// cout << p2 << "+" << k << " " << kmer2str(v2) << endl;
tmp[0]=seq_ident_table[(v1^v2)&IKMERMASK];
tmp[1]=seq_ident_table[(v1^(v2>>2ul))&IKMERMASK];
tmp[2]=seq_ident_table[(v1^(v2>>4ul))&IKMERMASK];
tmp[3]=seq_ident_table[(v2^(v1>>2ul))&IKMERMASK];
tmp[4]=seq_ident_table[(v2^(v1>>4ul))&IKMERMASK];
itmp=0;
for (int i=1; i<5; ++i){
if (tmp[i]>tmp[itmp]) itmp=i;
}
if (itmp!=0 && tmp[itmp]>tmp[0]+IKMERSIZE/4){
switch(itmp){
case 1: p2+=1; adata.gaps+=1; break;
case 2: p2+=2; adata.gaps+=2; break;
case 3: p1+=1; adata.gaps+=1; break;
case 4: p1+=2; adata.gaps+=2; break;
}
break;
}else{
adata.matches+=tmp[0]; adata.mismatches+=IKMERSIZE-tmp[0];
}
}
}
long l=(e1-p1<e2-p2?e1-p1:e2-p2);
// cout << "missing: " << p1 << " l: " << l << endl;
if (l<=0) return;
v1=pstr1[p1/32u]>>(2u*(p1%32u));
v2=pstr2[p2/32u]>>(2u*(p2%32u));
// cout << p1 << " " << long2str(v1) << endl;