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(bam1_t *b, const char *ref)
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 // find the start and end of the alignment
172 if (c->flag & BAM_FUNMAP) return -1;
173 x = c->pos, y = 0, yb = ye = xb = xe = -1;
174 for (k = 0; k < c->n_cigar; ++k) {
176 op = cigar[k]&0xf; l = cigar[k]>>4;
177 if (op == BAM_CMATCH) {
180 ye = y + l; xe = x + l;
182 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
183 else if (op == BAM_CDEL) x += l;
184 else if (op == BAM_CREF_SKIP) return -1;
186 // set bandwidth and the start and the end
188 if (abs((xe - xb) - (ye - yb)) > bw)
189 bw = abs((xe - xb) - (ye - yb)) + 3;
191 xb -= yb + bw/2; if (xb < 0) xb = 0;
192 xe += c->l_qseq - ye + bw/2;
193 if (xe - xb - c->l_qseq > bw)
194 xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
196 uint8_t *s, *r, *q, *seq = bam1_seq(b), *qual = bam1_qual(b);
198 s = calloc(c->l_qseq, 1);
199 for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
200 r = calloc(xe - xb, 1);
201 for (i = xb; i < xe; ++i)
202 r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
203 state = calloc(c->l_qseq, sizeof(int));
204 q = calloc(c->l_qseq, 1);
205 kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
206 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
207 int op = cigar[k]&0xf, l = cigar[k]>>4;
208 if (op == BAM_CMATCH) {
209 for (i = y; i < y + l; ++i) {
210 if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) qual[i] = 0;
211 else qual[i] = qual[i] < q[i]? qual[i] : q[i];
214 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
215 else if (op == BAM_CDEL) x += l;
217 free(s); free(r); free(q); free(state);
222 int bam_fillmd(int argc, char *argv[])
224 int c, is_equal = 0, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm = 0, is_realn, capQ = 0;
225 samfile_t *fp, *fpout = 0;
227 char *ref = 0, mode_w[8], mode_r[8];
230 is_bam_out = is_sam_in = is_uncompressed = is_realn = 0;
231 mode_w[0] = mode_r[0] = 0;
232 strcpy(mode_r, "r"); strcpy(mode_w, "w");
233 while ((c = getopt(argc, argv, "reubSC:n:")) >= 0) {
235 case 'r': is_realn = 1; break;
236 case 'e': is_equal = 1; break;
237 case 'b': is_bam_out = 1; break;
238 case 'u': is_uncompressed = is_bam_out = 1; break;
239 case 'S': is_sam_in = 1; break;
240 case 'n': max_nm = atoi(optarg); break;
241 case 'C': capQ = atoi(optarg); break;
242 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
245 if (!is_sam_in) strcat(mode_r, "b");
246 if (is_bam_out) strcat(mode_w, "b");
247 else strcat(mode_w, "h");
248 if (is_uncompressed) strcat(mode_w, "u");
249 if (optind + 1 >= argc) {
250 fprintf(stderr, "\n");
251 fprintf(stderr, "Usage: samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
252 fprintf(stderr, "Options: -e change identical bases to '='\n");
253 fprintf(stderr, " -u uncompressed BAM output (for piping)\n");
254 fprintf(stderr, " -b compressed BAM output\n");
255 fprintf(stderr, " -S the input is SAM with header\n");
256 fprintf(stderr, " -r read-independent local realignment\n\n");
259 fp = samopen(argv[optind], mode_r, 0);
260 if (fp == 0) return 1;
261 if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
262 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
265 fpout = samopen("-", mode_w, fp->header);
266 fai = fai_load(argv[optind+1]);
269 while ((ret = samread(fp, b)) >= 0) {
270 if (b->core.tid >= 0) {
271 if (tid != b->core.tid) {
273 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
276 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
277 fp->header->target_name[tid]);
279 if (is_realn) bam_prob_realn(b, ref);
281 int q = bam_cap_mapQ(b, ref, capQ);
282 if (b->core.qual > q) b->core.qual = q;
284 if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
292 samclose(fp); samclose(fpout);