]> git.donarmstrong.com Git - samtools.git/blob - bam_md.c
* samtools-0.1.5-11 (r408)
[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         old_md = bam_aux_get(b, "MD");
20         old_nm = bam_aux_get(b, "NM");
21         if (c->flag & BAM_FUNMAP) return;
22         if (old_nm) old_nm_i = bam_aux2i(old_nm);
23         str = (kstring_t*)calloc(1, sizeof(kstring_t));
24         for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
25                 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
26                 if (op == BAM_CMATCH) {
27                         for (j = 0; j < l; ++j) {
28                                 int z = y + j;
29                                 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
30                                 if (ref[x+j] == 0) break; // out of boundary
31                                 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
32                                         if (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
33                                         ++u;
34                                 } else {
35                                         ksprintf(str, "%d", u);
36                                         kputc(ref[x+j], str);
37                                         u = 0; ++nm;
38                                 }
39                         }
40                         if (j < l) break;
41                         x += l; y += l;
42                 } else if (op == BAM_CDEL) {
43                         ksprintf(str, "%d", u);
44                         kputc('^', str);
45                         for (j = 0; j < l; ++j) {
46                                 if (ref[x+j] == 0) break;
47                                 kputc(ref[x+j], str);
48                         }
49                         u = 0;
50                         if (j < l) break;
51                         x += l; nm += l;
52                 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
53                         y += l;
54                         if (op == BAM_CINS) nm += l;
55                 } else if (op == BAM_CREF_SKIP) {
56                         x += l;
57                 }
58         }
59         ksprintf(str, "%d", u);
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         if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
67         else {
68                 int is_diff = 0;
69                 if (strlen((char*)old_md+1) == str->l) {
70                         for (i = 0; i < str->l; ++i)
71                                 if (toupper(old_md[i+1]) != toupper(str->s[i]))
72                                         break;
73                         if (i < str->l) is_diff = 1;
74                 } else is_diff = 1;
75                 if (is_diff) {
76                         fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
77                         bam_aux_del(b, old_md);
78                         bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
79                 }
80         }
81         free(str->s); free(str);
82 }
83
84 int bam_fillmd(int argc, char *argv[])
85 {
86         int c, is_equal = 0, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed;
87         samfile_t *fp, *fpout = 0;
88         faidx_t *fai;
89         char *ref = 0, mode_w[8], mode_r[8];
90         bam1_t *b;
91
92         is_bam_out = is_sam_in = is_uncompressed = 0;
93         mode_w[0] = mode_r[0] = 0;
94         strcpy(mode_r, "r"); strcpy(mode_w, "w");
95         while ((c = getopt(argc, argv, "eubS")) >= 0) {
96                 switch (c) {
97                 case 'e': is_equal = 1; break;
98                 case 'b': is_bam_out = 1; break;
99                 case 'u': is_uncompressed = is_bam_out = 1; break;
100                 case 'S': is_sam_in = 1; break;
101                 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
102                 }
103         }
104         if (!is_sam_in) strcat(mode_r, "b");
105         if (is_bam_out) strcat(mode_w, "b");
106         else strcat(mode_w, "h");
107         if (is_uncompressed) strcat(mode_w, "u");
108         if (optind + 1 >= argc) {
109                 fprintf(stderr, "\n");
110                 fprintf(stderr, "Usage:   samtools fillmd [-eubS] <aln.bam> <ref.fasta>\n\n");
111                 fprintf(stderr, "Options: -e       change identical bases to '='\n");
112                 fprintf(stderr, "         -u       uncompressed BAM output (for piping)\n");
113                 fprintf(stderr, "         -b       compressed BAM output\n");
114                 fprintf(stderr, "         -S       the input is SAM with header\n\n");
115                 return 1;
116         }
117         fp = samopen(argv[optind], mode_r, 0);
118         if (fp == 0) return 1;
119         if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
120                 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
121                 return 1;
122         }
123         fpout = samopen("-", mode_w, fp->header);
124         fai = fai_load(argv[optind+1]);
125
126         b = bam_init1();
127         while ((ret = samread(fp, b)) >= 0) {
128                 if (b->core.tid >= 0) {
129                         if (tid != b->core.tid) {
130                                 free(ref);
131                                 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
132                                 tid = b->core.tid;
133                         }
134                         bam_fillmd1(b, ref, is_equal);
135                 }
136                 samwrite(fpout, b);
137         }
138         bam_destroy1(b);
139
140         free(ref);
141         fai_destroy(fai);
142         samclose(fp); samclose(fpout);
143         return 0;
144 }