]> git.donarmstrong.com Git - samtools.git/blob - bam_plcmd.c
* samtools-0.1.2-14
[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 #include "glf.h"
9 KHASH_SET_INIT_INT64(64)
10
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
15
16 typedef struct {
17         bam_header_t *h;
18         bam_maqcns_t *c;
19         bam_maqindel_opt_t *ido;
20         faidx_t *fai;
21         khash_t(64) *hash;
22         uint32_t format;
23         int tid, len, last_pos;
24         int mask;
25         char *ref;
26         glfFile fp; // for glf output only
27 } pu_data_t;
28
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);
32
33 static khash_t(64) *load_pos(const char *fn, bam_header_t *h)
34 {
35         int n, tmp, i;
36         char **list, *s;
37         uint64_t x;
38         khash_t(64) *hash;
39         bam_init_header_hash(h);
40         list = bam_load_pos(fn, &n);
41         hash = kh_init(64);
42         for (i = 0; i < n; ++i) {
43                 x = (uint64_t)bam_get_tid(h, list[i]) << 32;
44                 s = list[i];
45                 while (*s++);
46                 x |= *((uint32_t*)s) - 1;
47                 kh_put(64, hash, x, &tmp);
48                 free(list[i]);
49         }
50         free(list);
51         return hash;
52 }
53
54 // an analogy to pileup_func() below
55 static int glt3_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
56 {
57         pu_data_t *d = (pu_data_t*)data;
58         bam_maqindel_ret_t *r = 0;
59         int rb;
60         glf1_t *g;
61         glf3_t *g3;
62         if (d->fai == 0) {
63                 fprintf(stderr, "[glt3_func] reference sequence is required for generating GLT. Abort!\n");
64                 exit(1);
65         }
66         if (d->hash && kh_get(64, d->hash, (uint64_t)tid<<32|pos) == kh_end(d->hash)) return 0;
67         g3 = glf3_init1();
68         if (d->fai && (int)tid != d->tid) {
69                 if (d->ref) { // then write the end mark
70                         g3->rtype = GLF3_RTYPE_END;
71                         glf3_write1(d->fp, g3);
72                 }
73                 glf3_ref_write(d->fp, d->h->target_name[tid], d->h->target_len[tid]); // write reference
74                 free(d->ref);
75                 d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len);
76                 d->tid = tid;
77                 d->last_pos = 0;
78         }
79         rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N';
80         g = bam_maqcns_glfgen(n, pu, bam_nt16_table[rb], d->c);
81         memcpy(g3, g, sizeof(glf1_t));
82         g3->rtype = GLF3_RTYPE_SUB;
83         g3->offset = pos - d->last_pos;
84         d->last_pos = pos;
85         glf3_write1(d->fp, g3);
86         r = bam_maqindel(n, pos, d->ido, pu, d->ref);
87         if (r) { // then write indel line
88                 int het = 3 * n, min;
89                 min = het;
90                 if (min > r->gl[0]) min = r->gl[0];
91                 if (min > r->gl[1]) min = r->gl[1];
92                 g3->ref_base = 0;
93                 g3->rtype = GLF3_RTYPE_INDEL;
94                 memset(g3->lk, 0, 10);
95                 g3->lk[0] = r->gl[0] - min < 255? r->gl[0] - min : 255;
96                 g3->lk[1] = r->gl[1] - min < 255? r->gl[1] - min : 255;
97                 g3->lk[2] = het - min < 255? het - min : 255;
98                 g3->offset = 0;
99                 g3->indel_len[0] = r->indel1;
100                 g3->indel_len[1] = r->indel2;
101                 g3->min_lk = min < 255? min : 255;
102                 g3->max_len = (abs(r->indel1) > abs(r->indel2)? abs(r->indel1) : abs(r->indel2)) + 1;
103                 g3->indel_seq[0] = strdup(r->s[0]+1);
104                 g3->indel_seq[1] = strdup(r->s[1]+1);
105                 glf3_write1(d->fp, g3);
106                 bam_maqindel_ret_destroy(r);
107         }
108         free(g);
109         glf3_destroy1(g3);
110         return 0;
111 }
112
113 static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
114 {
115         pu_data_t *d = (pu_data_t*)data;
116         bam_maqindel_ret_t *r = 0;
117         int i, j, rb, max_mapq = 0;
118         uint32_t x;
119         if (d->hash && kh_get(64, d->hash, (uint64_t)tid<<32|pos) == kh_end(d->hash)) return 0;
120         if (d->format & BAM_PLF_GLF) return glt3_func(tid, pos, n, pu, data);
121         if (d->fai && (int)tid != d->tid) {
122                 free(d->ref);
123                 d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len);
124                 d->tid = tid;
125         }
126         rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N';
127         if (d->format & BAM_PLF_INDEL_ONLY) {
128                 for (i = 0; i < n; ++i)
129                         if (pu[i].indel != 0) break;
130                 if (i == n) return 0;
131         }
132         printf("%s\t%d\t%c\t", d->h->target_name[tid], pos + 1, rb);
133         if (d->format & BAM_PLF_CNS) { // consensus
134                 int ref_q, rb4 = bam_nt16_table[rb];
135                 x = bam_maqcns_call(n, pu, d->c);
136                 ref_q = 0;
137                 if (rb4 != 15 && x>>28 != 15 && x>>28 != rb4) { // a SNP
138                         ref_q = ((x>>24&0xf) == rb4)? x>>8&0xff : (x>>8&0xff) + (x&0xff);
139                         if (ref_q > 255) ref_q = 255;
140                 }
141                 printf("%c\t%d\t%d\t%d\t", bam_nt16_rev_table[x>>28], x>>8&0xff, ref_q, x>>16&0xff);
142         }
143         if (d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY)) {
144                 if (d->ref) // indel calling
145                         r = bam_maqindel(n, pos, d->ido, pu, d->ref);
146         }
147         // pileup strings
148         printf("%d\t", n);
149         for (i = 0; i < n; ++i) {
150                 const bam_pileup1_t *p = pu + i;
151                 if (max_mapq < p->b->core.qual) max_mapq = p->b->core.qual;
152                 if (p->is_head) printf("^%c", p->b->core.qual > 93? 126 : p->b->core.qual + 33);
153                 if (!p->is_del) {
154                         int c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
155                         if (toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
156                         else c = bam1_strand(p->b)? tolower(c) : toupper(c);
157                         putchar(c);
158                         if (p->indel > 0) {
159                                 printf("+%d", p->indel);
160                                 for (j = 1; j <= p->indel; ++j) {
161                                         c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
162                                         putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
163                                 }
164                         } else if (p->indel < 0) {
165                                 printf("%d", p->indel);
166                                 for (j = 1; j <= -p->indel; ++j) {
167                                         c = (d->ref && (int)pos+j < d->len)? d->ref[pos+j] : 'N';
168                                         putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
169                                 }
170                         }
171                 } else putchar('*');
172                 if (p->is_tail) putchar('$');
173         }
174         putchar('\t');
175         for (i = 0; i < n; ++i) {
176                 const bam_pileup1_t *p = pu + i;
177                 int c = bam1_qual(p->b)[p->qpos] + 33;
178                 if (c > 126) c = 126;
179                 putchar(c);
180         }
181         if (d->format & BAM_PLF_SIMPLE) {
182                 putchar('\t');
183                 for (i = 0; i < n; ++i) {
184                         int c = pu[i].b->core.qual + 33;
185                         if (c > 126) c = 126;
186                         putchar(c);
187                 }
188         }
189         putchar('\n');
190         if (r) { // then print indel line
191                 printf("%s\t%d\t*\t", d->h->target_name[tid], pos + 1);
192                 if (r->gt < 2) printf("%s/%s\t", r->s[r->gt], r->s[r->gt]);
193                 else printf("%s/%s\t", r->s[0], r->s[1]);
194                 printf("%d\t%d\t", r->q_cns, r->q_ref);
195                 printf("%d\t%d\t", max_mapq, n);
196                 printf("%s\t%s\t", r->s[0], r->s[1]);
197                 //printf("%d\t%d\t", r->gl[0], r->gl[1]);
198                 printf("%d\t%d\t%d\t%d\n", r->cnt1, r->cnt2, r->cnt_ambi, r->cnt_anti);
199                 bam_maqindel_ret_destroy(r);
200         }
201         return 0;
202 }
203
204 int bam_pileup(int argc, char *argv[])
205 {
206         int c;
207         char *fn_list = 0, *fn_fa = 0, *fn_pos = 0;
208         pu_data_t *d = (pu_data_t*)calloc(1, sizeof(pu_data_t));
209         d->tid = -1; d->mask = BAM_DEF_MASK;
210         d->c = bam_maqcns_init();
211         d->ido = bam_maqindel_opt_init();
212         while ((c = getopt(argc, argv, "st:f:cT:N:r:l:im:gI:G:")) >= 0) {
213                 switch (c) {
214                 case 's': d->format |= BAM_PLF_SIMPLE; break;
215                 case 't': fn_list = strdup(optarg); break;
216                 case 'l': fn_pos = strdup(optarg); break;
217                 case 'f': fn_fa = strdup(optarg); break;
218                 case 'T': d->c->theta = atof(optarg); break;
219                 case 'N': d->c->n_hap = atoi(optarg); break;
220                 case 'r': d->c->het_rate = atoi(optarg); break;
221                 case 'c': d->format |= BAM_PLF_CNS; break;
222                 case 'i': d->format |= BAM_PLF_INDEL_ONLY; break;
223                 case 'm': d->mask = atoi(optarg); break;
224                 case 'g': d->format |= BAM_PLF_GLF; break;
225                 case 'I': d->ido->q_indel = atoi(optarg); break;
226                 case 'G': d->ido->r_indel = atof(optarg); break;
227                 default: fprintf(stderr, "Unrecognizd option '-%c'.\n", c); return 1;
228                 }
229         }
230         if (optind == argc) {
231                 fprintf(stderr, "\n");
232                 fprintf(stderr, "Usage:  bamtk pileup [options] <in.bam>|<in.sam>\n\n");
233                 fprintf(stderr, "Option: -s        simple (yet incomplete) pileup format\n");
234                 fprintf(stderr, "        -i        only show lines/consensus with indels\n");
235                 fprintf(stderr, "        -m INT    filtering reads with bits in INT [%d]\n", d->mask);
236                 fprintf(stderr, "        -t FILE   list of reference sequences (assume the input is in SAM)\n");
237                 fprintf(stderr, "        -l FILE   list of sites at which pileup is output\n");
238                 fprintf(stderr, "        -f FILE   reference sequence in the FASTA format\n\n");
239                 fprintf(stderr, "        -c        output the maq consensus sequence\n");
240                 fprintf(stderr, "        -g        output in the extended GLT format (suppressing -c/-i/-s)\n");
241                 fprintf(stderr, "        -T FLOAT  theta in maq consensus calling model (for -c/-g) [%f]\n", d->c->theta);
242                 fprintf(stderr, "        -N INT    number of haplotypes in the sample (for -c/-g) [%d]\n", d->c->n_hap);
243                 fprintf(stderr, "        -r FLOAT  prior of a difference between two haplotypes (for -c/-g) [%f]\n", d->c->het_rate);
244                 fprintf(stderr, "        -G FLOAT  prior of an indel between two haplotypes (for -c/-g) [%f]\n", d->ido->r_indel);
245                 fprintf(stderr, "        -I INT    phred prob. of an indel in sequencing/prep. (for -c/-g) [%d]\n", d->ido->q_indel);
246                 fprintf(stderr, "\n");
247                 free(fn_list); free(fn_fa); free(d);
248                 return 1;
249         }
250         if (fn_fa) d->fai = fai_load(fn_fa);
251         free(fn_fa);
252         bam_maqcns_prepare(d->c);
253         if (d->format & BAM_PLF_GLF) {
254                 glf3_header_t *h;
255                 h = glf3_header_init();
256                 d->fp = bgzf_fdopen(fileno(stdout), "w");
257                 glf3_header_write(d->fp, h);
258                 glf3_header_destroy(h);
259         }
260         if (fn_list) {
261                 tamFile fp;
262                 bam1_t *b;
263                 int ret;
264                 bam_plbuf_t *buf = bam_plbuf_init(pileup_func, d);
265                 bam_plbuf_set_mask(buf, d->mask);
266                 d->h = sam_header_read2(fn_list);
267                 if (fn_pos) d->hash = load_pos(fn_pos, d->h);
268                 fp = sam_open(argv[optind]);
269                 b = (bam1_t*)calloc(1, sizeof(bam1_t));
270                 while ((ret = sam_read1(fp, d->h, b)) >= 0)
271                         bam_plbuf_push(b, buf);
272                 bam_plbuf_push(0, buf);
273                 bam_plbuf_destroy(buf);
274                 bam_destroy1(b);
275                 sam_close(fp);
276         } else {
277                 bamFile fp;
278                 fp = (strcmp(argv[optind], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[optind], "r");
279                 d->h = bam_header_read(fp);
280                 if (fn_pos) d->hash = load_pos(fn_pos, d->h);
281                 bam_pileup_file(fp, d->mask, pileup_func, d);
282                 bam_close(fp);
283         }
284         if (d->format & BAM_PLF_GLF) bgzf_close(d->fp);
285         free(fn_pos); free(fn_list);
286         kh_destroy(64, d->hash);
287         bam_header_destroy(d->h);
288         if (d->fai) fai_destroy(d->fai);
289         bam_maqcns_destroy(d->c);
290         free(d->ido); free(d->ref); free(d);
291         return 0;
292 }