6 #include "bam_maqcns.h"
8 KHASH_SET_INIT_INT64(64)
10 #define BAM_PLF_SIMPLE 0x01
11 #define BAM_PLF_CNS 0x02
16 bam_maqindel_opt_t *ido;
24 char **bam_load_pos(const char *fn, int *_n);
25 void bam_init_header_hash(bam_header_t *header);
26 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name);
28 static khash_t(64) *load_pos(const char *fn, bam_header_t *h)
34 bam_init_header_hash(h);
35 list = bam_load_pos(fn, &n);
37 for (i = 0; i < n; ++i) {
38 x = (uint64_t)bam_get_tid(h, list[i]) << 32;
41 x |= *((uint32_t*)s) - 1;
42 kh_put(64, hash, x, &tmp);
49 static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
51 pu_data_t *d = (pu_data_t*)data;
52 bam_maqindel_ret_t *r = 0;
55 if (d->hash && kh_get(64, d->hash, (uint64_t)tid<<32|pos) == kh_end(d->hash)) return 0;
56 if (d->fai && (int)tid != d->tid) {
58 d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len);
61 rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N';
62 printf("%s\t%d\t%c\t", d->h->target_name[tid], pos + 1, rb);
63 if (d->format & BAM_PLF_CNS) { // consensus
64 int ref_q, rb4 = bam_nt16_table[rb];
65 x = bam_maqcns_call(n, pu, d->c);
67 if (rb4 != 15 && x>>28 != 15 && x>>28 != rb4) { // a SNP
68 ref_q = ((x>>24&0xf) == rb4)? x>>8&0xff : (x>>8&0xff) + (x&0xff);
69 if (ref_q > 255) ref_q = 255;
71 printf("%c\t%d\t%d\t%d\t", bam_nt16_rev_table[x>>28], x>>8&0xff, ref_q, x>>16&0xff);
72 if (d->ref) // indel calling
73 r = bam_maqindel(n, pos, d->ido, pu, d->ref);
77 for (i = 0; i < n; ++i) {
78 const bam_pileup1_t *p = pu + i;
79 if (p->is_head) printf("^%c", p->b->core.qual > 93? 126 : p->b->core.qual + 33);
81 int c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
82 if (toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
83 else bam1_strand(p->b)? tolower(c) : toupper(c);
86 printf("+%d", p->indel);
87 for (j = 1; j <= p->indel; ++j) {
88 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
89 putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
91 } else if (p->indel < 0) {
92 printf("%d", p->indel);
93 for (j = 1; j <= -p->indel; ++j) {
94 c = (d->ref && (int)pos+j < d->len)? d->ref[pos+j] : 'N';
95 putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
99 if (p->is_tail) putchar('$');
102 for (i = 0; i < n; ++i) {
103 const bam_pileup1_t *p = pu + i;
104 int c = bam1_qual(p->b)[p->qpos] + 33;
105 if (c > 126) c = 126;
108 if (d->format & BAM_PLF_SIMPLE) {
110 for (i = 0; i < n; ++i) {
111 int c = pu[i].b->core.qual + 33;
112 if (c > 126) c = 126;
117 if (r) { // then print indel line
118 printf("%s\t%d\t*\t%s/%s\t", d->h->target_name[tid], pos + 1, r->s1, r->s2);
119 printf("%d\t%d\t%d\t%d\n", r->cnt1, r->cnt2, r->cnt_ambi, r->cnt_anti);
120 bam_maqindel_ret_destroy(r);
125 int bam_pileup(int argc, char *argv[])
128 char *fn_list = 0, *fn_fa = 0, *fn_pos = 0;
129 pu_data_t *d = (pu_data_t*)calloc(1, sizeof(pu_data_t));
131 d->c = bam_maqcns_init();
132 while ((c = getopt(argc, argv, "st:f:cT:N:r:l:")) >= 0) {
134 case 's': d->format |= BAM_PLF_SIMPLE; break;
135 case 't': fn_list = strdup(optarg); break;
136 case 'l': fn_pos = strdup(optarg); break;
137 case 'f': fn_fa = strdup(optarg); break;
138 case 'T': d->c->theta = atof(optarg); break;
139 case 'N': d->c->n_hap = atoi(optarg); break;
140 case 'r': d->c->het_rate = atoi(optarg); break;
141 case 'c': d->format |= BAM_PLF_CNS; break;
142 default: fprintf(stderr, "Unrecognizd option '-%c'.\n", c); return 1;
145 if (optind == argc) {
146 fprintf(stderr, "\n");
147 fprintf(stderr, "Usage: bamtk pileup [options] <in.bam>|<in.sam>\n\n");
148 fprintf(stderr, "Option: -s simple (yet incomplete) pileup format\n");
149 fprintf(stderr, " -t FILE list of reference sequences (assume the input is in SAM)\n");
150 fprintf(stderr, " -l FILE list of sites at which pileup is output\n");
151 fprintf(stderr, " -f FILE reference sequence in the FASTA format\n\n");
152 fprintf(stderr, " -c output the maq consensus sequence\n");
153 fprintf(stderr, " -T FLOAT theta in maq consensus calling model (for -c only) [%f]\n", d->c->theta);
154 fprintf(stderr, " -N INT number of haplotypes in the sample (for -c only) [%d]\n", d->c->n_hap);
155 fprintf(stderr, " -r FLOAT prior of a difference between any two haplotypes (for -c only) [%f]\n\n",
157 free(fn_list); free(fn_fa); free(d);
160 if (fn_fa) d->fai = fai_load(fn_fa);
162 bam_maqcns_prepare(d->c);
163 d->ido = bam_maqindel_opt_init();
168 bam_plbuf_t *buf = bam_plbuf_init(pileup_func, d);
169 d->h = sam_header_read2(fn_list);
170 if (fn_pos) d->hash = load_pos(fn_pos, d->h);
171 fp = sam_open(argv[optind]);
172 b = (bam1_t*)calloc(1, sizeof(bam1_t));
173 while ((ret = sam_read1(fp, d->h, b)) >= 0)
174 bam_plbuf_push(b, buf);
175 bam_plbuf_push(0, buf);
176 bam_plbuf_destroy(buf);
181 fp = (strcmp(argv[optind], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[optind], "r");
182 d->h = bam_header_read(fp);
183 if (fn_pos) d->hash = load_pos(fn_pos, d->h);
184 bam_pileup_file(fp, pileup_func, d);
187 free(fn_pos); free(fn_list);
188 kh_destroy(64, d->hash);
189 bam_header_destroy(d->h);
190 if (d->fai) fai_destroy(d->fai);
191 bam_maqcns_destroy(d->c);
192 free(d->ido); free(d->ref); free(d);