File: PairedReadMatcher.cpp

package info (click to toggle)
snap-aligner 1.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,988 kB
  • sloc: cpp: 36,500; ansic: 5,239; python: 227; makefile: 85; sh: 28
file content (433 lines) | stat: -rw-r--r-- 16,891 bytes parent folder | download
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
/*++

Module Name:

    PairedReadMatcher.cpp

Abstract:

    Match paired-end reads coming in from different streams

Authors:

    Bill Bolosky, November, 2012

Environment:

    User mode service.

Revision History:


--*/

#include "stdafx.h"
#include <map>
#include "Compat.h"
#include "Util.h"
#include "Read.h"
#include "DataReader.h"
#include "VariableSizeMap.h"
#include "PairedEndAligner.h"
#include "SAM.h"
#include "Error.h"

// turn on to debug matching process
//#define VALIDATE_MATCH

// turn on to gather paired stats
//#define STATISTICS

using std::pair;


class PairedReadMatcher: public PairedReadReader
{
public:
    PairedReadMatcher(ReadReader* i_single, bool i_quicklyDropUnpairedReads);

    // PairedReadReader

    virtual ~PairedReadMatcher();

    virtual bool getNextReadPair(Read *read1, Read *read2);
    
    virtual void reinit(_int64 startingOffset, _int64 amountOfFileToProcess)
    { single->reinit(startingOffset, amountOfFileToProcess); }

    virtual void holdBatch(DataBatch batch)
    { single->holdBatch(batch); }

    virtual bool releaseBatch(DataBatch batch);

    virtual ReaderContext* getContext()
    { return single->getContext(); }

private:

    ReadWithOwnMemory* allocOverflowRead();
    void freeOverflowRead(ReadWithOwnMemory* read);
    
    ReadReader* single; // reader for single reads
    typedef _uint64 StringHash;
    typedef VariableSizeMap<StringHash,Read> ReadMap;
    DataBatch currentBatch; // for dropped reads
    bool allDroppedInCurrentBatch;
    DataBatch batch[2]; // 0 = current, 1 = previous
    ReadMap unmatched[2]; // read id -> Read
    typedef VariableSizeMap<PairedReadMatcher::StringHash,ReadWithOwnMemory*,150,MapNumericHash<PairedReadMatcher::StringHash>,80,0,true> OverflowMap;
    OverflowMap overflow; // read id -> Read
    typedef VariableSizeVector<ReadWithOwnMemory*> OverflowReadVector;
    OverflowReadVector blocks; // BigAlloc blocks
    static const int BlockSize = 10000; // # ReadWithOwnMemory per block
    ExclusiveLock blockLock; // protects adding to blocks list
    ReadWithOwnMemory* freeList; // head of free list, NULL if empty, use interlocked ops to update
    typedef VariableSizeMap<_uint64,OverflowReadVector*> OverflowReadReleaseMap;
    OverflowReadReleaseMap overflowRelease;
#ifdef VALIDATE_MATCH
    typedef VariableSizeMap<StringHash,char*> StringMap;
    StringMap strings;
    typedef VariableSizeMap<StringHash,int> HashSet;
    HashSet overflowUsed;
#endif
    int overflowTotal, overflowPeak;

    bool quicklyDropUnpairedReads;
    _uint64 nReadsQuicklyDropped;

    Read localRead;

#ifdef STATISTICS
    typedef struct
    {
        _int64 oldPairs; // # pairs matched from overflow
        _int64 oldBatches; // # distinct matches matched from overflow
        _int64 internalPairs; // #pairs matched within batch
        _int64 previousPairs; // #pairs matched with previous batch
        _int64 overflowPairs; // #pairs left over
        _int64 totalReads; // total reads in batch
        void clear() { memset(this, 0, sizeof(*this)); }
    } BatchStats;
    BatchStats currentStats, totalStats;
    VariableSizeMap<_int64,int> currentBatches;
#endif
};

PairedReadMatcher::PairedReadMatcher(
    ReadReader* i_single,
    bool i_quicklyDropUnpairedReads)
    : single(i_single),
    overflowTotal(0), overflowPeak(0),
    quicklyDropUnpairedReads(i_quicklyDropUnpairedReads),
    nReadsQuicklyDropped(0), freeList(NULL),
    currentBatch(0, 0), allDroppedInCurrentBatch(false)
{
    new (&unmatched[0]) VariableSizeMap<StringHash,Read>(10000);
    new (&unmatched[1]) VariableSizeMap<StringHash,Read>(10000);
    InitializeExclusiveLock(&blockLock);
#ifdef STATISTICS
    currentStats.clear();
    totalStats.clear();
#endif
}
    
PairedReadMatcher::~PairedReadMatcher()
{
    for (OverflowReadVector::iterator i = blocks.begin(); i != blocks.end(); i++) {
        BigDealloc(*i);
    }
    delete single;
	DestroyExclusiveLock(&blockLock);
}

    ReadWithOwnMemory*
PairedReadMatcher::allocOverflowRead()
{
    while (true) {
        ReadWithOwnMemory* next = freeList;
        if (next == NULL) {
            // alloc & init a new block of reads
            ReadWithOwnMemory* block = (ReadWithOwnMemory*) BigAlloc(BlockSize * sizeof(ReadWithOwnMemory));
            AcquireExclusiveLock(&blockLock);
            blocks.push_back(block);
            ReleaseExclusiveLock(&blockLock);
            for (int i = 0; i < BlockSize - 1; i++) {
                *(ReadWithOwnMemory**)&block[i] = &block[i+1];
            }
            while (true) {
                ReadWithOwnMemory* head = freeList;
                *(ReadWithOwnMemory**)&block[BlockSize-1] = head;
                if (InterlockedCompareExchangePointerAndReturnOldValue((void*volatile*)&freeList, block, head) == head) {
                    break;
                }
            }
        } else {
            ReadWithOwnMemory* nextHead = *(ReadWithOwnMemory**)next;
            if (InterlockedCompareExchangePointerAndReturnOldValue((void*volatile*)&freeList, nextHead, next) == next) {
                return next;
            }
        }
    }
}

    void
PairedReadMatcher::freeOverflowRead(
    ReadWithOwnMemory* read)
{
    while (true) {
        ReadWithOwnMemory* head = freeList;
        *(ReadWithOwnMemory**)read = head;
        if (InterlockedCompareExchangePointerAndReturnOldValue((void*volatile*)&freeList, read, head) == head) {
            return;
        }
    }
}

    bool
PairedReadMatcher::getNextReadPair(
    Read *read1,
    Read *read2)
{
    Read *outputReads[NUM_READS_PER_PAIR];
    outputReads[0] = read1;
    outputReads[1] = read2;
    int readOneToOutputRead;    // This is used to determine which of the output reads corresponds to one (the read that just came from getNextRead())
                                // That, in turn, is determined by the S/BAM flags in the read saying whether it was first-in-template.

    int skipped = 0;
    while (true) {
        if (skipped++ == 10000) {
            WriteErrorMessage( "warning: no matching read pairs in 10,000 reads, input file might be unsorted or have unexpected read id format\n");
        }

        if (! single->getNextRead(&localRead)) {
#ifdef USE_DEVTEAM_OPTIONS
            WriteErrorMessage("overflow total %d, peak %d\n", overflowTotal, overflowPeak);
#endif
            int n = unmatched[0].size() + unmatched[1].size() + overflow.size();
            if (n > 0) {
                WriteErrorMessage( " warning: PairedReadMatcher discarding %d unpaired reads at eof\n", n);
#ifdef USE_DEVTEAM_OPTIONS
                int printed = 0;
                char buffer[200];
                for (OverflowMap::iterator i = overflow.begin(); i != overflow.end() && printed < 10; i = overflow.next(i)) {
                    int l = min((unsigned) sizeof(buffer)-1, i->value->getIdLength());
                    memcpy(buffer, i->value->getId(), l);
                    buffer[l] = 0;
                    WriteErrorMessage("%s\n", buffer);
                    printed++;
                }
#endif
#ifdef VALIDATE_MATCH
                for (int i = 0; i < 2; i++) {
                    fprintf(stdout, "unmatched[%d]\n", i);
                    for (ReadMap::iterator j = unmatched[i].begin(); j != unmatched[i].end(); j = unmatched[i].next(j)) {
                        fprintf(stdout, "%s\n", strings[j->key]);
                    }
                }
                int printed = 0;
                fprintf(stdout, "sample of overflow\n");
                for (OverflowMap::iterator o = overflow.begin(); printed < 500 && o != overflow.end(); o = overflow.next(o)) {
                    if (NULL == overflowUsed.tryFind(o->key)) {
                        printed++;
                        fprintf(stdout, "%s\n", strings[o->key]);
                    }
                }
#endif
            }
            if (nReadsQuicklyDropped > 0) {
                WriteErrorMessage(" warning: PairedReadMatcher dropped %lld reads because they didn't have RNEXT and PNEXT filled in.\n"
                               " If your input file was generated by a single-end alignment (or this seems too big), use the -ku flag\n",
                    nReadsQuicklyDropped);
            }
            single->releaseBatch(batch[0]);
            single->releaseBatch(batch[1]);
            return false;
        }

        if (quicklyDropUnpairedReads) {
            if (((localRead.getOriginalSAMFlags() & SAM_NEXT_UNMAPPED) == 0) && (localRead.getOriginalPNEXT() == 0 || (localRead.getOriginalRNEXTLength() == 1 && localRead.getOriginalRNEXT()[0] == '*'))) {
                nReadsQuicklyDropped++;
                skipped--;
                continue;
            }
        }

        if (localRead.getOriginalSAMFlags() & SAM_FIRST_SEGMENT) {
            readOneToOutputRead = 0;
        } else {
            readOneToOutputRead = 1;
        }

        // build key for pending read table, removing /1 or /2 at end
        const char* id = localRead.getId();
        unsigned idLength = localRead.getIdLength();
        // truncate at space or slash
        char* slash = (char*) memchr((void*)id, '/', idLength);
        if (slash != NULL) {
            idLength = (unsigned)(slash - id);
        }
        char* space = (char*) memchr((void*)id, ' ', idLength);
        if (space != NULL) {
            idLength = (unsigned)(space - id);
        }
        StringHash key = util::hash64(id, idLength);
#ifdef VALIDATE_MATCH
        char* s = new char[idLength+1];
        memcpy(s, id, idLength);
        s[idLength] = 0;
        char** p = strings.tryFind(key);
        if (p != NULL && strcmp(*p, s)) {
          WriteErrorMessage( "hash collision %ld of %s and %s\n", key, *p, s);
          soft_exit(1);
        }
        if (p == NULL) {
          strings.put(key, s);
        }
#endif
        if (localRead.getBatch() != batch[0]) {
#ifdef STATISTICS
            currentStats.oldBatches = currentBatches.size();
            currentStats.overflowPairs = unmatched[1].size();
            totalStats.internalPairs += currentStats.internalPairs;
            totalStats.previousPairs += currentStats.previousPairs;
            totalStats.oldBatches += currentStats.oldBatches;
            totalStats.oldPairs += currentStats.oldPairs;
            totalStats.overflowPairs += currentStats.overflowPairs;
            totalStats.totalReads += currentStats.totalReads;
            fprintf(stderr,"batch %d:%d: internal %d pairs, previous %d pairs, old %d pairs from %d batches, overflow %d pairs\n"
                "cumulative: internal %d pairs, previous %d pairs, old %d pairs from %d batches, overflow %d pairs\n",
                batch[0].fileID, batch[0].batchID, currentStats.internalPairs, currentStats.previousPairs, currentStats.oldPairs, currentStats.oldBatches, currentStats.overflowPairs,
                totalStats.internalPairs, totalStats.previousPairs, totalStats.oldPairs, totalStats.oldBatches, totalStats.overflowPairs);
            currentStats.clear();
            currentBatches.clear();
#endif
            // roll over batches
            if (unmatched[1].size() > 0) {
                // copy remaining reads into overflow map
                //fprintf(stderr,"warning: PairedReadMatcher overflow %d unpaired reads from %d:%d\n", unmatched[1].size(), batch[1].fileID, batch[1].batchID); //!!
                //char* buf = (char*) alloca(500);
                for (ReadMap::iterator r = unmatched[1].begin(); r != unmatched[1].end(); r = unmatched[1].next(r)) {
                    ReadWithOwnMemory* p = allocOverflowRead();
                    new (p) ReadWithOwnMemory(r->value);
                    _ASSERT(p->getData()[0]);
                    overflow.put(r->key, p);
#ifdef VALIDATE_MATCH
                    char*s2 = *strings.tryFind(r->key);
                    int len = strlen(s2);
                    _ASSERT(! strncmp(s2, r->value.getId(), len));
                    ReadWithOwnMemory* rd = overflow.tryFind(r->key);
                    _ASSERT(! strncmp(s2, rd->getId(), len));
#endif
                    //memcpy(buf, r->value.getId(), r->value.getIdLength());
                    //buf[r->value.getIdLength()] = 0;
                    //fprintf(stderr, "overflow add %d:%d %s\n", batch[1].fileID, batch[1].batchID, buf);
                }
                overflowTotal += unmatched[1].size();
                overflowPeak = max(overflow.size(), overflowPeak);
            }
            for (ReadMap::iterator i = unmatched[1].begin(); i != unmatched[1].end(); i = unmatched[1].next(i)) {
                i->value.dispose();
            }
            unmatched[1].exchange(unmatched[0]);
            unmatched[0].clear();
            single->releaseBatch(batch[1]);
            batch[1] = batch[0];
            batch[0] = localRead.getBatch();
            single->holdBatch(batch[0]);
#ifdef STATISTICS
        currentStats.totalReads++;
#endif
        }

        ReadMap::iterator found = unmatched[0].find(key);
        if (found != unmatched[0].end()) {
            *outputReads[1-readOneToOutputRead] = found->value;
             //fprintf(stderr, "current matched %d:%d->%d:%d %s\n", outputReads[1-readOneToOutputRead]->getBatch().fileID, outputReads[1-readOneToOutputRead]->getBatch().batchID, batch[0].fileID, batch[0].batchID, outputReads[1-readOneToOutputRead]->getId()); //!!
            unmatched[0].erase(found->key);
#ifdef STATISTICS
            currentStats.internalPairs++;
#endif
        } else {
            // try previous batch
            found = unmatched[1].find(key);
            if (found == unmatched[1].end()) {
                // try overflow
                OverflowMap::iterator found2 = overflow.find(key);
                if (found2 == overflow.end()) {
                    // no match, remember it for later matching
                    unmatched[0].put(key, localRead);
                    _ASSERT(localRead.getData()[0] && unmatched[0][key].getData()[0]);
                    //fprintf(stderr, "unmatched add %d:%d %lx\n", batch[0].fileID, batch[0].batchID, key); //!!
                    continue;
                } else {
                    // copy data into read, move from overflow table to release vector for current batch
                    found2->value->setBatch(batch[0]); // overwrite batch to match current
                    *outputReads[1-readOneToOutputRead] = * (Read*) found2->value;
                    _ASSERT(outputReads[1-readOneToOutputRead]->getData()[0]);
                    OverflowReadVector* v;
                    if (! overflowRelease.tryGet(batch[0].asKey(), &v)) {
                        v = new OverflowReadVector();
                        overflowRelease.put(batch[0].asKey(), v);
                        //fprintf(stderr,"overflow fetch into %d:%d\n", batch[0].fileID, batch[0].batchID);
                    }
                    v->push_back(found2->value);
                    overflow.erase(key);
                    //fprintf(stderr,"overflow matched %d:%d %s\n", read2->getBatch().fileID, read2->getBatch().batchID, read2->getId()); //!!
#ifdef VALIDATE_MATCH
                    overflowUsed.put(key, 1);
#endif
#ifdef STATISTICS
                    currentStats.oldPairs++;
                    currentBatches.put(read2->getBatch().asKey(), 1);
#endif
                }
            } else {
                // found a match in preceding batch
                *outputReads[1-readOneToOutputRead] = found->value;
                //fprintf(stderr,"prior matched %d:%d->%d:%d %s\n", found->value.getBatch().fileID, found->gvalue.etBatch().batchID, batch[0].fileID, batch[0].batchID, found->value.getId()); //!!
                unmatched[1].erase(found->key);
#ifdef STATISTICS
                currentStats.previousPairs++;
#endif
            }
        }

        // found a match
        *outputReads[readOneToOutputRead] = localRead;
        return true;
    }
}

    bool
PairedReadMatcher::releaseBatch(
    DataBatch batch)
{
    if (batch.asKey() == 0) {
        return true;
    } else if (single->releaseBatch(batch)) {
        OverflowReadVector* v = NULL;
        if (overflowRelease.tryGet(batch.asKey(), &v)) {
            // free memory for overflow reads
            //fprintf(stderr, "PairedReadMatcher release %d overflow reads for batch %d:%d\n", v->size(), batch.fileID, batch.batchID);
            for (OverflowReadVector::iterator i = v->begin(); i != v->end(); i++) {
                freeOverflowRead(*i);
            }
            delete v;
            overflowRelease.erase(batch.asKey());
        }
        return true;
    } else {
        return false;
    }
}

// define static factory function

    PairedReadReader*
PairedReadReader::PairMatcher(
    ReadReader* single,
    bool quicklyDropUnpairedReads)
{
    return new PairedReadMatcher(single, quicklyDropUnpairedReads);
}