11 void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
13 uint8_t *seq = bam1_seq(b);
14 uint32_t *cigar = bam1_cigar(b);
15 bam1_core_t *c = &b->core;
18 uint8_t *old_md, *old_nm;
19 int32_t old_nm_i = -1, nm = 0;
21 str = (kstring_t*)calloc(1, sizeof(kstring_t));
22 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
23 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
24 if (op == BAM_CMATCH) {
25 for (j = 0; j < l; ++j) {
27 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
28 if (ref[x+j] == 0) break; // out of boundary
29 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
30 if (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
33 ksprintf(str, "%d", u);
40 } else if (op == BAM_CDEL) {
41 ksprintf(str, "%d", u);
43 for (j = 0; j < l; ++j) {
44 if (ref[x+j] == 0) break;
50 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
52 if (op == BAM_CINS) nm += l;
53 } else if (op == BAM_CREF_SKIP) {
57 ksprintf(str, "%d", u);
59 if (max_nm > 0 && nm >= max_nm) {
60 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
61 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
62 if (op == BAM_CMATCH) {
63 for (j = 0; j < l; ++j) {
65 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
66 if (ref[x+j] == 0) break; // out of boundary
67 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
68 seq[z/2] |= (z&1)? 0x0f : 0xf0;
74 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
75 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
79 old_nm = bam_aux_get(b, "NM");
80 if (c->flag & BAM_FUNMAP) return;
81 if (old_nm) old_nm_i = bam_aux2i(old_nm);
82 if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
83 else if (nm != old_nm_i) {
84 fprintf(stderr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
85 bam_aux_del(b, old_nm);
86 bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
89 old_md = bam_aux_get(b, "MD");
90 if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
93 if (strlen((char*)old_md+1) == str->l) {
94 for (i = 0; i < str->l; ++i)
95 if (toupper(old_md[i+1]) != toupper(str->s[i]))
97 if (i < str->l) is_diff = 1;
100 fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
101 bam_aux_del(b, old_md);
102 bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
105 free(str->s); free(str);
108 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
110 bam_fillmd1_core(b, ref, is_equal, 0);
113 int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
115 uint8_t *seq = bam1_seq(b), *qual = bam1_qual(b);
116 uint32_t *cigar = bam1_cigar(b);
117 bam1_core_t *c = &b->core;
118 int i, x, y, mm, q, len, clip_l, clip_q;
120 if (thres < 0) thres = 40; // set the default
121 mm = q = len = clip_l = clip_q = 0;
122 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
123 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
124 if (op == BAM_CMATCH) {
125 for (j = 0; j < l; ++j) {
127 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
128 if (ref[x+j] == 0) break; // out of boundary
129 if (c2 != 15 && c1 != 15 && qual[z] >= 13) { // not ambiguous
131 if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
133 q += qual[z] > 33? 33 : qual[z];
138 x += l; y += l; len += l;
139 } else if (op == BAM_CDEL) {
140 for (j = 0; j < l; ++j)
141 if (ref[x+j] == 0) break;
144 } else if (op == BAM_CSOFT_CLIP) {
145 for (j = 0; j < l; ++j) clip_q += qual[y+j];
148 } else if (op == BAM_CHARD_CLIP) {
151 } else if (op == BAM_CINS) y += l;
152 else if (op == BAM_CREF_SKIP) x += l;
154 for (i = 0, t = 1; i < mm; ++i)
155 t *= (double)len / (i+1);
156 t = q - 4.343 * log(t) + clip_q / 5.;
157 if (t > thres) return -1;
159 t = sqrt((thres - t) / thres) * thres;
160 // fprintf(stderr, "%s %lf %d\n", bam1_qname(b), t, q);
161 return (int)(t + .499);
166 #define MIN_REF_LEN 10
167 #define MIN_BAND_WIDTH 11
169 int bam_realn(bam1_t *b, const char *ref)
171 int k, score, q[2], r[2], kk[2], kl[2], x, y, max, j, n_cigar, endx = -1;
172 uint32_t *cigar = bam1_cigar(b);
173 uint8_t *seq = bam1_seq(b);
174 bam1_core_t *c = &b->core;
175 q[0] = q[1] = r[0] = r[1] = kk[0] = kk[1] = kl[0] = kl[1] = -1;
176 // find the right boundary
177 for (k = 0, score = max = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
178 int op = cigar[k]&0xf;
179 int ol = cigar[k]>>4;
180 if (op == BAM_CMATCH) {
181 for (j = 0; j < ol; ++j) {
182 int c1, c2, z = y + j;
183 c1 = bam_nt16_nt4_table[bam1_seqi(seq, z)];
184 if (ref[x+j] == 0) return -1;
185 c2 = bam_nt16_nt4_table[(int)bam_nt16_table[(int)ref[x+j]]];
186 if (c1 < 3 && c2 < 3)
187 score += c1 == c2? 5 : -4;
188 if (score < 0) score = 0;
189 if (score > max) max = score, q[1] = z, r[1] = x+j, kk[1] = k, kl[1] = j + 1;
192 } else if (op == BAM_CINS) {
195 if (score < 0) score = 0;
196 } else if (op == BAM_CDEL) {
199 if (score < 0) score = 0;
200 } else if (op == BAM_CSOFT_CLIP) y += ol;
201 else if (op == BAM_CREF_SKIP) x += ol;
203 if (q[1] < 0) return -1; // no high scoring segments
205 // find the left boundary
206 for (k = c->n_cigar - 1, score = max = 0, x = x-1, y = y-1; k >= 0; --k) {
207 int op = cigar[k]&0xf;
208 int ol = cigar[k]>>4;
209 if (op == BAM_CMATCH) {
210 for (j = 0; j < ol; ++j) {
211 int c1, c2, z = y - j;
212 c1 = bam_nt16_nt4_table[bam1_seqi(seq, z)];
213 if (ref[x+j] == 0) return -1;
214 c2 = bam_nt16_nt4_table[(int)bam_nt16_table[(int)ref[x-j]]];
215 if (c1 < 3 && c2 < 3)
216 score += c1 == c2? 5 : -4;
217 if (score < 0) score = 0;
218 if (score > max) max = score, q[0] = z, r[0] = x-j, kk[0] = k, kl[0] = j + 1;
221 } else if (op == BAM_CINS) {
224 if (score < 0) score = 0;
225 } else if (op == BAM_CDEL) {
228 if (score < 0) score = 0;
229 } else if (op == BAM_CSOFT_CLIP) y -= ol;
230 else if (op == BAM_CREF_SKIP) x -= ol;
232 if (q[0] < 0 || q[1] - q[0] < 15) return -1; // the high-scoring segment is too short
235 cigar = calloc(c->n_cigar + 4, 4);
236 if (q[0] != 0) cigar[n_cigar++] = (uint32_t)q[0]<<4 | BAM_CSOFT_CLIP;
237 if (r[0] != c->pos) cigar[n_cigar++] = (uint32_t)(r[0] - c->pos)<<4 | BAM_CREF_SKIP;
238 if (kk[0] == kk[1]) {
239 cigar[n_cigar++] = (uint32_t)(kl[0] + kl[1] - (bam1_cigar(b)[kk[0]]>>4))<<4 | BAM_CMATCH;
241 cigar[n_cigar++] = (uint32_t)kl[0]<<4 | BAM_CMATCH;
242 for (k = kk[0] + 1; k < kk[1]; ++k)
243 cigar[n_cigar++] = bam1_cigar(b)[k];
244 cigar[n_cigar++] = (uint32_t)kl[1]<<4 | BAM_CMATCH; // FIXME: add ref_skip after this line
246 if (q[1] + 1 != c->l_qseq)
247 cigar[n_cigar++] = (uint32_t)(c->l_qseq - q[1] - 1)<<4 | BAM_CSOFT_CLIP;
248 // copy over the alignment
249 if (4 * (n_cigar - (int)c->n_cigar) + b->data_len > b->m_data) { // enlarge b->data
250 b->m_data = 4 * (n_cigar - (int)c->n_cigar) + b->data_len;
251 kroundup32(b->m_data);
252 b->data = realloc(b->data, b->m_data);
254 if (n_cigar != (int)c->n_cigar) { // move data
255 memmove(b->data + c->l_qname + 4 * n_cigar, bam1_seq(b), b->data_len - c->l_qname - 4 * c->n_cigar);
256 b->data_len += 4 * (n_cigar - (int)c->n_cigar);
258 memcpy(bam1_cigar(b), cigar, n_cigar * 4);
259 c->n_cigar = n_cigar;
264 int bam_fillmd(int argc, char *argv[])
266 int c, is_equal = 0, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm = 0, is_realn, is_capQ;
267 samfile_t *fp, *fpout = 0;
269 char *ref = 0, mode_w[8], mode_r[8];
272 is_bam_out = is_sam_in = is_uncompressed = is_realn = is_capQ = 0;
273 mode_w[0] = mode_r[0] = 0;
274 strcpy(mode_r, "r"); strcpy(mode_w, "w");
275 while ((c = getopt(argc, argv, "reubSCn:")) >= 0) {
277 case 'r': is_realn = 1; break;
278 case 'e': is_equal = 1; break;
279 case 'b': is_bam_out = 1; break;
280 case 'u': is_uncompressed = is_bam_out = 1; break;
281 case 'S': is_sam_in = 1; break;
282 case 'n': max_nm = atoi(optarg); break;
283 case 'C': is_capQ = 1; break;
284 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
287 if (!is_sam_in) strcat(mode_r, "b");
288 if (is_bam_out) strcat(mode_w, "b");
289 else strcat(mode_w, "h");
290 if (is_uncompressed) strcat(mode_w, "u");
291 if (optind + 1 >= argc) {
292 fprintf(stderr, "\n");
293 fprintf(stderr, "Usage: samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
294 fprintf(stderr, "Options: -e change identical bases to '='\n");
295 fprintf(stderr, " -u uncompressed BAM output (for piping)\n");
296 fprintf(stderr, " -b compressed BAM output\n");
297 fprintf(stderr, " -S the input is SAM with header\n");
298 fprintf(stderr, " -r read-independent local realignment\n\n");
301 fp = samopen(argv[optind], mode_r, 0);
302 if (fp == 0) return 1;
303 if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
304 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
307 fpout = samopen("-", mode_w, fp->header);
308 fai = fai_load(argv[optind+1]);
311 while ((ret = samread(fp, b)) >= 0) {
312 if (b->core.tid >= 0) {
313 if (tid != b->core.tid) {
315 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
318 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
319 fp->header->target_name[tid]);
321 if (is_realn) bam_realn(b, ref);
323 int q = bam_cap_mapQ(b, ref, 40);
324 if (b->core.qual > q) b->core.qual = q;
326 if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
334 samclose(fp); samclose(fpout);