12 void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
14 uint8_t *seq = bam1_seq(b);
15 uint32_t *cigar = bam1_cigar(b);
16 bam1_core_t *c = &b->core;
19 uint8_t *old_md, *old_nm;
20 int32_t old_nm_i = -1, nm = 0;
22 str = (kstring_t*)calloc(1, sizeof(kstring_t));
23 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
24 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
25 if (op == BAM_CMATCH) {
26 for (j = 0; j < l; ++j) {
28 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
29 if (ref[x+j] == 0) break; // out of boundary
30 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
31 if (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
34 ksprintf(str, "%d", u);
41 } else if (op == BAM_CDEL) {
42 ksprintf(str, "%d", u);
44 for (j = 0; j < l; ++j) {
45 if (ref[x+j] == 0) break;
51 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
53 if (op == BAM_CINS) nm += l;
54 } else if (op == BAM_CREF_SKIP) {
58 ksprintf(str, "%d", u);
60 if (max_nm > 0 && nm >= max_nm) {
61 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
62 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
63 if (op == BAM_CMATCH) {
64 for (j = 0; j < l; ++j) {
66 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
67 if (ref[x+j] == 0) break; // out of boundary
68 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
69 seq[z/2] |= (z&1)? 0x0f : 0xf0;
75 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
76 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
80 old_nm = bam_aux_get(b, "NM");
81 if (c->flag & BAM_FUNMAP) return;
82 if (old_nm) old_nm_i = bam_aux2i(old_nm);
83 if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
84 else if (nm != old_nm_i) {
85 fprintf(stderr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
86 bam_aux_del(b, old_nm);
87 bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
90 old_md = bam_aux_get(b, "MD");
91 if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
94 if (strlen((char*)old_md+1) == str->l) {
95 for (i = 0; i < str->l; ++i)
96 if (toupper(old_md[i+1]) != toupper(str->s[i]))
98 if (i < str->l) is_diff = 1;
101 fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
102 bam_aux_del(b, old_md);
103 bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
106 free(str->s); free(str);
109 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
111 bam_fillmd1_core(b, ref, is_equal, 0);
114 int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
116 uint8_t *seq = bam1_seq(b), *qual = bam1_qual(b);
117 uint32_t *cigar = bam1_cigar(b);
118 bam1_core_t *c = &b->core;
119 int i, x, y, mm, q, len, clip_l, clip_q;
121 if (thres < 0) thres = 40; // set the default
122 mm = q = len = clip_l = clip_q = 0;
123 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
124 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
125 if (op == BAM_CMATCH) {
126 for (j = 0; j < l; ++j) {
128 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
129 if (ref[x+j] == 0) break; // out of boundary
130 if (c2 != 15 && c1 != 15 && qual[z] >= 13) { // not ambiguous
132 if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
134 q += qual[z] > 33? 33 : qual[z];
139 x += l; y += l; len += l;
140 } else if (op == BAM_CDEL) {
141 for (j = 0; j < l; ++j)
142 if (ref[x+j] == 0) break;
145 } else if (op == BAM_CSOFT_CLIP) {
146 for (j = 0; j < l; ++j) clip_q += qual[y+j];
149 } else if (op == BAM_CHARD_CLIP) {
152 } else if (op == BAM_CINS) y += l;
153 else if (op == BAM_CREF_SKIP) x += l;
155 for (i = 0, t = 1; i < mm; ++i)
156 t *= (double)len / (i+1);
157 t = q - 4.343 * log(t) + clip_q / 5.;
158 if (t > thres) return -1;
160 t = sqrt((thres - t) / thres) * thres;
161 // fprintf(stderr, "%s %lf %d\n", bam1_qname(b), t, q);
162 return (int)(t + .499);
165 int bam_prob_realn_core(bam1_t *b, const char *ref, int apply_baq)
167 int k, i, bw, x, y, yb, ye, xb, xe;
168 uint32_t *cigar = bam1_cigar(b);
169 bam1_core_t *c = &b->core;
170 kpa_par_t conf = kpa_par_def;
171 uint8_t *bq = 0, *zq = 0, *qual = bam1_qual(b);
172 if (c->flag & BAM_FUNMAP) return -1; // do nothing
173 // test if BQ or ZQ is present
174 if ((bq = bam_aux_get(b, "BQ")) != 0) ++bq;
175 if ((zq = bam_aux_get(b, "ZQ")) != 0 && *zq == 'Z') ++zq;
176 if (bq && zq) { // remove the ZQ tag
177 bam_aux_del(b, zq-1);
181 if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing
182 if (bq && apply_baq) { // then convert BQ to ZQ
183 for (i = 0; i < c->l_qseq; ++i)
184 qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64);
186 } else if (zq && !apply_baq) { // then convert ZQ to BQ
187 for (i = 0; i < c->l_qseq; ++i)
188 qual[i] += (int)zq[i] - 64;
193 // find the start and end of the alignment
194 x = c->pos, y = 0, yb = ye = xb = xe = -1;
195 for (k = 0; k < c->n_cigar; ++k) {
197 op = cigar[k]&0xf; l = cigar[k]>>4;
198 if (op == BAM_CMATCH) {
201 ye = y + l; xe = x + l;
203 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
204 else if (op == BAM_CDEL) x += l;
205 else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip
207 // set bandwidth and the start and the end
209 if (abs((xe - xb) - (ye - yb)) > bw)
210 bw = abs((xe - xb) - (ye - yb)) + 3;
212 xb -= yb + bw/2; if (xb < 0) xb = 0;
213 xe += c->l_qseq - ye + bw/2;
214 if (xe - xb - c->l_qseq > bw)
215 xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
217 uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq;
219 bq = calloc(c->l_qseq + 1, 1);
220 memcpy(bq, qual, c->l_qseq);
221 s = calloc(c->l_qseq, 1);
222 for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
223 r = calloc(xe - xb, 1);
224 for (i = xb; i < xe; ++i)
225 r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
226 state = calloc(c->l_qseq, sizeof(int));
227 q = calloc(c->l_qseq, 1);
228 kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
229 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
230 int op = cigar[k]&0xf, l = cigar[k]>>4;
231 if (op == BAM_CMATCH) {
232 for (i = y; i < y + l; ++i) {
233 if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0;
234 else bq[i] = bq[i] < q[i]? bq[i] : q[i];
237 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
238 else if (op == BAM_CDEL) x += l;
240 for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ
242 for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual
243 bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq);
244 } else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq);
245 free(bq); free(s); free(r); free(q); free(state);
250 int bam_prob_realn(bam1_t *b, const char *ref)
252 return bam_prob_realn_core(b, ref, 1);
255 int bam_fillmd(int argc, char *argv[])
257 int c, is_equal, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, apply_baq;
258 samfile_t *fp, *fpout = 0;
260 char *ref = 0, mode_w[8], mode_r[8];
263 is_equal = is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = apply_baq = capQ = 0;
264 mode_w[0] = mode_r[0] = 0;
265 strcpy(mode_r, "r"); strcpy(mode_w, "w");
266 while ((c = getopt(argc, argv, "reubSC:n:A")) >= 0) {
268 case 'r': is_realn = 1; break;
269 case 'e': is_equal = 1; break;
270 case 'b': is_bam_out = 1; break;
271 case 'u': is_uncompressed = is_bam_out = 1; break;
272 case 'S': is_sam_in = 1; break;
273 case 'n': max_nm = atoi(optarg); break;
274 case 'C': capQ = atoi(optarg); break;
275 case 'A': apply_baq = 1; break;
276 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
279 if (!is_sam_in) strcat(mode_r, "b");
280 if (is_bam_out) strcat(mode_w, "b");
281 else strcat(mode_w, "h");
282 if (is_uncompressed) strcat(mode_w, "u");
283 if (optind + 1 >= argc) {
284 fprintf(stderr, "\n");
285 fprintf(stderr, "Usage: samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
286 fprintf(stderr, "Options: -e change identical bases to '='\n");
287 fprintf(stderr, " -u uncompressed BAM output (for piping)\n");
288 fprintf(stderr, " -b compressed BAM output\n");
289 fprintf(stderr, " -S the input is SAM with header\n");
290 fprintf(stderr, " -A modify the quality string\n");
291 fprintf(stderr, " -r read-independent local realignment\n\n");
294 fp = samopen(argv[optind], mode_r, 0);
295 if (fp == 0) return 1;
296 if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
297 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
300 fpout = samopen("-", mode_w, fp->header);
301 fai = fai_load(argv[optind+1]);
304 while ((ret = samread(fp, b)) >= 0) {
305 if (b->core.tid >= 0) {
306 if (tid != b->core.tid) {
308 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
311 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
312 fp->header->target_name[tid]);
314 if (is_realn) bam_prob_realn_core(b, ref, apply_baq);
316 int q = bam_cap_mapQ(b, ref, capQ);
317 if (b->core.qual > q) b->core.qual = q;
319 if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
327 samclose(fp); samclose(fpout);