Merge commit 'upstream/0.1.7.dfsg'
[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(bam1_t *b, char *ref, int is_equal)
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         // update NM
57         old_nm = bam_aux_get(b, "NM");
58         if (c->flag & BAM_FUNMAP) return;
59         if (old_nm) old_nm_i = bam_aux2i(old_nm);
60         if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
61         else if (nm != old_nm_i) {
62                 fprintf(stderr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
63                 bam_aux_del(b, old_nm);
64                 bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
65         }
66         // update MD
67         old_md = bam_aux_get(b, "MD");
68         if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
69         else {
70                 int is_diff = 0;
71                 if (strlen((char*)old_md+1) == str->l) {
72                         for (i = 0; i < str->l; ++i)
73                                 if (toupper(old_md[i+1]) != toupper(str->s[i]))
74                                         break;
75                         if (i < str->l) is_diff = 1;
76                 } else is_diff = 1;
77                 if (is_diff) {
78                         fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
79                         bam_aux_del(b, old_md);
80                         bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
81                 }
82         }
83         free(str->s); free(str);
84 }
85
86 int bam_fillmd(int argc, char *argv[])
87 {
88         int c, is_equal = 0, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed;
89         samfile_t *fp, *fpout = 0;
90         faidx_t *fai;
91         char *ref = 0, mode_w[8], mode_r[8];
92         bam1_t *b;
93
94         is_bam_out = is_sam_in = is_uncompressed = 0;
95         mode_w[0] = mode_r[0] = 0;
96         strcpy(mode_r, "r"); strcpy(mode_w, "w");
97         while ((c = getopt(argc, argv, "eubS")) >= 0) {
98                 switch (c) {
99                 case 'e': is_equal = 1; break;
100                 case 'b': is_bam_out = 1; break;
101                 case 'u': is_uncompressed = is_bam_out = 1; break;
102                 case 'S': is_sam_in = 1; break;
103                 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
104                 }
105         }
106         if (!is_sam_in) strcat(mode_r, "b");
107         if (is_bam_out) strcat(mode_w, "b");
108         else strcat(mode_w, "h");
109         if (is_uncompressed) strcat(mode_w, "u");
110         if (optind + 1 >= argc) {
111                 fprintf(stderr, "\n");
112                 fprintf(stderr, "Usage:   samtools fillmd [-eubS] <aln.bam> <ref.fasta>\n\n");
113                 fprintf(stderr, "Options: -e       change identical bases to '='\n");
114                 fprintf(stderr, "         -u       uncompressed BAM output (for piping)\n");
115                 fprintf(stderr, "         -b       compressed BAM output\n");
116                 fprintf(stderr, "         -S       the input is SAM with header\n\n");
117                 return 1;
118         }
119         fp = samopen(argv[optind], mode_r, 0);
120         if (fp == 0) return 1;
121         if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
122                 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
123                 return 1;
124         }
125         fpout = samopen("-", mode_w, fp->header);
126         fai = fai_load(argv[optind+1]);
127
128         b = bam_init1();
129         while ((ret = samread(fp, b)) >= 0) {
130                 if (b->core.tid >= 0) {
131                         if (tid != b->core.tid) {
132                                 free(ref);
133                                 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
134                                 tid = b->core.tid;
135                                 if (ref == 0)
136                                         fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
137                                                         fp->header->target_name[tid]);
138                         }
139                         if (ref) bam_fillmd1(b, ref, is_equal);
140                 }
141                 samwrite(fpout, b);
142         }
143         bam_destroy1(b);
144
145         free(ref);
146         fai_destroy(fai);
147         samclose(fp); samclose(fpout);
148         return 0;
149 }