annotate pyPRADA_1.2/tools/samtools-0.1.16/bam_md.c @ 3:f17965495ec9 draft default tip

Uploaded
author siyuan
date Tue, 11 Mar 2014 12:14:01 -0400
parents acc2ca1a3ba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <unistd.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include <assert.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include <ctype.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 #include <math.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 #include "faidx.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 #include "sam.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 #include "kstring.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 #include "kaln.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 #include "kprobaln.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 uint8_t *seq = bam1_seq(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 uint32_t *cigar = bam1_cigar(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 bam1_core_t *c = &b->core;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 int i, x, y, u = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 kstring_t *str;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 uint8_t *old_md, *old_nm;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 int32_t old_nm_i = -1, nm = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 str = (kstring_t*)calloc(1, sizeof(kstring_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 if (op == BAM_CMATCH) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 for (j = 0; j < l; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 int z = y + j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 if (ref[x+j] == 0) break; // out of boundary
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 if (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 ++u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 ksprintf(str, "%d", u);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 kputc(ref[x+j], str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 u = 0; ++nm;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 if (j < l) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 x += l; y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 } else if (op == BAM_CDEL) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 ksprintf(str, "%d", u);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 kputc('^', str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 for (j = 0; j < l; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 if (ref[x+j] == 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 kputc(ref[x+j], str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 u = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 if (j < l) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 x += l; nm += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 if (op == BAM_CINS) nm += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 } else if (op == BAM_CREF_SKIP) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 x += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 ksprintf(str, "%d", u);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 // apply max_nm
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 if (max_nm > 0 && nm >= max_nm) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 if (op == BAM_CMATCH) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 for (j = 0; j < l; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 int z = y + j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 if (ref[x+j] == 0) break; // out of boundary
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 seq[z/2] |= (z&1)? 0x0f : 0xf0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 bam1_qual(b)[z] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 if (j < l) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 x += l; y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 // update NM
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 old_nm = bam_aux_get(b, "NM");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 if (c->flag & BAM_FUNMAP) return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 if (old_nm) old_nm_i = bam_aux2i(old_nm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 else if (nm != old_nm_i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 fprintf(stderr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 bam_aux_del(b, old_nm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 // update MD
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 old_md = bam_aux_get(b, "MD");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 int is_diff = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 if (strlen((char*)old_md+1) == str->l) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 for (i = 0; i < str->l; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 if (toupper(old_md[i+1]) != toupper(str->s[i]))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 if (i < str->l) is_diff = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 } else is_diff = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 if (is_diff) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 bam_aux_del(b, old_md);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 free(str->s); free(str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 bam_fillmd1_core(b, ref, is_equal, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 uint8_t *seq = bam1_seq(b), *qual = bam1_qual(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 uint32_t *cigar = bam1_cigar(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 bam1_core_t *c = &b->core;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 int i, x, y, mm, q, len, clip_l, clip_q;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 double t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 if (thres < 0) thres = 40; // set the default
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 mm = q = len = clip_l = clip_q = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 int j, l = cigar[i]>>4, op = cigar[i]&0xf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 if (op == BAM_CMATCH) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 for (j = 0; j < l; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 int z = y + j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 if (ref[x+j] == 0) break; // out of boundary
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 if (c2 != 15 && c1 != 15 && qual[z] >= 13) { // not ambiguous
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 ++len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 ++mm;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 q += qual[z] > 33? 33 : qual[z];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 if (j < l) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 x += l; y += l; len += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 } else if (op == BAM_CDEL) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 for (j = 0; j < l; ++j)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 if (ref[x+j] == 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 if (j < l) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 x += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 } else if (op == BAM_CSOFT_CLIP) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 for (j = 0; j < l; ++j) clip_q += qual[y+j];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 clip_l += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 } else if (op == BAM_CHARD_CLIP) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 clip_q += 13 * l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 clip_l += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 } else if (op == BAM_CINS) y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 else if (op == BAM_CREF_SKIP) x += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 for (i = 0, t = 1; i < mm; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 t *= (double)len / (i+1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 t = q - 4.343 * log(t) + clip_q / 5.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 if (t > thres) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 if (t < 0) t = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 t = sqrt((thres - t) / thres) * thres;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 // fprintf(stderr, "%s %lf %d\n", bam1_qname(b), t, q);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 return (int)(t + .499);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 int bam_prob_realn_core(bam1_t *b, const char *ref, int flag)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 int k, i, bw, x, y, yb, ye, xb, xe, apply_baq = flag&1, extend_baq = flag>>1&1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 uint32_t *cigar = bam1_cigar(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 bam1_core_t *c = &b->core;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 kpa_par_t conf = kpa_par_def;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 uint8_t *bq = 0, *zq = 0, *qual = bam1_qual(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174 if ((c->flag & BAM_FUNMAP) || b->core.l_qseq == 0) return -1; // do nothing
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 // test if BQ or ZQ is present
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 if ((bq = bam_aux_get(b, "BQ")) != 0) ++bq;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 if ((zq = bam_aux_get(b, "ZQ")) != 0 && *zq == 'Z') ++zq;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 if (bq && zq) { // remove the ZQ tag
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 bam_aux_del(b, zq-1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180 zq = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182 if (bq || zq) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 if (bq && apply_baq) { // then convert BQ to ZQ
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 for (i = 0; i < c->l_qseq; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186 qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187 *(bq - 3) = 'Z';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 } else if (zq && !apply_baq) { // then convert ZQ to BQ
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 for (i = 0; i < c->l_qseq; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 qual[i] += (int)zq[i] - 64;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 *(zq - 3) = 'B';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 // find the start and end of the alignment
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196 x = c->pos, y = 0, yb = ye = xb = xe = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 for (k = 0; k < c->n_cigar; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198 int op, l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 op = cigar[k]&0xf; l = cigar[k]>>4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 if (op == BAM_CMATCH) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 if (yb < 0) yb = y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 if (xb < 0) xb = x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203 ye = y + l; xe = x + l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 x += l; y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206 else if (op == BAM_CDEL) x += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 // set bandwidth and the start and the end
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
210 bw = 7;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
211 if (abs((xe - xb) - (ye - yb)) > bw)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
212 bw = abs((xe - xb) - (ye - yb)) + 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
213 conf.bw = bw;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
214 xb -= yb + bw/2; if (xb < 0) xb = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
215 xe += c->l_qseq - ye + bw/2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
216 if (xe - xb - c->l_qseq > bw)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
217 xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
218 { // glocal
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
219 uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
220 int *state;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
221 bq = calloc(c->l_qseq + 1, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
222 memcpy(bq, qual, c->l_qseq);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
223 s = calloc(c->l_qseq, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
224 for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
225 r = calloc(xe - xb, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
226 for (i = xb; i < xe; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
227 if (ref[i] == 0) { xe = i; break; }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
228 r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
229 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
230 state = calloc(c->l_qseq, sizeof(int));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
231 q = calloc(c->l_qseq, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
232 kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
233 if (!extend_baq) { // in this block, bq[] is capped by base quality qual[]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
234 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
235 int op = cigar[k]&0xf, l = cigar[k]>>4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
236 if (op == BAM_CMATCH) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
237 for (i = y; i < y + l; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
238 if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
239 else bq[i] = bq[i] < q[i]? bq[i] : q[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
240 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
241 x += l; y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
242 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
243 else if (op == BAM_CDEL) x += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
244 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
245 for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
246 } else { // in this block, bq[] is BAQ that can be larger than qual[] (different from the above!)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
247 uint8_t *left, *rght;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
248 left = calloc(c->l_qseq, 1); rght = calloc(c->l_qseq, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
249 for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
250 int op = cigar[k]&0xf, l = cigar[k]>>4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
251 if (op == BAM_CMATCH) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
252 for (i = y; i < y + l; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
253 bq[i] = ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y))? 0 : q[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
254 for (left[y] = bq[y], i = y + 1; i < y + l; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
255 left[i] = bq[i] > left[i-1]? bq[i] : left[i-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
256 for (rght[y+l-1] = bq[y+l-1], i = y + l - 2; i >= y; --i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
257 rght[i] = bq[i] > rght[i+1]? bq[i] : rght[i+1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
258 for (i = y; i < y + l; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
259 bq[i] = left[i] < rght[i]? left[i] : rght[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
260 x += l; y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
261 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
262 else if (op == BAM_CDEL) x += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
263 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
264 for (i = 0; i < c->l_qseq; ++i) bq[i] = 64 + (qual[i] <= bq[i]? 0 : qual[i] - bq[i]); // finalize BQ
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
265 free(left); free(rght);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
266 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
267 if (apply_baq) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
268 for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
269 bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
270 } else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
271 free(bq); free(s); free(r); free(q); free(state);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
272 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
273 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
274 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
275
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
276 int bam_prob_realn(bam1_t *b, const char *ref)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
277 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
278 return bam_prob_realn_core(b, ref, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
279 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
280
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
281 int bam_fillmd(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
282 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
283 int c, is_equal, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, baq_flag;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
284 samfile_t *fp, *fpout = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
285 faidx_t *fai;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
286 char *ref = 0, mode_w[8], mode_r[8];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
287 bam1_t *b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
288
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
289 is_equal = is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = capQ = baq_flag = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
290 mode_w[0] = mode_r[0] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
291 strcpy(mode_r, "r"); strcpy(mode_w, "w");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
292 while ((c = getopt(argc, argv, "EreubSC:n:A")) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
293 switch (c) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
294 case 'r': is_realn = 1; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
295 case 'e': is_equal = 1; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
296 case 'b': is_bam_out = 1; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
297 case 'u': is_uncompressed = is_bam_out = 1; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
298 case 'S': is_sam_in = 1; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
299 case 'n': max_nm = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
300 case 'C': capQ = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
301 case 'A': baq_flag |= 1; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
302 case 'E': baq_flag |= 2; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
303 default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
304 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
305 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
306 if (!is_sam_in) strcat(mode_r, "b");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
307 if (is_bam_out) strcat(mode_w, "b");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
308 else strcat(mode_w, "h");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
309 if (is_uncompressed) strcat(mode_w, "u");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
310 if (optind + 1 >= argc) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
311 fprintf(stderr, "\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
312 fprintf(stderr, "Usage: samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
313 fprintf(stderr, "Options: -e change identical bases to '='\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
314 fprintf(stderr, " -u uncompressed BAM output (for piping)\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
315 fprintf(stderr, " -b compressed BAM output\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
316 fprintf(stderr, " -S the input is SAM with header\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
317 fprintf(stderr, " -A modify the quality string\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
318 fprintf(stderr, " -r compute the BQ tag (without -A) or cap baseQ by BAQ (with -A)\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
319 fprintf(stderr, " -E extended BAQ for better sensitivity but lower specificity\n\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
320 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
321 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
322 fp = samopen(argv[optind], mode_r, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
323 if (fp == 0) return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
324 if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
325 fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
326 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
327 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
328 fpout = samopen("-", mode_w, fp->header);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
329 fai = fai_load(argv[optind+1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
330
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
331 b = bam_init1();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
332 while ((ret = samread(fp, b)) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
333 if (b->core.tid >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
334 if (tid != b->core.tid) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
335 free(ref);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
336 ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
337 tid = b->core.tid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
338 if (ref == 0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
339 fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
340 fp->header->target_name[tid]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
341 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
342 if (is_realn) bam_prob_realn_core(b, ref, baq_flag);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
343 if (capQ > 10) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
344 int q = bam_cap_mapQ(b, ref, capQ);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
345 if (b->core.qual > q) b->core.qual = q;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
346 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
347 if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
348 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
349 samwrite(fpout, b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
350 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
351 bam_destroy1(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
352
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
353 free(ref);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
354 fai_destroy(fai);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
355 samclose(fp); samclose(fpout);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
356 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
357 }