-
Notifications
You must be signed in to change notification settings - Fork 77
/
aligner.h
2053 lines (1946 loc) · 63.6 KB
/
aligner.h
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
/**
* aligner.h
*
* A generic class providing a stateful way to find alignments.
*/
#ifndef ALIGNER_H_
#define ALIGNER_H_
#include <iostream>
#include <set>
#include <stdint.h>
#include <vector>
#include "aligner_metrics.h"
#include "assert_helpers.h"
#include "ds.h"
#include "ebwt.h"
#include "pat.h"
#include "range.h"
#include "range_chaser.h"
#include "range_source.h"
#include "ref_aligner.h"
#include "reference.h"
#include "search_globals.h"
#include "sstring.h"
#ifdef PER_THREAD_TIMING
/// Based on http://stackoverflow.com/questions/16862620/numa-get-current-node-core
void get_cpu_and_node(int& cpu, int& node) {
unsigned long a,d,c;
__asm__ volatile("rdtscp" : "=a" (a), "=d" (d), "=c" (c));
node = (c & 0xFFF000)>>12;
cpu = c & 0xFFF;
}
#endif
/**
* State machine for carrying out an alignment, which usually consists
* of a series of phases that conduct different alignments using
* different backtracking constraints.
*
* Each Aligner should have a dedicated PatternSourcePerThread.
*/
class Aligner {
public:
Aligner(bool _done, bool rangeMode) :
done(_done), patsrc_(NULL), bufa_(NULL), bufb_(NULL),
rangeMode_(rangeMode)
{ }
virtual ~Aligner() { }
/// Advance the range search by one memory op
virtual bool advance() = 0;
/// Prepare Aligner for the next read
virtual void setQuery(PatternSourcePerThread *patsrc) {
assert(patsrc != NULL);
patsrc_ = patsrc;
bufa_ = &patsrc->bufa();
assert(bufa_ != NULL);
bufb_ = &patsrc->bufb();
alen_ = bufa_->length();
blen_ = (bufb_ != NULL) ? bufb_->length() : 0;
rand_.init(bufa_->seed);
}
/**
* Set to true if all searching w/r/t the current query is
* finished or if there is no current query.
*/
bool done;
protected:
// Current read pair
PatternSourcePerThread* patsrc_;
Read* bufa_;
uint32_t alen_;
Read* bufb_;
uint32_t blen_;
bool rangeMode_;
RandomSource rand_;
};
/**
* Abstract parent factory class for constructing aligners of all kinds.
*/
class AlignerFactory {
public:
virtual ~AlignerFactory() { }
virtual Aligner* create() const = 0;
/**
* Allocate a vector of n Aligners; use destroy(std::vector...) to
* free the memory.
*/
virtual EList<Aligner*>* create(uint32_t n) const {
EList<Aligner*>* v = new EList<Aligner*>;
for(uint32_t i = 0; i < n; i++) {
v->push_back(create());
assert(v->back() != NULL);
}
return v;
}
/// Free memory associated with the aligner
virtual void destroy(Aligner* al) const {
assert(al != NULL);
// Free the Aligner
delete al;
}
/// Free memory associated with an aligner list
virtual void destroy(EList<Aligner*>* als) const {
assert(als != NULL);
// Free all of the Aligners
for(size_t i = 0; i < als->size(); i++) {
if((*als)[i] != NULL) {
delete (*als)[i];
(*als)[i] = NULL;
}
}
// Free the vector
delete als;
}
};
/**
* Coordinates multiple aligners of the same type (i.e. either all
* single-end or all paired-end).
*/
class MultiAligner {
public:
MultiAligner(
uint32_t n,
uint32_t qUpto,
const AlignerFactory& alignFact,
const PatternSourcePerThreadFactory& patsrcFact) :
n_(n), qUpto_(qUpto),
alignFact_(alignFact), patsrcFact_(patsrcFact),
aligners_(NULL), patsrcs_(NULL)
{
aligners_ = alignFact_.create(n_);
assert(aligners_ != NULL);
patsrcs_ = patsrcFact_.create(n_);
assert(patsrcs_ != NULL);
}
/// Free memory associated with the aligners and their pattern sources.
virtual ~MultiAligner() {
alignFact_.destroy(aligners_);
patsrcFact_.destroy(patsrcs_);
}
/**
* Advance an array of aligners in parallel, using prefetches to
* try to hide all the latency.
*/
void run() {
bool saw_last_read = false;
while(!saw_last_read) {
for(uint32_t i = 0; i < n_; i++) {
if(!(*aligners_)[i]->done) {
// Advance an aligner already in progress
(*aligners_)[i]->advance();
} else {
if(saw_last_read) {
break;
}
// Get a new read and initialize an aligner with it
pair<bool, bool> ret = (*patsrcs_)[i]->nextReadPair();
saw_last_read = ret.second;
if(ret.first && (*patsrcs_)[i]->rdid() < qUpto_) {
(*aligners_)[i]->setQuery((*patsrcs_)[i]);
} else if (ret.first) {
saw_last_read = true;
}
}
}
}
}
protected:
uint32_t n_; /// Number of aligners
uint32_t qUpto_; /// Number of reads to align before stopping
const AlignerFactory& alignFact_;
const PatternSourcePerThreadFactory& patsrcFact_;
EList<Aligner *>* aligners_;
EList<PatternSourcePerThread *>* patsrcs_;
};
/**
* Coordinates multiple single-end and paired-end aligners, routing
* reads to one or the other type as appropriate.
*/
class MixedMultiAligner {
public:
MixedMultiAligner(
uint32_t n,
uint32_t qUpto,
const AlignerFactory& alignSEFact,
const AlignerFactory& alignPEFact,
const PatternSourcePerThreadFactory& patsrcFact) :
n_(n), qUpto_(qUpto),
alignSEFact_(alignSEFact),
alignPEFact_(alignPEFact),
patsrcFact_(patsrcFact),
alignersSE_(NULL),
alignersPE_(NULL),
seOrPe_(NULL),
patsrcs_(NULL)
{
// Instantiate all single-end aligners
alignersSE_ = alignSEFact_.create(n_);
assert(alignersSE_ != NULL);
// Instantiate all paired-end aligners
alignersPE_ = alignPEFact_.create(n_);
assert(alignersPE_ != NULL);
// Allocate array of boolean flags indicating whether each of
// the slots is currently using the single-end or paired-end
// aligner
seOrPe_ = new bool[n_];
for(uint32_t i = 0; i < n_; i++) {
seOrPe_[i] = true;
}
// Instantiate all read sources
patsrcs_ = patsrcFact_.create(n_);
assert(patsrcs_ != NULL);
}
/// Free memory associated with the aligners and their pattern sources.
virtual ~MixedMultiAligner() {
alignSEFact_.destroy(alignersSE_);
alignPEFact_.destroy(alignersPE_);
patsrcFact_.destroy(patsrcs_);
delete[] seOrPe_;
}
/**
* Advance an array of aligners in parallel, using prefetches to
* try to hide all the latency.
*/
void run(bool verbose = false, int tid = -1) {
#ifdef PER_THREAD_TIMING
uint64_t ncpu_changeovers = 0;
uint64_t nnuma_changeovers = 0;
int current_cpu = 0, current_node = 0;
get_cpu_and_node(current_cpu, current_node);
std::stringstream ss;
std::string msg;
ss << "thread: " << tid << " time: ";
msg = ss.str();
Timer timer(std::cout, msg.c_str());
#endif
bool first = true;
bool saw_last_read = false;
if(n_ == 1) {
Aligner *al = seOrPe_[0] ? (*alignersSE_)[0] : (*alignersPE_)[0];
PatternSourcePerThread *ps = (*patsrcs_)[0];
while(true) {
#ifdef PER_THREAD_TIMING
int cpu = 0, node = 0;
get_cpu_and_node(cpu, node);
if(cpu != current_cpu) {
ncpu_changeovers++;
current_cpu = cpu;
}
if(node != current_node) {
nnuma_changeovers++;
current_node = node;
}
#endif
if(!first && !al->done) {
// Advance an aligner already in progress; this is
// the common case
al->advance();
} else {
if(saw_last_read) {
break;
}
// Get a new read
pair<bool, bool> ret = ps->nextReadPair();
saw_last_read = ret.second;
if(ret.first && ps->rdid() < qUpto_) {
if(ps->paired()) {
// Read currently in buffer is paired-end
(*alignersPE_)[0]->setQuery(ps);
al = (*alignersPE_)[0];
seOrPe_[0] = false; // false -> paired
} else {
// Read currently in buffer is single-end
(*alignersSE_)[0]->setQuery(ps);
al = (*alignersSE_)[0];
seOrPe_[0] = true; // true = unpaired
}
} else if (ret.first) {
break;
}
}
first = false;
}
} else {
while(true) {
#ifdef PER_THREAD_TIMING
int cpu = 0, node = 0;
get_cpu_and_node(cpu, node);
if(cpu != current_cpu) {
ncpu_changeovers++;
current_cpu = cpu;
}
if(node != current_node) {
nnuma_changeovers++;
current_node = node;
}
#endif
bool saw_last_read = false;
for(uint32_t i = 0; i < n_; i++) {
Aligner *al = seOrPe_[i] ? (*alignersSE_)[i] :
(*alignersPE_)[i];
if(!first && !al->done) {
// Advance an aligner already in progress; this is
// the common case
al->advance();
} else {
if(saw_last_read) {
break;
}
// Feed a new read to a vacant aligner
PatternSourcePerThread *ps = (*patsrcs_)[i];
// Get a new read
pair<bool, bool> ret = ps->nextReadPair();
saw_last_read = ret.second;
if (ret.first && ps->rdid() < qUpto_) {
if(ps->paired()) {
// Read currently in buffer is paired-end
(*alignersPE_)[i]->setQuery(ps);
seOrPe_[i] = false; // false -> paired
} else {
// Read currently in buffer is single-end
(*alignersSE_)[i]->setQuery(ps);
seOrPe_[i] = true; // true = unpaired
}
} else if (ret.first) {
break;
}
}
}
first = false;
}
}
#ifdef PER_THREAD_TIMING
ss.str("");
ss.clear();
ss << "thread: " << tid << " cpu_changeovers: " << ncpu_changeovers << std::endl
<< "thread: " << tid << " node_changeovers: " << nnuma_changeovers << std::endl;
std::cout << ss.str();
#endif
}
protected:
uint32_t n_; /// Number of aligners
uint32_t qUpto_; /// Number of reads to align before stopping
const AlignerFactory& alignSEFact_;
const AlignerFactory& alignPEFact_;
const PatternSourcePerThreadFactory& patsrcFact_;
EList<Aligner *>* alignersSE_;
EList<Aligner *>* alignersPE_;
bool * seOrPe_;
EList<PatternSourcePerThread *>* patsrcs_;
};
/**
* An aligner for finding exact matches of unpaired reads. Always
* tries the forward-oriented version of the read before the reverse-
* oriented read.
*/
template<typename TRangeSource>
class UnpairedAlignerV2 : public Aligner {
typedef RangeSourceDriver<TRangeSource> TDriver;
public:
UnpairedAlignerV2(
EbwtSearchParams* params,
TDriver* driver,
RangeChaser* rchase,
HitSink& sink,
const HitSinkPerThreadFactory& sinkPtFactory,
HitSinkPerThread* sinkPt,
EList<BTRefString >& os, // TODO: remove this, not used
BitPairReference *refs,
bool rangeMode,
bool verbose,
bool quiet,
int maxBts,
ChunkPool *pool,
int *btCnt = NULL,
AlignerMetrics *metrics = NULL) :
Aligner(true, rangeMode),
doneFirst_(true),
firstIsFw_(true),
chase_(false),
sinkPtFactory_(sinkPtFactory),
sinkPt_(sinkPt),
refs_(refs),
params_(params),
rchase_(rchase),
driver_(driver),
verbose_(verbose),
quiet_(quiet),
maxBts_(maxBts),
pool_(pool),
btCnt_(btCnt),
metrics_(metrics)
{
assert(pool_ != NULL);
assert(sinkPt_ != NULL);
assert(params_ != NULL);
assert(driver_ != NULL);
}
virtual ~UnpairedAlignerV2() {
delete driver_; driver_ = NULL;
delete params_; params_ = NULL;
delete rchase_; rchase_ = NULL;
delete[] btCnt_; btCnt_ = NULL;
sinkPtFactory_.destroy(sinkPt_); sinkPt_ = NULL;
}
/**
* Prepare this aligner for the next read.
*/
virtual void setQuery(PatternSourcePerThread* patsrc) {
Aligner::setQuery(patsrc); // set fields & random seed
if(metrics_ != NULL) {
metrics_->nextRead(patsrc->bufa().patFw);
}
pool_->reset(&patsrc->bufa().name, (uint32_t)patsrc->rdid());
if(patsrc->bufa().length() < 4) {
if(!quiet_) {
cerr << "Warning: Skipping read " << patsrc->bufa().name
<< " because it is less than 4 characters long" << endl;
}
this->done = true;
sinkPt_->finishRead(*patsrc_, true, true);
return;
}
driver_->setQuery(patsrc, NULL);
this->done = driver_->done;
doneFirst_ = false;
// Reset #-backtrack countdown
if(btCnt_ != NULL) *btCnt_ = maxBts_;
if(sinkPt_->setHits(patsrc->bufa().hitset)) {
this->done = true;
sinkPt_->finishRead(*patsrc_, true, true);
}
// Grab a bit from the pseudo-random seed to determine whether
// to start with forward or reverse complement
firstIsFw_ = ((patsrc->bufa().seed & 0x10) == 0);
chase_ = false;
}
/**
* Helper for reporting an alignment.
*/
inline bool report(const Range& ra,
TIndexOffU first,
TIndexOffU second,
uint32_t tlen)
{
bool ebwtFw = ra.ebwt->fw();
params_->setFw(ra.fw);
return params_->reportHit(
ra.fw ? (ebwtFw? bufa_->patFw : bufa_->patFwRev) :
(ebwtFw? bufa_->patRc : bufa_->patRcRev),
ra.fw ? (ebwtFw? &bufa_->qual : &bufa_->qualRev) :
(ebwtFw? &bufa_->qualRev : &bufa_->qual),
&bufa_->name,
ebwtFw,
ra.mms, // mismatch positions
ra.refcs, // reference characters for mms
ra.numMms, // # mismatches
make_pair(first, second), // position
make_pair<TIndexOffU,TIndexOffU>(0, 0), // (bogus) mate position
true, // (bogus) mate orientation
0, // (bogus) mate length
make_pair(ra.top, ra.bot),// arrows
tlen, // textlen
alen_, // qlen
ra.stratum, // alignment stratum
ra.cost, // cost, including qual penalty
ra.bot - ra.top - 1, // # other hits
(uint32_t)patsrc_->rdid(),// pattern id
bufa_->seed, // pseudo-random seed
0); // mate (0 = unpaired)
}
/**
* Advance the aligner. Return true iff we're
* done with this read.
*/
virtual bool advance() {
assert(!this->done);
if(chase_) {
assert(!rangeMode_);
assert(driver_->foundRange);
assert(!sinkPt_->irrelevantCost(driver_->range().cost));
if(!rchase_->foundOff() && !rchase_->done) {
rchase_->advance();
return false;
}
if(rchase_->foundOff()) {
this->done = report(driver_->range(), rchase_->off().first,
rchase_->off().second, rchase_->tlen());
rchase_->reset();
} else {
assert(rchase_->done);
// Forget this range; keep looking for ranges
chase_ = false;
driver_->foundRange = false;
this->done = driver_->done;
}
}
// Still advancing a
if(!this->done && !chase_) {
assert(!driver_->done || driver_->foundRange);
if(driver_->foundRange) {
const Range& ra = driver_->range();
assert(!sinkPt_->irrelevantCost(ra.cost));
assert(ra.repOk());
if(rangeMode_) {
this->done = report(ra, ra.top, ra.bot, 0);
driver_->foundRange = false;
} else {
rchase_->setTopBot(ra.top, ra.bot, alen_, rand_, ra.ebwt);
if(rchase_->foundOff()) {
this->done = report(
ra, rchase_->off().first,
rchase_->off().second, rchase_->tlen());
rchase_->reset();
}
if(!rchase_->done && !sinkPt_->irrelevantCost(ra.cost)) {
// Keep chasing this range
chase_ = true;
} else {
driver_->foundRange = false;
}
}
} else {
this->done = sinkPt_->irrelevantCost(driver_->minCost);
if(!this->done) {
driver_->advance(ADV_COST_CHANGES);
} else {
// No longer necessarily true with chain input
//assert(!sinkPt_->spanStrata());
}
}
if(driver_->done && !driver_->foundRange && !chase_) {
this->done = true;
}
}
if(this->done) {
sinkPt_->finishRead(*patsrc_, true, true);
}
return this->done;
}
protected:
// Reference sequences (needed for colorspace decoding)
const BitPairReference* refs_;
// Progress state
bool doneFirst_;
bool firstIsFw_;
bool chase_;
// Temporary HitSink; to be deleted
const HitSinkPerThreadFactory& sinkPtFactory_;
HitSinkPerThread* sinkPt_;
// State for alignment
EbwtSearchParams* params_;
// State for getting alignments from ranges statefully
RangeChaser* rchase_;
// Range-finding state
TDriver* driver_;
bool verbose_; // be talkative
bool quiet_; // don't print informational/warning info
const int maxBts_;
ChunkPool *pool_;
int *btCnt_;
AlignerMetrics *metrics_;
};
/**
* An aligner for finding paired alignments while operating entirely
* within the Burrows-Wheeler domain.
*/
template<typename TRangeSource>
class PairedBWAlignerV1 : public Aligner {
typedef std::pair<TIndexOffU,TIndexOffU> UPair;
typedef EList<UPair> UPairVec;
typedef EList<Range> TRangeVec;
typedef RangeSourceDriver<TRangeSource> TDriver;
typedef std::pair<uint64_t, uint64_t> TU64Pair;
typedef std::set<TU64Pair> TSetPairs;
public:
PairedBWAlignerV1(
EbwtSearchParams* params,
TDriver* driver1Fw, TDriver* driver1Rc,
TDriver* driver2Fw, TDriver* driver2Rc,
RefAligner* refAligner,
RangeChaser* rchase,
HitSink& sink,
const HitSinkPerThreadFactory& sinkPtFactory,
HitSinkPerThread* sinkPt,
bool fw1, bool fw2,
uint32_t minInsert,
uint32_t maxInsert,
bool dontReconcile,
uint32_t symCeiling,
uint32_t mixedThresh,
uint32_t mixedAttemptLim,
const BitPairReference* refs,
bool rangeMode,
bool verbose,
bool quiet,
int maxBts,
ChunkPool *pool,
int *btCnt) :
Aligner(true, rangeMode),
refs_(refs),
patsrc_(NULL), qlen1_(0), qlen2_(0), doneFw_(true),
doneFwFirst_(true),
chase1Fw_(false), chase1Rc_(false),
chase2Fw_(false), chase2Rc_(false),
delayedChase1Fw_(false), delayedChase1Rc_(false),
delayedChase2Fw_(false), delayedChase2Rc_(false),
refAligner_(refAligner),
sinkPtFactory_(sinkPtFactory),
sinkPt_(sinkPt),
params_(params),
minInsert_(minInsert),
maxInsert_(maxInsert),
dontReconcile_(dontReconcile),
symCeiling_(symCeiling),
mixedThresh_(mixedThresh),
mixedAttemptLim_(mixedAttemptLim),
mixedAttempts_(0),
fw1_(fw1), fw2_(fw2),
rchase_(rchase),
verbose_(verbose),
quiet_(quiet),
maxBts_(maxBts),
pool_(pool),
btCnt_(btCnt),
driver1Fw_(driver1Fw), driver1Rc_(driver1Rc),
offs1FwSz_(0), offs1RcSz_(0),
driver2Fw_(driver2Fw), driver2Rc_(driver2Rc),
offs2FwSz_(0), offs2RcSz_(0),
chaseL_fw_ (fw1_ ? chase1Fw_ : chase1Rc_),
chaseR_fw_ (fw2_ ? chase2Fw_ : chase2Rc_),
delayedchaseL_fw_(fw1_ ? delayedChase1Fw_ : delayedChase1Rc_),
delayedchaseR_fw_(fw2_ ? delayedChase2Fw_ : delayedChase2Rc_),
drL_fw_ (fw1_ ? *driver1Fw_ : *driver1Rc_),
drR_fw_ (fw2_ ? *driver2Fw_ : *driver2Rc_),
offsLarr_fw_ (fw1_ ? offs1FwArr_ : offs1RcArr_),
offsRarr_fw_ (fw2_ ? offs2FwArr_ : offs2RcArr_),
rangesLarr_fw_ (fw1_ ? ranges1FwArr_ : ranges1RcArr_),
rangesRarr_fw_ (fw2_ ? ranges2FwArr_ : ranges2RcArr_),
offsLsz_fw_ (fw1_ ? offs1FwSz_ : offs1RcSz_),
offsRsz_fw_ (fw2_ ? offs2FwSz_ : offs2RcSz_),
chaseL_rc_ (fw2_ ? chase2Rc_ : chase2Fw_),
chaseR_rc_ (fw1_ ? chase1Rc_ : chase1Fw_),
delayedchaseL_rc_(fw2_ ? delayedChase2Rc_ : delayedChase2Fw_),
delayedchaseR_rc_(fw1_ ? delayedChase1Rc_ : delayedChase1Fw_),
drL_rc_ (fw2_ ? *driver2Rc_ : *driver2Fw_),
drR_rc_ (fw1_ ? *driver1Rc_ : *driver1Fw_),
offsLarr_rc_ (fw2_ ? offs2RcArr_ : offs2FwArr_),
offsRarr_rc_ (fw1_ ? offs1RcArr_ : offs1FwArr_),
rangesLarr_rc_ (fw2_ ? ranges2RcArr_ : ranges2FwArr_),
rangesRarr_rc_ (fw1_ ? ranges1RcArr_ : ranges1FwArr_),
offsLsz_rc_ (fw2_ ? offs2RcSz_ : offs2FwSz_),
offsRsz_rc_ (fw1_ ? offs1RcSz_ : offs1FwSz_),
chaseL_ (&chaseL_fw_),
chaseR_ (&chaseR_fw_),
delayedchaseL_(&delayedchaseL_fw_),
delayedchaseR_(&delayedchaseR_fw_),
drL_ (&drL_fw_),
drR_ (&drR_fw_),
offsLarr_ (offsLarr_fw_),
offsRarr_ (offsRarr_fw_),
rangesLarr_ (rangesLarr_fw_),
rangesRarr_ (rangesRarr_fw_),
offsLsz_ (&offsLsz_fw_),
offsRsz_ (&offsRsz_fw_),
donePair_ (&doneFw_),
fwL_(fw1),
fwR_(fw2),
verbose2_(false)
{
assert(pool_ != NULL);
assert(sinkPt_ != NULL);
assert(params_ != NULL);
assert(driver1Fw_ != NULL);
assert(driver1Rc_ != NULL);
assert(driver2Fw_ != NULL);
assert(driver2Rc_ != NULL);
}
virtual ~PairedBWAlignerV1() {
delete driver1Fw_; driver1Fw_ = NULL;
delete driver1Rc_; driver1Rc_ = NULL;
delete driver2Fw_; driver2Fw_ = NULL;
delete driver2Rc_; driver2Rc_ = NULL;
delete params_; params_ = NULL;
delete rchase_; rchase_ = NULL;
delete[] btCnt_; btCnt_ = NULL;
delete refAligner_; refAligner_ = NULL;
sinkPtFactory_.destroy(sinkPt_); sinkPt_ = NULL;
}
/**
* Prepare this aligner for the next read.
*/
virtual void setQuery(PatternSourcePerThread* patsrc) {
Aligner::setQuery(patsrc); // set fields & random seed
assert(!patsrc->bufb().empty());
// Give all of the drivers pointers to the relevant read info
patsrc_ = patsrc;
pool_->reset(&patsrc->bufa().name, (uint32_t)patsrc->rdid());
if(patsrc->bufa().length() < 4 || patsrc->bufb().length() < 4) {
if(!quiet_) {
cerr << "Warning: Skipping pair " << patsrc->bufa().name
<< " because a mate is less than 4 characters long" << endl;
}
this->done = true;
sinkPt_->finishRead(*patsrc_, true, true);
return;
}
driver1Fw_->setQuery(patsrc, NULL);
driver1Rc_->setQuery(patsrc, NULL);
driver2Fw_->setQuery(patsrc, NULL);
driver2Rc_->setQuery(patsrc, NULL);
qlen1_ = patsrc_->bufa().length();
qlen2_ = patsrc_->bufb().length();
if(btCnt_ != NULL) (*btCnt_) = maxBts_;
// Neither orientation is done
doneFw_ = false;
doneFwFirst_ = true;
this->done = false;
// No ranges are being chased yet
chase1Fw_ = false;
chase1Rc_ = false;
chase2Fw_ = false;
chase2Rc_ = false;
delayedChase1Fw_ = false;
delayedChase1Rc_ = false;
delayedChase2Fw_ = false;
delayedChase2Rc_ = false;
// Clear all intermediate ranges
for(size_t i = 0; i < 32; i++) {
offs1FwArr_[i].clear(); offs1RcArr_[i].clear();
offs2FwArr_[i].clear(); offs2RcArr_[i].clear();
ranges1FwArr_[i].clear(); ranges1RcArr_[i].clear();
ranges2FwArr_[i].clear(); ranges2RcArr_[i].clear();
}
offs1FwSz_ = offs1RcSz_ = offs2FwSz_ = offs2RcSz_ = 0;
chaseL_ = &chaseL_fw_;
chaseR_ = &chaseR_fw_;
delayedchaseL_ = &delayedchaseL_fw_;
delayedchaseR_ = &delayedchaseR_fw_;
drL_ = &drL_fw_;
drR_ = &drR_fw_;
offsLarr_ = offsLarr_fw_;
offsRarr_ = offsRarr_fw_;
rangesLarr_ = rangesLarr_fw_;
rangesRarr_ = rangesRarr_fw_;
offsLsz_ = &offsLsz_fw_;
offsRsz_ = &offsRsz_fw_;
donePair_ = &doneFw_;
fwL_ = fw1_;
fwR_ = fw2_;
mixedAttempts_ = 0;
pairs_fw_.clear();
pairs_rc_.clear();
#ifndef NDEBUG
allTopsL_fw_.clear();
allTopsR_fw_.clear();
allTopsL_rc_.clear();
allTopsR_rc_.clear();
#endif
}
/**
* Advance the aligner by one memory op. Return true iff we're
* done with this read.
*
* A call to this function does one of many things:
* 1. Advance a RangeSourceDriver and check if it found a new range
* 2. Advance a RowChaseDriver and check if it found a reference
* offset for a an alignment in a range
*/
virtual bool advance() {
assert(!this->done);
if(doneFw_ && doneFwFirst_) {
if(verbose2_) cout << "--" << endl;
chaseL_ = &chaseL_rc_;
chaseR_ = &chaseR_rc_;
delayedchaseL_ = &delayedchaseL_rc_;
delayedchaseR_ = &delayedchaseR_rc_;
drL_ = &drL_rc_;
drR_ = &drR_rc_;
offsLarr_ = offsLarr_rc_;
offsRarr_ = offsRarr_rc_;
rangesLarr_ = rangesLarr_rc_;
rangesRarr_ = rangesRarr_rc_;
offsLsz_ = &offsLsz_rc_;
offsRsz_ = &offsRsz_rc_;
donePair_ = &this->done;
fwL_ = !fw2_;
fwR_ = !fw1_;
doneFwFirst_ = false;
mixedAttempts_ = 0;
}
bool chasing = *chaseL_ || *chaseR_;
if(chasing && !rchase_->foundOff() && !rchase_->done) {
rchase_->advance();
return false;
}
advanceOrientation(!doneFw_);
if(this->done) {
if(verbose2_) cout << "----" << endl;
sinkPt_->finishRead(*patsrc_, true, true);
}
return this->done;
}
protected:
/**
* Helper for reporting a pair of alignments. As of now, we report
* a paired alignment by reporting two consecutive alignments, one
* for each mate.
*/
bool report(const Range& rL, // range for upstream mate
const Range& rR, // range for downstream mate
TIndexOffU first, // ref idx
TIndexOffU upstreamOff, // offset for upstream mate
TIndexOffU dnstreamOff, // offset for downstream mate
TIndexOffU tlen, // length of ref
bool pairFw, // whether the pair is being mapped to fw strand
bool ebwtFwL,
bool ebwtFwR)
{
assert(gAllowMateContainment || upstreamOff < dnstreamOff);
TIndexOffU spreadL = rL.bot - rL.top;
TIndexOffU spreadR = rR.bot - rR.top;
TIndexOffU oms = min(spreadL, spreadR) - 1;
Read* bufL = pairFw ? bufa_ : bufb_;
Read* bufR = pairFw ? bufb_ : bufa_;
TIndexOffU lenL = pairFw ? alen_ : blen_;
TIndexOffU lenR = pairFw ? blen_ : alen_;
bool ret;
assert(!params_->sink().exceededOverThresh());
params_->setFw(rL.fw);
// Print upstream mate first
ret = params_->reportHit(
rL.fw ? (ebwtFwL? bufL->patFw : bufL->patFwRev) :
(ebwtFwL? bufL->patRc : bufL->patRcRev),
rL.fw ? (ebwtFwL? &bufL->qual : &bufL->qualRev) :
(ebwtFwL? &bufL->qualRev : &bufL->qual),
&bufL->name,
ebwtFwL,
rL.mms, // mismatch positions
rL.refcs, // reference characters for mms
rL.numMms, // # mismatches
make_pair(first, upstreamOff),// position
make_pair(first, dnstreamOff),// mate position
rR.fw, // mate orientation
lenR, // mate length
make_pair(rL.top, rL.bot), // arrows
tlen, // textlen
lenL, // qlen
rL.stratum, // alignment stratum
rL.cost, // cost, including quality penalty
oms, // # other hits
bufL->patid,
bufL->seed,
pairFw ? 1 : 2);
if(ret) {
return true; // can happen when -m is set
}
params_->setFw(rR.fw);
ret = params_->reportHit(
rR.fw ? (ebwtFwR? bufR->patFw : bufR->patFwRev) :
(ebwtFwR? bufR->patRc : bufR->patRcRev),
rR.fw ? (ebwtFwR? &bufR->qual : &bufR->qualRev) :
(ebwtFwR? &bufR->qualRev : &bufR->qual),
&bufR->name,
ebwtFwR,
rR.mms, // mismatch positions
rR.refcs, // reference characters for mms
rR.numMms, // # mismatches
make_pair(first, dnstreamOff),// position
make_pair(first, upstreamOff),// mate position
rL.fw, // mate orientation
lenL, // mate length
make_pair(rR.top, rR.bot), // arrows
tlen, // textlen
lenR, // qlen
rR.stratum, // alignment stratum
rR.cost, // cost, including quality penalty
oms, // # other hits
bufR->patid,
bufR->seed,
pairFw ? 2 : 1);
return ret;
}
bool report(const Range& rL, // range for upstream mate
const Range& rR, // range for downstream mate
TIndexOffU first, // ref idx
TIndexOffU upstreamOff, // offset for upstream mate
TIndexOffU dnstreamOff, // offset for downstream mate
TIndexOffU tlen, // length of ref
bool pairFw) // whether the pair is being mapped to fw strand
{
return report(rL, rR, first, upstreamOff,
dnstreamOff, tlen,
pairFw, rL.ebwt->fw(), rR.ebwt->fw());
}
/**
* Given a vector of reference positions where one of the two mates
* (the "anchor" mate) has aligned, look directly at the reference
* sequence for instances where the other mate (the "outstanding"
* mate) aligns such that mating constraint is satisfied.
*/
bool resolveOutstandingInRef(const bool off1,
const UPair& off,
const TIndexOffU tlen,
const Range& range)
{
assert(refs_->loaded());
assert_lt(off.first, refs_->numRefs());
// If matchRight is true, then we're trying to align the other
// mate to the right of the already-aligned mate. Otherwise,
// to the left.
bool matchRight = (off1 ? !doneFw_ : doneFw_);
// Sequence and quals for mate to be matched
bool fw = off1 ? fw2_ : fw1_; // whether outstanding mate is fw/rc
if(doneFw_) fw = !fw;
// 'seq' gets sequence of outstanding mate w/r/t the forward
// reference strand
const BTDnaString& seq = fw ? (off1 ? patsrc_->bufb().patFw :
patsrc_->bufa().patFw) :
(off1 ? patsrc_->bufb().patRc :
patsrc_->bufa().patRc);
// 'seq' gets qualities of outstanding mate w/r/t the forward
// reference strand
const BTString& qual = fw ? (off1 ? patsrc_->bufb().qual :
patsrc_->bufa().qual) :
(off1 ? patsrc_->bufb().qualRev :
patsrc_->bufa().qualRev);
uint32_t qlen = (uint32_t)seq.length(); // length of outstanding mate
uint32_t alen = (off1 ? patsrc_->bufa().length() :
patsrc_->bufb().length());
int minins = minInsert_;
int maxins = maxInsert_;
// Let minins and maxins be the minimum and maximum insert
// lengths once we account for the trimming applied to the
// mates. The idea is that the insert constraint placed by
// the user applies to the raw reads, not the trimmed reads.
if(fw1_) {
minins = max<int>(0, minins - patsrc_->bufa().trimmed5);
maxins = max<int>(0, maxins - patsrc_->bufa().trimmed5);
} else {
minins = max<int>(0, minins - patsrc_->bufa().trimmed3);
maxins = max<int>(0, maxins - patsrc_->bufa().trimmed3);
}
if(fw2_) {
minins = max<int>(0, minins - patsrc_->bufb().trimmed3);
maxins = max<int>(0, maxins - patsrc_->bufb().trimmed3);
} else {
minins = max<int>(0, minins - patsrc_->bufb().trimmed5);
maxins = max<int>(0, maxins - patsrc_->bufb().trimmed5);
}
// Sanity check minins and maxins