-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalker.cpp
276 lines (228 loc) · 8.11 KB
/
walker.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
#include "walker.hpp"
#include <assert.h>
// lots of this code derived from rrBAM
#define CLAMPH(x, high) (((x) > (high)) ? (high) : (x))
#define PACK_CIG(type, length) (((type) << 14) | (CLAMPH((length), 0x3FFF) & 0x3FFF))
using namespace std;
namespace walker {
walker::walker(const string& bam_in, const string& ref_fa) {
// load BAM
if(!this->reader.Open(bam_in)) {
fprintf(stderr, "Error in %s : couldn't open BAM file!\n", __func__);
exit(-1);
}
// load its header
header = reader.Header();
// load reference
if(ref_fa != "") {
if(!reference.LoadIndex(ref_fa)) {
fprintf(stderr, "Error in %s : couldn't load reference!\n", __func__);
exit(-1);
}
}
}
walker::~walker() {
if(outfile != NULL) fclose(outfile);
}
bool walker::EDz(const SeqLib::BamRecord& record) {
int32_t nm;
return record.GetIntTag("NM", nm) ? nm == 0 : true;
}
vector<nonref_pos_t> walker::nonref_pos(const SeqLib::BamRecord& record) { // {{{
/* lower 32 bits contain absolute reference position of mismatch
* high 32 bits contain:
* - lower 16: position along the
* - upper 16: 2 bits for CIGAR op (as defined in sam.h, with caveat for S),
* 14 bits for op. length. Note that CIGAR op "S" (soft clip) is 4 in sam.h,
* but it is 3 here (to fit in 2 bits). Since we overload 3 with "N" (RNA-seq skip),
* these ops must be distinguished based on their position in the read --
* soft clips can only occur at the read beginnings/ends, whereas "N" can never
* occur there.
*/
vector<nonref_pos_t> output;
// query reference over this read
// for contigs with no N padding (e.g. chrM), reads can overhang the start of
// the reference.
// let's just ignore such reads.
if(record.PositionWithSClips() < 0) return output;
string ror = reference.QueryRegion(header.IDtoName(record.ChrID()),
record.PositionWithSClips(),
record.PositionEndWithSClips());
string readseq = record.Sequence();
SeqLib::Cigar c = record.GetCigar();
int readpos = 0;
int refpos = 0;
int jointpos = 0; // position over joint read/ref alignment, e.g.
/*
0 4 8
ACGTACGTA--CGT REF
ACGCAC--ACCCGT READ
^ ^^ ^^
3 56 78
*/
for(auto c_f : c) {
switch(c_f.Type()) {
/* this operator consumes bases over the ref and read
*/
case 'M' :
/* IDEA: since we expect the set of mismatches to be sparse,
rather than comparing each byte at a time, can we
cast pointer to int64, bitwise xor read and reference,
and compare 8 bytes at a time?
could potentially be even faster if we do a binary search
for nonzero xor'd bits through the int64, rather than
always iterating over all 8 bytes
*/
for(int i = 0; i < c_f.Length(); i++) {
if(pack_2bit[ror[refpos]] != pack_2bit[readseq[readpos]]) {
output.push_back({
.refpos = record.PositionWithSClips() + refpos,
.readpos = CLAMPH(readpos, 0xFFFF) & 0xFFFF,
.jointpos = CLAMPH(jointpos, 0xFFFF) & 0xFFFF,
.cig = PACK_CIG(0, 1)
});
}
readpos++;
refpos++;
jointpos++;
}
break;
// this operator consumes bases of the ref and read, but we don't assess
// their (mis)match status; we just add them to the vector.
// note that we denote "S" as 3, which deviates from definition in sam.h
// (see above)
case 'S' :
output.push_back({
.refpos = record.PositionWithSClips() + refpos,
.readpos = CLAMPH(readpos, 0xFFFF) & 0xFFFF,
.jointpos = CLAMPH(jointpos, 0xFFFF) & 0xFFFF,
.cig = PACK_CIG(3, c_f.Length())
});
readpos += c_f.Length();
refpos += c_f.Length();
jointpos += c_f.Length();
break;
// these operators consume bases over the ref but not the read
case 'D' :
case 'N' :
output.push_back({
.refpos = record.PositionWithSClips() + refpos,
.readpos = CLAMPH(readpos, 0xFFFF) & 0xFFFF,
.jointpos = CLAMPH(jointpos, 0xFFFF) & 0xFFFF,
.cig = PACK_CIG(c_f.RawType(), c_f.Length())
});
refpos += c_f.Length();
jointpos += c_f.Length();
break;
// this operator consumes bases over the read but not the ref
case 'I' :
output.push_back({
.refpos = record.PositionWithSClips() + refpos,
.readpos = CLAMPH(readpos, 0xFFFF) & 0xFFFF,
.jointpos = CLAMPH(jointpos, 0xFFFF) & 0xFFFF,
.cig = PACK_CIG(c_f.RawType(), c_f.Length())
});
readpos += c_f.Length();
jointpos += c_f.Length();
break;
}
}
return output;
} // }}}
//
// default iterators {{{
void walker::walk() {
while(reader.GetNextRecord(cur_read)) {
// get stats
if(!(n_reads % 100000)) print_status();
n_reads++;
// apply default filters
if(filter_read(cur_read)) continue;
// apply user-specified action to cur_read
if(!walk_apply(cur_read)) break;
}
}
void walker::walk(const SeqLib::GenomicRegion& region) {
if(!reader.SetRegion(region)) {
fprintf(stderr, "Error setting region!\n");
exit(1);
}
walk();
}
void walker::walk(const SeqLib::GenomicRegionCollection<>& region_collection) {
if(!reader.SetMultipleRegions(region_collection)) {
fprintf(stderr, "Error setting multiple regions!\n");
exit(1);
}
walk();
}
// }}}
void walker::increment_pos(uint16_t& curchr, uint32_t& curpos) {
// TODO: ensure that curchr isn't out of bounds
if(curpos >= header.GetSequenceLength(curchr) - 1) {
curchr++;
curpos = 0;
} else curpos++;
}
bool walker::filter_read(const SeqLib::BamRecord& record) {
// skip if this read is unmapped
if(!record.MappedFlag()) return true;
// skip if this read is vendor fail
if(record.QCFailFlag()) return true;
// skip if read is a duplicate
if(record.DuplicateFlag()) return true;
// skip if read is not primary
if(record.shared_pointer().get()->core.flag & 0x900) return true;
// skip if read is MQZ
if(record.MapQuality() == 0) return true;
return false;
}
void walker::print_status() {
if(n_reads == 0) {
time_last = chrono::steady_clock::now();
return;
}
// count reads per second
time_now = chrono::steady_clock::now();
double RPS = ((double) (n_reads - n_reads_last)) /
chrono::duration_cast<chrono::duration<double>>(time_now - time_last).count();
n_reads_last = n_reads;
double RPS_proc = ((double) (n_reads_proc - n_reads_last_proc)) /
chrono::duration_cast<chrono::duration<double>>(time_now - time_last).count();
n_reads_last_proc = n_reads_proc;
time_last = chrono::steady_clock::now();
// print status
if(n_reads_proc > 0) fprintf(stderr,
"%s:%d (%0.2f, %0.2f r/s [tot., proc.])\n",
header.IDtoName(cur_read.ChrID()).c_str(),
cur_read.Position(),
RPS,
RPS_proc
);
else fprintf(stderr,
"%s:%d (%0.2f r/s)\n",
header.IDtoName(cur_read.ChrID()).c_str(),
cur_read.Position(),
RPS
);
return;
}
bool walker::set_output_file(const string& outfile) {
if(outfile == "-") this->outfile = stdout;
else {
this->outfile = fopen(outfile.c_str(), "w");
if(!this->outfile) {
fprintf(stderr, "Could not open output file for writing!\n");
return 0;
}
this->outfile_name = outfile;
}
return 1;
}
bool walker::close_output_file() {
assert(fileno(this->outfile) > 2); // should not be closing standard I/O streams
if(fclose(this->outfile)) return 0;
this->outfile = NULL;
return 1;
}
}