-
Notifications
You must be signed in to change notification settings - Fork 66
/
PairedAligner.cpp
660 lines (548 loc) · 24.3 KB
/
PairedAligner.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
/*++
Module Name:
PairedAligner.cpp
Abstract:
Functions for running the paired end aligner sub-program.
Authors:
Matei Zaharia, February, 2012
Environment:
`
User mode service.
Revision History:
Adapted from cSNAP, which was in turn adapted from the scala prototype
--*/
//
// TODO: This is really similar to the single-end aligner overall. It would be nice
// to avoid code duplication.
//
#include "stdafx.h"
#include "options.h"
#include <time.h>
#include "Compat.h"
#include "RangeSplitter.h"
#include "GenomeIndex.h"
#include "Range.h"
#include "SAM.h"
#include "ChimericPairedEndAligner.h"
#include "Tables.h"
#include "AlignerOptions.h"
#include "AlignerContext.h"
#include "AlignerStats.h"
#include "FASTQ.h"
#include "PairedAligner.h"
#include "MultiInputReadSupplier.h"
#include "Util.h"
#include "IntersectingPairedEndAligner.h"
#include "exit.h"
#include "Error.h"
using namespace std;
using util::stringEndsWith;
static const int DEFAULT_MIN_SPACING = 50;
static const int DEFAULT_MAX_SPACING = 1000;
bool
isFLT3ITD(Read *read, GenomeIndex *index, LandauVishkin<1> *lv, SingleAlignmentResult *result);
struct PairedAlignerStats : public AlignerStats
{
// TODO: make these constants configurable
static const int MAX_DISTANCE = 1000;
static const int MAX_SCORE = 15;
_int64 sameComplement;
_int64* distanceCounts; // histogram of distances
// TODO: could save a bit of memory & time since this is a triangular matrix
_int64* scoreCounts; // 2-d histogram of scores for paired ends
static const unsigned maxMapq = 70;
static const unsigned nTimeBuckets = 32;
static const unsigned nHitsBuckets = 32;
static const unsigned nLVCallsBuckets = 32;
_int64 alignTogetherByMapqHistogram[maxMapq+1][nTimeBuckets];
_int64 totalTimeByMapqHistogram[maxMapq+1][nTimeBuckets];
_int64 nSmallHitsByTimeHistogram[nHitsBuckets][nTimeBuckets];
_int64 nLVCallsByTimeHistogram[nLVCallsBuckets][nTimeBuckets];
_int64 mapqByNLVCallsHistogram[maxMapq+1][nLVCallsBuckets];
_int64 mapqByNSmallHitsHistogram[maxMapq+1][nHitsBuckets];
PairedAlignerStats(AbstractStats* i_extra = NULL);
virtual ~PairedAlignerStats();
inline void incrementDistance(int distance) {
distanceCounts[max(0, min(MAX_DISTANCE, distance))]++;
}
inline void incrementScore(int s0, int s1)
{
// ensure s0 <= s1, both within range
s0 = max(0, min(MAX_SCORE, s0));
s1 = max(0, min(MAX_SCORE, s1));
if (s0 > s1) {
int t = s0; s0 = s1; s1 = t;
}
scoreCounts[s0*(MAX_SCORE+1)+s1]++;
}
inline void recordAlignTogetherMapqAndTime(unsigned mapq, _int64 timeInNanos, unsigned nSmallHits, unsigned nLVCalls) {
int timeBucket;
_int64 dividedTime = timeInNanos;
for (timeBucket = 0; timeBucket < nTimeBuckets-1; timeBucket++) {
if (dividedTime == 0) break;
dividedTime /= 2;
}
alignTogetherByMapqHistogram[mapq][timeBucket]++;
totalTimeByMapqHistogram[mapq][timeBucket] += timeInNanos;
int nHitsBucket;
int dividedHits = nSmallHits;
for (nHitsBucket = 0; nHitsBucket < nHitsBuckets; nHitsBucket++) {
if (0 == dividedHits) break;
dividedHits /= 2;
}
_ASSERT((char *)&nSmallHitsByTimeHistogram[nHitsBucket][timeBucket] < (char *)(this + 1));
nSmallHitsByTimeHistogram[nHitsBucket][timeBucket]++;
int nLVCallsBucket;
int dividedLVCalls = nLVCalls;
for (nLVCallsBucket = 0; nLVCallsBucket < nLVCallsBuckets; nLVCallsBucket++) {
if (dividedLVCalls == 0) break;
dividedLVCalls /= 2;
}
_ASSERT((char *)&nLVCallsByTimeHistogram[nLVCallsBucket][timeBucket] < (char *)(this + 1));
nLVCallsByTimeHistogram[nLVCallsBucket][timeBucket]++;
_ASSERT((char *)&mapqByNLVCallsHistogram[mapq][nLVCallsBucket] < (char *)(this + 1));
mapqByNLVCallsHistogram[mapq][nLVCallsBucket]++;
_ASSERT((char *)&mapqByNSmallHitsHistogram[mapq][nHitsBucket] < (char *)(this + 1));
mapqByNSmallHitsHistogram[mapq][nHitsBucket]++;
}
virtual void add(const AbstractStats * other);
virtual void printHistograms(FILE* output);
};
const int PairedAlignerStats::MAX_DISTANCE;
const int PairedAlignerStats::MAX_SCORE;
PairedAlignerStats::PairedAlignerStats(AbstractStats* i_extra)
: AlignerStats(i_extra),
sameComplement(0)
{
int dsize = sizeof(_int64) * (MAX_DISTANCE+1);
distanceCounts = (_int64*)BigAlloc(dsize);
memset(distanceCounts, 0, dsize);
int ssize = sizeof(_int64) * (MAX_SCORE+1)*(MAX_SCORE+1);
scoreCounts = (_int64*)BigAlloc(ssize);
memset(scoreCounts, 0, ssize);
for (unsigned mapq = 0; mapq <= maxMapq; mapq++) {
for (unsigned timeBucket = 0; timeBucket < nTimeBuckets; timeBucket++) {
alignTogetherByMapqHistogram[mapq][timeBucket] = 0;
totalTimeByMapqHistogram[mapq][timeBucket] = 0;
}
for (unsigned smallHits = 0; smallHits < nHitsBuckets; smallHits++) {
mapqByNSmallHitsHistogram[mapq][smallHits] = 0;
}
for (unsigned lvCalls = 0; lvCalls < nLVCallsBuckets; lvCalls++) {
mapqByNLVCallsHistogram[mapq][lvCalls] = 0;
}
}
for (unsigned timeBucket = 0; timeBucket < nTimeBuckets; timeBucket++) {
for (unsigned smallHits = 0; smallHits < nHitsBuckets; smallHits++) {
nSmallHitsByTimeHistogram[smallHits][timeBucket] = 0;
}
for (unsigned lvCalls = 0; lvCalls < nLVCallsBuckets; lvCalls++) {
nLVCallsByTimeHistogram[lvCalls][timeBucket] = 0;
}
}
}
PairedAlignerStats::~PairedAlignerStats()
{
BigDealloc(distanceCounts);
BigDealloc(scoreCounts);
}
void PairedAlignerStats::add(const AbstractStats * i_other)
{
AlignerStats::add(i_other);
PairedAlignerStats* other = (PairedAlignerStats*) i_other;
for (int i = 0; i < MAX_DISTANCE + 1; i++) {
distanceCounts[i] += other->distanceCounts[i];
}
for (int i = 0; i < (MAX_SCORE + 1) * (MAX_SCORE + 1); i++) {
scoreCounts[i] += other->scoreCounts[i];
}
for (unsigned mapq = 0; mapq <= maxMapq; mapq++) {
for (unsigned timeBucket = 0; timeBucket < nTimeBuckets; timeBucket++) {
alignTogetherByMapqHistogram[mapq][timeBucket] += other->alignTogetherByMapqHistogram[mapq][timeBucket];
totalTimeByMapqHistogram[mapq][timeBucket] += other->totalTimeByMapqHistogram[mapq][timeBucket];
}
for (unsigned smallHits = 0; smallHits < nHitsBuckets; smallHits++) {
mapqByNSmallHitsHistogram[mapq][smallHits] += other->mapqByNSmallHitsHistogram[mapq][smallHits];
}
for (unsigned lvCalls = 0; lvCalls < nLVCallsBuckets; lvCalls++) {
mapqByNLVCallsHistogram[mapq][lvCalls] += other->mapqByNLVCallsHistogram[mapq][lvCalls];
}
}
for (unsigned timeBucket = 0; timeBucket < nTimeBuckets; timeBucket++) {
for (unsigned smallHits = 0; smallHits < nHitsBuckets; smallHits++) {
nSmallHitsByTimeHistogram[smallHits][timeBucket] += other->nSmallHitsByTimeHistogram[smallHits][timeBucket];
}
for (unsigned lvCalls = 0; lvCalls < nLVCallsBuckets; lvCalls++) {
nLVCallsByTimeHistogram[lvCalls][timeBucket] += other->nLVCallsByTimeHistogram[lvCalls][timeBucket];
}
}
}
void PairedAlignerStats::printHistograms(FILE* output)
{
AlignerStats::printHistograms(output);
}
PairedAlignerOptions::PairedAlignerOptions(const char* i_commandLine)
: AlignerOptions(i_commandLine, true),
minSpacing(DEFAULT_MIN_SPACING),
maxSpacing(DEFAULT_MAX_SPACING),
forceSpacing(false),
intersectingAlignerMaxHits(DEFAULT_INTERSECTING_ALIGNER_MAX_HITS),
maxCandidatePoolSize(DEFAULT_MAX_CANDIDATE_POOL_SIZE),
quicklyDropUnpairedReads(true)
{
}
void PairedAlignerOptions::usageMessage()
{
AlignerOptions::usageMessage();
WriteErrorMessage(
" -s min and max spacing to allow between paired ends (default: %d %d).\n"
" -fs force spacing to lie between min and max.\n"
" -H max hits for intersecting aligner (default: %d).\n"
" -mcp specifies the maximum candidate pool size (An internal data structure. \n"
" Only increase this if you get an error message saying to do so. If you're running\n"
" out of memory, you may want to reduce it. Default: %d)\n"
" -F b additional option to -F to require both mates to satisfy filter (default is just one)\n",
" out of memory, you may want to reduce it. Default: %d).\n"
" -ku Keep unpaired-looking reads in SAM/BAM input. Ordinarily, if a read doesn't specify\n"
" mate information (RNEXT field is * and/or PNEXT is 0) then the code that matches reads will immdeiately\n"
" discard it. Specifying this flag may cause large memory usage for some input files,\n"
" but may be necessary for some strangely formatted input files. You'll also need to specify this\n"
" flag for SAM/BAM files that were aligned by a single-end aligner.\n"
,
DEFAULT_MIN_SPACING,
DEFAULT_MAX_SPACING,
DEFAULT_INTERSECTING_ALIGNER_MAX_HITS,
DEFAULT_MAX_CANDIDATE_POOL_SIZE);
}
bool PairedAlignerOptions::parse(const char** argv, int argc, int& n, bool *done)
{
*done = false;
if (strcmp(argv[n], "-s") == 0) {
if (n + 2 < argc) {
minSpacing = atoi(argv[n+1]);
maxSpacing = atoi(argv[n+2]);
n += 2;
return true;
}
return false;
} else if (strcmp(argv[n], "-H") == 0) {
if (n + 1 < argc) {
intersectingAlignerMaxHits = atoi(argv[n+1]);
n += 1;
return true;
}
return false;
} else if (strcmp(argv[n], "-fs") == 0) {
forceSpacing = true;
return true;
} else if (strcmp(argv[n], "-ku") == 0) {
quicklyDropUnpairedReads = false;
return true;
} else if (strcmp(argv[n], "-mcp") == 0) {
if (n + 1 < argc) {
maxCandidatePoolSize = atoi(argv[n+1]);
n += 1;
return true;
}
return false;
} else if (strcmp(argv[n], "-F") == 0 && n + 1 < argc && strcmp(argv[n + 1],"b") == 0) {
filterFlags |= FilterBothMatesMatch;
n += 1;
return true;
}
return AlignerOptions::parse(argv, argc, n, done);
}
PairedAlignerContext::PairedAlignerContext(AlignerExtension* i_extension)
: AlignerContext( 0, NULL, NULL, i_extension)
{
}
void PairedAlignerContext::initialize()
{
AlignerContext::initialize();
PairedAlignerOptions* options2 = (PairedAlignerOptions*) options;
minSpacing = options2->minSpacing;
maxSpacing = options2->maxSpacing;
forceSpacing = options2->forceSpacing;
maxCandidatePoolSize = options2->maxCandidatePoolSize;
intersectingAlignerMaxHits = options2->intersectingAlignerMaxHits;
ignoreMismatchedIDs = options2->ignoreMismatchedIDs;
quicklyDropUnpairedReads = options2->quicklyDropUnpairedReads;
noUkkonen = options->noUkkonen;
noOrderedEvaluation = options->noOrderedEvaluation;
}
AlignerStats* PairedAlignerContext::newStats()
{
return new PairedAlignerStats();
}
void PairedAlignerContext::runTask()
{
ParallelTask<PairedAlignerContext> task(this);
task.run();
}
void PairedAlignerContext::runIterationThread()
{
PreventMachineHibernationWhileThisThreadIsAlive();
PairedReadSupplier *supplier = pairedReadSupplierGenerator->generateNewPairedReadSupplier();
if (NULL == supplier) {
//
// No work for this thread to do.
//
return;
}
if (extension->runIterationThread(supplier, this)) {
delete supplier;
return;
}
if (index == NULL) {
// no alignment, just input/output
Read *read0;
Read *read1;
PairedAlignmentResult result;
memset(&result, 0, sizeof(result));
result.location[0] = result.location[1] = InvalidGenomeLocation;
while (supplier->getNextReadPair(&read0,&read1)) {
// Check that the two IDs form a pair; they will usually be foo/1 and foo/2 for some foo.
if (!ignoreMismatchedIDs && !readIdsMatch(read0, read1)) {
unsigned n[2] = {min(read0->getIdLength(), 200u), min(read1->getIdLength(), 200u)};
char* p[2] = {(char*) alloca(n[0] + 1), (char*) alloca(n[1] + 1)};
memcpy(p[0], read0->getId(), n[0]); p[0][n[0]] = 0;
memcpy(p[1], read1->getId(), n[1]); p[1][n[1]] = 0;
WriteErrorMessage( "Unmatched read IDs '%s' and '%s'. Use the -I option to ignore this.\n", p[0], p[1]);
soft_exit(1);
}
stats->totalReads += 2;
writePair(read0, read1, &result, false);
}
delete supplier;
return;
}
int maxReadSize = MAX_READ_LENGTH;
size_t memoryPoolSize = IntersectingPairedEndAligner::getBigAllocatorReservation(index, intersectingAlignerMaxHits, maxReadSize, index->getSeedLength(),
numSeedsFromCommandLine, seedCoverage, maxDist, extraSearchDepth, maxCandidatePoolSize);
memoryPoolSize += ChimericPairedEndAligner::getBigAllocatorReservation(index, maxReadSize, maxHits, index->getSeedLength(), numSeedsFromCommandLine, seedCoverage, maxDist,
extraSearchDepth, maxCandidatePoolSize);
unsigned maxPairedSecondaryHits;
unsigned maxSingleSecondaryHits;
if (maxSecondaryAligmmentAdditionalEditDistance < 0) {
maxPairedSecondaryHits = 0;
maxSingleSecondaryHits = 0;
} else {
maxPairedSecondaryHits = IntersectingPairedEndAligner::getMaxSecondaryResults(numSeedsFromCommandLine, seedCoverage, maxReadSize, maxHits, index->getSeedLength(), minSpacing, maxSpacing);
maxSingleSecondaryHits = ChimericPairedEndAligner::getMaxSingleEndSecondaryResults(numSeedsFromCommandLine, seedCoverage, maxReadSize, maxHits, index->getSeedLength());
}
memoryPoolSize += maxPairedSecondaryHits * sizeof(PairedAlignmentResult) + maxSingleSecondaryHits * sizeof(SingleAlignmentResult);
BigAllocator *allocator = new BigAllocator(memoryPoolSize);
IntersectingPairedEndAligner *intersectingAligner = new (allocator) IntersectingPairedEndAligner(index, maxReadSize, maxHits, maxDist, numSeedsFromCommandLine,
seedCoverage, minSpacing, maxSpacing, intersectingAlignerMaxHits, extraSearchDepth,
maxCandidatePoolSize, allocator, noUkkonen, noOrderedEvaluation);
ChimericPairedEndAligner *aligner = new (allocator) ChimericPairedEndAligner(
index,
maxReadSize,
maxHits,
maxDist,
numSeedsFromCommandLine,
seedCoverage,
forceSpacing,
extraSearchDepth,
noUkkonen,
noOrderedEvaluation,
intersectingAligner,
allocator);
LandauVishkin<1> lv;
allocator->checkCanaries();
PairedAlignmentResult *secondaryResults = (PairedAlignmentResult *)allocator->allocate(maxPairedSecondaryHits * sizeof(*secondaryResults));
SingleAlignmentResult *singleSecondaryResults = (SingleAlignmentResult *)allocator->allocate(maxSingleSecondaryHits * sizeof(*singleSecondaryResults));
ReadWriter *readWriter = this->readWriter;
#ifdef _MSC_VER
if (options->useTimingBarrier) {
if (0 == InterlockedDecrementAndReturnNewValue(nThreadsAllocatingMemory)) {
AllowEventWaitersToProceed(memoryAllocationCompleteBarrier);
} else {
WaitForEvent(memoryAllocationCompleteBarrier);
}
}
#endif // _MSC_VER
// Align the reads.
Read *read0;
Read *read1;
_uint64 lastReportTime = timeInMillis();
_uint64 readsWhenLastReported = 0;
while (supplier->getNextReadPair(&read0,&read1)) {
// Check that the two IDs form a pair; they will usually be foo/1 and foo/2 for some foo.
if (!ignoreMismatchedIDs) {
Read::checkIdMatch(read0, read1);
}
stats->totalReads += 2;
// Skip the pair if there are too many Ns or 2s.
int maxDist = this->maxDist;
bool useful0 = read0->getDataLength() >= 50 && (int)read0->countOfNs() <= maxDist;
bool useful1 = read1->getDataLength() >= 50 && (int)read1->countOfNs() <= maxDist;
if (!useful0 && !useful1) {
PairedAlignmentResult result;
result.status[0] = NotFound;
result.status[1] = NotFound;
result.location[0] = InvalidGenomeLocation;
result.location[1] = InvalidGenomeLocation;
//writePair(read0, read1, &result, false);
continue;
} else {
// Here one the reads might still be hopeless, but maybe we can align the other.
stats->usefulReads += (useful0 && useful1) ? 2 : 1;
}
if (AlignerOptions::useHadoopErrorMessages && stats->totalReads % 10000 == 0 && timeInMillis() - lastReportTime > 10000) {
fprintf(stderr,"reporter:counter:SNAP,readsAligned,%lu\n",stats->totalReads - readsWhenLastReported);
readsWhenLastReported = stats->totalReads;
lastReportTime = timeInMillis();
}
PairedAlignmentResult result;
#if TIME_HISTOGRAM
_int64 startTime = timeInNanos();
#endif // TIME_HISTOGRAM
int nSecondaryResults;
int nSingleSecondaryResults[2];
aligner->align(read0, read1, &result, maxSecondaryAligmmentAdditionalEditDistance, maxPairedSecondaryHits, &nSecondaryResults, secondaryResults,
maxSingleSecondaryHits, &nSingleSecondaryResults[0], &nSingleSecondaryResults[1],singleSecondaryResults);
Read *reads[NUM_READS_PER_PAIR] = {read0, read1};
result.correctAlignmentForSoftClipping(reads, index->getGenome());
for (int i = 0; i < nSecondaryResults; i++) {
secondaryResults[i].correctAlignmentForSoftClipping(reads, index->getGenome());
}
for (int i = 0; i < nSingleSecondaryResults[0]; i++) {
singleSecondaryResults[i].correctAlignmentForSoftClipping(read0, index->getGenome());
}
for (int i = nSingleSecondaryResults[0]; i < nSingleSecondaryResults[0] + nSingleSecondaryResults[1]; i++) {
singleSecondaryResults[i].correctAlignmentForSoftClipping(read1, index->getGenome());
}
#if TIME_HISTOGRAM
_int64 runTime = timeInNanos() - startTime;
int timeBucket = min(30, cheezyLogBase2(runTime));
stats->countByTimeBucket[timeBucket]++;
stats->nanosByTimeBucket[timeBucket] += runTime;
#endif // TIME_HISTOGRAM
unsigned numUnaligned = 0;
unsigned whichUnaligned;
for (int i = 0; i < NUM_READS_PER_PAIR; i++) {
if (result.status[i] == NotFound) {
numUnaligned++;
whichUnaligned = i;
}
}
extern GenomeLocation flt3itdLowerBound, flt3itdUpperBound;
SingleAlignmentResult singleResult;
if (1 == numUnaligned && result.location[1 - whichUnaligned] > flt3itdLowerBound - 1000 && result.location[1 - whichUnaligned] < flt3itdUpperBound + 1000 && isFLT3ITD(reads[whichUnaligned], index, &lv, &singleResult)) {
result.status[whichUnaligned] = singleResult.status;
result.location[whichUnaligned] = singleResult.location;
result.direction[whichUnaligned] = singleResult.direction;
result.status[1 - whichUnaligned] = NotFound;
} else {
for (int i = 0; i < NUM_READS_PER_PAIR; i++) {
result.status[i] = NotFound;
}
}
#if 0
if (forceSpacing && isOneLocation(result.status[0]) != isOneLocation(result.status[1])) {
// either both align or neither do
result.status[0] = result.status[1] = NotFound;
result.location[0] = result.location[1] = InvalidGenomeLocation;
}
writePair(read0, read1, &result, false);
for (int i = 0; i < nSecondaryResults; i++) {
writePair(read0, read1, secondaryResults + i, true);
}
for (int i = 0; i < nSingleSecondaryResults[0] + nSingleSecondaryResults[1]; i++) {
Read *read = i < nSingleSecondaryResults[0] ? read0 : read1;
if (readWriter != NULL && (options->passFilter(read, singleSecondaryResults[i].status))) {
readWriter->writeRead(read, singleSecondaryResults[i].status, singleSecondaryResults[i].mapq, singleSecondaryResults[i].location, singleSecondaryResults[i].direction, true);
}
}
#endif // 0
updateStats((PairedAlignerStats*) stats, read0, read1, &result);
}
stats->lvCalls = aligner->getLocationsScored();
allocator->checkCanaries();
aligner->~ChimericPairedEndAligner();
delete supplier;
intersectingAligner->~IntersectingPairedEndAligner();
delete allocator;
}
void PairedAlignerContext::writePair(Read* read0, Read* read1, PairedAlignmentResult* result, bool secondary)
{
bool pass0 = options->passFilter(read0, result->status[0]);
bool pass1 = options->passFilter(read1, result->status[1]);
bool pass = (options->filterFlags & AlignerOptions::FilterBothMatesMatch)
? (pass0 && pass1) : (pass0 || pass1);
if (readWriter != NULL && pass) {
readWriter->writePair(read0, read1, result, secondary);
}
}
void PairedAlignerContext::updateStats(PairedAlignerStats* stats, Read* read0, Read* read1, PairedAlignmentResult* result)
{
// Update stats
for (int r = 0; r < 2; r++) {
if (isOneLocation(result->status[r])) {
stats->singleHits++;
} else if (result->status[r] == MultipleHits) {
stats->multiHits++;
} else {
_ASSERT(result->status[r] == NotFound);
stats->notFound++;
}
// Add in MAPQ stats
if (result->status[r] != NotFound) {
int mapq = result->mapq[r];
_ASSERT(mapq >= 0 && mapq <= AlignerStats::maxMapq);
stats->mapqHistogram[mapq]++;
}
}
if (result->direction[0] == result->direction[1]) {
stats->sameComplement++;
}
if (isOneLocation(result->status[0]) && isOneLocation(result->status[1])) {
stats->incrementDistance(abs((int) (result->location[0] - result->location[1])));
stats->incrementScore(result->score[0], result->score[1]);
}
if (result->fromAlignTogether) {
stats->recordAlignTogetherMapqAndTime(__max(result->mapq[0], result->mapq[1]), result->nanosInAlignTogether, result->nSmallHits, result->nLVCalls);
}
if (result->alignedAsPair) {
stats->alignedAsPairs += 2; // They are a pair, after all. Hence, +2.
}
}
void
PairedAlignerContext::typeSpecificBeginIteration()
{
if (1 == options->nInputs) {
//
// We've only got one input, so just connect it directly to the consumer.
//
pairedReadSupplierGenerator = options->inputs[0].createPairedReadSupplierGenerator(options->numThreads, quicklyDropUnpairedReads, readerContext);
} else {
//
// We've got multiple inputs, so use a MultiInputReadSupplier to combine the individual inputs.
//
PairedReadSupplierGenerator **generators = new PairedReadSupplierGenerator *[options->nInputs];
// use separate context for each supplier, initialized from common
for (int i = 0; i < options->nInputs; i++) {
ReaderContext context(readerContext);
generators[i] = options->inputs[i].createPairedReadSupplierGenerator(options->numThreads, quicklyDropUnpairedReads, context);
}
pairedReadSupplierGenerator = new MultiInputPairedReadSupplierGenerator(options->nInputs,generators);
}
ReaderContext* context = pairedReadSupplierGenerator->getContext();
readerContext.header = context->header;
readerContext.headerBytes = context->headerBytes;
readerContext.headerLength = context->headerLength;
readerContext.headerMatchesIndex = context->headerMatchesIndex;
}
void
PairedAlignerContext::typeSpecificNextIteration()
{
if (readerContext.header != NULL) {
delete [] readerContext.header;
readerContext.header = NULL;
readerContext.headerLength = readerContext.headerBytes = 0;
readerContext.headerMatchesIndex = false;
}
delete pairedReadSupplierGenerator;
pairedReadSupplierGenerator = NULL;
}