-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparaSA.cpp
1359 lines (1209 loc) · 35.6 KB
/
paraSA.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<seqan/seeds.h>
//#include<seqan/include/seqan/seeds.h>
//#include<seqan/include/seqan/sequence.h>
#include<cstdlib>
#include<stdio.h>
#include <math.h>
#include <pthread.h>
#include <limits.h>
#include "fasta.hpp"
#include "paraSA.hpp"
using namespace seqan;
// seed extension part | seed content test
template<typename TSeed, typename TSeq>
void writeSeed(TSeed & seed, TSeq const & seq0, TSeq const & seq1)
{
cout << "Ref seed from position "<<leftPosition(seed, 0);
cout << " to " << rightPosition(seed, 0)<<" : ";
cout << infix(seq0, leftPosition(seed, 0), rightPosition(seed, 0)+1)<<endl;
cout << "query seed from position " <<leftPosition(seed, 1);
cout << " to " << rightPosition(seed, 1)<<" : ";
cout << infix(seq1, leftPosition(seed, 1), rightPosition(seed, 1)+1)<<endl;
cout <<endl;
}
// Order G0 | based on ref
bool compareg0(const mumi_unit lhs, const mumi_unit rhs)
{
if( lhs.g0init != rhs.g0init ) return lhs.g0init < rhs.g0init;
if( lhs.g0long != rhs.g0long ) return rhs.g0long < lhs.g0long;
return lhs.g1init < rhs.g1init;
};
// Order G1 | based on query
bool compareg1(const mumi_unit lhs, const mumi_unit rhs)
{
if( lhs.g1init != rhs.g1init ) return lhs.g1init < rhs.g1init;
if( lhs.g1long != rhs.g1long ) return rhs.g1long < lhs.g1long;
return lhs.g0init < rhs.g0init;
};
// Compute Mems identity.
bool ComputeMemIdentity(vector<GenomeClustInfo> &totalgenomes,
vector<Genome> &allpartgenomes,
vector<mumi_unit> &mumiunits,
long beginclust,
long id,
int MEMiden,
bool part,
long chunk,
char strand,
int ext, //extension options
int mas,
int umas,
int gapo,
int gape,
int drops)
{
double miniden;
double distance;
bool hitted = false;
long addsize, sid;
vector<mumi_unit> singleunits;
miniden=(double)MEMiden/100;
if (!part){
sid=beginclust+chunk;
}else{
sid=id;
}
for (long i=beginclust;i<sid;i++)
{
addsize=0;
singleunits.clear();
// Collect single ref genome matches.
for (long j=0;j<(long)mumiunits.size();j++){
if (mumiunits[j].ref==i){
singleunits.push_back(mumiunits[j]);
addsize+=mumiunits[j].g0long;
}
}
//if ((double)addsize/totalgenomes[id].size < miniden ){ continue; }
// Sort matches base on ref genome.
sort(singleunits.begin(), singleunits.end(),compareg0);
collectg0(singleunits);
// Sort matches base on query genome.
sort(singleunits.begin(), singleunits.end(),compareg1);
collectg1(singleunits);
vector<mumi_unit> removes;
// Remove symetrical matches.
Remove_symetrically(singleunits,removes);
sort(removes.begin(), removes.end(), compareg0);
doublecollectg0(removes);
sort(removes.begin(), removes.end(), compareg1);
doublecollectg1(removes);
vector<mumi_unit> dremoves;
Remove_symetrically(removes,dremoves);
sort(dremoves.begin(), dremoves.end(), compareg0);
trimendg0(dremoves);
//preG1_postG0
sort(dremoves.begin(), dremoves.end(),compareg1);
vector<mumi_unit> ddremoves;
Remove_symetrically(dremoves,ddremoves);
//treat_chG1
sort(ddremoves.begin(), ddremoves.end(),compareg1);
trimendg1(ddremoves);
//continue if no unit
if ((long)ddremoves.size()==0){ continue; }
//merge near units for extension
vector<mumi_unit> mergeunits;
merging(ddremoves, mergeunits);
if (ext!=0){
//seed extension part | checking and extending
seedextensions(mergeunits, allpartgenomes, id, i, strand,
ext, mas, umas, gapo, gape, drops);
}
distance=tell_me(mergeunits,totalgenomes[id].size);
//loading clustering info
if (distance >= miniden){
hit thit;
thit.id=i;
thit.strand=strand;
thit.identity=distance;
totalgenomes[id].clusters.push_back(thit);
hitted=true;
if (!part){ break; } // Hitted and exit
} // Do MuMi computing.
}
return hitted;
}
// Collect ref matches.
void collectg0(vector<mumi_unit> &singleunits)
{
long j, i=0;
while (i<(long)(singleunits.size()-1))
{
if (singleunits[i].g0fin >= singleunits[i+1].g0fin)
{
singleunits[i+1].dgdelete=true;
j=i;
j++;
while ((j<(long)(singleunits.size()-1))&&\
(singleunits[i].g0fin >= singleunits[j+1].g0fin))
{
singleunits[j+1].dgdelete=true;
j++;
}
i=j+1;
}else{
i++;
}
}
}
// Collect query matches.
void collectg1(vector<mumi_unit> &singleunits)
{
long j, i=0;
while (i<(long)(singleunits.size()-1))
{
if (singleunits[i].g1fin >= singleunits[i+1].g1fin)
{
singleunits[i+1].dgdelete=true;
j=i;
j++;
while ((j<(long)(singleunits.size()-1))&&\
(singleunits[i].g1fin >= singleunits[j+1].g1fin))
{
singleunits[j+1].dgdelete=true;
j++;
}
i=j+1;
}else{
i++;
}
}
}
// Remove symetrical matches.
void Remove_symetrically(vector<mumi_unit> &singleunits,
vector<mumi_unit> &removes)
{
for (long i=0;i<(long)singleunits.size();i++)
{
if (!singleunits[i].dgdelete){
removes.push_back(singleunits[i]);
}
}
}
// Double collect ref matches.
void doublecollectg0(vector<mumi_unit> &removes)
{
for (long i=removes.size()-2;i>0;i--)
{
if ( (removes[i-1].g0fin >= removes[i+1].g0init-1)&&\
(removes[i].g0fin <= removes[i+1].g0fin))
{
removes[i].dgdelete=true;
}
}
}
// Double collecting ...
void doublecollectg1(vector<mumi_unit> &removes)
{
for (long i=removes.size()-2;i>0;i--)
{
if ( (removes[i-1].g1fin >= removes[i+1].g1init-1)&&\
(removes[i].g1fin <= removes[i+1].g1fin))
{
removes[i].dgdelete=true;
}
}
}
// Trim match part.
void trimendg0(vector<mumi_unit> &dremoves)
{
long cptrim=0, tmplen=0;
for (long i=(dremoves.size()-2);i>=0;i--)
{
if (dremoves[i].g0fin >= dremoves[i+1].g0init)
{
cptrim++;
tmplen=dremoves[i].g0fin-dremoves[i+1].g0init+1;
dremoves[i].g0fin=dremoves[i+1].g0init-1;
dremoves[i].g0long=dremoves[i].g0fin-dremoves[i].g0init+1;
if (dremoves[i].g1sens==1){
dremoves[i].g1fin=dremoves[i].g1fin-tmplen;
dremoves[i].g1long=dremoves[i].g1fin-dremoves[i].g1init+1;
}else{
dremoves[i].g1init=dremoves[i].g1init+tmplen;
dremoves[i].g1long=dremoves[i].g1fin-dremoves[i].g1init+1;
}
}
}
}
// Trim ...
void trimendg1(vector<mumi_unit> &dremoves)
{
long cptrim=0, tmplen=0;
for (long i=(dremoves.size()-2);i>=0;i--)
{
if (dremoves[i].g1fin >= dremoves[i+1].g1init)
{
cptrim++;
tmplen=dremoves[i].g1fin-dremoves[i+1].g1init+1;
dremoves[i].g1fin=dremoves[i+1].g1init-1;
dremoves[i].g1long=dremoves[i].g1fin-dremoves[i].g1init+1;
if (dremoves[i].g1sens==1){
dremoves[i].g0fin=dremoves[i].g0fin-tmplen;
dremoves[i].g0long=dremoves[i].g0fin-dremoves[i].g0init+1;
}else{
dremoves[i].g0init=dremoves[i].g0init+tmplen;
dremoves[i].g0long=dremoves[i].g0fin-dremoves[i].g0init+1;
}
}
}
}
//merge near units.
void merging(vector<mumi_unit> &ddremoves,
vector<mumi_unit> &mergeunits)
{
//merge units gap <= 3bps
mumi_unit tmu, tmu0, tmu1;
tmu = ddremoves[0];
//only single unit
if (ddremoves.size()==1)
{
mergeunits.push_back(tmu);
return;
}
//merge part
for (long i=1; i<(long)ddremoves.size();i++)
{
tmu0=ddremoves[i];
if ( (((tmu0.g0init-tmu.g0fin)>0)&&((tmu0.g0init-tmu.g0fin)<=4))&&\
((tmu0.g1init-tmu.g1fin)<=4))
{
tmu1=tmu;
tmu1.g0long=tmu.g0long+tmu0.g0long;
tmu1.g0fin=tmu0.g0fin;
tmu1.g1long=tmu1.g0long;
tmu1.g1fin=tmu0.g1fin;
tmu=tmu1;
}else{
mergeunits.push_back(tmu);
tmu=tmu0;
}
}
//last one unit
mergeunits.push_back(tmu);
return;
}
// Get distance.
double tell_me(vector<mumi_unit> &ddremoves,long size)
{
long add=0;
for (long i=0;i<(long)ddremoves.size();i++)
{
add+=ddremoves[i].g1long;
}
return (double)add/size;
}
// Seed extension with two directions
void seedextensions(vector<mumi_unit> &ddremoves,
vector<Genome> &allpartgenomes,
long id,
long iref,
char strand,
int ext, //extension options
int mas,
int umas,
int gapo,
int gape,
int drops)
{
// Total Units
long units=(long)ddremoves.size();
long g1leftband, g1rightband;
long g1leftbegin=0;
long g0infixbegin, g1infixbegin;
long g0infixend, g1infixend;
long seedfrom, seedto;
long extensionlen;
long g1rightend;
mumi_unit tmu0;
// For right extension
//g1rightend = allpartgenomes[id].size-1;
// Get DNA content
DnaString seq0, seq1;
if(allpartgenomes.size() != 0)
{//add judgment statement for variable allpartgenomes
seq0 = allpartgenomes[iref].cont;
seq1 = allpartgenomes[id].cont;
}
else
{
return;
}
// Get reverse complement DNA sequence
if (strand == '-')
{
reverseComplement(seq1);
}
// left seed extension part
for (long i=0; i<units;i++)
{
/*
cout<<ddremoves[i].ref<<"\t"<<
id<<"\t"<<
ddremoves[i].g0sens<<"\t"<<
ddremoves[i].g0init<<"\t"<<
ddremoves[i].g0long<<"\t"<<
ddremoves[i].g0fin<<"\t"<<
ddremoves[i].g1sens<<"\t"<<
ddremoves[i].g1init<<"\t"<<
ddremoves[i].g1long<<"\t"<<
ddremoves[i].g1fin<<"\t"<<" | ";
//cout<<endl;
*/
tmu0 = ddremoves[i];
//left extension
if ( (tmu0.g1init-2) > g1leftbegin )
{
g1infixbegin = g1leftbegin;
//length of candidate extension part
g1leftband=tmu0.g1init-g1leftbegin-1;
if (tmu0.g0init > (g1leftband+10))
{
g0infixbegin=tmu0.g0init-g1leftband-10-1;
}else{
g0infixbegin=0;
}
g1infixend=tmu0.g1init;
g0infixend=tmu0.g0init;
//get infix part instead whole sequence for extension
typedef Infix<DnaString>::Type TInfix;
TInfix infix0 = infix(seq0, g0infixbegin, g0infixend);
TInfix infix1 = infix(seq1, g1infixbegin, g1infixend);
long seedbegin0;
if (g0infixbegin==0)
{
seedbegin0=g0infixend-1;
}else{
seedbegin0=g1leftband+10;
}
typedef Seed<> TSeed;
TSeed seed(seedbegin0, g1leftband, 1);
seedfrom=leftPosition(seed, 1);
//extension parameters
typedef int TScore;
//Score<TScore, Simple> scoreMatrix(1, -1, -1);
//TScore scoreDropOff = 1;
Score<TScore, Simple> scoreMatrix(mas, umas, gape, gapo);
TScore scoreDropOff = drops;
//Gapped or ungapped extension
if (ext==1){
extendSeed(seed, scoreDropOff, scoreMatrix, infix0, infix1, 0, GappedXDrop());
}else{
extendSeed(seed, scoreDropOff, scoreMatrix, infix0, infix1, 0, UngappedXDrop());
}
seedto=leftPosition(seed, 1);
extensionlen=seedfrom-seedto;
ddremoves[i].g1init-=extensionlen;
ddremoves[i].g1long+=extensionlen;
//global alignment of extension part | if need
/*
Align<Infix<DnaString>::Type > align;
resize(rows(align), 2);
assignSource(row(align, 0), infix(infix0, leftPosition(seed, 0), \
rightPosition(seed, 0)+1));
assignSource(row(align, 1), infix(infix1, leftPosition(seed, 1), \
rightPosition(seed, 1)+1));
std::cout << std::endl << "Banded Alignment:" << std::endl;
std::cout << "Score: " << globalAlignment(align, stringSet(align), scoreMatrix, \
-leftDiagonal(seed) - 2, -rightDiagonal(seed) + 2, \
BandedNeedlemanWunsch()) << std::endl;
std::cout << align;
*/
}
g1leftbegin = tmu0.g1fin;
}
//right extension part
g1rightend = allpartgenomes[id].size-1;
for (long i=(units-1); i>=0; i--)
{
tmu0 = ddremoves[i];
//right extension
//do extension only when gap >=3
if ( (tmu0.g1fin+2) < g1rightend )
{
g1infixbegin = tmu0.g1fin-1;
g0infixbegin = tmu0.g0fin-1;
g1rightband=g1rightend-tmu0.g1fin+2;
g0infixend = g0infixbegin+g1rightband+10;
g1infixend = g1infixbegin+g1rightband;
typedef Infix<DnaString>::Type TInfix;
TInfix infix0 = infix(seq0, g0infixbegin, g0infixend);
TInfix infix1 = infix(seq1, g1infixbegin, g1infixend);
typedef Seed<> TSeed;
TSeed seed(0, 0, 1);
//extension parameters
typedef int TScore;
//Score<TScore, Simple> scoreMatrix(1, -1, -1);
//TScore scoreDropOff = 1;
Score<TScore, Simple> scoreMatrix(mas, umas, gape, gapo);
TScore scoreDropOff = drops;
//Gapped or ungapped extension
if (ext==1){
extendSeed(seed, scoreDropOff, scoreMatrix, infix0, infix1, 1, GappedXDrop());
}else{
extendSeed(seed, scoreDropOff, scoreMatrix, infix0, infix1, 1, UngappedXDrop());
}
extensionlen=rightPosition(seed, 1);
//update unit info
ddremoves[i].g1fin+=extensionlen;
ddremoves[i].g1long+=extensionlen;
}
g1rightend = tmu0.g1init-2;
}
}
paraSA::paraSA(string &S_,
vector<long> &descr_,
vector<long> &startpos_,
bool __4column,
long K_) : descr(descr_), startpos(startpos_), S(S_)
{
_4column = __4column;
K = K_;
if(S.length() % K != 0) {
long appendK = K - S.length() % K ;
for(long i = 0; i < appendK; i++) S += '$';
}
// Make sure last K-sampled characeter is this special character as well!!
// Append "special" end character. Note: It must be lexicographically less.
for(long i = 0; i < K; i++) S += '$';
N = S.length();
// Sparse suffix array construction part
if(K > 1)
{
long bucketNr = 1;
int *intSA = new int[N/K+1]; for(int i = 0; i < N/K; i++) intSA[i] = i; // Init SA.
int* t_new = new int[N/K+1];
long* BucketBegin = new long[256]; // array to save current bucket beginnings
radixStep(t_new, intSA, bucketNr, BucketBegin, 0, N/K-1, 0); // start radix sort
t_new[N/K] = 0; // Terminate new integer string.
delete[] BucketBegin;
// Suffix sort integer text.
cerr<<"suffix sorting ...."<<endl;
suffixsort(t_new, intSA, N/K, bucketNr, 0);
cerr <<"suffix sorting done ...." << endl;
delete[] t_new;
// Translate suffix array.
SA.resize(N/K);
for (long i=0; i<N/K; i++) SA[i] = (unsigned int)intSA[i+1] * K;
delete[] intSA;
// Build ISA using sparse SA.
ISA.resize(N/K);
for(long i = 0; i < N/K; i++) { ISA[SA[i]/K] = i; }
}else{
SA.resize(N);
ISA.resize(N);
int char2int[UCHAR_MAX+1]; // Map from char to integer alphabet.
// Zero char2int mapping.
for (int i=0; i<=UCHAR_MAX; i++) char2int[i]=0;
// Determine which characters are used in the string S.
for (long i = 0; i < N; i++) {
char2int[(int)S[i]]=1;
}
// Count the size of the alphabet.
int alphasz = 0;
for(int i=0; i <= UCHAR_MAX; i++) {
if (char2int[i]) char2int[i]=alphasz++;
else char2int[i] = -1;
}
// Remap the alphabet.
for(long i = 0; i < N; i++) ISA[i] = (int)S[i];
for (long i = 0; i < N; i++) {
ISA[i]=char2int[ISA[i]] + 1;
}
// First "character" equals 1 because of above plus one, l=1 in suffixsort().
int alphalast = alphasz + 1;
// Use LS algorithm to construct the suffix array.
int *SAint = (int*)(&SA[0]);
cerr<<"suffix sorting ...."<<endl;
suffixsort(&ISA[0], SAint , N-1, alphalast, 1);
cerr <<"suffix sorting done ...." << endl;
}
cerr << "N=" << N << endl;
// Adjust to "sampled" size.
logN = (long)ceil(log(N/K) / log(2.0));
cerr<<"logN \t"<<logN<<endl;
LCP.resize(N/K);
cerr << "N/K=" << N/K << endl;
// Use algorithm by Kasai et al to construct LCP array.
computeLCP(); // SA + ISA -> LCP
LCP.init();
NKm1 = N/K-1;
cerr<< "NKm1= "<<NKm1<<endl;
}
// Suffix sort part.
void paraSA::update_group(int *pl, int *pm)
{
int g;
g=pm-I; /* group number.*/
V[*pl]=g; /* update group number of first position.*/
if (pl==pm)
*pl=-1; /* one element, sorted group.*/
else
do /* more than one element, unsorted group.*/
V[*++pl]=g; /* update group numbers.*/
while (pl<pm);
}
void paraSA::select_sort_split(int *p, int n)
{
int *pa, *pb, *pi, *pn;
int f, v;
pa=p; /* pa is start of group being picked out.*/
pn=p+n-1; /* pn is last position of subarray.*/
while (pa<pn) {
for (pi=pb=pa+1, f=KEY(pa); pi<=pn; ++pi)
if ((v=KEY(pi))<f) {
f=v; /* f is smallest key found.*/
SWAP(pi, pa); /* place smallest element at beginning.*/
pb=pa+1; /* pb is position for elements equal to f.*/
} else if (v==f) { /* if equal to smallest key.*/
SWAP(pi, pb); /* place next to other smallest elements.*/
++pb;
}
update_group(pa, pb-1); /* update group values for new group.*/
pa=pb; /* continue sorting rest of the subarray.*/
}
if (pa==pn) { /* check if last part is single element.*/
V[*pa]=pa-I;
*pa=-1; /* sorted group.*/
}
}
int paraSA::choose_pivot(int *p, int n)
{
int *pl, *pm, *pn;
int s;
pm=p+(n>>1); /* small arrays, middle element.*/
if (n>7) {
pl=p;
pn=p+n-1;
if (n>40) { /* big arrays, pseudomedian of 9.*/
s=n>>3;
pl=MED3(pl, pl+s, pl+s+s);
pm=MED3(pm-s, pm, pm+s);
pn=MED3(pn-s-s, pn-s, pn);
}
pm=MED3(pl, pm, pn); /* midsize arrays, median of 3.*/
}
return KEY(pm);
}
void paraSA::sort_split(int *p, int n)
{
int *pa, *pb, *pc, *pd, *pl, *pm, *pn;
int f, v, s, t;
if (n<7) { /* multi-selection sort smallest arrays.*/
select_sort_split(p, n);
return;
}
v=choose_pivot(p, n);
pa=pb=p;
pc=pd=p+n-1;
while (1) { /* split-end partition.*/
while (pb<=pc && (f=KEY(pb))<=v) {
if (f==v) {
SWAP(pa, pb);
++pa;
}
++pb;
}
while (pc>=pb && (f=KEY(pc))>=v) {
if (f==v) {
SWAP(pc, pd);
--pd;
}
--pc;
}
if (pb>pc)
break;
SWAP(pb, pc);
++pb;
--pc;
}
pn=p+n;
if ((s=pa-p)>(t=pb-pa))
s=t;
for (pl=p, pm=pb-s; s; --s, ++pl, ++pm)
SWAP(pl, pm);
if ((s=pd-pc)>(t=pn-pd-1))
s=t;
for (pl=pb, pm=pn-s; s; --s, ++pl, ++pm)
SWAP(pl, pm);
s=pb-pa;
t=pd-pc;
if (s>0)
sort_split(p, s);
update_group(p+s, p+n-t-1);
if (t>0)
sort_split(p+n-t, t);
}
void paraSA::bucketsort(int *x, int *p, int n, int k)
{
int *pi, i, c, d, g;
for (pi=p; pi<p+k; ++pi)
*pi=-1; /* mark linked lists empty.*/
for (i=0; i<=n; ++i) {
x[i]=p[c=x[i]]; /* insert in linked list.*/
p[c]=i;
}
for (pi=p+k-1, i=n; pi>=p; --pi) {
d=x[c=*pi]; /* c is position, d is next in list.*/
x[c]=g=i; /* last position equals group number.*/
if (d>=0) { /* if more than one element in group.*/
p[i--]=c; /* p is permutation for the sorted x.*/
do {
d=x[c=d]; /* next in linked list.*/
x[c]=g; /* group number in x.*/
p[i--]=c; /* permutation in p.*/
} while (d>=0);
} else
p[i--]=-1; /* one element, sorted group.*/
}
}
int paraSA::transform(int *x, int *p, int n, int k, int l, int q)
{
int b, c, d, e, i, j, m, s;
int *pi, *pj;
for (s=0, i=k-l; i; i>>=1)
++s; /* s is number of bits in old symbol.*/
e=INT_MAX>>s; /* e is for overflow checking.*/
for (b=d=r=0; r<n && d<=e && (c=d<<s|(k-l))<=q; ++r) {
b=b<<s|(x[r]-l+1); /* b is start of x in chunk alphabet.*/
d=c; /* d is max symbol in chunk alphabet.*/
}
m=(1<<(r-1)*s)-1; /* m masks off top old symbol from chunk.*/
x[n]=l-1; /* emulate zero terminator.*/
if (d<=n) { /* if bucketing possible, compact alphabet.*/
for (pi=p; pi<=p+d; ++pi)
*pi=0; /* zero transformation table.*/
for (pi=x+r, c=b; pi<=x+n; ++pi) {
p[c]=1; /* mark used chunk symbol.*/
c=(c&m)<<s|(*pi-l+1); /* shift in next old symbol in chunk.*/
}
for (i=1; i<r; ++i) { /* handle last r-1 positions.*/
p[c]=1; /* mark used chunk symbol.*/
c=(c&m)<<s; /* shift in next old symbol in chunk.*/
}
for (pi=p, j=1; pi<=p+d; ++pi)
if (*pi)
*pi=j++; /* j is new alphabet size.*/
for (pi=x, pj=x+r, c=b; pj<=x+n; ++pi, ++pj) {
*pi=p[c]; /* transform to new alphabet.*/
c=(c&m)<<s|(*pj-l+1); /* shift in next old symbol in chunk.*/
}
while (pi<x+n) { /* handle last r-1 positions.*/
*pi++=p[c]; /* transform to new alphabet.*/
c=(c&m)<<s; /* shift right-end zero in chunk.*/
}
} else { /* bucketing not possible, don't compact.*/
for (pi=x, pj=x+r, c=b; pj<=x+n; ++pi, ++pj) {
*pi=c; /* transform to new alphabet.*/
c=(c&m)<<s|(*pj-l+1); /* shift in next old symbol in chunk.*/
}
while (pi<x+n) { /* handle last r-1 positions.*/
*pi++=c; /* transform to new alphabet.*/
c=(c&m)<<s; /* shift right-end zero in chunk.*/
}
j=d+1; /* new alphabet size.*/
}
x[n]=0; /* end-of-string symbol is zero.*/
return j; /* return new alphabet size.*/
}
// LS suffix sorter (integer alphabet).
void paraSA::suffixsort(int *x, int *p, int n, int k, int l)
{
int *pi, *pk;
int i, j, s, sl;
V=x; /* set global values.*/
I=p;
if (n>=k-l)
{ /* if bucketing possible,*/
j=transform(V, I, n, k, l, n);
bucketsort(V, I, n, j); /* bucketsort on first r positions.*/
} else
{
transform(V, I, n, k, l, INT_MAX);
for (i=0; i<=n; ++i)
I[i]=i; /* initialize I with suffix numbers.*/
h=0;
sort_split(I, n+1); /* quicksort on first r positions.*/
}
h=r; /* number of symbols aggregated by transform.*/
while (*I>=-n)
{
pi=I; /* pi is first position of group.*/
sl=0; /* sl is negated length of sorted groups.*/
do {
if ((s=*pi)<0)
{
pi-=s; /* skip over sorted group.*/
sl+=s; /* add negated length to sl.*/
} else {
if (sl)
{
*(pi+sl)=sl; /* combine sorted groups before pi.*/
sl=0;
}
pk=I+V[s]+1; /* pk-1 is last position of unsorted group.*/
sort_split(pi, pk-pi);
pi=pk; /* next group.*/
}
} while (pi<=I+n);
if (sl) /* if the array ends with a sorted group.*/
*(pi+sl)=sl; /* combine sorted groups at end of I.*/
h=2*h; /* double sorted-depth.*/
}
for (i=0; i<=n; ++i) /* reconstruct suffix array from inverse.*/
I[V[i]]=i;
}
// End of suffix sort code.
// Uses the algorithm of Kasai et al 2001 which was described in
// Manzini 2004 to compute the LCP array. Modified to handle sparse
// suffix arrays and inverse sparse suffix arrays.
void paraSA::computeLCP()
{
long h=0;
for(long i = 0; i < N; i+=K)
{
long m = ISA[i/K];
if(m==0) LCP.set(m, 0); // LCP[m]=0;
else{
long j = SA[m-1];
while(i+h < N && j+h < N && S[i+h] == S[j+h]) h++;
LCP.set(m, h); //LCP[m] = h;
}
h = max(0L, h - K);
}
}
// Implements a variant of American flag sort (McIlroy radix sort).
// Recurse until big-K size prefixes are sorted. Adapted from the C++
// source code for the wordSA implementation from the following paper:
// Ferragina and Fischer. Suffix Arrays on Words. CPM 2007.
void paraSA::radixStep(int *t_new,
int *SA,
long &bucketNr,
long *BucketBegin,
long l,
long r,
long h)
{
if(h >= K) return;
// first pass: count
vector<long> Sigma(256, 0); // Sigma counts occurring characters in bucket
// count characters
for (long i = l; i <= r; i++) Sigma[ S[ SA[i]*K + h ] ]++;
BucketBegin[0] = l;
for (long i = 1; i < 256; i++)
{
BucketBegin[i] = Sigma[i-1] + BucketBegin[i-1];
} // accumulate
// second pass: move (this variant does *not* need an additional array!)
unsigned char currentKey = 0; // character of current bucket
long end = l-1+Sigma[currentKey]; // end of current bucket
long pos = l; // 'pos' is current position in bucket
while (1)
{
if (pos > end)
{ // Reached the end of the bucket.
if (currentKey == 255) break; // Last character?
currentKey++; // Advance to next characer.
pos = BucketBegin[currentKey]; // Next bucket start.
end += Sigma[currentKey]; // Next bucket end.
}else{
// American flag sort of McIlroy et al. 1993. BucketBegin keeps
// track of current position where to add to bucket set.
int tmp = SA[ BucketBegin[ S[ SA[pos]*K + h ] ] ];
// Move bucket beginning to the right, and replace
SA[ BucketBegin[ S[ SA[pos]*K + h] ]++ ] = SA[pos];
SA[ pos ] = tmp; // Save value at bucket beginning.
// Advance to next position if the right character.
if (S[ SA[pos]*K + h ] == currentKey) pos++;
}
}
// recursively refine buckets and calculate new text:
long beg = l; end = l-1;
for (long i = 1; i < 256; i++)
{ // step through Sigma to find bucket borders
end += Sigma[i];
if (beg <= end)
{
if(h == K-1)
{
for (long j = beg; j <= end; j++)
{
t_new[ SA[j] ] = bucketNr; // set new text
}
bucketNr++;
}else {
// recursive refinement
radixStep(t_new, SA, bucketNr, BucketBegin, beg, end, h+1);
}
beg = end + 1; // advance to next bucket
}
}
}
// Binary search for left boundry of interval.
long paraSA::bsearch_left(char c, long i, long s, long e)
{
if(c == S[SA[s]+i]) return s;
long l = s, r = e;
while (r - l > 1)
{
long m = (l+r) / 2;
if (c <= S[SA[m] + i]) r = m;
else l = m;
}
return r;
}
// Binary search for right boundry of interval.
long paraSA::bsearch_right(char c, long i, long s, long e)
{
if(c == S[SA[e]+i]) return e;
long l = s, r = e;
while (r - l > 1)
{
long m = (l+r) / 2;
if (c < S[SA[m] + i]) r = m;
else l = m;
}
return l;
}
// Simple top down traversal of a suffix array.
bool paraSA::top_down(char c, long i, long &start, long &end)
{
if(c < S[SA[start]+i]) return false;
if(c > S[SA[end]+i]) return false;
long l = bsearch_left(c, i, start, end);
long l2 = bsearch_right(c, i, start, end);
start = l; end = l2;
return l <= l2;
}
// Top down traversal of the suffix array to match a pattern. NOTE:
// NO childtab as in the enhanced suffix array (ESA).
bool paraSA::search(string &P, long &start, long &end)
{
start = 0; end = N - 1;
long i = 0;
while(i < (long)P.length())
{
if(top_down(P[i], i, start, end) == false)
{
return false;
}
i++;
}
return true;