-
Notifications
You must be signed in to change notification settings - Fork 0
/
sureMap.cpp
2377 lines (2228 loc) · 81.2 KB
/
sureMap.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
/**
* @file sureMap.cpp
* @author MohammadJavad Rezaei Seraji <mjrezaei (at) ce.sharif.edu>
*
* @section LICENCE
*
*
* Copyright (C) 2017-2020
* MohammadJavad Rezaei Seraji <mjrezaei (at) ce.sharif.edu>
* Seyed Abolfazl Motahari <motahari (at) sharif.edu
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
**/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#include <memory.h>
#include <chrono>
#include <set>
#include <fstream>
#include <unordered_map>
#include <queue>
#include <unordered_map>
#include <mutex>
#include <thread>
#include <boost/atomic.hpp>
#include <string>
#include <iostream>
#include <algorithm>
#include <dirent.h>
#include <iomanip>
#include <sstream>
#include "BWT2.h"
#define pci pair< char, int >
#define NV N[v]
#define MAXTHREADS 40
#define MAXSTATE 100000
#define MAXTOT MAXSTATE
#define MAXINROW MAXSTATE
#define MAXINPLEN 100000
#define MAXINDXES 1000000
#define pll pair< long long, long long >
#define puu std::pair< unsigned int, unsigned int >
#define shift 21
#define seed1 1000000007
#define seed2 1000019
#define seed3 100000007
#define P pair< int, int >
#define PLI pair< long long, int >
#define MAXREADSIZE 100000
#define MAXREPORTSIZE 409600
#define MAXSHORTREADLEN 300
#define MAXMASK 500
#define psu pair< string, uint32_t >
#define MAX_WRITER_THREAD 1
#define INF ( 1 << 22 )
#define pInterval pair< intervalNode, intervalNode >
#define retVal pair< bool, bwtNode >
#define pss pair< string, string >
#pragma comment(linker, "/STACK:500000000")
using namespace std;
struct compressedArray{
unsigned long long* row = NULL;
unsigned long long HASHCode;
unsigned short rowSize;
compressedArray(){}
compressedArray( vector<unsigned short> _row, unsigned short _base ){
unsigned short base = _base;
unsigned char cellWidth = 0;
unsigned char cntInRow = 0;
while( ( 1 << cellWidth ) - 1 < base + 1 )
cellWidth++;
HASHCode = 0;
cntInRow = 64 / cellWidth;
rowSize = ( _row.size() + cntInRow - 1 ) / ( cntInRow );
row = new unsigned long long[rowSize];
for( int i = 0; i < rowSize; i++ )
row[i] = 0;
for( int i = 0; i < _row.size(); i += cntInRow ){
for( int j = min( (int)_row.size() - 1, (int)( i + cntInRow - 1 ) ); j >= i; j-- ){
int idx = i / ( cntInRow );
row[idx] <<= ( cellWidth );
row[idx] += _row[j];
}
}
for( int i = 0; i < rowSize; i++ )
HASHCode = HASHCode * seed3 + row[i];
}
compressedArray( const compressedArray& m ){
HASHCode = m.HASHCode;
rowSize = m.rowSize;
row = new unsigned long long[rowSize];
for( int i = 0; i < rowSize; i++ )
row[i] = m.row[i];
}
unsigned short getCell( int pos, unsigned short base ){
unsigned char cellWidth = 0;
unsigned char cntInRow = 0;
while( ( 1 << cellWidth ) - 1 < base + 1 )
cellWidth++;
cntInRow = 64 / cellWidth;
int idx = pos / ( cntInRow );
return ( row[idx] >> (cellWidth * ( pos % cntInRow ) ) ) & ( ( 1 << cellWidth ) - 1 ) ;
}
vector<unsigned short> getRow( unsigned short base ){
vector<unsigned short> ret( 2 * base + 1 );
for( int i = 0; i < ret.size(); i++ )
ret[i] = getCell( i, base );
return ret;
}
bool operator == ( const compressedArray& m ) const{
return HASHCode == m.HASHCode;
}
compressedArray& operator=(compressedArray arg) // copy/move constructor is called to construct arg
{
HASHCode = arg.HASHCode;
rowSize = arg.rowSize;
row = new unsigned long long[rowSize];
for( int i = 0; i < rowSize; i++ )
row[i] = arg.row[i];
return *this;
}
int getHachCode(){
return HASHCode;
}
~compressedArray(){
delete[] row;
}
};
struct HASHNode{
compressedArray to;
unsigned long long mask;
unsigned long long m2;
bool isValid;
int base;
HASHNode( ){}
HASHNode( compressedArray _to, unsigned long long _mask, int _base, bool _isValid ){
to = _to, mask = _mask, base = _base, isValid = _isValid;
}
};
struct bwtNode{
unsigned int first, second, len;
int acceptedValue;
uint32_t refRow;
char flag;
bwtNode( unsigned int _f, unsigned int _s, unsigned int _l, int _ac ){
first = _f, second = _s, len = _l, acceptedValue = _ac;
}
bwtNode(){}
bool operator < ( const bwtNode& m ) const{
if( first != m.first )
return first < m.first;
if( second != m.second )
return second < m.second;
return len < m.len;
}
bool operator == ( const bwtNode& m ) const{
return first == m.first && second == m.second && len == m.len;
}
void print(){
cerr << first << ' ' << second << ' ' << len << ' ' << acceptedValue << ' ' << endl;
}
};
struct mappingInfo{
string qName, read, QC;
uint32_t refPos;
psu cigar;
int len, acceptedValue, flag;
int startPos;
int order;
mappingInfo(){}
mappingInfo( string _q, string _r, uint32_t _rf, int _len, int _ac, int _flag, string _QC, psu _c = psu( "", 0 ) ){
qName = _q, read = _r, refPos = _rf, len = _len, acceptedValue = _ac, flag = _flag, QC = _QC, cigar = _c;
}
void print(){
cerr << qName << endl << read << ' ' << QC << endl;
cerr << refPos << ' ' << len << ' ' << acceptedValue << ' ' << flag << endl;
}
bool operator < ( const mappingInfo& m ) const{
return order < m.order;
}
};
struct intervalNode{
unsigned int left, right;
int editDistance, leftToRight;
int mask;
intervalNode(){}
intervalNode( unsigned int _l, unsigned int _r, int _ed, int _lr, int _m ){
left = _l, right = _r, editDistance = _ed, leftToRight = _lr, mask = _m;
}
};
struct nextState{
int acceptedValue;
vector<unsigned short> nextRowDp;
bool valid;
puu nxInterval;
nextState( int edit ){
nextRowDp.resize( 2 * edit + 1, edit + 1 );
valid = false;
acceptedValue = -1;
}
nextState(){}
bool operator < ( const nextState& m ) const{
if( acceptedValue == -1 )
return false;
if( m.acceptedValue == -1 )
return true;
return acceptedValue < m.acceptedValue;
}
};
struct nextState3{
int acceptedValue;
compressedArray nextRowDp;
bool valid;
puu nxInterval;
nextState3( int edit ){
valid = false;
acceptedValue = -1;
}
nextState3(){}
bool operator < ( const nextState3& m ) const{
if( acceptedValue == -1 )
return false;
if( m.acceptedValue == -1 )
return true;
return acceptedValue < m.acceptedValue;
}
};
struct nextState2{
int acceptedValue, error;
bool valid;
puu nxInterval;
nextState2( int edit ){
valid = false;
acceptedValue = -1;
}
nextState2(){}
bool operator < ( const nextState2& m ) const{
return error < m.error;
}
};
struct readMappingInfo{
string read, qual, readName;
int maxReport, bestOne, maxDiffMismatch, maxDiffEdit, gap, uniqeOption;
double noisePercent = -1.0;
readMappingInfo( string _read, string _qual, string _readName, int _maxReport, int _bestOne, int _maxDiffMismatch, int _maxDiffEdit,
int _gap, int _uniqeOption, double _noicePercent ){
read = _read, qual = _qual, readName = _readName, maxReport = _maxReport, bestOne = _bestOne, maxDiffMismatch = _maxDiffMismatch,
maxDiffEdit = _maxDiffEdit, gap = _gap, uniqeOption = _uniqeOption, noisePercent = _noicePercent;
}
};
BWT fmIdx, revIdx;
CompressedString Ref;
mutex mu;
/* runing options */
int mc = 10;
int core = 1;
int globalMaxReport = 1;
int maxReport[MAXTHREADS];
int globalBestOne = 0;
string globalMode = "normal";
int bestOne[MAXTHREADS];
double globalNoisePercent = -0.1;
double noisePercent[MAXTHREADS];
int globalMaxDiffMismatch = 1;
int maxDiffMismatch[MAXTHREADS];
int globalMaxDiffEdit = 1;
int maxDiffEdit[MAXTHREADS];
int globalGap = 0;
int gap[MAXTHREADS];
int globalUniqeOption = 0;
int globalLongReadNoice = 30;
int uniqeOption[MAXTHREADS];
string outputAdr = "report.sam";
bool longRead = false;
int mxFailed[MAXTHREADS];
int minVal[MAXTHREADS];
int hardToMap = 0;
int Mode = 20;
int fragLen = 500;
/********** GLOBAL VARIABLES ********************/
bwtNode indexes[MAXTHREADS][MAXINDXES];
vector< bwtNode > threadResults[MAXTHREADS];
boost::atomic<bool> readVector[MAXTHREADS], writeVector[MAXTHREADS];
boost::atomic<int> readAccess[MAXTHREADS];
boost::atomic<bool> finished;
boost::atomic<bool> reading[seed2], writing[seed2];
HASHNode HASH[seed2];
bool isSetHASH[seed2];
string reads[MAXREADSIZE + 1000];
string qc[MAXREADSIZE + 1000];
string readsName[MAXREADSIZE + 1000];
string readsPerThread[MAXTHREADS];
unsigned char nonComplete[MAXTHREADS][MAXINPLEN * 4];
int leftIndex[MAXTHREADS], rightIndex[MAXTHREADS];
char dna[] = {'a', 'c', 'g', 't'};
char tst[100];
map< P, int > failedTry[MAXTHREADS][MAXMASK];
vector< mappingInfo > toWrite;
vector< string > refNames;
vector< string > findAllStrings[MAXTHREADS];
vector< uint32_t > refOffSets;
vector< long long > ms;
vector<mappingInfo> results[MAXTHREADS];
long long cntReads;
long long sptr = 0;
long long vis = 0;
long long mch[MAXTHREADS] = {0};
long long lp[MAXTHREADS];
long long totReads = 0;
unsigned long long needMask[64];
long long zeroGen[64][64];
long long windowMask[MAXTHREADS][6][MAXMASK][MAXMASK];
int Flag[MAXTHREADS];
int dbound[MAXTHREADS][MAXMASK][MAXMASK];
int gg = 1;
int sampleRate = 11;
int MAXD = 0;
int debg = 0;
int cntDFS = 0;
int cntBut = 0;
int hit = 0;
int unhit = 0;
long long rc = 0;
int drX[3] = { 0, -1, -1 };
int drY[3] = { -1, -1, 0 };
int totChar;
int indexesPtr[MAXTHREADS];
int dbg = 0;
int loopTh = 100;
int resPtr[MAXTHREADS];
int direction[MAXTHREADS];
int readPtr = 0;
string running_mode;
unsigned int refLen;
unsigned int preSearched[MAXTHREADS][MAXMASK * 4 + 1];
bool forceStop[MAXTHREADS];
bool onlyMis = true;
bool threadJobDone[100];
/*****************************************************************/
bool dfsButtumUpWithGap( bwtNode curMap, int leftIdx, int rightIdx, int curRow, int tIdx, vector<unsigned short>& curRowDp,
int maxEditDistance, int leftToRight, BWT& fm, vector<pInterval>& path, int idxPath, int DArray[], int lastAc, int lower, bool isChecked );
bool CompressedDfsButtumUpWithGap( bwtNode curMap, int leftIdx, int rightIdx, int curRow, int tIdx, compressedArray& dpRow,
int maxEditDistance, int leftToRight, BWT& fm, vector<pInterval>& path, int idxPath, int DArray[], int lastAc, int lower, bool isChecked );
bool dfsButtumUp( bwtNode curMap, int leftIdx, int rightIdx, int curRow, int tIdx, int error,
int maxEditDistance, int leftToRight, BWT& fm, vector<pInterval>& path, int idxPath, int DArray[], int lastAc, int lower, bool isChecked );
template<typename T>
void print( vector<T> v){
for( int i = 0; i < v.size(); i++ )
cerr << v[i] << ' ';
cerr << endl;
}
inline bool isValidDnaCharacter( char ch ){
return ( ch == 'a' || ch == 'c' || ch == 'g' || ch == 't' );
}
void toLower( char& ch ){
if( ch >= 'A' && ch <= 'Z' )
ch = ( 'a' + ch - 'A' );
}
inline unsigned int locateBwt( unsigned int idx ){
return fmIdx.locateRow( idx );
}
inline unsigned int locateInvBwt( unsigned int idx ){
return revIdx.locateRow( idx );
}
bool newCmp( const bwtNode a, const bwtNode b ){
return a.acceptedValue < b.acceptedValue;
}
psu getPosition( uint32_t offSet ){
int idx = 0;
for( idx = 0; idx < refNames.size() - 1; idx++ )
if( offSet >= refOffSets[idx] && offSet < refOffSets[idx + 1] )
break;
return psu( refNames[idx], offSet - refOffSets[idx] + 1 );
}
void resetControllers(){
for( int i = 0; i < core; i++ ){
readVector[i] = writeVector[i] = false;
readAccess[i] = -1;
}
for( int i = 0; i < seed2; i++ )
reading[i] = writing[i] = false;
finished = false;
}
string reverseComplement( string s ){
reverse( s.begin(), s.end() );
for( int i = 0; i < s.length(); i++ ){
if( s[i] == 'n' )
continue;
if( s[i] == 'a' ) s[i] = 't';
else
if( s[i] == 't' ) s[i] = 'a';
else
if( s[i] == 'c' ) s[i] = 'g';
else
if( s[i] == 'g' ) s[i] = 'c';
}
return s;
}
int getMinEdit( int leftIdx, int rightIdx, int tIdx ){
unsigned int lb = 0, rb = fmIdx.n - 1;
int z = 0, j = 0;
for( int i = leftIdx; i <= rightIdx; i++ ){
int id = fmIdx . remap[readsPerThread[tIdx][i]];
revIdx . updateInterval( lb, rb, id );
if( lb > rb ){
z++;
lb = 0, rb = fmIdx.n - 1;
}
}
return z;
}
puu getInvToBwt( int tIdx ){
unsigned int tl = 0, tr = fmIdx.n - 1, iter = 0;
for( int i = rightIndex[tIdx] - 1; i >= leftIndex[tIdx]; i-- ){
int id = fmIdx.remap[nonComplete[tIdx][i]];
fmIdx.updateInterval( tl, tr, id );
//cerr << iter++ << ' ' << tl << ' ' << tr << ' ' << (char)nonComplete[tIdx][i] << ' ' << id << endl;
}
return puu( tl, tr );
}
puu getBwtToInv( int tIdx ){
unsigned int tl = 0, tr = fmIdx.n - 1;
for( int i = leftIndex[tIdx]; i < rightIndex[tIdx]; i++ ){
int id = fmIdx.remap[nonComplete[tIdx][i]];
revIdx.updateInterval( tl, tr, id );
}
return puu( tl, tr );
}
puu getInterVal( string& q, BWT& fm ){
unsigned int tl = 0, tr = fm.n - 1;
for( int i = 0; i < q.length(); i++ ){
int id = fmIdx . remap[q[i]];
fm . updateInterval( tl, tr, id );
}
return puu( tl, tr );
}
puu getNextInterval( bwtNode& cur, BWT& fm, int id ){
unsigned int tl = cur.first;
unsigned int tr = cur.second;
fm . updateInterval( tl, tr, id );
return puu( tl, tr );
}
void buildLevstein( string q, bool REV, int tId, BWT& fm ){
if( REV )
reverse( q.begin(), q.end() );
unsigned int lb = 0, rb = fm.n - 1;
for( int i = 0; i < q.length(); i++ ){
fm.updateInterval( lb, rb, fm.remap[q[i]] );
if( lb > rb )
break;
}
if( lb <= rb )
indexes[tId][indexesPtr[tId]++] = (bwtNode( lb, rb, q.length(), 0 ) );
}
void newBuildDArray( int leftIdx, int rightIdx, int st, int dir, int tIdx, int DArr[], BWT& fm, int D ){
unsigned int lb = 0, rb = fm.n - 1;
int len = rightIdx - leftIdx + 1;
for( int i = 0; i < len; i++ )
DArr[i] = D + 1;
int z = 0, j = 0, idx = 0;
for( int I = st; ( I >= leftIdx && I <= rightIdx ); I += dir, idx++ ){
int i = fmIdx . remap[readsPerThread[tIdx][I]];
fm.updateInterval( lb, rb, i );
if( lb > rb ){
z++;
j = i + 1;
lb = 0, rb = fm.n - 1;
}
DArr[idx] = z;
}
}
vector< int > getLastRow( string s1, string s2, int maxDiff, int& bestEnd, int mode = 0 ){
vector< int > lastRows[2];
lastRows[0] = vector< int >( s2.length() + 1, 10 * maxDiff + 1 );
lastRows[1] = vector< int >( s2.length() + 1, 10 * maxDiff + 1 );
int turn = 1;
int curBest = 10000000;
for( int i = 0; i < lastRows[turn].size(); i++ )
lastRows[1 - turn][i] = i;
for( int i = 1; i <= s1.length(); i++ ){
int preRow = 1 - turn;
for( int j = 0; j <= s2.length(); j++ ){
if( j < 0 || j > s2.length() )
continue;
if( j == 0 )
lastRows[turn][j] = ( mode == 0 ) ? 0 : i;
else{
lastRows[turn][j] = 10 * maxDiff + 1;
lastRows[turn][j] = min( lastRows[preRow][j] + 1, lastRows[turn][j - 1] + 1 );
lastRows[turn][j] = min( lastRows[turn][j], lastRows[preRow][j - 1] + 1 );
if( s1[i - 1] == s2[j - 1] )
lastRows[turn][j] = min( lastRows[turn][j], lastRows[preRow][j - 1] );
}
}
if( lastRows[turn][s2.length()] < curBest ){
bestEnd = i;
curBest = lastRows[turn][s2.length()];
}
turn = 1 - turn;
}
return lastRows[1 - turn];
}
int getBestStartingPos( string& s1, string& s2, int maxDiff, bool canCutFirst = true, bool canCutLast = true ){
int bestEnd;
if( canCutLast == true ){
getLastRow( s1, s2, maxDiff, bestEnd, 0 );
s1 = s1.substr( 0, bestEnd );
}
if( canCutFirst == false ){
return 0;
}
reverse( s1.begin(), s1.end() );
reverse( s2.begin(), s2.end() );
getLastRow( s1, s2, maxDiff, bestEnd, 0 );
int ret = s1.length() - bestEnd;
s1 = s1.substr( 0, bestEnd );
reverse( s1.begin(), s1.end() );
reverse( s2.begin(), s2.end() );
return ret;
}
string Rev( string s1 ){
reverse( s1.begin(), s1.end() );
return s1;
}
vector< int > Rev( vector< int > v ){
reverse( v.begin(), v.end() );
return v;
}
// return pair( Z, W )
pss Hirschberg( string s1, string s2, int maxDiff, int depth = 0 ){
pss ret;
string Z, W;
int tmp;
if( s1.length() == 0 ){
for( int i = 0; i < s2.length(); i++ ){
Z += s2[i];
W += '-';
}
return pss( Z, W );
}
else if( s2.length() == 0 ){
for( int i = 0; i < s1.length(); i++ ){
Z += '-';
W += s1[i];
}
return pss( Z, W );
}
else if( s1.length() == 1 ){
for( int i = 0; i < s2.length(); i++ ){
Z += s2[i];
W += '-';
}
if( s2.find( s1[0] ) != string::npos )
W[s2.find( s1[0] )] = s1[0];
else W[0] = s1[0];
return pss( Z, W );
}
else if( s2.length() == 1 ){
for( int i = 0; i < s1.length(); i++ ){
Z += '-';
W += s1[i];
}
if( s1.find( s2[0] ) != string::npos )
Z[s1.find( s2[0] )] = s2[0];
else Z[0] = s2[0];
return pss( Z, W );
}
int xlen = s1.length();
int xmid = xlen / 2;
int ylen = s2.length();
vector< int > ScoreL = getLastRow( s1.substr( 0, xmid ), s2, maxDiff, tmp, 1 );
vector< int > ScoreR = Rev( getLastRow( Rev( s1.substr( xmid ) ), Rev(s2), maxDiff, tmp, 1 ) );
int ymid = 0, best = 10000000;
for( int i = 0; i <= s2.length(); i++ )
if( ScoreL[i] + ScoreR[i] < best ){
ymid = i;
best = ScoreL[i] + ScoreR[i];
}
pss h1 = Hirschberg(s1.substr( 0, xmid ), s2.substr( 0, ymid ), maxDiff, depth + 1 );
pss h2 = Hirschberg(s1.substr( xmid ), s2.substr( ymid ), maxDiff, depth + 1 );
return pss( h1.first + h2.first, h1.second + h2.second );
}
void testHeirch( string s1, string s2, int maxDiff ){
cerr << s1 << ' ' << s2 << endl;
getBestStartingPos( s1, s2, maxDiff );
cerr << s1 << ' ' << s2 << endl;
cerr << Hirschberg( s1, s2, maxDiff ).first << endl << Hirschberg( s1, s2, maxDiff ).second << endl;
}
bool processResult( bwtNode& curMap, int tIdx, int preLeftToRight ){
if( preLeftToRight == 1 ){
puu nx = getInvToBwt( tIdx );
curMap.first = nx.first, curMap.second = nx.second;
}
for( long long BwtIdx = curMap.first; BwtIdx <= curMap.second; BwtIdx++ ){
bwtNode newCurMap = curMap;
newCurMap.first = newCurMap.second = BwtIdx;
newCurMap.refRow = fmIdx.locateRow( newCurMap.first );
bool valid = true;
for( int i = 0; i < resPtr[tIdx]; i++ ){
if( abs( (long long)threadResults[tIdx][i].refRow - (long long)newCurMap.refRow ) <= readsPerThread[tIdx].length() ){ //max( threadResults[tIdx][i].acceptedValue, newCurMap.acceptedValue ) ){
if( newCurMap.acceptedValue >= threadResults[tIdx][i].acceptedValue ){
valid = false;
break;
}
else{
swap( threadResults[tIdx][resPtr[tIdx] - 1], threadResults[tIdx][i] );
threadResults[tIdx].pop_back();
resPtr[tIdx]--;
i--;
}
}
}
if( valid == false )
continue;
newCurMap.flag = ( direction[tIdx] == 0 ) ? 0 : 16;
threadResults[tIdx].push_back( newCurMap );
resPtr[tIdx]++;
if( uniqeOption[tIdx] && resPtr[tIdx] > 1 ){
forceStop[tIdx] = true;
resPtr[tIdx] = 0;
return true;
}
if( resPtr[tIdx] >= maxReport[tIdx] )
return true;
}
return false;
}
bool buttumUpMapping( bwtNode curMap, vector<pInterval>& path, int idxPath, int preLeftToRight, int tIdx ){
if( lp[tIdx] > loopTh )
return false;
if( resPtr[tIdx] >= maxReport[tIdx] )
return false;
cntBut++;
if( idxPath == -1 )
return processResult( curMap, tIdx, preLeftToRight );
//cerr << idxPath << endl;
int curLeftToRight = ( path[idxPath].first.left < path[idxPath].second.left ) ? 1 : 0;
if( curLeftToRight == 1 && preLeftToRight == 0 ){
puu nx = getBwtToInv( tIdx );
curMap.first = nx.first, curMap.second = nx.second;
}
if( curLeftToRight == 0 && preLeftToRight == 1 ){
puu nx = getInvToBwt( tIdx );
curMap.first = nx.first, curMap.second = nx.second;
}
int curMask = path[idxPath].first.mask;
int edit = path[idxPath].second.editDistance;
int len = path[idxPath].second.right - path[idxPath].second.left + 1;
int* DArray = new int[len];
if( curLeftToRight == 1 ){
newBuildDArray( path[idxPath].second.left, path[idxPath].second.right, path[idxPath].second.left, 1, tIdx, DArray, revIdx, path[idxPath].second.editDistance );
}
else{
newBuildDArray( path[idxPath].second.left, path[idxPath].second.right, path[idxPath].second.right, -1, tIdx, DArray, fmIdx, path[idxPath].second.editDistance );
}
if( DArray[path[idxPath].second.right - path[idxPath].second.left] > edit )
return false;
bool nx = false;
int prePtVal = resPtr[tIdx];
if( gap[tIdx] ){
vector< unsigned short > root( 2 * edit + 1, edit + 1 );
for( int i = edit; i < root.size(); i++ )
root[i] = min( edit + 1, abs( i - edit ) + curMap.acceptedValue );
int lower = (curLeftToRight == 0 ) ? ( curMap.acceptedValue + path[idxPath].second.editDistance / 2 ) : -1;
curMap.acceptedValue = -1;
if( edit <= 30 ){//&& readsPerThread[tIdx].length() < MAXMASK / 3 - 50 ){
compressedArray tmp( root, edit );
nx = CompressedDfsButtumUpWithGap(curMap, path[idxPath].second.left, path[idxPath].second.right, 0, tIdx, tmp, edit, curLeftToRight, ( curLeftToRight == 1 ) ? revIdx : fmIdx,
path, idxPath, DArray, edit + 10, lower, false );
}
else{
nx = dfsButtumUpWithGap(curMap, path[idxPath].second.left, path[idxPath].second.right, 0, tIdx, root, edit, curLeftToRight, ( curLeftToRight == 1 ) ? revIdx : fmIdx,
path, idxPath, DArray, edit + 10, lower, false );
}
}
else{
int edit = path[idxPath].second.editDistance;
nx = dfsButtumUp(curMap, path[idxPath].second.left, path[idxPath].second.right, 0, tIdx, curMap.acceptedValue, edit, curLeftToRight, ( curLeftToRight == 1 ) ? revIdx : fmIdx,
path, idxPath, DArray, edit + 10, (curLeftToRight == 0 ) ? ( curMap.acceptedValue + path[idxPath].second.editDistance / 2 ) : -1, false );
}
delete[] DArray;
if( resPtr[tIdx] == prePtVal )
preSearched[tIdx][curMask]++;
if( nx )
return true;
return false;
}
bool dfsButtumUp( bwtNode curMap, int leftIdx, int rightIdx, int curRow, int tIdx, int curError,
int maxEditDistance, int leftToRight, BWT& fm, vector<pInterval>& path, int idxPath, int DArray[], int lastAc, int lower, bool isChecked ){
//cerr << leftIdx << ' ' << rightIdx << ' ' << curError << ' ' << curRow << ' ' << maxEditDistance << endl;
cntDFS++;
int mask = path[idxPath].first.mask;
/*int tmask = mask;
vector< int > ups;
while( mask ){
ups.push_back( mask );
mask /= 2;
}
reverse( ups.begin(), ups.end() );
bool inv = false;
for( int i = 1; i < ups.size(); i++ ){
if( preSearched[tIdx][ups[i]] > ( 1 << ( i + Mode ) ) + i * mc )
inv = true;
}
if( inv )
return false;
*/
int cm = curError;//dpRow.getCell( maxEditDistance, maxEditDistance );
if( failedTry[tIdx][mask][P( curRow,cm )] >= mxFailed[tIdx] && ( dbound[tIdx][mask][curRow] < cm ) ){
return false;
}
if( resPtr[tIdx] >= maxReport[tIdx] )
return false;
//cerr << curMap.first << ' ' << curMap.second << ' ' << fm.bwt.charAt( curMap.first ) << endl;
int len = ( rightIdx - leftIdx + 1 );
int nextRowNum = curRow + 1;
nextState2 nxState[10];
for( int i = 0; i < totChar; i++ )
nxState[i] = nextState2( maxEditDistance );
if( curMap.first > curMap.second || fm.bwt.charAt( curMap.first ) == 0 )
return false;
char on = ( curMap.first == curMap.second ) ? fm.remap[fm.bwt.charAt( curMap.first )] : -1;
int lch = 1, rch = totChar - 1;
if( on != -1 )
lch = rch = on;
int preVal = resPtr[tIdx];
bool useLess = true;
for( int nextChar = lch; nextChar <= rch; nextChar++ ){
if( fm.remap_reverse[nextChar] == 'n' )
continue;
puu nxInterval = getNextInterval(curMap, fm, nextChar);
if( nxInterval.first > nxInterval.second ){
continue;
}
int col = nextRowNum;
nxState[nextChar].nxInterval = nxInterval;
int minAcVal = maxEditDistance + 1, chk = maxEditDistance + 1;
int ref = curError + 1;
if( leftToRight == 1 )
ref = min( ((( nextChar == fm.remap[readsPerThread[tIdx][leftIdx + col - 1]] ) ? 0 : 1 ) + curError), ref );
else
ref = min( ((( nextChar == fm.remap[readsPerThread[tIdx][rightIdx - col + 1]] ) ? 0 : 1 ) + curError), ref );
if( ref <= maxEditDistance ){
nxState[nextChar].valid = true;
nxState[nextChar].error = ref;
if( col == len )
nxState[nextChar].acceptedValue = ref;
}
}
if( on == -1 ){
lch = 1, rch = totChar - 1;
}
for( int nextChar = lch; nextChar <= rch; nextChar++ ){
if( nxState[nextChar].valid == false )
continue;
if( nxState[nextChar].acceptedValue != -1
&& nxState[nextChar].acceptedValue > lower ){
if( leftToRight )
nonComplete[tIdx][rightIndex[tIdx]++] = fm.remap_reverse[nextChar];
else nonComplete[tIdx][--leftIndex[tIdx]] = fm.remap_reverse[nextChar];
bwtNode nextMap = curMap;
nextMap.first = nxState[nextChar].nxInterval.first;
nextMap.second = nxState[nextChar].nxInterval.second;
nextMap.len++;
nextMap.acceptedValue = nxState[nextChar].acceptedValue;
bool nx = buttumUpMapping( nextMap, path, idxPath - 1, leftToRight, tIdx );
if( leftToRight )
rightIndex[tIdx]--;
else leftIndex[tIdx]++;
if( nx )
return true;
}
{
if( nxState[nextChar].acceptedValue != -1 )
continue;
if( leftToRight )
nonComplete[tIdx][rightIndex[tIdx]++] = fm.remap_reverse[nextChar];
else nonComplete[tIdx][--leftIndex[tIdx]] = fm.remap_reverse[nextChar];
bwtNode nextMap = curMap;
nextMap.first = nxState[nextChar].nxInterval.first;
nextMap.second = nxState[nextChar].nxInterval.second;
nextMap.len++;
bool nx = dfsButtumUp( nextMap, leftIdx, rightIdx, nextRowNum, tIdx, nxState[nextChar].error, maxEditDistance, leftToRight, fm, path, idxPath,
DArray, ( nxState[nextChar].acceptedValue == -1 ) ? lastAc : nxState[nextChar].acceptedValue, lower, isChecked );
if( leftToRight )
rightIndex[tIdx]--;
else leftIndex[tIdx]++;
if( nx )
return true;
}
}
if( resPtr[tIdx] > preVal )
useLess = false;
/*if( useLess ){
dbound[tIdx][mask][curRow] = min( dbound[tIdx][mask][curRow], cm );
failedTry[tIdx][mask][P( curRow,cm )]++;
}*/
return false;
}
bool dfsButtumUpWithGap( bwtNode curMap, int leftIdx, int rightIdx, int curRow, int tIdx, vector<unsigned short>& curRowDp,
int maxEditDistance, int leftToRight, BWT& fm, vector<pInterval>& path, int idxPath, int DArray[], int lastAc, int lower, bool isChecked ){
rc++;
cntDFS++;
int mask = path[idxPath].first.mask;
int cm = curRowDp[maxEditDistance];
/*if( failedTry[tIdx][mask][P( curRow,cm )] >= mxFailed[tIdx] && ( dbound[tIdx][mask][curRow] < cm ) ){
return false;
}*/
vector< int > ups;
if( resPtr[tIdx] >= maxReport[tIdx] )
return true;
int nextRowNum = curRow + 1;
int len = rightIdx - leftIdx + 1;
nextState nxState[6];
for( int i = 0; i < totChar; i++ )
nxState[i] = nextState( maxEditDistance );
char on = ( curMap.first == curMap.second ) ? fm.remap[fm.bwt.charAt( curMap.first )] : -1;
if( on == 0 )
return false;
int lch = 1, rch = totChar - 1;
if( on != -1 )
lch = rch = on;
for( int nextChar = lch; nextChar <= rch; nextChar++ ){
if( fm.remap_reverse[nextChar] == 'n' )
continue;
puu nxInterval = getNextInterval(curMap, fm, nextChar);
if( nxInterval.first > nxInterval.second ){
continue;
}
nxState[nextChar].nxInterval = nxInterval;
int minAcVal = maxEditDistance + 1, chk = maxEditDistance + 1 ;
for( int k = 0; k < 2 * maxEditDistance + 1; k++ ){
int col = nextRowNum + ( k - maxEditDistance );
unsigned short& ref = nxState[nextChar].nextRowDp[k];
ref = maxEditDistance + 1;
if( col >= 0 && col <= len ){
if( k > 0 )
ref = min( (unsigned short)( 1 + nxState[nextChar].nextRowDp[k - 1] ), ref );
if( k < curRowDp.size() && col > 0 ){
if( leftToRight == 1 )
ref = min( (unsigned short)((( nextChar == fm . remap[readsPerThread[tIdx][leftIdx + col - 1]] ) ? 0 : 1 ) + curRowDp[k]), ref );
else
ref = min( (unsigned short)((( nextChar == fm . remap[readsPerThread[tIdx][rightIdx - col + 1]] ) ? 0 : 1 ) + curRowDp[k]), ref );
}
if( k + 1 < curRowDp.size() )
ref = min( (unsigned short)( 1 + curRowDp[k + 1] ), ref );
if( col < len )
minAcVal = min( minAcVal, ref + DArray[len - col - 1] );
else
minAcVal = min( minAcVal, (int)ref );
if( col == len && ref <= maxEditDistance )
nxState[nextChar].acceptedValue = ref;
}
}
nxState[nextChar].valid = minAcVal <= maxEditDistance;
}
if( on == -1 ){
lch = 0, rch = totChar - 1;
}
int preVal = resPtr[tIdx];
bool useLess = true;
bool cn = false;
bool shTry = false;
for( int nextChar = lch; nextChar <= rch; nextChar++ ){
if( nxState[nextChar].valid == false )
continue;
{
cn = true;
bwtNode nextMap = curMap;
nextMap.first = nxState[nextChar].nxInterval.first;
nextMap.second = nxState[nextChar].nxInterval.second;
nextMap.acceptedValue = nxState[nextChar].acceptedValue;
nextMap.len++;
bool cc = true;
if( nxState[nextChar].acceptedValue != -1 && nxState[nextChar].acceptedValue >= lastAc ){
shTry = true;
}
if( nxState[nextChar].acceptedValue != -1 && nxState[nextChar].acceptedValue > lastAc ){
cc = false;
}
if( leftToRight )
nonComplete[tIdx][rightIndex[tIdx]++] = fm.remap_reverse[nextChar];
else nonComplete[tIdx][--leftIndex[tIdx]] = fm.remap_reverse[nextChar];
bool nx = dfsButtumUpWithGap( nextMap, leftIdx, rightIdx, nextRowNum, tIdx, nxState[nextChar].nextRowDp, maxEditDistance, leftToRight, fm, path, idxPath,
DArray, ( nxState[nextChar].acceptedValue == -1 ) ? lastAc : nxState[nextChar].acceptedValue, lower, cc );
if( nx )
return true;
if( leftToRight )
rightIndex[tIdx]--;
else leftIndex[tIdx]++;
}
}
if( isChecked && ( shTry || cn == false ) && curMap.acceptedValue != -1 && curMap.acceptedValue <= maxEditDistance && curMap.acceptedValue > lower ){
bwtNode nextMap = curMap;
bool nx = buttumUpMapping( nextMap, path, idxPath - 1, leftToRight, tIdx );