0
|
1 #include <unistd.h>
|
|
2 #include <assert.h>
|
|
3 #include <string.h>
|
|
4 #include <ctype.h>
|
|
5 #include <math.h>
|
|
6 #include "faidx.h"
|
|
7 #include "sam.h"
|
|
8 #include "kstring.h"
|
|
9 #include "kaln.h"
|
|
10 #include "kprobaln.h"
|
|
11
|
|
12 char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
|
|
13
|
|
14 void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
|
|
15 {
|
|
16 uint8_t *seq = bam1_seq(b);
|
|
17 uint32_t *cigar = bam1_cigar(b);
|
|
18 bam1_core_t *c = &b->core;
|
|
19 int i, x, y, u = 0;
|
|
20 kstring_t *str;
|
|
21 uint8_t *old_md, *old_nm;
|
|
22 int32_t old_nm_i = -1, nm = 0;
|
|
23
|
|
24 str = (kstring_t*)calloc(1, sizeof(kstring_t));
|
|
25 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
|
|
26 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
|
|
27 if (op == BAM_CMATCH) {
|
|
28 for (j = 0; j < l; ++j) {
|
|
29 int z = y + j;
|
|
30 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
|
|
31 if (ref[x+j] == 0) break; // out of boundary
|
|
32 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
|
|
33 if (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
|
|
34 ++u;
|
|
35 } else {
|
|
36 ksprintf(str, "%d", u);
|
|
37 kputc(ref[x+j], str);
|
|
38 u = 0; ++nm;
|
|
39 }
|
|
40 }
|
|
41 if (j < l) break;
|
|
42 x += l; y += l;
|
|
43 } else if (op == BAM_CDEL) {
|
|
44 ksprintf(str, "%d", u);
|
|
45 kputc('^', str);
|
|
46 for (j = 0; j < l; ++j) {
|
|
47 if (ref[x+j] == 0) break;
|
|
48 kputc(ref[x+j], str);
|
|
49 }
|
|
50 u = 0;
|
|
51 if (j < l) break;
|
|
52 x += l; nm += l;
|
|
53 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
|
|
54 y += l;
|
|
55 if (op == BAM_CINS) nm += l;
|
|
56 } else if (op == BAM_CREF_SKIP) {
|
|
57 x += l;
|
|
58 }
|
|
59 }
|
|
60 ksprintf(str, "%d", u);
|
|
61 // apply max_nm
|
|
62 if (max_nm > 0 && nm >= max_nm) {
|
|
63 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
|
|
64 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
|
|
65 if (op == BAM_CMATCH) {
|
|
66 for (j = 0; j < l; ++j) {
|
|
67 int z = y + j;
|
|
68 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
|
|
69 if (ref[x+j] == 0) break; // out of boundary
|
|
70 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
|
|
71 seq[z/2] |= (z&1)? 0x0f : 0xf0;
|
|
72 bam1_qual(b)[z] = 0;
|
|
73 }
|
|
74 }
|
|
75 if (j < l) break;
|
|
76 x += l; y += l;
|
|
77 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
|
|
78 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
|
|
79 }
|
|
80 }
|
|
81 // update NM
|
|
82 old_nm = bam_aux_get(b, "NM");
|
|
83 if (c->flag & BAM_FUNMAP) return;
|
|
84 if (old_nm) old_nm_i = bam_aux2i(old_nm);
|
|
85 if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
|
|
86 else if (nm != old_nm_i) {
|
|
87 fprintf(stderr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
|
|
88 bam_aux_del(b, old_nm);
|
|
89 bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
|
|
90 }
|
|
91 // update MD
|
|
92 old_md = bam_aux_get(b, "MD");
|
|
93 if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
|
|
94 else {
|
|
95 int is_diff = 0;
|
|
96 if (strlen((char*)old_md+1) == str->l) {
|
|
97 for (i = 0; i < str->l; ++i)
|
|
98 if (toupper(old_md[i+1]) != toupper(str->s[i]))
|
|
99 break;
|
|
100 if (i < str->l) is_diff = 1;
|
|
101 } else is_diff = 1;
|
|
102 if (is_diff) {
|
|
103 fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
|
|
104 bam_aux_del(b, old_md);
|
|
105 bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
|
|
106 }
|
|
107 }
|
|
108 free(str->s); free(str);
|
|
109 }
|
|
110
|
|
111 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
|
|
112 {
|
|
113 bam_fillmd1_core(b, ref, is_equal, 0);
|
|
114 }
|
|
115
|
|
116 int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
|
|
117 {
|
|
118 uint8_t *seq = bam1_seq(b), *qual = bam1_qual(b);
|
|
119 uint32_t *cigar = bam1_cigar(b);
|
|
120 bam1_core_t *c = &b->core;
|
|
121 int i, x, y, mm, q, len, clip_l, clip_q;
|
|
122 double t;
|
|
123 if (thres < 0) thres = 40; // set the default
|
|
124 mm = q = len = clip_l = clip_q = 0;
|
|
125 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
|
|
126 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
|
|
127 if (op == BAM_CMATCH) {
|
|
128 for (j = 0; j < l; ++j) {
|
|
129 int z = y + j;
|
|
130 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
|
|
131 if (ref[x+j] == 0) break; // out of boundary
|
|
132 if (c2 != 15 && c1 != 15 && qual[z] >= 13) { // not ambiguous
|
|
133 ++len;
|
|
134 if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
|
|
135 ++mm;
|
|
136 q += qual[z] > 33? 33 : qual[z];
|
|
137 }
|
|
138 }
|
|
139 }
|
|
140 if (j < l) break;
|
|
141 x += l; y += l; len += l;
|
|
142 } else if (op == BAM_CDEL) {
|
|
143 for (j = 0; j < l; ++j)
|
|
144 if (ref[x+j] == 0) break;
|
|
145 if (j < l) break;
|
|
146 x += l;
|
|
147 } else if (op == BAM_CSOFT_CLIP) {
|
|
148 for (j = 0; j < l; ++j) clip_q += qual[y+j];
|
|
149 clip_l += l;
|
|
150 y += l;
|
|
151 } else if (op == BAM_CHARD_CLIP) {
|
|
152 clip_q += 13 * l;
|
|
153 clip_l += l;
|
|
154 } else if (op == BAM_CINS) y += l;
|
|
155 else if (op == BAM_CREF_SKIP) x += l;
|
|
156 }
|
|
157 for (i = 0, t = 1; i < mm; ++i)
|
|
158 t *= (double)len / (i+1);
|
|
159 t = q - 4.343 * log(t) + clip_q / 5.;
|
|
160 if (t > thres) return -1;
|
|
161 if (t < 0) t = 0;
|
|
162 t = sqrt((thres - t) / thres) * thres;
|
|
163 // fprintf(stderr, "%s %lf %d\n", bam1_qname(b), t, q);
|
|
164 return (int)(t + .499);
|
|
165 }
|
|
166
|
|
167 int bam_prob_realn_core(bam1_t *b, const char *ref, int flag)
|
|
168 {
|
|
169 int k, i, bw, x, y, yb, ye, xb, xe, apply_baq = flag&1, extend_baq = flag>>1&1;
|
|
170 uint32_t *cigar = bam1_cigar(b);
|
|
171 bam1_core_t *c = &b->core;
|
|
172 kpa_par_t conf = kpa_par_def;
|
|
173 uint8_t *bq = 0, *zq = 0, *qual = bam1_qual(b);
|
|
174 if ((c->flag & BAM_FUNMAP) || b->core.l_qseq == 0) return -1; // do nothing
|
|
175 // test if BQ or ZQ is present
|
|
176 if ((bq = bam_aux_get(b, "BQ")) != 0) ++bq;
|
|
177 if ((zq = bam_aux_get(b, "ZQ")) != 0 && *zq == 'Z') ++zq;
|
|
178 if (bq && zq) { // remove the ZQ tag
|
|
179 bam_aux_del(b, zq-1);
|
|
180 zq = 0;
|
|
181 }
|
|
182 if (bq || zq) {
|
|
183 if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing
|
|
184 if (bq && apply_baq) { // then convert BQ to ZQ
|
|
185 for (i = 0; i < c->l_qseq; ++i)
|
|
186 qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64);
|
|
187 *(bq - 3) = 'Z';
|
|
188 } else if (zq && !apply_baq) { // then convert ZQ to BQ
|
|
189 for (i = 0; i < c->l_qseq; ++i)
|
|
190 qual[i] += (int)zq[i] - 64;
|
|
191 *(zq - 3) = 'B';
|
|
192 }
|
|
193 return 0;
|
|
194 }
|
|
195 // find the start and end of the alignment
|
|
196 x = c->pos, y = 0, yb = ye = xb = xe = -1;
|
|
197 for (k = 0; k < c->n_cigar; ++k) {
|
|
198 int op, l;
|
|
199 op = cigar[k]&0xf; l = cigar[k]>>4;
|
|
200 if (op == BAM_CMATCH) {
|
|
201 if (yb < 0) yb = y;
|
|
202 if (xb < 0) xb = x;
|
|
203 ye = y + l; xe = x + l;
|
|
204 x += l; y += l;
|
|
205 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
|
|
206 else if (op == BAM_CDEL) x += l;
|
|
207 else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip
|
|
208 }
|
|
209 // set bandwidth and the start and the end
|
|
210 bw = 7;
|
|
211 if (abs((xe - xb) - (ye - yb)) > bw)
|
|
212 bw = abs((xe - xb) - (ye - yb)) + 3;
|
|
213 conf.bw = bw;
|
|
214 xb -= yb + bw/2; if (xb < 0) xb = 0;
|
|
215 xe += c->l_qseq - ye + bw/2;
|
|
216 if (xe - xb - c->l_qseq > bw)
|
|
217 xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
|
|
218 { // glocal
|
|
219 uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq;
|
|
220 int *state;
|
|
221 bq = calloc(c->l_qseq + 1, 1);
|
|
222 memcpy(bq, qual, c->l_qseq);
|
|
223 s = calloc(c->l_qseq, 1);
|
|
224 for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
|
|
225 r = calloc(xe - xb, 1);
|
|
226 for (i = xb; i < xe; ++i) {
|
|
227 if (ref[i] == 0) { xe = i; break; }
|
|
228 r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
|
|
229 }
|
|
230 state = calloc(c->l_qseq, sizeof(int));
|
|
231 q = calloc(c->l_qseq, 1);
|
|
232 kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
|
|
233 if (!extend_baq) { // in this block, bq[] is capped by base quality qual[]
|
|
234 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
|
|
235 int op = cigar[k]&0xf, l = cigar[k]>>4;
|
|
236 if (op == BAM_CMATCH) {
|
|
237 for (i = y; i < y + l; ++i) {
|
|
238 if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0;
|
|
239 else bq[i] = bq[i] < q[i]? bq[i] : q[i];
|
|
240 }
|
|
241 x += l; y += l;
|
|
242 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
|
|
243 else if (op == BAM_CDEL) x += l;
|
|
244 }
|
|
245 for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ
|
|
246 } else { // in this block, bq[] is BAQ that can be larger than qual[] (different from the above!)
|
|
247 uint8_t *left, *rght;
|
|
248 left = calloc(c->l_qseq, 1); rght = calloc(c->l_qseq, 1);
|
|
249 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
|
|
250 int op = cigar[k]&0xf, l = cigar[k]>>4;
|
|
251 if (op == BAM_CMATCH) {
|
|
252 for (i = y; i < y + l; ++i)
|
|
253 bq[i] = ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y))? 0 : q[i];
|
|
254 for (left[y] = bq[y], i = y + 1; i < y + l; ++i)
|
|
255 left[i] = bq[i] > left[i-1]? bq[i] : left[i-1];
|
|
256 for (rght[y+l-1] = bq[y+l-1], i = y + l - 2; i >= y; --i)
|
|
257 rght[i] = bq[i] > rght[i+1]? bq[i] : rght[i+1];
|
|
258 for (i = y; i < y + l; ++i)
|
|
259 bq[i] = left[i] < rght[i]? left[i] : rght[i];
|
|
260 x += l; y += l;
|
|
261 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
|
|
262 else if (op == BAM_CDEL) x += l;
|
|
263 }
|
|
264 for (i = 0; i < c->l_qseq; ++i) bq[i] = 64 + (qual[i] <= bq[i]? 0 : qual[i] - bq[i]); // finalize BQ
|
|
265 free(left); free(rght);
|
|
266 }
|
|
267 if (apply_baq) {
|
|
268 for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual
|
|
269 bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq);
|
|
270 } else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq);
|
|
271 free(bq); free(s); free(r); free(q); free(state);
|
|
272 }
|
|
273 return 0;
|
|
274 }
|
|
275
|
|
276 int bam_prob_realn(bam1_t *b, const char *ref)
|
|
277 {
|
|
278 return bam_prob_realn_core(b, ref, 1);
|
|
279 }
|
|
280
|
|
281 int bam_fillmd(int argc, char *argv[])
|
|
282 {
|
|
283 int c, is_equal, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, baq_flag;
|
|
284 samfile_t *fp, *fpout = 0;
|
|
285 faidx_t *fai;
|
|
286 char *ref = 0, mode_w[8], mode_r[8];
|
|
287 bam1_t *b;
|
|
288
|
|
289 is_equal = is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = capQ = baq_flag = 0;
|
|
290 mode_w[0] = mode_r[0] = 0;
|
|
291 strcpy(mode_r, "r"); strcpy(mode_w, "w");
|
|
292 while ((c = getopt(argc, argv, "EreubSC:n:A")) >= 0) {
|
|
293 switch (c) {
|
|
294 case 'r': is_realn = 1; break;
|
|
295 case 'e': is_equal = 1; break;
|
|
296 case 'b': is_bam_out = 1; break;
|
|
297 case 'u': is_uncompressed = is_bam_out = 1; break;
|
|
298 case 'S': is_sam_in = 1; break;
|
|
299 case 'n': max_nm = atoi(optarg); break;
|
|
300 case 'C': capQ = atoi(optarg); break;
|
|
301 case 'A': baq_flag |= 1; break;
|
|
302 case 'E': baq_flag |= 2; break;
|
|
303 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
|
|
304 }
|
|
305 }
|
|
306 if (!is_sam_in) strcat(mode_r, "b");
|
|
307 if (is_bam_out) strcat(mode_w, "b");
|
|
308 else strcat(mode_w, "h");
|
|
309 if (is_uncompressed) strcat(mode_w, "u");
|
|
310 if (optind + 1 >= argc) {
|
|
311 fprintf(stderr, "\n");
|
|
312 fprintf(stderr, "Usage: samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
|
|
313 fprintf(stderr, "Options: -e change identical bases to '='\n");
|
|
314 fprintf(stderr, " -u uncompressed BAM output (for piping)\n");
|
|
315 fprintf(stderr, " -b compressed BAM output\n");
|
|
316 fprintf(stderr, " -S the input is SAM with header\n");
|
|
317 fprintf(stderr, " -A modify the quality string\n");
|
|
318 fprintf(stderr, " -r compute the BQ tag (without -A) or cap baseQ by BAQ (with -A)\n");
|
|
319 fprintf(stderr, " -E extended BAQ for better sensitivity but lower specificity\n\n");
|
|
320 return 1;
|
|
321 }
|
|
322 fp = samopen(argv[optind], mode_r, 0);
|
|
323 if (fp == 0) return 1;
|
|
324 if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
|
|
325 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
|
|
326 return 1;
|
|
327 }
|
|
328 fpout = samopen("-", mode_w, fp->header);
|
|
329 fai = fai_load(argv[optind+1]);
|
|
330
|
|
331 b = bam_init1();
|
|
332 while ((ret = samread(fp, b)) >= 0) {
|
|
333 if (b->core.tid >= 0) {
|
|
334 if (tid != b->core.tid) {
|
|
335 free(ref);
|
|
336 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
|
|
337 tid = b->core.tid;
|
|
338 if (ref == 0)
|
|
339 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
|
|
340 fp->header->target_name[tid]);
|
|
341 }
|
|
342 if (is_realn) bam_prob_realn_core(b, ref, baq_flag);
|
|
343 if (capQ > 10) {
|
|
344 int q = bam_cap_mapQ(b, ref, capQ);
|
|
345 if (b->core.qual > q) b->core.qual = q;
|
|
346 }
|
|
347 if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
|
|
348 }
|
|
349 samwrite(fpout, b);
|
|
350 }
|
|
351 bam_destroy1(b);
|
|
352
|
|
353 free(ref);
|
|
354 fai_destroy(fai);
|
|
355 samclose(fp); samclose(fpout);
|
|
356 return 0;
|
|
357 }
|