]> git.donarmstrong.com Git - samtools.git/blob - bam_plcmd.c
Create trunk copy
[samtools.git] / bam_plcmd.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <ctype.h>
4 #include "bam.h"
5 #include "faidx.h"
6 #include "bam_maqcns.h"
7 #include "khash.h"
8 KHASH_SET_INIT_INT64(64)
9
10 #define BAM_PLF_SIMPLE 0x01
11 #define BAM_PLF_CNS 0x02
12
13 typedef struct {
14         bam_header_t *h;
15         bam_maqcns_t *c;
16         bam_maqindel_opt_t *ido;
17         faidx_t *fai;
18         khash_t(64) *hash;
19         uint32_t format;
20         int tid, len;
21         char *ref;
22 } pu_data_t;
23
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);
27
28 static khash_t(64) *load_pos(const char *fn, bam_header_t *h)
29 {
30         int n, tmp, i;
31         char **list, *s;
32         uint64_t x;
33         khash_t(64) *hash;
34         bam_init_header_hash(h);
35         list = bam_load_pos(fn, &n);
36         hash = kh_init(64);
37         for (i = 0; i < n; ++i) {
38                 x = (uint64_t)bam_get_tid(h, list[i]) << 32;
39                 s = list[i];
40                 while (*s++);
41                 x |= *((uint32_t*)s) - 1;
42                 kh_put(64, hash, x, &tmp);
43                 free(list[i]);
44         }
45         free(list);
46         return hash;
47 }
48
49 static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
50 {
51         pu_data_t *d = (pu_data_t*)data;
52         bam_maqindel_ret_t *r = 0;
53         int i, j, rb;
54         uint32_t x;
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) {
57                 free(d->ref);
58                 d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len);
59                 d->tid = tid;
60         }
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);
66                 ref_q = 0;
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;
70                 }
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);
74         }
75         // pileup strings
76         printf("%d\t", n);
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);
80                 if (!p->is_del) {
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);
84                         putchar(c);
85                         if (p->indel > 0) {
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));
90                                 }
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));
96                                 }
97                         }
98                 } else putchar('*');
99                 if (p->is_tail) putchar('$');
100         }
101         putchar('\t');
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;
106                 putchar(c);
107         }
108         if (d->format & BAM_PLF_SIMPLE) {
109                 putchar('\t');
110                 for (i = 0; i < n; ++i) {
111                         int c = pu[i].b->core.qual + 33;
112                         if (c > 126) c = 126;
113                         putchar(c);
114                 }
115         }
116         putchar('\n');
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);
121         }
122         return 0;
123 }
124
125 int bam_pileup(int argc, char *argv[])
126 {
127         int c;
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));
130         d->tid = -1;
131         d->c = bam_maqcns_init();
132         while ((c = getopt(argc, argv, "st:f:cT:N:r:l:")) >= 0) {
133                 switch (c) {
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;
143                 }
144         }
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",
156                                 d->c->het_rate);
157                 free(fn_list); free(fn_fa); free(d);
158                 return 1;
159         }
160         if (fn_fa) d->fai = fai_load(fn_fa);
161         free(fn_fa);
162         bam_maqcns_prepare(d->c);
163         d->ido = bam_maqindel_opt_init();
164         if (fn_list) {
165                 tamFile fp;
166                 bam1_t *b;
167                 int ret;
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);
177                 bam_destroy1(b);
178                 sam_close(fp);
179         } else {
180                 bamFile fp;
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);
185                 bam_close(fp);
186         }
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);
193         return 0;
194 }