File: bam_scanner_cache.h

package info (click to toggle)
seqan2 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228,748 kB
  • sloc: cpp: 257,602; ansic: 91,967; python: 8,326; sh: 1,056; xml: 570; makefile: 229; awk: 51; javascript: 21
file content (309 lines) | stat: -rw-r--r-- 10,705 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
// ==========================================================================
//                 SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of Knut Reinert or the FU Berlin nor the names of
//       its contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: David Weese <david.weese@fu-berlin.de>
// ==========================================================================

#ifndef INCLUDE_SEQAN_BAM_IO_BAM_SCANNER_CACHE_H_
#define INCLUDE_SEQAN_BAM_IO_BAM_SCANNER_CACHE_H_

#include <functional>
#include <unordered_map>

namespace seqan2 {

using namespace std;

// ============================================================================
// Forwards
// ============================================================================

// ============================================================================
// Tags, Classes, Enums
// ============================================================================

struct BamScannerCacheKey_
{
    int32_t     rID;
    int32_t     beginPos;
    uint64_t    qnameHash;

    bool operator== (BamScannerCacheKey_ const &other) const
    {
        return rID == other.rID && beginPos == other.beginPos && qnameHash == other.qnameHash;
    }
};

struct BamScannerCacheSearchKey_
{
    typedef uint16_t TFlag;

    BamScannerCacheKey_ cacheKey;
    TFlag flags;
    TFlag flagsMask;
};

struct BamScannerCacheHash_ :
    std::function<size_t(BamScannerCacheKey_)>
{
    size_t operator()(BamScannerCacheKey_ const &v) const
    {
        return std::hash<int32_t>()(v.rID) ^ std::hash<int32_t>()(v.beginPos) ^ std::hash<uint64_t>()(v.qnameHash);
    }
};

class BamScannerCache
{
public:
    // The Key is a pair of (genomic pos, name) where genomic pos is a pair of (rId, pos).
    typedef String<BamAlignmentRecord> TRecords;
    typedef Size<TRecords>::Type TRecordId;
    typedef BamScannerCacheKey_ TKey;

    // A mapping from the key type to the BamAlignmentRecord at this position.
    typedef unordered_multimap<TKey, TRecordId, BamScannerCacheHash_> TMap;
    typedef TMap::const_iterator TMapIter;

    TRecords            records;
    String<TRecordId>   unusedIds;
    TMap                map;
    BamAlignmentRecord  tmpRecord;

    static const TRecordId INVALID_ID = (TRecordId)-1;
};


// ============================================================================
// Metafunctions
// ============================================================================

// ============================================================================
// Functions
// ============================================================================

template <typename TSequence>
uint64_t _suffixHash(TSequence const &sequence)
{
    typedef typename Iterator<TSequence const, Standard>::Type  TIter;
    typedef typename Value<TSequence>::Type                     TValue;
    typedef typename Size<TSequence>::Type                      TSize;

    const uint64_t ALPH_SIZE = ValueSize<TValue>::VALUE;
    const unsigned MAX_LEN = LogN<~(ALPH_SIZE - 1) / ALPH_SIZE, ALPH_SIZE>::VALUE + 1;

    TSize len = length(sequence);
    TIter itEnd = end(sequence, Standard());

    if (len > 2 && (sequence[len - 2] == ':' || sequence[len - 2] == '/'))
    {
        len -= 2;
        itEnd -= 2;
    }

    if (len > MAX_LEN)
        len = MAX_LEN;

    TIter it = itEnd - len;

    uint64_t hash = 0;
    for (; it != itEnd; ++it)
        hash = hash * ALPH_SIZE + ordValue(*it);
    return hash;
}

inline void
insertRecord(BamScannerCache &cache, seqan2::BamAlignmentRecord const &record)
{
    int _id;
    if (empty(cache.unusedIds))
    {
        _id = length(cache.records);
        appendValue(cache.records, record);
    }
    else
    {
        _id = back(cache.unusedIds);
        eraseBack(cache.unusedIds);
        cache.records[_id] = record;
    }

    BamScannerCache::TKey key = { record.rID, record.beginPos, _suffixHash(record.qName) };
    cache.map.insert(std::make_pair(key, _id));
}

template <typename TQName1, typename TQName2>
inline bool
_qNamesEqual(TQName1 const &name1, TQName2 const &name2)
{
    unsigned len1 = length(name1);
    unsigned len2 = length(name2);

    if (len1 != len2)
        return false;

    if (len1 > 2 && len2 > 2)
    {
        if (name1[len1 - 2] == ':' || name1[len1 - 2] == '/')
            return prefix(name1, len1 - 1) == prefix(name2, len2 - 1);
    }
    return name1 == name2;
}


// search a certain segment that might refer to lastRecord
// if recursively search for a intermediate segments that might refer to lastRecord
inline bool
_recursivelyFindSegmentGraph(
    String<BamAlignmentRecord> &records,
    BamScannerCacheSearchKey_ &searchKey,
    unsigned segmentNo,
    BamScannerCache &cache)
{
    typedef BamScannerCache::TMapIter TMapIter;
    typedef BamScannerCacheSearchKey_::TFlag TFlag;

    // search for segment using contigId, position
    std::pair<TMapIter, TMapIter> range = cache.map.equal_range(searchKey.cacheKey);
    for (TMapIter iter = range.first; iter != range.second;)
    {
        TMapIter it = iter;
        ++iter;
        BamAlignmentRecord &record = cache.records[it->second];

        // ... additionally check flags and qName
        if ((record.flag & searchKey.flagsMask) == searchKey.flags &&
            _qNamesEqual(record.qName, records[0].qName))
        {
            if (record.rNextId == records[0].rID && record.pNext == records[0].beginPos)
            {
                resize(records, segmentNo + 2);
                records[segmentNo + 1] = records[0];
                records[segmentNo] = record;
                appendValue(cache.unusedIds, it->second);
                cache.map.erase(it);
                return true;
            }
        }
    }

    for (; range.first != range.second; ++range.first)
    {
        BamAlignmentRecord &record = cache.records[range.first->second];

        // ... additionally check flags and qName
        if ((record.flag & searchKey.flagsMask) == searchKey.flags &&
            _qNamesEqual(record.qName, records[0].qName))
        {
            BamScannerCacheSearchKey_ newSearchKey = {
                { record.rNextId, record.pNext, searchKey.cacheKey.qnameHash },
                static_cast<TFlag>((record.flag & BAM_FLAG_MULTIPLE) | ((record.flag & BAM_FLAG_NEXT_RC) >> 1)),
                BAM_FLAG_MULTIPLE | BAM_FLAG_RC
            };
            if (_recursivelyFindSegmentGraph(records, newSearchKey, segmentNo + 1, cache))
            {
                records[segmentNo] = record;
                appendValue(cache.unusedIds, range.first->second);
                cache.map.erase(range.first);
                return true;
            }
        }
    }

    return false;
}


inline void
readMultiRecords(String<BamAlignmentRecord> &records, BamFileIn &bamFile, BamScannerCache &cache)
{
    typedef BamScannerCacheSearchKey_::TFlag TFlag;

    if (empty(records))
        resize(records, 1);

    while (!atEnd(bamFile))
    {
        // read next record
        BamAlignmentRecord &record = records[0];
        readRecord(record, bamFile);

        // is this a single-end read or single alignment?
        if (!hasFlagMultiple(record) ||
            record.rID == BamAlignmentRecord::INVALID_REFID ||
            record.beginPos == BamAlignmentRecord::INVALID_POS ||
            record.rNextId == BamAlignmentRecord::INVALID_REFID ||
            record.pNext == BamAlignmentRecord::INVALID_POS)
        {
            resize(records, 1);
            return;
        }

        if (record.rID < record.rNextId || (record.rID == record.rNextId && record.beginPos < record.pNext))
        {
            // store record to retrieve it later
            insertRecord(cache, record);
            continue;
        }

        // search mates in case of multiple templates
        BamScannerCacheSearchKey_ searchKey = {
            { record.rNextId, record.pNext, _suffixHash(record.qName) },
            static_cast<TFlag>((record.flag & BAM_FLAG_MULTIPLE) | ((record.flag & BAM_FLAG_NEXT_RC) >> 1)),
            BAM_FLAG_MULTIPLE | BAM_FLAG_RC
        };
        if (_recursivelyFindSegmentGraph(records, searchKey, 0, cache))
        {
            // order paired-end reads by first and second segment in the template
//            if (length(records) == 2)
//            {
//                if (hasFlagLast(records[0]))
//                    std::swap(records[0], records[1]);
//            }
            return;
        }
        else
        {
            // we could get here if both mates align to same position and we are the first
            // hence, insert our record to be retrieved by the second.
            if (records[0].beginPos != records[0].pNext)
            {
                std::cerr << "WARNING: Mate could not be found for:\n";
                write(std::cerr, records[0], context(bamFile), seqan2::Sam());
            }
            insertRecord(cache, records[0]);
        }
    }
    clear(records);
}

}  // namespace seqan2;

#endif  // #ifndef INCLUDE_SEQAN_BAM_IO_BAM_SCANNER_CACHE_H_