]> git.donarmstrong.com Git - samtools.git/blob - bam_md.c
* samtools-0.1.9-15 (r821)
[samtools.git] / bam_md.c
1 #include <unistd.h>
2 #include <assert.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <math.h>
6 #include "faidx.h"
7 #include "sam.h"
8 #include "kstring.h"
9 #include "kaln.h"
10 #include "kprobaln.h"
11
12 void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
13 {
14         uint8_t *seq = bam1_seq(b);
15         uint32_t *cigar = bam1_cigar(b);
16         bam1_core_t *c = &b->core;
17         int i, x, y, u = 0;
18         kstring_t *str;
19         uint8_t *old_md, *old_nm;
20         int32_t old_nm_i = -1, nm = 0;
21
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) {
27                                 int z = y + 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;
32                                         ++u;
33                                 } else {
34                                         ksprintf(str, "%d", u);
35                                         kputc(ref[x+j], str);
36                                         u = 0; ++nm;
37                                 }
38                         }
39                         if (j < l) break;
40                         x += l; y += l;
41                 } else if (op == BAM_CDEL) {
42                         ksprintf(str, "%d", u);
43                         kputc('^', str);
44                         for (j = 0; j < l; ++j) {
45                                 if (ref[x+j] == 0) break;
46                                 kputc(ref[x+j], str);
47                         }
48                         u = 0;
49                         if (j < l) break;
50                         x += l; nm += l;
51                 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
52                         y += l;
53                         if (op == BAM_CINS) nm += l;
54                 } else if (op == BAM_CREF_SKIP) {
55                         x += l;
56                 }
57         }
58         ksprintf(str, "%d", u);
59         // apply max_nm
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) {
65                                         int z = y + 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;
70                                                 bam1_qual(b)[z] = 0;
71                                         }
72                                 }
73                                 if (j < l) break;
74                                 x += l; y += l;
75                         } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
76                         else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
77                 }
78         }
79         // update NM
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);
88         }
89         // update MD
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);
92         else {
93                 int is_diff = 0;
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]))
97                                         break;
98                         if (i < str->l) is_diff = 1;
99                 } else is_diff = 1;
100                 if (is_diff) {
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);
104                 }
105         }
106         free(str->s); free(str);
107 }
108
109 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
110 {
111         bam_fillmd1_core(b, ref, is_equal, 0);
112 }
113
114 int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
115 {
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;
120         double t;
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) {
127                                 int z = y + 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
131                                         ++len;
132                                         if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
133                                                 ++mm;
134                                                 q += qual[z] > 33? 33 : qual[z];
135                                         }
136                                 }
137                         }
138                         if (j < l) break;
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;
143                         if (j < l) break;
144                         x += l;
145                 } else if (op == BAM_CSOFT_CLIP) {
146                         for (j = 0; j < l; ++j) clip_q += qual[y+j];
147                         clip_l += l;
148                         y += l;
149                 } else if (op == BAM_CHARD_CLIP) {
150                         clip_q += 13 * l;
151                         clip_l += l;
152                 } else if (op == BAM_CINS) y += l;
153                 else if (op == BAM_CREF_SKIP) x += l;
154         }
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;
159         if (t < 0) t = 0;
160         t = sqrt((thres - t) / thres) * thres;
161 //      fprintf(stderr, "%s %lf %d\n", bam1_qname(b), t, q);
162         return (int)(t + .499);
163 }
164
165 int bam_prob_realn_core(bam1_t *b, const char *ref, int write_bq)
166 {
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         if (c->flag & BAM_FUNMAP) return -1;
172         if (bam_aux_get(b, "BQ")) return -2;
173         // find the start and end of the alignment      
174         x = c->pos, y = 0, yb = ye = xb = xe = -1;
175         for (k = 0; k < c->n_cigar; ++k) {
176                 int op, l;
177                 op = cigar[k]&0xf; l = cigar[k]>>4;
178                 if (op == BAM_CMATCH) {
179                         if (yb < 0) yb = y;
180                         if (xb < 0) xb = x;
181                         ye = y + l; xe = x + l;
182                         x += l; y += l;
183                 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
184                 else if (op == BAM_CDEL) x += l;
185                 else if (op == BAM_CREF_SKIP) return -1;
186         }
187         // set bandwidth and the start and the end
188         bw = 7;
189         if (abs((xe - xb) - (ye - yb)) > bw)
190                 bw = abs((xe - xb) - (ye - yb)) + 3;
191         conf.bw = bw;
192         xb -= yb + bw/2; if (xb < 0) xb = 0;
193         xe += c->l_qseq - ye + bw/2;
194         if (xe - xb - c->l_qseq > bw)
195                 xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
196         { // glocal
197                 uint8_t *s, *r, *q, *seq = bam1_seq(b), *qual = bam1_qual(b), *bq = 0;
198                 int *state;
199                 if (write_bq) {
200                         bq = calloc(c->l_qseq + 1, 1);
201                         memcpy(bq, qual, c->l_qseq);
202                 }
203                 s = calloc(c->l_qseq, 1);
204                 for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
205                 r = calloc(xe - xb, 1);
206                 for (i = xb; i < xe; ++i)
207                         r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
208                 state = calloc(c->l_qseq, sizeof(int));
209                 q = calloc(c->l_qseq, 1);
210                 kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
211                 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
212                         int op = cigar[k]&0xf, l = cigar[k]>>4;
213                         if (op == BAM_CMATCH) {
214                                 for (i = y; i < y + l; ++i) {
215                                         if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) qual[i] = 0;
216                                         else qual[i] = qual[i] < q[i]? qual[i] : q[i];
217                                 }
218                                 x += l; y += l;
219                         } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
220                         else if (op == BAM_CDEL) x += l;
221                 }
222                 if (write_bq) {
223                         for (i = 0; i < c->l_qseq; ++i) bq[i] = bq[i] - qual[i] + 33;
224                         bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq);
225                         free(bq);
226                 }
227                 free(s); free(r); free(q); free(state);
228         }
229         return 0;
230 }
231
232 int bam_prob_realn(bam1_t *b, const char *ref)
233 {
234         return bam_prob_realn_core(b, ref, 0);
235 }
236
237 int bam_fillmd(int argc, char *argv[])
238 {
239         int c, is_equal = 0, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm = 0, is_realn, capQ = 0;
240         samfile_t *fp, *fpout = 0;
241         faidx_t *fai;
242         char *ref = 0, mode_w[8], mode_r[8];
243         bam1_t *b;
244
245         is_bam_out = is_sam_in = is_uncompressed = is_realn = 0;
246         mode_w[0] = mode_r[0] = 0;
247         strcpy(mode_r, "r"); strcpy(mode_w, "w");
248         while ((c = getopt(argc, argv, "reubSC:n:")) >= 0) {
249                 switch (c) {
250                 case 'r': is_realn = 1; break;
251                 case 'e': is_equal = 1; break;
252                 case 'b': is_bam_out = 1; break;
253                 case 'u': is_uncompressed = is_bam_out = 1; break;
254                 case 'S': is_sam_in = 1; break;
255                 case 'n': max_nm = atoi(optarg); break;
256                 case 'C': capQ = atoi(optarg); break;
257                 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
258                 }
259         }
260         if (!is_sam_in) strcat(mode_r, "b");
261         if (is_bam_out) strcat(mode_w, "b");
262         else strcat(mode_w, "h");
263         if (is_uncompressed) strcat(mode_w, "u");
264         if (optind + 1 >= argc) {
265                 fprintf(stderr, "\n");
266                 fprintf(stderr, "Usage:   samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
267                 fprintf(stderr, "Options: -e       change identical bases to '='\n");
268                 fprintf(stderr, "         -u       uncompressed BAM output (for piping)\n");
269                 fprintf(stderr, "         -b       compressed BAM output\n");
270                 fprintf(stderr, "         -S       the input is SAM with header\n");
271                 fprintf(stderr, "         -r       read-independent local realignment\n\n");
272                 return 1;
273         }
274         fp = samopen(argv[optind], mode_r, 0);
275         if (fp == 0) return 1;
276         if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
277                 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
278                 return 1;
279         }
280         fpout = samopen("-", mode_w, fp->header);
281         fai = fai_load(argv[optind+1]);
282
283         b = bam_init1();
284         while ((ret = samread(fp, b)) >= 0) {
285                 if (b->core.tid >= 0) {
286                         if (tid != b->core.tid) {
287                                 free(ref);
288                                 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
289                                 tid = b->core.tid;
290                                 if (ref == 0)
291                                         fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
292                                                         fp->header->target_name[tid]);
293                         }
294                         if (is_realn) bam_prob_realn_core(b, ref, 1);
295                         if (capQ > 10) {
296                                 int q = bam_cap_mapQ(b, ref, capQ);
297                                 if (b->core.qual > q) b->core.qual = q;
298                         }
299                         if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
300                 }
301                 samwrite(fpout, b);
302         }
303         bam_destroy1(b);
304
305         free(ref);
306         fai_destroy(fai);
307         samclose(fp); samclose(fpout);
308         return 0;
309 }