11 KHASH_SET_INIT_INT64(set64)
14 KSTREAM_INIT(gzFile, gzread, 16384)
28 khash_t(set64) *bcf_load_pos(const char *fn, bcf_hdr_t *_h)
33 int ret, dret, lineno = 1;
35 khash_t(set64) *hash = 0;
37 hash = kh_init(set64);
38 str2id = bcf_build_refhash(_h);
39 str = calloc(1, sizeof(kstring_t));
40 fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
42 while (ks_getuntil(ks, 0, str, &dret) >= 0) {
43 int tid = bcf_str2id(str2id, str->s);
44 if (tid >= 0 && dret != '\n') {
45 if (ks_getuntil(ks, 0, str, &dret) >= 0) {
46 uint64_t x = (uint64_t)tid<<32 | (atoi(str->s) - 1);
47 kh_put(set64, hash, x, &ret);
49 } else fprintf(stderr, "[%s] %s is not a reference name (line %d).\n", __func__, str->s, lineno);
50 if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n');
54 bcf_str2id_destroy(str2id);
57 free(str->s); free(str);
61 static double test_hwe(const double g[3])
63 extern double kf_gammaq(double p, double x);
64 double fexp, chi2, f[3], n;
66 n = g[0] + g[1] + g[2];
67 fexp = (2. * g[2] + g[1]) / (2. * n);
68 if (fexp > 1. - 1e-10) fexp = 1. - 1e-10;
69 if (fexp < 1e-10) fexp = 1e-10;
70 f[0] = n * (1. - fexp) * (1. - fexp);
71 f[1] = n * 2. * fexp * (1. - fexp);
72 f[2] = n * fexp * fexp;
73 for (i = 0, chi2 = 0.; i < 3; ++i)
74 chi2 += (g[i] - f[i]) * (g[i] - f[i]) / f[i];
75 return kf_gammaq(.5, chi2 / 2.);
78 extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
80 static double test_fisher(bcf1_t *b, const char *key, int d[4])
82 double left, right, two;
85 if ((p = strstr(b->info, key)) == 0) return -1.;
87 for (i = 0; i < 4; ++i) {
88 d[i] = strtol(p, &p, 10);
89 if (errno == EINVAL || errno == ERANGE) return -2.;
92 kt_fisher_exact(d[0], d[1], d[2], d[3], &left, &right, &two);
96 static void rm_info(int n_smpl, bcf1_t *b, const char *key)
99 if ((p = strstr(b->info, key)) == 0) return;
100 for (q = p; *q && *q != ';'; ++q);
101 if (p > b->info && *(p-1) == ';') --p;
102 memmove(p, q, b->l_str - (q - b->str));
107 static int update_bcf1(int n_smpl, bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p1rst_t *pr, double pref, int flag)
110 int d[4], x, is_var = (pr->p_ref < pref);
111 double p_hwe, p_dp, p_ed, r = is_var? pr->p_ref : 1. - pr->p_ref;
113 p_hwe = test_hwe(pr->g);
114 p_ed = test_fisher(b, "ED4=", d);
115 p_dp = test_fisher(b, "DP4=", d);
116 rm_info(n_smpl, b, "ED4=");
118 memset(&s, 0, sizeof(kstring_t));
119 kputc('\0', &s); kputs(b->ref, &s); kputc('\0', &s);
123 kputc('\0', &s); kputc('\0', &s);
125 if (p_dp >= 0. && d[2] + d[3] > 0) { // has at least one non-reference base
126 if (b->info[0]) kputc(';', &s);
127 ksprintf(&s, "AF1=%.3lf;AFE=%.3lf", 1.-pr->f_em, 1.-pr->f_exp);
129 if (p_hwe <= .2) ksprintf(&s, ";HWE=%.3lf", p_hwe);
130 if (p_dp >= 0. && p_dp <= .2) ksprintf(&s, ";TDP=%.3lf", p_dp);
131 if (p_ed >= 0. && p_ed <= .2) ksprintf(&s, ";TED=%.3lf", p_ed);
133 kputs(b->fmt, &s); kputc('\0', &s);
135 b->m_str = s.m; b->l_str = s.l; b->str = s.s;
136 x = (int)(r < 1e-100? 99 : -3.434 * log(r) + .499);
137 b->qual = x > 99? 99 : x;
142 int bcfview(int argc, char *argv[])
144 bcf_t *bp, *bout = 0;
147 uint64_t n_processed = 0;
152 khash_t(set64) *hash = 0;
154 tid = begin = end = -1;
155 memset(&vc, 0, sizeof(viewconf_t));
156 vc.prior_type = -1; vc.theta = 1e-3; vc.pref = 0.9;
157 while ((c = getopt(argc, argv, "l:cGvLbP:t:p:")) >= 0) {
159 case 'l': vc.fn_list = strdup(optarg); break;
160 case 'G': vc.flag |= VC_NO_GENO; break;
161 case 'L': vc.flag |= VC_NO_PL; break;
162 case 'b': vc.flag |= VC_BCF; break;
163 case 'c': vc.flag |= VC_CALL; break;
164 case 'v': vc.flag |= VC_VARONLY; break;
165 case 't': vc.theta = atof(optarg); break;
166 case 'p': vc.pref = atof(optarg); break;
168 if (strcmp(optarg, "full") == 0) vc.prior_type = MC_PTYPE_FULL;
169 else if (strcmp(optarg, "cond2") == 0) vc.prior_type = MC_PTYPE_COND2;
170 else if (strcmp(optarg, "flat") == 0) vc.prior_type = MC_PTYPE_FLAT;
174 if (argc == optind) {
175 fprintf(stderr, "Usage: bcftools view [-cGPb] [-l list] <in.bcf> [reg]\n");
179 b = calloc(1, sizeof(bcf1_t));
180 bp = bcf_open(argv[optind], "r");
181 h = bcf_hdr_read(bp);
182 if (vc.flag & VC_BCF) {
183 bout = bcf_open("-", "w");
184 bcf_hdr_write(bout, h);
186 if (vc.flag & VC_CALL) {
187 p1 = bcf_p1_init(h->n_smpl);
188 bcf_p1_init_prior(p1, vc.prior_type, vc.theta);
190 if (vc.fn_list) hash = bcf_load_pos(vc.fn_list, h);
191 if (optind + 1 < argc) {
192 void *str2id = bcf_build_refhash(h);
193 if (bcf_parse_region(str2id, argv[optind+1], &tid, &begin, &end) >= 0) {
195 idx = bcf_idx_load(argv[optind]);
198 off = bcf_idx_query(idx, tid, begin, end);
199 bgzf_seek(bp->fp, off, SEEK_SET);
200 bcf_idx_destroy(idx);
204 while (bcf_read(bp, h, b) > 0) {
206 uint64_t x = (uint64_t)b->tid<<32 | b->pos;
207 khint_t k = kh_get(set64, hash, x);
208 if (k == kh_end(hash)) continue;
211 int l = strlen(b->ref);
212 l = b->pos + (l > 0? l : 1);
213 if (b->tid != tid || b->pos >= end) break;
214 if (!(l > begin && end > b->pos)) continue;
216 if (vc.flag & VC_CALL) {
218 bcf_p1_cal(b, p1, &pr);
219 if ((n_processed + 1) % 50000 == 0) bcf_p1_dump_afs(p1);
220 if (pr.p_ref >= vc.pref && (vc.flag & VC_VARONLY)) continue;
221 update_bcf1(h->n_smpl, b, p1, &pr, vc.pref, vc.flag);
223 if (vc.flag & VC_BCF) bcf_write(bout, h, b);
226 if (vc.flag & VC_NO_GENO) {
236 if (vc.flag & VC_CALL) bcf_p1_dump_afs(p1);
238 bcf_close(bp); bcf_close(bout);
240 if (hash) kh_destroy(set64, hash);
241 if (vc.fn_list) free(vc.fn_list);
242 if (p1) bcf_p1_destroy(p1);