17b0a4a4fa6943313be1b6d78b18c5255fe44982
[samtools.git] / bam_md.c
1 #include <unistd.h>
2 #include <assert.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "faidx.h"
6 #include "sam.h"
7 #include "kstring.h"
8
9 void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
10 {
11         uint8_t *seq = bam1_seq(b);
12         uint32_t *cigar = bam1_cigar(b);
13         bam1_core_t *c = &b->core;
14         int i, x, y, u = 0;
15         kstring_t *str;
16         uint8_t *old_md, *old_nm;
17         int32_t old_nm_i = -1, nm = 0;
18
19         str = (kstring_t*)calloc(1, sizeof(kstring_t));
20         for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
21                 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
22                 if (op == BAM_CMATCH) {
23                         for (j = 0; j < l; ++j) {
24                                 int z = y + j;
25                                 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
26                                 if (ref[x+j] == 0) break; // out of boundary
27                                 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
28                                         if (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
29                                         ++u;
30                                 } else {
31                                         ksprintf(str, "%d", u);
32                                         kputc(ref[x+j], str);
33                                         u = 0; ++nm;
34                                 }
35                         }
36                         if (j < l) break;
37                         x += l; y += l;
38                 } else if (op == BAM_CDEL) {
39                         ksprintf(str, "%d", u);
40                         kputc('^', str);
41                         for (j = 0; j < l; ++j) {
42                                 if (ref[x+j] == 0) break;
43                                 kputc(ref[x+j], str);
44                         }
45                         u = 0;
46                         if (j < l) break;
47                         x += l; nm += l;
48                 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
49                         y += l;
50                         if (op == BAM_CINS) nm += l;
51                 } else if (op == BAM_CREF_SKIP) {
52                         x += l;
53                 }
54         }
55         ksprintf(str, "%d", u);
56         // apply max_nm
57         if (max_nm > 0 && nm >= max_nm) {
58                 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
59                         int j, l = cigar[i]>>4, op = cigar[i]&0xf;
60                         if (op == BAM_CMATCH) {
61                                 for (j = 0; j < l; ++j) {
62                                         int z = y + j;
63                                         int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
64                                         if (ref[x+j] == 0) break; // out of boundary
65                                         if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
66                                                 seq[z/2] |= (z&1)? 0x0f : 0xf0;
67                                                 bam1_qual(b)[z] = 0;
68                                         }
69                                 }
70                                 if (j < l) break;
71                                 x += l; y += l;
72                         } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
73                         else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
74                 }
75         }
76         // update NM
77         old_nm = bam_aux_get(b, "NM");
78         if (c->flag & BAM_FUNMAP) return;
79         if (old_nm) old_nm_i = bam_aux2i(old_nm);
80         if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
81         else if (nm != old_nm_i) {
82                 fprintf(stderr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
83                 bam_aux_del(b, old_nm);
84                 bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
85         }
86         // update MD
87         old_md = bam_aux_get(b, "MD");
88         if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
89         else {
90                 int is_diff = 0;
91                 if (strlen((char*)old_md+1) == str->l) {
92                         for (i = 0; i < str->l; ++i)
93                                 if (toupper(old_md[i+1]) != toupper(str->s[i]))
94                                         break;
95                         if (i < str->l) is_diff = 1;
96                 } else is_diff = 1;
97                 if (is_diff) {
98                         fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
99                         bam_aux_del(b, old_md);
100                         bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
101                 }
102         }
103         free(str->s); free(str);
104 }
105
106 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
107 {
108         bam_fillmd1_core(b, ref, is_equal, 0);
109 }
110
111 int bam_fillmd(int argc, char *argv[])
112 {
113         int c, is_equal = 0, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm = 0;
114         samfile_t *fp, *fpout = 0;
115         faidx_t *fai;
116         char *ref = 0, mode_w[8], mode_r[8];
117         bam1_t *b;
118
119         is_bam_out = is_sam_in = is_uncompressed = 0;
120         mode_w[0] = mode_r[0] = 0;
121         strcpy(mode_r, "r"); strcpy(mode_w, "w");
122         while ((c = getopt(argc, argv, "eubSn:")) >= 0) {
123                 switch (c) {
124                 case 'e': is_equal = 1; break;
125                 case 'b': is_bam_out = 1; break;
126                 case 'u': is_uncompressed = is_bam_out = 1; break;
127                 case 'S': is_sam_in = 1; break;
128                 case 'n': max_nm = atoi(optarg); break;
129                 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
130                 }
131         }
132         if (!is_sam_in) strcat(mode_r, "b");
133         if (is_bam_out) strcat(mode_w, "b");
134         else strcat(mode_w, "h");
135         if (is_uncompressed) strcat(mode_w, "u");
136         if (optind + 1 >= argc) {
137                 fprintf(stderr, "\n");
138                 fprintf(stderr, "Usage:   samtools fillmd [-eubS] <aln.bam> <ref.fasta>\n\n");
139                 fprintf(stderr, "Options: -e       change identical bases to '='\n");
140                 fprintf(stderr, "         -u       uncompressed BAM output (for piping)\n");
141                 fprintf(stderr, "         -b       compressed BAM output\n");
142                 fprintf(stderr, "         -S       the input is SAM with header\n\n");
143                 return 1;
144         }
145         fp = samopen(argv[optind], mode_r, 0);
146         if (fp == 0) return 1;
147         if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
148                 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
149                 return 1;
150         }
151         fpout = samopen("-", mode_w, fp->header);
152         fai = fai_load(argv[optind+1]);
153
154         b = bam_init1();
155         while ((ret = samread(fp, b)) >= 0) {
156                 if (b->core.tid >= 0) {
157                         if (tid != b->core.tid) {
158                                 free(ref);
159                                 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
160                                 tid = b->core.tid;
161                                 if (ref == 0)
162                                         fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
163                                                         fp->header->target_name[tid]);
164                         }
165                         if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
166                 }
167                 samwrite(fpout, b);
168         }
169         bam_destroy1(b);
170
171         free(ref);
172         fai_destroy(fai);
173         samclose(fp); samclose(fpout);
174         return 0;
175 }