-
Notifications
You must be signed in to change notification settings - Fork 1
/
augmented_bam_record.cpp
550 lines (468 loc) · 15.3 KB
/
augmented_bam_record.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
/* The MIT License
Copyright (c) 2015 Adrian Tan <atks@umich.edu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "augmented_bam_record.h"
/**
* Constructor.
*/
AugmentedBAMRecord::AugmentedBAMRecord()
{
clear();
}
/**
* Constructor.
*/
AugmentedBAMRecord::AugmentedBAMRecord(bam_hdr_t* h, bam1_t* s)
{
clear();
initialize(h, s);
}
/**
* Initialize.
*/
void AugmentedBAMRecord::initialize(bam_hdr_t* h, bam1_t* s)
{
clear();
this->h = h;
this->s = s;
beg1 = bam_get_pos1(s);
end1 = beg1;
uint8_t* seq = bam_get_seq(s);
//CIGAR related variables
int32_t n_cigar_op = bam_get_n_cigar_op(s);
uint32_t *cigar = bam_get_cigar(s);
char opchr;
int32_t oplen;
//get MD tag
uint8_t *md_aux;
char* md = 0;
((md_aux=bam_aux_get(s, "MD")) && (md = bam_aux2Z(md_aux)));
char* mdp = md; //pointer to md
//variables for keep track of CIGAR and MD tag
uint32_t rpos0 = 0; //current 0-based position in read sequence and qual field
uint32_t md_mlen_left = 0; //unprocessed MD matches that embeds insertions
bool seenM = false; //to check read is aligned
bool expectedI = false; //true when the next state expected is I, for error checking
for (int32_t i = 0; i < n_cigar_op; ++i)
{
opchr = bam_cigar_opchr(cigar[i]);
oplen = bam_cigar_oplen(cigar[i]);
if (expectedI && opchr!='I')
{
print();
fprintf(stderr, "[%s:%d %s] Inconsistent CIGAR and MD.\n", __FILE__, __LINE__, __FUNCTION__);
}
if (opchr=='S')
{
aug_cigar.push_back(cigar[i]);
aug_ref.push_back("");
aug_alt.push_back("");
rpos0 += oplen;
}
else if ( opchr=='H')
{
aug_cigar.push_back(cigar[i]);
aug_ref.push_back("");
aug_alt.push_back("");
}
else if (opchr=='M')
{
seenM = true;
uint32_t mlen = oplen; //match length
//left over MD matches to handle.
//this occurs when I is embedded.
if (md_mlen_left)
{
//cigar I embedded in MD matches
//CIGAR: 6M4I6M
//MD : 12
//to process the next insertion or SNP
if (md_mlen_left>mlen)
{
aug_cigar.push_back(bam_cigar_gen(mlen, BAM_CEQUAL));
aug_ref.push_back("");
aug_alt.push_back("");
md_mlen_left -= mlen;
rpos0 += mlen;
//no need to process MD tag since we are in the midst of perfect matches
//we skip to process the next insertion cigar operation
expectedI = true;
mlen = 0;
continue;
}
//this is very important
//CIGAR: 12M
//MD : 6A5
//need to process MD tag
//to process the next insertion or SNP
else
{
aug_cigar.push_back(bam_cigar_gen(md_mlen_left, BAM_CEQUAL));
aug_ref.push_back("");
aug_alt.push_back("");
mlen -= md_mlen_left;
rpos0 += md_mlen_left;
md_mlen_left = 0;
//go to loop in the next section
}
}
//might have multiple mismatches
while (*mdp)
{
if (isalpha(*mdp)) //mismatches
{
aug_cigar.push_back(bam_cigar_gen(1, BAM_CDIFF));
// std::cerr << "ADDING " << ((char)toupper(*mdp)) << " " << bam_base2char(bam_seqi(seq, rpos0)) << "\n";
aug_ref.push_back(std::string(1, toupper(*mdp)));
aug_alt.push_back(std::string(1, bam_base2char(bam_seqi(seq, rpos0))));
++mdp;
++rpos0;
--mlen;
}
else if (isdigit(*mdp)) //matches
{
char* end = 0;
int32_t len = std::strtol(mdp, &end, 10);
mdp = end;
if (len)
{
//another I
if (len>mlen)
{
aug_cigar.push_back(bam_cigar_gen(mlen, BAM_CEQUAL));
aug_ref.push_back("");
aug_alt.push_back("");
md_mlen_left = len - mlen;
rpos0 += mlen;
mlen = 0;
expectedI = true;
break;
}
//another mismatch
else
{
aug_cigar.push_back(bam_cigar_gen(len, BAM_CEQUAL));
aug_ref.push_back("");
aug_alt.push_back("");
mlen -= len;
rpos0 += len;
//continue processing MD tag
}
}
}
else // deletion, to be handled in the cigar's D operation
{
expectedI = false;
break;
}
}
}
else if (opchr=='D')
{
bool is_del = false;
if (*mdp=='0') ++mdp;
if (*mdp!='^')
{
fprintf(stderr, "[%s:%d %s] Inconsistent CIGAR and MD where deletion is expected\n", __FILE__, __LINE__, __FUNCTION__);
std::cerr << "mdp: " << mdp << "\n";
std::cerr << "inconsistent MD and cigar, deletion does not occur at the right place.\n";
exit(1);
}
++mdp;
std::string del = "";
while (isalpha(*mdp))
{
del += toupper(*mdp);
++mdp;
}
aug_cigar.push_back(cigar[i]);
aug_ref.push_back(del);
aug_alt.push_back("");
}
else if (opchr=='I')
{
expectedI = false;
//leading Is
if (!seenM)
{
//convert to Ss
aug_cigar.push_back(bam_cigar_gen(oplen, BAM_CSOFT_CLIP));
aug_ref.push_back("");
aug_alt.push_back("");
rpos0 += oplen;
}
//trailing Is
else if (i==n_cigar_op-1 || (i+2==n_cigar_op && ( bam_cigar_opchr(cigar[n_cigar_op-1])=='S' || bam_cigar_opchr(cigar[n_cigar_op-1])=='H' ) ) )
{
aug_cigar.push_back(bam_cigar_gen(oplen, BAM_CSOFT_CLIP));
aug_ref.push_back("");
aug_alt.push_back("");
rpos0 += oplen;
}
else
{
//insertions are not present in MD tags
//may be handled independently of future matches
std::string ins = "";
for (size_t i=0; i<oplen ; ++i)
{
ins += bam_base2char(bam_seqi(seq, rpos0+i));
}
aug_cigar.push_back(bam_cigar_gen(oplen, BAM_CINS));
aug_ref.push_back("");
aug_alt.push_back(ins);
rpos0 += oplen;
}
}
else
{
fprintf(stderr, "[%s:%d %s] Cigar state not handled %c\n", __FILE__, __LINE__, __FUNCTION__, opchr);
exit(1);
}
}
//count number of mismatches
//compute end1 position of alignment
uint8_t* qual = bam_get_qual(s);
rpos0 = 0;
for (uint32_t i=0; i<aug_cigar.size(); ++i)
{
uint32_t oplen = bam_cigar_oplen(aug_cigar[i]);
char opchr = bam_cigar_opchr(aug_cigar[i]);
if (opchr=='S')
{
rpos0 += oplen;
}
else if ( opchr == 'H' )
{
// do nothing
}
else if (opchr=='=')
{
rpos0 += oplen;
end1 +=oplen;
}
else if (opchr=='X')
{
if (qual[rpos0]>=20)
{
++no_mismatches;
}
++rpos0;
++end1;
}
else if (opchr=='I')
{
rpos0 += oplen;
++no_mismatches;
}
else if (opchr=='D')
{
++no_mismatches;
end1 +=oplen;
}
else
{
std::cerr << "unrecognized cigar state " << opchr << "\n";
}
}
--end1;
print();
}
/**
* Left align indels in augmented cigar.
*/
bool AugmentedBAMRecord::left_align()
{
//chec if any indel is not left aligned!
return true;
}
/**
* Right align indels in augmented cigar.
*/
bool AugmentedBAMRecord::right_align()
{
return true;
}
/**
* Clear.
*/
void AugmentedBAMRecord::clear()
{
s = NULL;
aug_cigar.clear();
aug_ref.clear();
aug_alt.clear();
no_mismatches = 0;
}
/**
* Prints alignment of record.
*/
void AugmentedBAMRecord::print()
{
return;
bool has_indels = false;
if (true)
{
const char* chrom = bam_get_chrom(h, s);
uint32_t pos1 = bam_get_pos1(s);
kstring_t seq = {0,0,0};
bam_get_seq_string(s, &seq);
uint32_t len = bam_get_l_qseq(s);
kstring_t qual = {0,0,0};
bam_get_qual_string(s, &qual);
kstring_t cigar_string = {0,0,0};
bam_get_cigar_string(s, &cigar_string);
kstring_t cigar_expanded_string = {0,0,0};
bam_get_cigar_expanded_string(s, &cigar_expanded_string);
uint16_t flag = bam_get_flag(s);
uint32_t mapq = bam_get_mapq(s);
uint8_t *aux;
char* md = NULL;
(aux=bam_aux_get(s, "MD")) && (md = bam_aux2Z(aux));
if (strchr(md, '^'))
{
has_indels = true;
}
for (uint32_t i=0; i<aug_cigar.size(); ++i)
{
int32_t oplen = bam_cigar_oplen(aug_cigar[i]);
char opchr = bam_cigar_opchr(aug_cigar[i]);
if (opchr=='I' || opchr=='D')
{
has_indels = true;
break;
}
}
std::cerr << "##################" << "\n";
std::cerr << "chrom:pos: " << chrom << ":" << pos1 << "\n";
std::cerr << "read : " << seq.s << "\n";
std::cerr << "qual : " << qual.s << "\n";
std::cerr << "cigar_str: " << cigar_string.s << "\n";
std::cerr << "cigar : " << cigar_expanded_string.s << "\n";
std::cerr << "len : " << len << "\n";
std::cerr << "mapq : " << mapq << "\n";
std::cerr << "mpos1 : " << bam_get_mpos1(s) << "\n";
std::cerr << "mtid : " << bam_get_mtid(s) << "\n";
std::cerr << "md : " << (aux?md:"") << "\n";
std::cerr << "nm : " << no_mismatches << "\n";
int32_t nm = 0;
(aux=bam_aux_get(s, "NM")) && (nm = bam_aux2i(aux));
std::cerr << "bam nm : " << nm << "\n";
std::cerr << "##################" << "\n";
if (seq.m) free(seq.s);
if (qual.m) free(qual.s);
if (cigar_string.m) free(cigar_string.s);
if (cigar_expanded_string.m) free(cigar_expanded_string.s);
if (strchr(md, '^'))
{
has_indels = true;
}
for (uint32_t i=0; i<aug_cigar.size(); ++i)
{
int32_t oplen = bam_cigar_oplen(aug_cigar[i]);
char opchr = bam_cigar_opchr(aug_cigar[i]);
if (opchr=='I' || opchr=='D')
{
has_indels = true;
break;
}
}
}
std::cerr << "##################" << "\n";
std::cerr << "Augmented CIGAR" << "\n";
std::cerr << "##################" << "\n";
int32_t oplen;
char opchr;
std::cerr << "AUG_CIGAR : ";
for (uint32_t i=0; i<aug_cigar.size(); ++i)
{
oplen = bam_cigar_oplen(aug_cigar[i]);
opchr = bam_cigar_opchr(aug_cigar[i]);
std::cerr << oplen << opchr;
}
std::cerr << "\n";
std::string ref;
std::string align;
std::string seq;
std::string quals;
int32_t spos0 = 0;
uint8_t* qual = bam_get_qual(s);
for (uint32_t i=0; i<aug_cigar.size(); ++i)
{
oplen = bam_cigar_oplen(aug_cigar[i]);
opchr = bam_cigar_opchr(aug_cigar[i]);
if (opchr=='S')
{
ref.append(oplen, '-');
for (uint32_t j=0; j<oplen; ++j)
{
seq.append(1, bam_base2char(bam_seqi(bam_get_seq(this->s), spos0+j)));
quals.append(1, qual[spos0+j]+33);
}
align.append(oplen, 'S');
}
else if (opchr=='H') {
// nothing needed
}
else if (opchr=='=')
{
for (uint32_t j=0; j<oplen; ++j)
{
ref.append(1, bam_base2char(bam_seqi(bam_get_seq(this->s), spos0+j)));
seq.append(1, bam_base2char(bam_seqi(bam_get_seq(this->s), spos0+j)));
quals.append(1, qual[spos0+j]+33);
}
align.append(oplen, '=');
spos0 += oplen;
}
else if (opchr=='X')
{
//assume oplen is always 1.
ref.append(aug_ref[i]);
seq.append(aug_alt[i]);
align.append(1, 'X');
quals.append(1, qual[spos0]+33);
spos0 += 1;
}
else if (opchr=='I')
{
ref.append(oplen, '-');
seq.append(aug_alt[i]);
align.append( oplen, 'I');
for (uint32_t j=0; j<oplen; ++j)
{
quals.append(1, qual[spos0+j]+33);
}
spos0 += oplen;
}
else if (opchr=='D')
{
ref.append(aug_ref[i]);
seq.append(oplen, '-');
align.append(oplen, 'D');
}
else
{
std::cerr << "unrecognized cigar state " << opchr << "\n";
}
}
std::cerr << "REF : "<< ref << "\n";
std::cerr << "ALIGN : "<< align << "\n";
std::cerr << "READ : "<< seq << "\n";
std::cerr << "QUAL : "<< quals << "\n";
}