-
Notifications
You must be signed in to change notification settings - Fork 159
/
aln_sink.cpp
2564 lines (2479 loc) · 74.3 KB
/
aln_sink.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
/*
* Copyright 2011, Ben Langmead <[email protected]>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bowtie 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iomanip>
#include <limits>
#include "aln_sink.h"
#include "aligner_seed.h"
#include "util.h"
using namespace std;
/**
* Initialize state machine with a new read. The state we start in depends
* on whether it's paired-end or unpaired.
*/
void ReportingState::nextRead(bool paired) {
paired_ = paired;
if(paired) {
state_ = CONCORDANT_PAIRS;
doneConcord_ = false;
doneDiscord_ = p_.discord ? false : true;
doneUnpair1_ = p_.mixed ? false : true;
doneUnpair2_ = p_.mixed ? false : true;
exitConcord_ = ReportingState::EXIT_DID_NOT_EXIT;
exitDiscord_ = p_.discord ?
ReportingState::EXIT_DID_NOT_EXIT :
ReportingState::EXIT_DID_NOT_ENTER;
exitUnpair1_ = p_.mixed ?
ReportingState::EXIT_DID_NOT_EXIT :
ReportingState::EXIT_DID_NOT_ENTER;
exitUnpair2_ = p_.mixed ?
ReportingState::EXIT_DID_NOT_EXIT :
ReportingState::EXIT_DID_NOT_ENTER;
} else {
// Unpaired
state_ = UNPAIRED;
doneConcord_ = true;
doneDiscord_ = true;
doneUnpair1_ = false;
doneUnpair2_ = true;
exitConcord_ = ReportingState::EXIT_DID_NOT_ENTER; // not relevant
exitDiscord_ = ReportingState::EXIT_DID_NOT_ENTER; // not relevant
exitUnpair1_ = ReportingState::EXIT_DID_NOT_EXIT;
exitUnpair2_ = ReportingState::EXIT_DID_NOT_ENTER; // not relevant
}
doneUnpair_ = doneUnpair1_ && doneUnpair2_;
done_ = false;
nconcord_ = ndiscord_ = nunpair1_ = nunpair2_ = 0;
}
/**
* Caller uses this member function to indicate that one additional
* concordant alignment has been found.
*/
bool ReportingState::foundConcordant() {
assert(paired_);
assert_geq(state_, ReportingState::CONCORDANT_PAIRS);
assert(!doneConcord_);
nconcord_++;
areDone(nconcord_, doneConcord_, exitConcord_);
// No need to search for discordant alignments if there are one or more
// concordant alignments.
doneDiscord_ = true;
exitDiscord_ = ReportingState::EXIT_SHORT_CIRCUIT_TRUMPED;
if(doneConcord_) {
// If we're finished looking for concordant alignments, do we have to
// continue on to search for unpaired alignments? Only if our exit
// from the concordant stage is EXIT_SHORT_CIRCUIT_M. If it's
// EXIT_SHORT_CIRCUIT_k or EXIT_WITH_ALIGNMENTS, we can skip unpaired.
assert_neq(ReportingState::EXIT_NO_ALIGNMENTS, exitConcord_);
if(exitConcord_ != ReportingState::EXIT_SHORT_CIRCUIT_M) {
if(!doneUnpair1_) {
doneUnpair1_ = true;
exitUnpair1_ = ReportingState::EXIT_SHORT_CIRCUIT_TRUMPED;
}
if(!doneUnpair2_) {
doneUnpair2_ = true;
exitUnpair2_ = ReportingState::EXIT_SHORT_CIRCUIT_TRUMPED;
}
}
}
updateDone();
return done();
}
/**
* Caller uses this member function to indicate that one additional unpaired
* mate alignment has been found for the specified mate.
*/
bool ReportingState::foundUnpaired(bool mate1) {
assert_gt(state_, ReportingState::NO_READ);
// Note: it's not right to assert !doneUnpair1_/!doneUnpair2_ here.
// Even if we're done with finding
if(mate1) {
nunpair1_++;
// Did we just finish with this mate?
if(!doneUnpair1_) {
areDone(nunpair1_, doneUnpair1_, exitUnpair1_);
if(doneUnpair1_) {
doneUnpair_ = doneUnpair1_ && doneUnpair2_;
updateDone();
}
}
if(nunpair1_ > 1) {
doneDiscord_ = true;
exitDiscord_ = ReportingState::EXIT_NO_ALIGNMENTS;
}
} else {
nunpair2_++;
// Did we just finish with this mate?
if(!doneUnpair2_) {
areDone(nunpair2_, doneUnpair2_, exitUnpair2_);
if(doneUnpair2_) {
doneUnpair_ = doneUnpair1_ && doneUnpair2_;
updateDone();
}
}
if(nunpair2_ > 1) {
doneDiscord_ = true;
exitDiscord_ = ReportingState::EXIT_NO_ALIGNMENTS;
}
}
return done();
}
/**
* Called to indicate that the aligner has finished searching for
* alignments. This gives us a chance to finalize our state.
*
* TODO: Keep track of short-circuiting information.
*/
void ReportingState::finish() {
if(!doneConcord_) {
doneConcord_ = true;
exitConcord_ =
((nconcord_ > 0) ?
ReportingState::EXIT_WITH_ALIGNMENTS :
ReportingState::EXIT_NO_ALIGNMENTS);
}
assert_gt(exitConcord_, EXIT_DID_NOT_EXIT);
if(!doneUnpair1_) {
doneUnpair1_ = true;
exitUnpair1_ =
((nunpair1_ > 0) ?
ReportingState::EXIT_WITH_ALIGNMENTS :
ReportingState::EXIT_NO_ALIGNMENTS);
}
assert_gt(exitUnpair1_, EXIT_DID_NOT_EXIT);
if(!doneUnpair2_) {
doneUnpair2_ = true;
exitUnpair2_ =
((nunpair2_ > 0) ?
ReportingState::EXIT_WITH_ALIGNMENTS :
ReportingState::EXIT_NO_ALIGNMENTS);
}
assert_gt(exitUnpair2_, EXIT_DID_NOT_EXIT);
if(!doneDiscord_) {
// Check if the unpaired alignments should be converted to a single
// discordant paired-end alignment.
assert_eq(0, ndiscord_);
if(nconcord_ == 0 && nunpair1_ == 1 && nunpair2_ == 1) {
convertUnpairedToDiscordant();
}
doneDiscord_ = true;
exitDiscord_ =
((ndiscord_ > 0) ?
ReportingState::EXIT_WITH_ALIGNMENTS :
ReportingState::EXIT_NO_ALIGNMENTS);
}
assert(!paired_ || exitDiscord_ > ReportingState::EXIT_DID_NOT_EXIT);
doneUnpair_ = done_ = true;
assert(done());
}
/**
* Populate given counters with the number of various kinds of alignments
* to report for this read. Concordant alignments are preferable to (and
* mutually exclusive with) discordant alignments, and paired-end
* alignments are preferable to unpaired alignments.
*
* The caller also needs some additional information for the case where a
* pair or unpaired read aligns repetitively. If the read is paired-end
* and the paired-end has repetitive concordant alignments, that should be
* reported, and 'pairMax' is set to true to indicate this. If the read is
* paired-end, does not have any conordant alignments, but does have
* repetitive alignments for one or both mates, then that should be
* reported, and 'unpair1Max' and 'unpair2Max' are set accordingly.
*
* Note that it's possible in the case of a paired-end read for the read to
* have repetitive concordant alignments, but for one mate to have a unique
* unpaired alignment.
*/
void ReportingState::getReport(
uint64_t& nconcordAln, // # concordant alignments to report
uint64_t& ndiscordAln, // # discordant alignments to report
uint64_t& nunpair1Aln, // # unpaired alignments for mate #1 to report
uint64_t& nunpair2Aln, // # unpaired alignments for mate #2 to report
bool& pairMax, // repetitive concordant alignments
bool& unpair1Max, // repetitive alignments for mate #1
bool& unpair2Max) // repetitive alignments for mate #2
const
{
nconcordAln = ndiscordAln = nunpair1Aln = nunpair2Aln = 0;
pairMax = unpair1Max = unpair2Max = false;
assert_gt(p_.khits, 0);
assert_gt(p_.mhits, 0);
if(paired_) {
// Do we have 1 or more concordant alignments to report?
if(exitConcord_ == ReportingState::EXIT_SHORT_CIRCUIT_k) {
// k at random
assert_geq(nconcord_, (uint64_t)p_.khits);
nconcordAln = p_.khits;
return;
} else if(exitConcord_ == ReportingState::EXIT_SHORT_CIRCUIT_M) {
assert(p_.msample);
assert_gt(nconcord_, 0);
pairMax = true; // repetitive concordant alignments
if(p_.mixed) {
unpair1Max = nunpair1_ > (uint64_t)p_.mhits;
unpair2Max = nunpair2_ > (uint64_t)p_.mhits;
}
// Not sure if this is OK
nconcordAln = 1; // 1 at random
return;
} else if(exitConcord_ == ReportingState::EXIT_WITH_ALIGNMENTS) {
assert_gt(nconcord_, 0);
// <= k at random
nconcordAln = min<uint64_t>(nconcord_, p_.khits);
return;
}
assert(!p_.mhitsSet() || nconcord_ <= (uint64_t)p_.mhits+1);
// Do we have a discordant alignment to report?
if(exitDiscord_ == ReportingState::EXIT_WITH_ALIGNMENTS) {
// Report discordant
assert(p_.discord);
ndiscordAln = 1;
return;
}
}
assert_neq(ReportingState::EXIT_SHORT_CIRCUIT_TRUMPED, exitUnpair1_);
assert_neq(ReportingState::EXIT_SHORT_CIRCUIT_TRUMPED, exitUnpair2_);
if((paired_ && !p_.mixed) || nunpair1_ + nunpair2_ == 0) {
// Unpaired alignments either not reportable or non-existant
return;
}
// Do we have 1 or more alignments for mate #1 to report?
if(exitUnpair1_ == ReportingState::EXIT_SHORT_CIRCUIT_k) {
// k at random
assert_geq(nunpair1_, (uint64_t)p_.khits);
nunpair1Aln = p_.khits;
} else if(exitUnpair1_ == ReportingState::EXIT_SHORT_CIRCUIT_M) {
assert(p_.msample);
assert_gt(nunpair1_, 0);
unpair1Max = true; // repetitive alignments for mate #1
nunpair1Aln = 1; // 1 at random
} else if(exitUnpair1_ == ReportingState::EXIT_WITH_ALIGNMENTS) {
assert_gt(nunpair1_, 0);
// <= k at random
nunpair1Aln = min<uint64_t>(nunpair1_, (uint64_t)p_.khits);
}
assert(!p_.mhitsSet() || paired_ || nunpair1_ <= (uint64_t)p_.mhits+1);
// Do we have 2 or more alignments for mate #2 to report?
if(exitUnpair2_ == ReportingState::EXIT_SHORT_CIRCUIT_k) {
// k at random
nunpair2Aln = p_.khits;
} else if(exitUnpair2_ == ReportingState::EXIT_SHORT_CIRCUIT_M) {
assert(p_.msample);
assert_gt(nunpair2_, 0);
unpair2Max = true; // repetitive alignments for mate #1
nunpair2Aln = 1; // 1 at random
} else if(exitUnpair2_ == ReportingState::EXIT_WITH_ALIGNMENTS) {
assert_gt(nunpair2_, 0);
// <= k at random
nunpair2Aln = min<uint64_t>(nunpair2_, (uint64_t)p_.khits);
}
assert(!p_.mhitsSet() || paired_ || nunpair2_ <= (uint64_t)p_.mhits+1);
}
/**
* Given the number of alignments in a category, check whether we
* short-circuited out of the category. Set the done and exit arguments to
* indicate whether and how we short-circuited.
*/
inline void ReportingState::areDone(
uint64_t cnt, // # alignments in category
bool& done, // out: whether we short-circuited out of category
int& exit) const // out: if done, how we short-circuited (-k? -m? etc)
{
assert(!done);
// Have we exceeded the -k limit?
assert_gt(p_.khits, 0);
assert_gt(p_.mhits, 0);
if(cnt >= (uint64_t)p_.khits && !p_.mhitsSet()) {
done = true;
exit = ReportingState::EXIT_SHORT_CIRCUIT_k;
}
// Have we exceeded the -m or -M limit?
else if(p_.mhitsSet() && cnt > (uint64_t)p_.mhits) {
done = true;
assert(p_.msample);
exit = ReportingState::EXIT_SHORT_CIRCUIT_M;
}
}
static std::ostream& printPct(
std::ostream& os,
uint64_t num,
uint64_t denom)
{
double pct = 0.0f;
if(denom != 0) { pct = 100.0 * (double)num / (double)denom; }
os << fixed << setprecision(2) << pct << '%';
return os;
}
/**
* Print a friendly summary of:
*
* 1. How many reads were aligned and had one or more alignments
* reported
* 2. How many reads exceeded the -m or -M ceiling and therefore had
* their alignments suppressed or sampled
* 3. How many reads failed to align entirely
*
* Optionally print a series of Hadoop streaming-style counter updates
* with similar information.
*/
void AlnSink::printAlSumm(
const ReportingMetrics& met,
size_t repThresh, // threshold for uniqueness, or max if no thresh
bool discord, // looked for discordant alignments
bool mixed, // looked for unpaired alignments where paired failed?
bool hadoopOut) // output Hadoop counters?
{
// NOTE: there's a filtering step at the very beginning, so everything
// being reported here is post filtering
bool canRep = repThresh != MAX_SIZE_T;
if(hadoopOut) {
cerr << "reporter:counter:Bowtie,Reads processed," << met.nread << endl;
}
uint64_t totread = met.nread;
if(totread > 0) {
cerr << "" << met.nread << " reads; of these:" << endl;
} else {
assert_eq(0, met.npaired);
assert_eq(0, met.nunpaired);
cerr << "" << totread << " reads" << endl;
}
uint64_t totpair = met.npaired;
if(totpair > 0) {
// Paired output
cerr << " " << totpair << " (";
printPct(cerr, totpair, totread);
cerr << ") were paired; of these:" << endl;
// Concordants
cerr << " " << met.nconcord_0 << " (";
printPct(cerr, met.nconcord_0, met.npaired);
cerr << ") aligned concordantly 0 times" << endl;
if(canRep) {
// Print the number that aligned concordantly exactly once
assert_eq(met.nconcord_uni, met.nconcord_uni1+met.nconcord_uni2);
cerr << " " << met.nconcord_uni1 << " (";
printPct(cerr, met.nconcord_uni1, met.npaired);
cerr << ") aligned concordantly exactly 1 time" << endl;
// Print the number that aligned concordantly more than once but
// fewer times than the limit
cerr << " " << met.nconcord_uni2+met.nconcord_rep << " (";
printPct(cerr, met.nconcord_uni2+met.nconcord_rep, met.npaired);
cerr << ") aligned concordantly >1 times" << endl;
} else {
// Print the number that aligned concordantly exactly once
assert_eq(met.nconcord_uni, met.nconcord_uni1+met.nconcord_uni2);
cerr << " " << met.nconcord_uni1 << " (";
printPct(cerr, met.nconcord_uni1, met.npaired);
cerr << ") aligned concordantly exactly 1 time" << endl;
// Print the number that aligned concordantly more than once
cerr << " " << met.nconcord_uni2 << " (";
printPct(cerr, met.nconcord_uni2, met.npaired);
cerr << ") aligned concordantly >1 times" << endl;
}
if(discord) {
// TODO: what about discoardant and on separate chromosomes?
// Bring out the unaligned pair total so we can subtract discordants
cerr << " ----" << endl;
cerr << " " << met.nconcord_0
<< " pairs aligned concordantly 0 times; of these:" << endl;
// Discordants
cerr << " " << met.ndiscord << " (";
printPct(cerr, met.ndiscord, met.nconcord_0);
cerr << ") aligned discordantly 1 time" << endl;
}
uint64_t ncondiscord_0 = met.nconcord_0 - met.ndiscord;
if(mixed) {
// Bring out the unaligned pair total so we can subtract discordants
cerr << " ----" << endl;
cerr << " " << ncondiscord_0
<< " pairs aligned 0 times concordantly or discordantly; of these:" << endl;
cerr << " " << (ncondiscord_0 * 2) << " mates make up the pairs; of these:" << endl;
cerr << " " << met.nunp_0_0 << " " << "(";
printPct(cerr, met.nunp_0_0, ncondiscord_0 * 2);
cerr << ") aligned 0 times" << endl;
if(canRep) {
// Print the number that aligned exactly once
assert_eq(met.nunp_0_uni, met.nunp_0_uni1+met.nunp_0_uni2);
cerr << " " << met.nunp_0_uni1 << " (";
printPct(cerr, met.nunp_0_uni1, ncondiscord_0 * 2);
cerr << ") aligned exactly 1 time" << endl;
// Print the number that aligned more than once but fewer times
// than the limit
cerr << " " << met.nunp_0_uni2+met.nunp_0_rep << " (";
printPct(cerr, met.nunp_0_uni2+met.nunp_0_rep, ncondiscord_0 * 2);
cerr << ") aligned >1 times" << endl;
} else {
// Print the number that aligned exactly once
assert_eq(met.nunp_0_uni, met.nunp_0_uni1+met.nunp_0_uni2);
cerr << " " << met.nunp_0_uni1 << " (";
printPct(cerr, met.nunp_0_uni1, ncondiscord_0 * 2);
cerr << ") aligned exactly 1 time" << endl;
// Print the number that aligned more than once but fewer times
// than the limit
cerr << " " << met.nunp_0_uni2 << " (";
printPct(cerr, met.nunp_0_uni2, ncondiscord_0 * 2);
cerr << ") aligned >1 times" << endl;
}
//if(canRep) {
// // Bring out the repetitively aligned pair total so we can subtract discordants
// cerr << " ----" << endl;
// cerr << " " << met.nconcord_rep
// << " pairs aligned concordantly >" << repThresh
// << " times; of these:" << endl;
// cerr << " " << (met.nconcord_rep * 2) << " mates make up the pairs; of these:" << endl;
//
// cerr << " " << met.nunp_rep_0 << " (";
// printPct(cerr, met.nunp_rep_0, met.nconcord_rep * 2);
// cerr << ") aligned 0 times" << endl;
//
// cerr << " " << met.nunp_rep_uni << " (";
// printPct(cerr, met.nunp_rep_uni, met.nconcord_rep * 2);
// cerr << ") aligned >0 and <=" << repThresh << " times" << endl;
//
// cerr << " " << met.nunp_rep_rep << " (";
// printPct(cerr, met.nunp_rep_rep, met.nconcord_rep * 2);
// cerr << ") aligned >" << repThresh << " times" << endl;
//}
}
}
uint64_t totunpair = met.nunpaired;
if(totunpair > 0) {
// Unpaired output
cerr << " " << totunpair << " (";
printPct(cerr, totunpair, totread);
cerr << ") were unpaired; of these:" << endl;
cerr << " " << met.nunp_0 << " (";
printPct(cerr, met.nunp_0, met.nunpaired);
cerr << ") aligned 0 times" << endl;
if(hadoopOut) {
cerr << "reporter:counter:Bowtie 2,Unpaired reads with 0 alignments,"
<< met.nunpaired << endl;
}
if(canRep) {
// Print the number that aligned exactly once
assert_eq(met.nunp_uni, met.nunp_uni1+met.nunp_uni2);
cerr << " " << met.nunp_uni1 << " (";
printPct(cerr, met.nunp_uni1, met.nunpaired);
cerr << ") aligned exactly 1 time" << endl;
// Print the number that aligned more than once but fewer times
// than the limit
cerr << " " << met.nunp_uni2+met.nunp_rep << " (";
printPct(cerr, met.nunp_uni2+met.nunp_rep, met.nunpaired);
cerr << ") aligned >1 times" << endl;
} else {
// Print the number that aligned exactly once
assert_eq(met.nunp_uni, met.nunp_uni1+met.nunp_uni2);
cerr << " " << met.nunp_uni1 << " (";
printPct(cerr, met.nunp_uni1, met.nunpaired);
cerr << ") aligned exactly 1 time" << endl;
// Print the number that aligned more than once
cerr << " " << met.nunp_uni2 << " (";
printPct(cerr, met.nunp_uni2, met.nunpaired);
cerr << ") aligned >1 times" << endl;
}
}
uint64_t tot_al_cand = totunpair + totpair*2;
uint64_t tot_al =
(met.nconcord_uni + met.nconcord_rep)*2 +
(met.ndiscord)*2 +
met.nunp_0_uni +
met.nunp_0_rep +
met.nunp_uni +
met.nunp_rep;
assert_leq(tot_al, tot_al_cand);
printPct(cerr, tot_al, tot_al_cand);
cerr << " overall alignment rate" << endl;
}
/**
* Return true iff the read in rd1/rd2 matches the last read handled, which
* should still be in rd1_/rd2_.
*/
bool AlnSinkWrap::sameRead(
// One of the other of rd1, rd2 will = NULL if read is unpaired
const Read* rd1, // new mate #1
const Read* rd2, // new mate #2
bool qualitiesMatter) // aln policy distinguishes b/t quals?
{
bool same = false;
if(rd1_ != NULL || rd2_ != NULL) {
// This is not the first time the sink was initialized with
// a read. Check if new read/pair is identical to previous
// read/pair
if((rd1_ == NULL) == (rd1 == NULL) &&
(rd2_ == NULL) == (rd2 == NULL))
{
bool m1same = (rd1 == NULL && rd1_ == NULL);
if(!m1same) {
assert(rd1 != NULL);
assert(rd1_ != NULL);
m1same = Read::same(
rd1->patFw, // new seq
rd1->qual, // new quals
rd1_->patFw, // old seq
rd1_->qual, // old quals
qualitiesMatter);
}
if(m1same) {
bool m2same = (rd2 == NULL && rd2_ == NULL);
if(!m2same) {
m2same = Read::same(
rd2->patFw, // new seq
rd2->qual, // new quals
rd2_->patFw, // old seq
rd2_->qual, // old quals
qualitiesMatter);
}
same = m2same;
}
}
}
return same;
}
/**
* Initialize the wrapper with a new read pair and return an integer >= -1
* indicating which stage the aligner should start at. If -1 is returned, the
* aligner can skip the read entirely. Checks if the new read pair is
* identical to the previous pair. If it is, then we return the id of the
* first stage to run.
*/
int AlnSinkWrap::nextRead(
// One of the other of rd1, rd2 will = NULL if read is unpaired
const Read* rd1, // new mate #1
const Read* rd2, // new mate #2
TReadId rdid, // read ID for new pair
bool qualitiesMatter) // aln policy distinguishes b/t quals?
{
assert(!init_);
assert(rd1 != NULL || rd2 != NULL);
init_ = true;
// Keep copy of new read, so that we can compare it with the
// next one
if(rd1 != NULL) {
rd1_ = rd1;
} else rd1_ = NULL;
if(rd2 != NULL) {
rd2_ = rd2;
} else rd2_ = NULL;
rdid_ = rdid;
// Caller must now align the read
maxed1_ = false;
maxed2_ = false;
maxedOverall_ = false;
bestPair_ = best2Pair_ =
bestUnp1_ = best2Unp1_ =
bestUnp2_ = best2Unp2_ = std::numeric_limits<THitInt>::min();
rs1_.clear(); // clear out paired-end alignments
rs2_.clear(); // clear out paired-end alignments
rs1u_.clear(); // clear out unpaired alignments for mate #1
rs2u_.clear(); // clear out unpaired alignments for mate #2
st_.nextRead(readIsPair()); // reset state
assert(empty());
assert(!maxed());
// Start from the first stage
return 0;
}
/**
* Inform global, shared AlnSink object that we're finished with this read.
* The global AlnSink is responsible for updating counters, creating the output
* record, and delivering the record to the appropriate output stream.
*
* What gets reported for a paired-end alignment?
*
* 1. If there are reportable concordant alignments, report those and stop
* 2. If there are reportable discordant alignments, report those and stop
* 3. If unpaired alignments can be reported:
* 3a. Report
#
* Update metrics. Only ambiguity is: what if a pair aligns repetitively and
* one of its mates aligns uniquely?
*
* uint64_t al; // # mates w/ >= 1 reported alignment
* uint64_t unal; // # mates w/ 0 alignments
* uint64_t max; // # mates withheld for exceeding -M/-m ceiling
* uint64_t al_concord; // # pairs w/ >= 1 concordant alignment
* uint64_t al_discord; // # pairs w/ >= 1 discordant alignment
* uint64_t max_concord; // # pairs maxed out
* uint64_t unal_pair; // # pairs where neither mate aligned
*/
void AlnSinkWrap::finishRead(
const SeedResults *sr1, // seed alignment results for mate 1
const SeedResults *sr2, // seed alignment results for mate 2
bool exhaust1, // mate 1 exhausted?
bool exhaust2, // mate 2 exhausted?
bool nfilt1, // mate 1 N-filtered?
bool nfilt2, // mate 2 N-filtered?
bool scfilt1, // mate 1 score-filtered?
bool scfilt2, // mate 2 score-filtered?
bool lenfilt1, // mate 1 length-filtered?
bool lenfilt2, // mate 2 length-filtered?
bool qcfilt1, // mate 1 qc-filtered?
bool qcfilt2, // mate 2 qc-filtered?
RandomSource& rnd, // pseudo-random generator
ReportingMetrics& met, // reporting metrics
const PerReadMetrics& prm, // per-read metrics
const Scoring& sc, // scoring scheme
bool suppressSeedSummary, // = true
bool suppressAlignments, // = false
bool scUnMapped, // = false
bool xeq) // = false
{
obuf_.clear();
OutputQueueMark qqm(g_.outq(), obuf_, rdid_, threadid_);
assert(init_);
if(!suppressSeedSummary) {
if(sr1 != NULL) {
assert(rd1_ != NULL);
// Mate exists and has non-empty SeedResults
g_.reportSeedSummary(obuf_, *rd1_, rdid_, threadid_, *sr1, true);
} else if(rd1_ != NULL) {
// Mate exists but has NULL SeedResults
g_.reportEmptySeedSummary(obuf_, *rd1_, rdid_, true);
}
if(sr2 != NULL) {
assert(rd2_ != NULL);
// Mate exists and has non-empty SeedResults
g_.reportSeedSummary(obuf_, *rd2_, rdid_, threadid_, *sr2, true);
} else if(rd2_ != NULL) {
// Mate exists but has NULL SeedResults
g_.reportEmptySeedSummary(obuf_, *rd2_, rdid_, true);
}
}
if(!suppressAlignments) {
// Ask the ReportingState what to report
st_.finish();
uint64_t nconcord = 0, ndiscord = 0, nunpair1 = 0, nunpair2 = 0;
bool pairMax = false, unpair1Max = false, unpair2Max = false;
st_.getReport(
nconcord,
ndiscord,
nunpair1,
nunpair2,
pairMax,
unpair1Max,
unpair2Max);
assert_leq(nconcord, rs1_.size());
assert_leq(nunpair1, rs1u_.size());
assert_leq(nunpair2, rs2u_.size());
assert_leq(ndiscord, 1);
assert_gt(rp_.khits, 0);
assert_gt(rp_.mhits, 0);
assert(!pairMax || rs1_.size() >= (uint64_t)rp_.mhits);
assert(!unpair1Max || rs1u_.size() >= (uint64_t)rp_.mhits);
assert(!unpair2Max || rs2u_.size() >= (uint64_t)rp_.mhits);
met.nread++;
if(readIsPair()) {
met.npaired++;
} else {
met.nunpaired++;
}
// Report concordant paired-end alignments if possible
if(nconcord > 0) {
AlnSetSumm concordSumm(
rd1_, rd2_, &rs1_, &rs2_, &rs1u_, &rs2u_,
exhaust1, exhaust2, -1, -1);
// Sort by score then pick from low to high
AlnScore bestUScore, bestP1Score, bestP2Score, bestCScore;
AlnScore bestUDist, bestP1Dist, bestP2Dist, bestCDist;
AlnScore bestUnchosenUScore, bestUnchosenP1Score, bestUnchosenP2Score, bestUnchosenCScore;
AlnScore bestUnchosenUDist, bestUnchosenP1Dist, bestUnchosenP2Dist, bestUnchosenCDist;
// TODO: should probably package these variables up so it's not
// such a pain to pass them around
size_t off = selectByScore(
&rs1_, &rs2_,
nconcord, select1_,
&rs1u_, &rs2u_,
bestUScore,
bestUDist,
bestP1Score,
bestP1Dist,
bestP2Score,
bestP2Dist,
bestCScore,
bestCDist,
bestUnchosenUScore,
bestUnchosenUDist,
bestUnchosenP1Score,
bestUnchosenP1Dist,
bestUnchosenP2Score,
bestUnchosenP2Dist,
bestUnchosenCScore,
bestUnchosenCDist,
rnd);
concordSumm.setBest(
bestUScore,
bestUDist,
bestP1Score,
bestP1Dist,
bestP2Score,
bestP2Dist,
bestCScore,
bestCDist,
bestUnchosenUScore,
bestUnchosenUDist,
bestUnchosenP1Score,
bestUnchosenP1Dist,
bestUnchosenP2Score,
bestUnchosenP2Dist,
bestUnchosenCScore,
bestUnchosenCDist);
assert(concordSumm.bestScore(true).valid());
assert(concordSumm.bestScore(false).valid());
assert_lt(off, rs1_.size());
const AlnRes *rs1 = &rs1_[off];
const AlnRes *rs2 = &rs2_[off];
AlnFlags flags1(
ALN_FLAG_PAIR_CONCORD_MATE1,
st_.params().mhitsSet(),
unpair1Max,
pairMax,
nfilt1,
scfilt1,
lenfilt1,
qcfilt1,
st_.params().mixed,
true, // primary
true, // opp aligned
rs2->fw(), // opp fw
scUnMapped,
xeq);
AlnFlags flags2(
ALN_FLAG_PAIR_CONCORD_MATE2,
st_.params().mhitsSet(),
unpair2Max,
pairMax,
nfilt2,
scfilt2,
lenfilt2,
qcfilt2,
st_.params().mixed,
false, // primary
true, // opp aligned
rs1->fw(), // opp fw
scUnMapped,
xeq);
// Issue: we only set the flags once, but some of the flags might
// vary from pair to pair among the pairs we're reporting. For
// instance, whether the a given mate aligns to the forward strand.
SeedAlSumm ssm1, ssm2;
sr1->toSeedAlSumm(ssm1);
sr2->toSeedAlSumm(ssm2);
for(size_t i = 0; i < rs1_.size(); i++) {
rs1_[i].setMateParams(ALN_RES_TYPE_MATE1, &rs2_[i], flags1);
rs2_[i].setMateParams(ALN_RES_TYPE_MATE2, &rs1_[i], flags2);
assert_eq(abs(rs1_[i].fragmentLength()), abs(rs2_[i].fragmentLength()));
}
assert(!select1_.empty());
g_.reportHits(
obuf_,
staln_,
threadid_,
rd1_,
rd2_,
rdid_,
select1_,
NULL,
&rs1_,
&rs2_,
pairMax,
concordSumm,
ssm1,
ssm2,
&flags1,
&flags2,
prm,
mapq_,
sc,
false);
if(pairMax) {
met.nconcord_rep++;
} else {
met.nconcord_uni++;
assert(!rs1_.empty());
if(!concordSumm.bestUnchosenCScore().valid()) {
met.nconcord_uni1++;
} else {
met.nconcord_uni2++;
}
}
init_ = false;
//g_.outq().finishRead(obuf_, rdid_, threadid_);
return;
}
// Report disconcordant paired-end alignments if possible
else if(ndiscord > 0) {
ASSERT_ONLY(bool ret =) prepareDiscordants();
assert(ret);
assert_eq(1, rs1_.size());
assert_eq(1, rs2_.size());
AlnSetSumm discordSumm(
rd1_, rd2_, &rs1_, &rs2_, &rs1u_, &rs2u_,
exhaust1, exhaust2, -1, -1);
const AlnRes *rs1 = &rs1_[0];
const AlnRes *rs2 = &rs2_[0];
AlnFlags flags1(
ALN_FLAG_PAIR_DISCORD_MATE1,
st_.params().mhitsSet(),
false,
pairMax,
nfilt1,
scfilt1,
lenfilt1,
qcfilt1,
st_.params().mixed,
true, // primary
true, // opp aligned
rs2->fw(), // opp fw
scUnMapped,
xeq);
AlnFlags flags2(
ALN_FLAG_PAIR_DISCORD_MATE2,
st_.params().mhitsSet(),
false,
pairMax,
nfilt2,
scfilt2,
lenfilt2,
qcfilt2,
st_.params().mixed,
false, // primary
true, // opp aligned
rs1->fw(), // opp fw
scUnMapped,
xeq);
SeedAlSumm ssm1, ssm2;
sr1->toSeedAlSumm(ssm1);
sr2->toSeedAlSumm(ssm2);
for(size_t i = 0; i < rs1_.size(); i++) {
rs1_[i].setMateParams(ALN_RES_TYPE_MATE1, &rs2_[i], flags1);
rs2_[i].setMateParams(ALN_RES_TYPE_MATE2, &rs1_[i], flags2);
assert(rs1_[i].isFraglenSet() == rs2_[i].isFraglenSet());
assert(!rs1_[i].isFraglenSet() || abs(rs1_[i].fragmentLength()) == abs(rs2_[i].fragmentLength()));
}
AlnScore bestUScore, bestP1Score, bestP2Score, bestCScore;
AlnScore bestUDist, bestP1Dist, bestP2Dist, bestCDist;
AlnScore bestUnchosenUScore, bestUnchosenP1Score, bestUnchosenP2Score, bestUnchosenCScore;
AlnScore bestUnchosenUDist, bestUnchosenP1Dist, bestUnchosenP2Dist, bestUnchosenCDist;
ASSERT_ONLY(size_t off =) selectByScore(
&rs1_, &rs2_,
ndiscord, select1_,
&rs1u_, &rs2u_,
bestUScore,
bestUDist,
bestP1Score,
bestP1Dist,
bestP2Score,
bestP2Dist,
bestCScore,
bestCDist,
bestUnchosenUScore,
bestUnchosenUDist,
bestUnchosenP1Score,
bestUnchosenP1Dist,
bestUnchosenP2Score,
bestUnchosenP2Dist,
bestUnchosenCScore,
bestUnchosenCDist,
rnd);
discordSumm.setBest(
bestUScore,
bestUDist,
bestP1Score,
bestP1Dist,
bestP2Score,
bestP2Dist,
bestCScore,
bestCDist,
bestUnchosenUScore,
bestUnchosenUDist,
bestUnchosenP1Score,
bestUnchosenP1Dist,
bestUnchosenP2Score,
bestUnchosenP2Dist,
bestUnchosenCScore,
bestUnchosenCDist);
assert_eq(0, off);
assert(!select1_.empty());
g_.reportHits(
obuf_,
staln_,
threadid_,
rd1_,
rd2_,
rdid_,
select1_,
NULL,
&rs1_,
&rs2_,
pairMax,
discordSumm,
ssm1,
ssm2,
&flags1,
&flags2,
prm,
mapq_,
sc,
false);
met.nconcord_0++;
met.ndiscord++;
init_ = false;
//g_.outq().finishRead(obuf_, rdid_, threadid_);
return;
}
// If we're at this point, at least one mate failed to align.
// BTL: That's not true. It could be that there are no concordant
// alignments but both mates have unpaired alignments, with one of
// the mates having more than one.
//assert(nunpair1 == 0 || nunpair2 == 0);
assert(!pairMax);
// Update counters given that one mate didn't align
if(readIsPair()) {
met.nconcord_0++;
}
if(rd1_ != NULL) {
if(nunpair1 > 0) {
// Update counters
if(readIsPair()) {
if(unpair1Max) met.nunp_0_rep++;
else {
met.nunp_0_uni++;
assert(!rs1u_.empty());
if(rs1u_.size() == 1) {
met.nunp_0_uni1++;
} else {
met.nunp_0_uni2++;
}
}
} else {
if(unpair1Max) met.nunp_rep++;
else {
met.nunp_uni++;
assert(!rs1u_.empty());
if(rs1u_.size() == 1) {
met.nunp_uni1++;