-
Notifications
You must be signed in to change notification settings - Fork 0
/
chimeric_blacklist.awk
428 lines (412 loc) · 12.2 KB
/
chimeric_blacklist.awk
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
# Takes a SAM file, looks for chimeric reads and normal reads. Outputs
# only those reads mapped to chromosomes 1-24 with MAPQ > 0.
#
# Output to fname1 is of the form:
# strand1 chromosome1 position1 frag1 strand2 chromosome2 position2 frag2
# mapq1 cigar1 seq1 mapq2 cigar2 seq2
# where chr1 is always <= chr2
#
# Output to fname2 retains SAM format.
#
# Chimeric reads are treated as follows:
# "Normal" chimeras (with 3 reads, two with the ligation junction), where the
# third read (w/o ligation junction) is within 20Kbp of one of the ligation
# junction reads, are sent to fname1. All other chimeras (where either there
# are more than 3 reads, or they don't contain the ligation junction, or the
# one non-ligation junction end is far from the others, are sent to fname2.
#
# awk -f chimeric.awk -v fname1="norm_chimera" fname2="abnorm_chimera" fname3="unmapped"
# returns absolute value
function abs(value)
{
return (value<0?-value:value);
}
# returns minimum of two values
function min(value1,value2)
{
return (value1<value2?value1:value2);
}
# examines read1 (s1,c1,p1) versus read2 and returns true if
# the first read comes before the second read
# this is so duplicates can be found after sorting even when the strand and
# chromosome are the same
function less_than(s1,c1,p1,s2,c2,p2)
{
if (c1 < c2) return 1;
if (c1 > c2) return 0;
# c1 == c2
if (s1 < s2) return 1;
if (s1 > s2) return 0;
# s1 == s2 && c1 == c2
if (p1 < p2) return 1;
if (p1 > p2) return 0;
# all are equal, doesn't matter
return 1;
}
BEGIN{
OFS="\t";
tottot = -1; # will count first non-group
}
{
# input file is sorted by read name. Look at read name to group
# appropriately
split($1,a,"/");
if(a[1]==prev){
# move on to next record. look below this block for actions that occur
# regardless.
count++;
}
else {
# deal with read pair group
tottot++;
if (count==3) {
# chimeric read
for (j=1; j <= 3; j++) {
split(c[j], tmp);
split(tmp[1],readname,"/");
read[j] = readname[2];
name[j] = tmp[1];
# strand; Bit 16 set means reverse strand
str[j] = and(tmp[2],16);
# chromosome
chr[j] = tmp[3];
# position
pos[j] = tmp[4];
# mapq score
m[j] = tmp[5];
# cigar string
cigarstr[j] = tmp[6];
# sequence
seq[j] = tmp[10];
qual[j] = tmp[11];
# get rid of soft clipping to know correct position
if (str[j] == 0 && tmp[6] ~/^[0-9]+S/) {
split(tmp[6], cigar, "S");
pos[j] = pos[j] - cigar[1];
if (pos[j] <= 0) {
pos[j] = 1;
}
}
else if (str[j] == 16) {
# count Ms,Ds,Ns,Xs,=s for sequence length
seqlength=0;
currstr=tmp[6];
# can look like 15M10S20M, need to count all not just first
where=match(currstr, /[0-9]+[M|D|N|X|=]/);
while (where>0) {
seqlength+=substr(currstr,where,RLENGTH-1)+0;
currstr=substr(currstr,where+RLENGTH);
where=match(currstr, /[0-9]+[M|D|N|X|=]/);
}
pos[j] = pos[j] + seqlength - 1;
# add soft clipped bases in for proper position
if (tmp[6] ~ /[0-9]+S$/) {
where = match(tmp[6],/[0-9]+S$/);
cigloc = substr(tmp[6],where,RLENGTH-1) + 0;
pos[j] = pos[j] + cigloc;
}
# Mitochrondria loops around
if (chr[j] ~ /MT/ && pos[j] >= 16569) {
pos[j] = pos[j] - 16569;
}
}
# blacklist - if 3rd bit set (=4) it means unmapped
mapped[j] = and(tmp[2],4) == 0;
}
dist[12] = abs(chr[1]-chr[2])*10000000 + abs(pos[1]-pos[2]);
dist[23] = abs(chr[2]-chr[3])*10000000 + abs(pos[2]-pos[3]);
dist[13] = abs(chr[1]-chr[3])*10000000 + abs(pos[1]-pos[3]);
if (min(dist[12],min(dist[23],dist[13])) < 1000) {
# The paired ends look like A/B...B. Make sure we take A and B.
if (read[1] == read[2]) {
# take the unique one "B" for sure
read2 = 3;
# take the end of "A/B" that isn't close to "B"
read1 = dist[13] > dist[23] ? 1:2;
}
else if (read[1] == read[3]) {
read2 = 2;
read1 = dist[12] > dist[23] ? 1:3;
}
else if (read[2] == read[3]) {
read2 = 1;
read1 = dist[12] > dist[13] ? 2:3;
}
else {
printf("reads strange\n") > "/dev/stderr"
exit 1
}
if (mapped[read1] && mapped[read2]) {
count_norm++;
if (less_than(str[read1],chr[read1],pos[read1],str[read2],chr[read2],pos[read2])) {
print str[read1],chr[read1],pos[read1],str[read2],chr[read2],pos[read2],m[read1],cigarstr[read1],seq[read1],m[read2],cigarstr[read2],seq[read2],name[read1],name[read2] > fname1;
}
else {
print str[read2],chr[read2],pos[read2],str[read1],chr[read1],pos[read1],m[read2],cigarstr[read2],seq[read2],m[read1],cigarstr[read1],seq[read1],name[read2],name[read1] > fname1;
}
}
else {
for (i in c) {
print c[i] > fname3;
}
count_unmapped++;
}
}
else {
# chimeric read with the 3 ends > 1KB apart
count_abnorm++;
for (i in c) {
print c[i] > fname2;
}
}
}
else if (count > 3) {
# chimeric read > 3, too many to deal with
count_abnorm++;
for (i in c) {
print c[i] > fname2;
}
}
else if (count == 2) {
# code here should be same as above, but it's a "normal" read
j = 0;
for (i in c) {
split(c[i], tmp);
split(tmp[1],readname,"/");
str[j] = and(tmp[2],16);
chr[j] = tmp[3];
pos[j] = tmp[4];
m[j] = tmp[5];
cigarstr[j] = tmp[6];
seq[j] = tmp[10];
qual[j] = tmp[11];
name[j] = tmp[1];
# blacklist - if 3rd bit set (=4) it means unmapped
mapped[j] = and(tmp[2],4) == 0;
if (str[j] == 0 && tmp[6] ~/^[0-9]+S/) {
split(tmp[6], cigar, "S");
pos[j] = pos[j] - cigar[1];
if (pos[j] <= 0) {
pos[j] = 1;
}
}
else if (str[j] == 16) {
# count Ms,Ds,Ns,Xs,=s for sequence length
seqlength=0;
currstr=tmp[6];
where=match(currstr, /[0-9]+[M|D|N|X|=]/);
while (where>0) {
seqlength+=substr(currstr,where,RLENGTH-1)+0;
currstr=substr(currstr,where+RLENGTH);
where=match(currstr, /[0-9]+[M|D|N|X|=]/);
}
pos[j] = pos[j] + seqlength - 1;
if (tmp[6] ~ /[0-9]+S$/) {
where = match(tmp[6],/[0-9]+S$/);
cigloc = substr(tmp[6],where,RLENGTH-1) + 0;
pos[j] = pos[j] + cigloc;
}
if (chr[j] ~ /MT/ && pos[j] >= 16569) {
pos[j] = pos[j] - 16569;
}
}
j++;
}
if (mapped[0] && mapped[1]) {
count_reg++;
if (less_than(str[0],chr[0],pos[0],str[1],chr[1],pos[1])) {
# ideally we'll get rid of printing out cigar string at some point
print str[0],chr[0],pos[0],str[1],chr[1],pos[1],m[0],cigarstr[0],seq[0],m[1],cigarstr[1],seq[1],name[0],name[1] > fname1;
}
else {
print str[1],chr[1],pos[1],str[0],chr[0],pos[0],m[1],cigarstr[1],seq[1],m[0],cigarstr[0],seq[0],name[1],name[0] > fname1;
}
}
else {
for (i in c) {
print c[i] > fname3;
}
count_unmapped++;
}
}
# reset variables
delete c;
count=1;
prev=a[1];
}
# these happen no matter what, after the above processing
c[count] = $0;
}
END{
# deal with read pair group
tottot++;
if (count==3) {
# chimeric read
for (j=1; j <= 3; j++) {
split(c[j], tmp);
split(tmp[1],readname,"/");
read[j] = readname[2];
name[j] = tmp[1];
# strand
str[j] = and(tmp[2],16);
# chromosome
chr[j] = tmp[3];
# position
pos[j] = tmp[4];
# mapq score
m[j] = tmp[5];
# cigar string
cigarstr[j] = tmp[6];
# sequence
seq[j] = tmp[10];
qual[j] = tmp[11];
# get rid of soft clipping to know correct position
if (str[j] == 0 && tmp[6] ~/^[0-9]+S/) {
split(tmp[6], cigar, "S");
pos[j] = pos[j] - cigar[1];
if (pos[j] <= 0) {
pos[j] = 1;
}
}
else if (str[j] == 16) {
# count Ms,Ds,Ns,Xs,=s for sequence length
seqlength=0;
currstr=tmp[6];
# can look like 15M10S20M, need to count all not just first
where=match(currstr, /[0-9]+[M|D|N|X|=]/);
while (where>0) {
seqlength+=substr(currstr,where,RLENGTH-1)+0;
currstr=substr(currstr,where+RLENGTH);
where=match(currstr, /[0-9]+[M|D|N|X|=]/);
}
pos[j] = pos[j] + seqlength - 1;
# add soft clipped bases in for proper position
if (tmp[6] ~ /[0-9]+S$/) {
where = match(tmp[6],/[0-9]+S$/);
cigloc = substr(tmp[6],where,RLENGTH-1) + 0;
pos[j] = pos[j] + cigloc;
}
# Mitochrondria loops around
if (chr[j] ~ /MT/ && pos[j] >= 16569) {
pos[j] = pos[j] - 16569;
}
}
mapped[j] = and(tmp[2],4) == 0;
}
dist[12] = abs(chr[1]-chr[2])*10000000 + abs(pos[1]-pos[2]);
dist[23] = abs(chr[2]-chr[3])*10000000 + abs(pos[2]-pos[3]);
dist[13] = abs(chr[1]-chr[3])*10000000 + abs(pos[1]-pos[3]);
if (min(dist[12],min(dist[23],dist[13])) < 1000) {
# The paired ends look like A/B...B. Make sure we take A and B.
if (read[1] == read[2]) {
# take the unique one "B" for sure
read2 = 3;
# take the end of "A/B" that isn't close to "B"
read1 = dist[13] > dist[23] ? 1:2;
}
else if (read[1] == read[3]) {
read2 = 2;
read1 = dist[12] > dist[23] ? 1:3;
}
else if (read[2] == read[3]) {
read2 = 1;
read1 = dist[12] > dist[13] ? 2:3;
}
else {
printf("reads strange\n") > "/dev/stderr"
exit 1
}
if (mapped[read1] && mapped[read2]) {
count_norm++;
if (less_than(str[read1],chr[read1],pos[read1],str[read2],chr[read2],pos[read2])) {
print str[read1],chr[read1],pos[read1],str[read2],chr[read2],pos[read2],m[read1],cigarstr[read1],seq[read1],m[read2],cigarstr[read2],seq[read2],name[read1],name[read2] > fname1;
}
else {
print str[read2],chr[read2],pos[read2],str[read1],chr[read1],pos[read1],m[read2],cigarstr[read2],seq[read2],m[read1],cigarstr[read1],seq[read1],name[read2],name[read1] > fname1;
}
}
else {
for (i in c) {
print c[i] > fname3;
}
count_unmapped++;
}
}
else {
# chimeric read with the 3 ends > 1KB apart
count_abnorm++;
for (i in c) {
print c[i] > fname2;
}
}
}
else if (count > 3) {
# chimeric read > 3, too many to deal with
count_abnorm++;
for (i in c) {
print c[i] > fname2;
}
}
else if (count == 2) {
# code here should be same as above, but it's a "normal" read
j = 0;
for (i in c) {
split(c[i], tmp);
split(tmp[1],readname,"/");
str[j] = and(tmp[2],16);
chr[j] = tmp[3];
pos[j] = tmp[4];
m[j] = tmp[5];
cigarstr[j] = tmp[6];
seq[j] = tmp[10];
qual[j] = tmp[11];
name[j] = tmp[1];
if (str[j] == 0 && tmp[6] ~/^[0-9]+S/) {
split(tmp[6], cigar, "S");
pos[j] = pos[j] - cigar[1];
if (pos[j] <= 0) {
pos[j] = 1;
}
}
else if (str[j] == 16) {
# count Ms,Ds,Ns,Xs,=s for sequence length
seqlength=0;
currstr=tmp[6];
where=match(currstr, /[0-9]+[M|D|N|X|=]/);
while (where>0) {
seqlength+=substr(currstr,where,RLENGTH-1)+0;
currstr=substr(currstr,where+RLENGTH);
where=match(currstr, /[0-9]+[M|D|N|X|=]/);
}
pos[j] = pos[j] + seqlength - 1;
if (tmp[6] ~ /[0-9]+S$/) {
where = match(tmp[6],/[0-9]+S$/);
cigloc = substr(tmp[6],where,RLENGTH-1) + 0;
pos[j] = pos[j] + cigloc;
}
if (chr[j] ~ /MT/ && pos[j] >= 16569) {
pos[j] = pos[j] - 16569;
}
}
mapped[j] = and(tmp[2],4) == 0;
j++;
}
if (mapped[0] && mapped[1]) {
count_reg++;
if (less_than(str[0],chr[0],pos[0],str[1],chr[1],pos[1])) {
# ideally we'll get rid of printing out cigar string at some point
print str[0],chr[0],pos[0],str[1],chr[1],pos[1],m[0],cigarstr[0],seq[0],m[1],cigarstr[1],seq[1],name[0],name[1] > fname1;
}
else {
print str[1],chr[1],pos[1],str[0],chr[0],pos[0],m[1],cigarstr[1],seq[1],m[0],cigarstr[0],seq[0],name[1],name[0] > fname1;
}
}
else {
for (i in c) {
print c[i] > fname3;
}
count_unmapped++;
}
}
printf("%d %d %d %d %d\n", tottot, count_unmapped, count_reg, count_norm, count_abnorm) >> fname1".res.txt";
}