12 char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
14 void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
16 uint8_t *seq = bam1_seq(b);
17 uint32_t *cigar = bam1_cigar(b);
18 bam1_core_t *c = &b->core;
21 uint8_t *old_md, *old_nm;
22 int32_t old_nm_i = -1, nm = 0;
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) {
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;
36 ksprintf(str, "%d", u);
43 } else if (op == BAM_CDEL) {
44 ksprintf(str, "%d", u);
46 for (j = 0; j < l; ++j) {
47 if (ref[x+j] == 0) break;
53 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
55 if (op == BAM_CINS) nm += l;
56 } else if (op == BAM_CREF_SKIP) {
60 ksprintf(str, "%d", u);
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) {
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;
77 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
78 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
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);
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);
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]))
100 if (i < str->l) is_diff = 1;
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);
108 free(str->s); free(str);
111 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
113 bam_fillmd1_core(b, ref, is_equal, 0);
116 int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
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;
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) {
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
134 if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
136 q += qual[z] > 33? 33 : qual[z];
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;
147 } else if (op == BAM_CSOFT_CLIP) {
148 for (j = 0; j < l; ++j) clip_q += qual[y+j];
151 } else if (op == BAM_CHARD_CLIP) {
154 } else if (op == BAM_CINS) y += l;
155 else if (op == BAM_CREF_SKIP) x += l;
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;
162 t = sqrt((thres - t) / thres) * thres;
163 // fprintf(stderr, "%s %lf %d\n", bam1_qname(b), t, q);
164 return (int)(t + .499);
167 int bam_prob_realn_core(bam1_t *b, const char *ref, int flag)
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);
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);
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;
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) {
199 op = cigar[k]&0xf; l = cigar[k]>>4;
200 if (op == BAM_CMATCH) {
203 ye = y + l; xe = x + 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
209 // set bandwidth and the start and the end
211 if (abs((xe - xb) - (ye - yb)) > bw)
212 bw = abs((xe - xb) - (ye - yb)) + 3;
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;
219 uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq;
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]]];
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];
242 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
243 else if (op == BAM_CDEL) x += l;
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];
261 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
262 else if (op == BAM_CDEL) x += l;
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);
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);
276 int bam_prob_realn(bam1_t *b, const char *ref)
278 return bam_prob_realn_core(b, ref, 1);
281 int bam_fillmd(int argc, char *argv[])
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;
286 char *ref = 0, mode_w[8], mode_r[8];
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) {
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;
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");
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");
328 fpout = samopen("-", mode_w, fp->header);
329 fai = fai_load(argv[optind+1]);
332 while ((ret = samread(fp, b)) >= 0) {
333 if (b->core.tid >= 0) {
334 if (tid != b->core.tid) {
336 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
339 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
340 fp->header->target_name[tid]);
342 if (is_realn) bam_prob_realn_core(b, ref, baq_flag);
344 int q = bam_cap_mapQ(b, ref, capQ);
345 if (b->core.qual > q) b->core.qual = q;
347 if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
355 samclose(fp); samclose(fpout);