]> git.donarmstrong.com Git - samtools.git/blob - bcftools/vcfout.c
move bcftools to samtools
[samtools.git] / bcftools / vcfout.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <zlib.h>
5 #include "bcf.h"
6 #include "prob1.h"
7 #include "kstring.h"
8
9 #include "khash.h"
10 KHASH_SET_INIT_INT64(set64)
11
12 #include "kseq.h"
13 KSTREAM_INIT(gzFile, gzread, 16384)
14
15 #define VC_NO_PL   1
16 #define VC_NO_GENO 2
17 #define VC_BCF     4
18 #define VC_CALL    8
19 #define VC_VARONLY 16
20
21 typedef struct {
22         int flag, prior_type;
23         char *fn_list;
24         double theta;
25 } viewconf_t;
26
27 khash_t(set64) *bcf_load_pos(const char *fn, bcf_hdr_t *_h)
28 {
29         void *str2id;
30         gzFile fp;
31         kstream_t *ks;
32         int ret, dret, lineno = 1;
33         kstring_t *str;
34         khash_t(set64) *hash = 0;
35
36         hash = kh_init(set64);
37         str2id = bcf_build_refhash(_h);
38         str = calloc(1, sizeof(kstring_t));
39         fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
40         ks = ks_init(fp);
41         while (ks_getuntil(ks, 0, str, &dret) >= 0) {
42                 int tid = bcf_str2id(str2id, str->s);
43                 if (tid >= 0 && dret != '\n') {
44                         if (ks_getuntil(ks, 0, str, &dret) >= 0) {
45                                 uint64_t x = (uint64_t)tid<<32 | (atoi(str->s) - 1);
46                                 kh_put(set64, hash, x, &ret);
47                         } else break;
48                 } else fprintf(stderr, "[%s] %s is not a reference name (line %d).\n", __func__, str->s, lineno);
49                 if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
50                 if (dret < 0) break;
51                 ++lineno;
52         }
53         bcf_str2id_destroy(str2id);
54         ks_destroy(ks);
55         gzclose(fp);
56         free(str->s); free(str);
57         return hash;
58 }
59
60 static int update_bcf1(bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p1rst_t *pr, int flag)
61 {
62         kstring_t s;
63         int x, is_var = (pr->p_ref < .5);
64         double r = is_var? pr->p_ref : 1. - pr->p_ref;
65         memset(&s, 0, sizeof(kstring_t));
66         kputc('\0', &s); kputs(b->ref, &s); kputc('\0', &s);
67         if (is_var) {
68                 kputs(b->alt, &s);
69         }
70         kputc('\0', &s); kputc('\0', &s);
71         kputs(b->info, &s);
72         if (b->info[0]) kputc(';', &s);
73         ksprintf(&s, "AF1=%.3lf;AFE=%.3lf", 1.-pr->f_em, 1.-pr->f_exp);
74         kputc('\0', &s);
75         kputs(b->fmt, &s); kputc('\0', &s);
76         free(b->str);
77         b->m_str = s.m; b->l_str = s.l; b->str = s.s;
78         x = (int)(r < 1e-100? 99 : -3.434 * log(r) + .499);
79         b->qual = x > 99? 99 : x;
80         return is_var;
81 }
82
83 int bcfview(int argc, char *argv[])
84 {
85         bcf_t *bp, *bout = 0;
86         bcf1_t *b;
87         int c;
88         uint64_t n_processed = 0;
89         viewconf_t vc;
90         bcf_p1aux_t *p1 = 0;
91         int tid, begin, end;
92         khash_t(set64) *hash = 0;
93
94         tid = begin = end = -1;
95         memset(&vc, 0, sizeof(viewconf_t));
96         vc.prior_type = -1; vc.theta = 1e-3;
97         while ((c = getopt(argc, argv, "l:cGvLbP:t:")) >= 0) {
98                 switch (c) {
99                 case 'l': vc.fn_list = strdup(optarg); break;
100                 case 'G': vc.flag |= VC_NO_GENO; break;
101                 case 'L': vc.flag |= VC_NO_PL; break;
102                 case 'b': vc.flag |= VC_BCF; break;
103                 case 'c': vc.flag |= VC_CALL; break;
104                 case 'v': vc.flag |= VC_VARONLY; break;
105                 case 't': vc.theta = atof(optarg); break;
106                 case 'P':
107                         if (strcmp(optarg, "full") == 0) vc.prior_type = MC_PTYPE_FULL;
108                         else if (strcmp(optarg, "cond2") == 0) vc.prior_type = MC_PTYPE_COND2;
109                         else if (strcmp(optarg, "flat") == 0) vc.prior_type = MC_PTYPE_FLAT;
110                         break;
111                 }
112         }
113         if (argc == optind) {
114                 fprintf(stderr, "Usage: bcftools view [-cGPb] [-l list] <in.bcf> [reg]\n");
115                 return 1;
116         }
117
118         b = calloc(1, sizeof(bcf1_t));
119         bp = bcf_open(argv[optind], "r");
120         if (vc.flag & VC_BCF) {
121                 bout = bcf_open("-", "w");
122                 bcf_hdr_cpy(&bout->h, &bp->h);
123                 bcf_hdr_write(bout);
124         }
125         if (vc.flag & VC_CALL) {
126                 p1 = bcf_p1_init(bp->h.n_smpl);
127                 bcf_p1_init_prior(p1, vc.prior_type, vc.theta);
128         }
129         if (vc.fn_list) hash = bcf_load_pos(vc.fn_list, &bp->h);
130         if (optind + 1 < argc) {
131                 void *str2id = bcf_build_refhash(&bp->h);
132                 if (bcf_parse_region(str2id, argv[optind+1], &tid, &begin, &end) >= 0) {
133                         bcf_idx_t *idx;
134                         idx = bcf_idx_load(argv[optind]);
135                         if (idx) {
136                                 uint64_t off;
137                                 off = bcf_idx_query(idx, tid, begin, end);
138                                 bgzf_seek(bp->fp, off, SEEK_SET);
139                                 bcf_idx_destroy(idx);
140                         }
141                 }
142         }
143         while (bcf_read(bp, b) > 0) {
144                 if (hash) {
145                         uint64_t x = (uint64_t)b->tid<<32 | b->pos;
146                         khint_t k = kh_get(set64, hash, x);
147                         if (k == kh_end(hash)) continue;
148                 }
149                 if (tid >= 0) {
150                         int l = strlen(b->ref);
151                         l = b->pos + (l > 0? l : 1);
152                         if (b->tid != tid || b->pos >= end) break;
153                         if (!(l > begin && end > b->pos)) continue;
154                 }
155                 if (vc.flag & VC_CALL) {
156                         bcf_p1rst_t pr;
157                         int is_var;
158                         bcf_p1_cal(b, p1, &pr);
159                         is_var = update_bcf1(b, p1, &pr, vc.flag);
160                         bcf_sync(bp->h.n_smpl, b);
161                         if ((n_processed + 1) % 50000 == 0) bcf_p1_dump_afs(p1);
162                         if ((vc.flag & VC_VARONLY) && !is_var) continue;
163                 }
164                 if (vc.flag & VC_BCF) bcf_write(bout, b);
165                 else {
166                         char *vcf;
167                         if (vc.flag & VC_NO_GENO) {
168                                 b->n_gi = 0;
169                                 b->fmt[0] = '\0';
170                         }
171                         vcf = bcf_fmt(bp, b);
172                         puts(vcf);
173                         free(vcf);
174                 }
175                 ++n_processed;
176         }
177         if (vc.flag & VC_CALL) bcf_p1_dump_afs(p1);
178         bcf_close(bp); bcf_close(bout);
179         bcf_destroy(b);
180         if (hash) kh_destroy(set64, hash);
181         if (vc.fn_list) free(vc.fn_list);
182         if (p1) bcf_p1_destroy(p1);
183         return 0;
184 }
185