]> git.donarmstrong.com Git - samtools.git/blob - bam_md.c
9abff46290f183fa4e6f8fdd3323b7345513a3a8
[samtools.git] / bam_md.c
1 #include <unistd.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include "faidx.h"
5 #include "bam.h"
6 #include "kstring.h"
7
8 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
9 {
10         uint8_t *seq = bam1_seq(b);
11         uint32_t *cigar = bam1_cigar(b);
12         bam1_core_t *c = &b->core;
13         int i, x, y, u = 0;
14         kstring_t *str;
15         uint8_t *old_md;
16
17         old_md = bam_aux_get(b, "MD");
18         if (c->flag & BAM_FUNMAP) return;
19         if (old_md && !is_equal) return; // no need to add MD
20         str = (kstring_t*)calloc(1, sizeof(kstring_t));
21         for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
22                 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
23                 if (op == BAM_CMATCH) {
24                         for (j = 0; j < l; ++j) {
25                                 int z = y + j;
26                                 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
27                                 if (ref[x+j] == 0) break; // out of boundary
28                                 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) {
29                                         if (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
30                                         ++u;
31                                 } else {
32                                         ksprintf(str, "%d", u);
33                                         kputc(ref[x+j], str);
34                                         u = 0;
35                                 }
36                         }
37                         if (j < l) break;
38                         x += l; y += l;
39                 } else if (op == BAM_CDEL) {
40                         ksprintf(str, "%d", u);
41                         kputc('^', str);
42                         for (j = 0; j < l; ++j) {
43                                 if (ref[x+j] == 0) break;
44                                 kputc(ref[x+j], str);
45                         }
46                         u = 0;
47                         if (j < l) break;
48                         x += l;
49                 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
50                         y += l;
51                 } else if (op == BAM_CREF_SKIP) {
52                         x += l;
53                 }
54         }
55         ksprintf(str, "%d", u);
56         if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
57         else {
58                 int is_diff = 0;
59                 if (strlen((char*)old_md+1) == str->l) {
60                         for (i = 0; i < str->l; ++i)
61                                 if (toupper(old_md[i+1]) != toupper(str->s[i]))
62                                         break;
63                         if (i < str->l) is_diff = 1;
64                 } else is_diff = 1;
65                 if (is_diff)
66                         fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' != '%s'\n", bam1_qname(b), old_md+1, str->s);
67         }
68         free(str->s); free(str);
69 }
70
71 int bam_fillmd(int argc, char *argv[])
72 {
73         int c, is_equal = 0, tid = -2, ret, len;
74         bamFile fp, fpout = 0;
75         bam_header_t *header;
76         faidx_t *fai;
77         char *ref = 0;
78         bam1_t *b;
79
80         while ((c = getopt(argc, argv, "e")) >= 0) {
81                 switch (c) {
82                 case 'e': is_equal = 1; break;
83                 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
84                 }
85         }
86         if (optind + 1 >= argc) {
87                 fprintf(stderr, "Usage: bam fillmd [-e] <aln.bam> <ref.fasta>\n");
88                 return 1;
89         }
90         fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
91         assert(fp);
92         header = bam_header_read(fp);
93         fpout = bam_dopen(fileno(stdout), "w");
94         bam_header_write(fpout, header);
95         fai = fai_load(argv[optind+1]);
96
97         b = bam_init1();
98         while ((ret = bam_read1(fp, b)) >= 0) {
99                 if (b->core.tid >= 0) {
100                         if (tid != b->core.tid) {
101                                 free(ref);
102                                 ref = fai_fetch(fai, header->target_name[b->core.tid], &len);
103                                 tid = b->core.tid;
104                         }
105                         bam_fillmd1(b, ref, is_equal);
106                 }
107                 bam_write1(fpout, b);
108         }
109         bam_destroy1(b);
110
111         free(ref);
112         fai_destroy(fai);
113         bam_header_destroy(header);
114         bam_close(fp); bam_close(fpout);
115         return 0;
116 }