6 #include "bam_maqcns.h"
9 KHASH_SET_INIT_INT64(64)
11 #define BAM_PLF_SIMPLE 0x01
12 #define BAM_PLF_CNS 0x02
13 #define BAM_PLF_INDEL_ONLY 0x04
14 #define BAM_PLF_GLF 0x08
19 bam_maqindel_opt_t *ido;
26 glfFile fp; // for glf output only
29 char **bam_load_pos(const char *fn, int *_n);
30 void bam_init_header_hash(bam_header_t *header);
31 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name);
33 static khash_t(64) *load_pos(const char *fn, bam_header_t *h)
39 bam_init_header_hash(h);
40 list = bam_load_pos(fn, &n);
42 for (i = 0; i < n; ++i) {
43 x = (uint64_t)bam_get_tid(h, list[i]) << 32;
46 x |= *((uint32_t*)s) - 1;
47 kh_put(64, hash, x, &tmp);
54 // an analogy to pileup_func() below
55 static int glt_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
57 pu_data_t *d = (pu_data_t*)data;
58 bam_maqindel_ret_t *r = 0;
63 fprintf(stderr, "[glt_func] reference sequence is required for generating GLT. Abort!\n");
66 if (d->hash && kh_get(64, d->hash, (uint64_t)tid<<32|pos) == kh_end(d->hash)) return 0;
67 if (d->fai && (int)tid != d->tid) {
68 if (d->ref) { // then write the end mark
69 memset(&g2, 0, sizeof(glf2_t));
70 g2.type = GLF_TYPE_END;
71 bgzf_write(d->fp, &g2, sizeof(glf2_t));
73 glf_ref_write(d->fp, d->h->target_name[tid]); // write reference
75 d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len);
78 rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N';
79 g = bam_maqcns_glfgen(n, pu, bam_nt16_table[rb], d->c);
80 memcpy(&g2, g, sizeof(glf1_t));
81 g2.type = GLF_TYPE_NORMAL;
83 bgzf_write(d->fp, &g2, sizeof(glf2_t));
84 r = bam_maqindel(n, pos, d->ido, pu, d->ref);
85 if (r) { // then write indel line
88 if (min > r->gl[0]) min = r->gl[0];
89 if (min > r->gl[1]) min = r->gl[1];
91 g2.type = GLF_TYPE_INDEL;
93 g2.lk[0] = r->gl[0] - min < 255? r->gl[0] - min : 255;
94 g2.lk[1] = r->gl[1] - min < 255? r->gl[1] - min : 255;
95 g2.lk[2] = g3 - min < 255? g3 - min : 255;
96 *(int16_t*)(g2.lk + 3) = (int16_t)r->indel1;
97 *(int16_t*)(g2.lk + 5) = (int16_t)r->indel2;
98 g2.min_lk = min < 255? min : 255;
99 bgzf_write(d->fp, &g2, sizeof(glf2_t));
100 if (r->indel1) bgzf_write(d->fp, r->s[0]+1, r->indel1>0? r->indel1 : -r->indel1);
101 if (r->indel2) bgzf_write(d->fp, r->s[1]+1, r->indel2>0? r->indel2 : -r->indel2);
102 bam_maqindel_ret_destroy(r);
108 static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
110 pu_data_t *d = (pu_data_t*)data;
111 bam_maqindel_ret_t *r = 0;
114 if (d->hash && kh_get(64, d->hash, (uint64_t)tid<<32|pos) == kh_end(d->hash)) return 0;
115 if (d->format & BAM_PLF_GLF) return glt_func(tid, pos, n, pu, data);
116 if (d->fai && (int)tid != d->tid) {
118 d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len);
121 rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N';
122 if (d->format & BAM_PLF_INDEL_ONLY) {
123 for (i = 0; i < n; ++i)
124 if (pu[i].indel != 0) break;
125 if (i == n) return 0;
127 printf("%s\t%d\t%c\t", d->h->target_name[tid], pos + 1, rb);
128 if (d->format & BAM_PLF_CNS) { // consensus
129 int ref_q, rb4 = bam_nt16_table[rb];
130 x = bam_maqcns_call(n, pu, d->c);
132 if (rb4 != 15 && x>>28 != 15 && x>>28 != rb4) { // a SNP
133 ref_q = ((x>>24&0xf) == rb4)? x>>8&0xff : (x>>8&0xff) + (x&0xff);
134 if (ref_q > 255) ref_q = 255;
136 printf("%c\t%d\t%d\t%d\t", bam_nt16_rev_table[x>>28], x>>8&0xff, ref_q, x>>16&0xff);
138 if (d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY)) {
139 if (d->ref) // indel calling
140 r = bam_maqindel(n, pos, d->ido, pu, d->ref);
144 for (i = 0; i < n; ++i) {
145 const bam_pileup1_t *p = pu + i;
146 if (p->is_head) printf("^%c", p->b->core.qual > 93? 126 : p->b->core.qual + 33);
148 int c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
149 if (toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
150 else bam1_strand(p->b)? tolower(c) : toupper(c);
153 printf("+%d", p->indel);
154 for (j = 1; j <= p->indel; ++j) {
155 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
156 putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
158 } else if (p->indel < 0) {
159 printf("%d", p->indel);
160 for (j = 1; j <= -p->indel; ++j) {
161 c = (d->ref && (int)pos+j < d->len)? d->ref[pos+j] : 'N';
162 putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
166 if (p->is_tail) putchar('$');
169 for (i = 0; i < n; ++i) {
170 const bam_pileup1_t *p = pu + i;
171 int c = bam1_qual(p->b)[p->qpos] + 33;
172 if (c > 126) c = 126;
175 if (d->format & BAM_PLF_SIMPLE) {
177 for (i = 0; i < n; ++i) {
178 int c = pu[i].b->core.qual + 33;
179 if (c > 126) c = 126;
184 if (r) { // then print indel line
185 printf("%s\t%d\t*\t", d->h->target_name[tid], pos + 1);
186 if (r->gt < 2) printf("%s/%s\t", r->s[r->gt], r->s[r->gt]);
187 else printf("%s/%s\t", r->s[0], r->s[1]);
188 printf("%d\t%d\t", r->q_cns, r->q_ref);
189 printf("%s\t%s\t", r->s[0], r->s[1]);
190 //printf("%d\t%d\t", r->gl[0], r->gl[1]);
191 printf("%d\t%d\t%d\t%d\n", r->cnt1, r->cnt2, r->cnt_ambi, r->cnt_anti);
192 bam_maqindel_ret_destroy(r);
197 int bam_pileup(int argc, char *argv[])
200 char *fn_list = 0, *fn_fa = 0, *fn_pos = 0;
201 pu_data_t *d = (pu_data_t*)calloc(1, sizeof(pu_data_t));
202 d->tid = -1; d->mask = BAM_DEF_MASK;
203 d->c = bam_maqcns_init();
204 d->ido = bam_maqindel_opt_init();
205 while ((c = getopt(argc, argv, "st:f:cT:N:r:l:im:gI:G:")) >= 0) {
207 case 's': d->format |= BAM_PLF_SIMPLE; break;
208 case 't': fn_list = strdup(optarg); break;
209 case 'l': fn_pos = strdup(optarg); break;
210 case 'f': fn_fa = strdup(optarg); break;
211 case 'T': d->c->theta = atof(optarg); break;
212 case 'N': d->c->n_hap = atoi(optarg); break;
213 case 'r': d->c->het_rate = atoi(optarg); break;
214 case 'c': d->format |= BAM_PLF_CNS; break;
215 case 'i': d->format |= BAM_PLF_INDEL_ONLY; break;
216 case 'm': d->mask = atoi(optarg); break;
217 case 'g': d->format |= BAM_PLF_GLF; break;
218 case 'I': d->ido->q_indel = atoi(optarg); break;
219 case 'G': d->ido->r_indel = atof(optarg); break;
220 default: fprintf(stderr, "Unrecognizd option '-%c'.\n", c); return 1;
223 if (optind == argc) {
224 fprintf(stderr, "\n");
225 fprintf(stderr, "Usage: bamtk pileup [options] <in.bam>|<in.sam>\n\n");
226 fprintf(stderr, "Option: -s simple (yet incomplete) pileup format\n");
227 fprintf(stderr, " -i only show lines/consensus with indels\n");
228 fprintf(stderr, " -m INT filtering reads with bits in INT [%d]\n", d->mask);
229 fprintf(stderr, " -t FILE list of reference sequences (assume the input is in SAM)\n");
230 fprintf(stderr, " -l FILE list of sites at which pileup is output\n");
231 fprintf(stderr, " -f FILE reference sequence in the FASTA format\n\n");
232 fprintf(stderr, " -c output the maq consensus sequence\n");
233 fprintf(stderr, " -g output in the extended GLT format (suppressing -c/-i/-s)\n");
234 fprintf(stderr, " -T FLOAT theta in maq consensus calling model (for -c/-g) [%f]\n", d->c->theta);
235 fprintf(stderr, " -N INT number of haplotypes in the sample (for -c/-g) [%d]\n", d->c->n_hap);
236 fprintf(stderr, " -r FLOAT prior of a difference between two haplotypes (for -c/-g) [%f]\n", d->c->het_rate);
237 fprintf(stderr, " -G FLOAT prior of an indel between two haplotypes (for -c/-g) [%f]\n", d->ido->r_indel);
238 fprintf(stderr, " -I INT phred prob. of an indel in sequencing/prep. (for -c/-g) [%d]\n", d->ido->q_indel);
239 fprintf(stderr, "\n");
240 free(fn_list); free(fn_fa); free(d);
243 if (fn_fa) d->fai = fai_load(fn_fa);
245 bam_maqcns_prepare(d->c);
246 if (d->format & BAM_PLF_GLF) {
248 h = glf_header_init();
249 d->fp = bgzf_fdopen(fileno(stdout), "w");
250 glf_header_write(d->fp, h);
251 glf_header_destroy(h);
257 bam_plbuf_t *buf = bam_plbuf_init(pileup_func, d);
258 bam_plbuf_set_mask(buf, d->mask);
259 d->h = sam_header_read2(fn_list);
260 if (fn_pos) d->hash = load_pos(fn_pos, d->h);
261 fp = sam_open(argv[optind]);
262 b = (bam1_t*)calloc(1, sizeof(bam1_t));
263 while ((ret = sam_read1(fp, d->h, b)) >= 0)
264 bam_plbuf_push(b, buf);
265 bam_plbuf_push(0, buf);
266 bam_plbuf_destroy(buf);
271 fp = (strcmp(argv[optind], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[optind], "r");
272 d->h = bam_header_read(fp);
273 if (fn_pos) d->hash = load_pos(fn_pos, d->h);
274 bam_pileup_file(fp, d->mask, pileup_func, d);
277 if (d->format & BAM_PLF_GLF) bgzf_close(d->fp);
278 free(fn_pos); free(fn_list);
279 kh_destroy(64, d->hash);
280 bam_header_destroy(d->h);
281 if (d->fai) fai_destroy(d->fai);
282 bam_maqcns_destroy(d->c);
283 free(d->ido); free(d->ref); free(d);