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) return -2; // do not know what to do...
178 if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing
179 if (bq && apply_baq) { // then convert BQ to ZQ
180 for (i = 0; i < c->l_qseq; ++i)
181 qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64);
183 } else if (zq && !apply_baq) { // then convert ZQ to BQ
184 for (i = 0; i < c->l_qseq; ++i)
185 qual[i] += (int)zq[i] - 64;
190 // find the start and end of the alignment
191 x = c->pos, y = 0, yb = ye = xb = xe = -1;
192 for (k = 0; k < c->n_cigar; ++k) {
194 op = cigar[k]&0xf; l = cigar[k]>>4;
195 if (op == BAM_CMATCH) {
198 ye = y + l; xe = x + l;
200 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
201 else if (op == BAM_CDEL) x += l;
202 else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip
204 // set bandwidth and the start and the end
206 if (abs((xe - xb) - (ye - yb)) > bw)
207 bw = abs((xe - xb) - (ye - yb)) + 3;
209 xb -= yb + bw/2; if (xb < 0) xb = 0;
210 xe += c->l_qseq - ye + bw/2;
211 if (xe - xb - c->l_qseq > bw)
212 xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
214 uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq;
216 bq = calloc(c->l_qseq + 1, 1);
217 memcpy(bq, qual, c->l_qseq);
218 s = calloc(c->l_qseq, 1);
219 for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
220 r = calloc(xe - xb, 1);
221 for (i = xb; i < xe; ++i)
222 r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
223 state = calloc(c->l_qseq, sizeof(int));
224 q = calloc(c->l_qseq, 1);
225 kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
226 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
227 int op = cigar[k]&0xf, l = cigar[k]>>4;
228 if (op == BAM_CMATCH) {
229 for (i = y; i < y + l; ++i) {
230 if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0;
231 else bq[i] = bq[i] < q[i]? bq[i] : q[i];
234 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
235 else if (op == BAM_CDEL) x += l;
237 for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ
239 for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual
240 bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq);
241 } else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq);
242 free(bq); free(s); free(r); free(q); free(state);
247 int bam_prob_realn(bam1_t *b, const char *ref)
249 return bam_prob_realn_core(b, ref, 1);
252 int bam_fillmd(int argc, char *argv[])
254 int c, is_equal, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, apply_baq;
255 samfile_t *fp, *fpout = 0;
257 char *ref = 0, mode_w[8], mode_r[8];
260 is_equal = is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = apply_baq = capQ = 0;
261 mode_w[0] = mode_r[0] = 0;
262 strcpy(mode_r, "r"); strcpy(mode_w, "w");
263 while ((c = getopt(argc, argv, "reubSC:n:A")) >= 0) {
265 case 'r': is_realn = 1; break;
266 case 'e': is_equal = 1; break;
267 case 'b': is_bam_out = 1; break;
268 case 'u': is_uncompressed = is_bam_out = 1; break;
269 case 'S': is_sam_in = 1; break;
270 case 'n': max_nm = atoi(optarg); break;
271 case 'C': capQ = atoi(optarg); break;
272 case 'A': apply_baq = 1; break;
273 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
276 if (!is_sam_in) strcat(mode_r, "b");
277 if (is_bam_out) strcat(mode_w, "b");
278 else strcat(mode_w, "h");
279 if (is_uncompressed) strcat(mode_w, "u");
280 if (optind + 1 >= argc) {
281 fprintf(stderr, "\n");
282 fprintf(stderr, "Usage: samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
283 fprintf(stderr, "Options: -e change identical bases to '='\n");
284 fprintf(stderr, " -u uncompressed BAM output (for piping)\n");
285 fprintf(stderr, " -b compressed BAM output\n");
286 fprintf(stderr, " -S the input is SAM with header\n");
287 fprintf(stderr, " -A modify the quality string\n");
288 fprintf(stderr, " -r read-independent local realignment\n\n");
291 fp = samopen(argv[optind], mode_r, 0);
292 if (fp == 0) return 1;
293 if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
294 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
297 fpout = samopen("-", mode_w, fp->header);
298 fai = fai_load(argv[optind+1]);
301 while ((ret = samread(fp, b)) >= 0) {
302 if (b->core.tid >= 0) {
303 if (tid != b->core.tid) {
305 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
308 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
309 fp->header->target_name[tid]);
311 if (is_realn) bam_prob_realn_core(b, ref, apply_baq);
313 int q = bam_cap_mapQ(b, ref, capQ);
314 if (b->core.qual > q) b->core.qual = q;
316 if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
324 samclose(fp); samclose(fpout);