9 void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
11 uint8_t *seq = bam1_seq(b);
12 uint32_t *cigar = bam1_cigar(b);
13 bam1_core_t *c = &b->core;
16 uint8_t *old_md, *old_nm;
17 int32_t old_nm_i = -1, nm = 0;
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) {
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;
31 ksprintf(str, "%d", u);
38 } else if (op == BAM_CDEL) {
39 ksprintf(str, "%d", u);
41 for (j = 0; j < l; ++j) {
42 if (ref[x+j] == 0) break;
48 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
50 if (op == BAM_CINS) nm += l;
51 } else if (op == BAM_CREF_SKIP) {
55 ksprintf(str, "%d", u);
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) {
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;
72 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
73 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
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);
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);
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]))
95 if (i < str->l) is_diff = 1;
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);
103 free(str->s); free(str);
106 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
108 bam_fillmd1_core(b, ref, is_equal, 0);
111 int bam_fillmd(int argc, char *argv[])
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;
116 char *ref = 0, mode_w[8], mode_r[8];
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) {
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;
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");
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");
151 fpout = samopen("-", mode_w, fp->header);
152 fai = fai_load(argv[optind+1]);
155 while ((ret = samread(fp, b)) >= 0) {
156 if (b->core.tid >= 0) {
157 if (tid != b->core.tid) {
159 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
162 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
163 fp->header->target_name[tid]);
165 if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
173 samclose(fp); samclose(fpout);