10 KHASH_SET_INIT_INT64(set64)
13 KSTREAM_INIT(gzFile, gzread, 16384)
27 khash_t(set64) *bcf_load_pos(const char *fn, bcf_hdr_t *_h)
32 int ret, dret, lineno = 1;
34 khash_t(set64) *hash = 0;
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");
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);
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');
53 bcf_str2id_destroy(str2id);
56 free(str->s); free(str);
60 static int update_bcf1(bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p1rst_t *pr, int flag)
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);
70 kputc('\0', &s); kputc('\0', &s);
72 if (b->info[0]) kputc(';', &s);
73 ksprintf(&s, "AF1=%.3lf;AFE=%.3lf", 1.-pr->f_em, 1.-pr->f_exp);
75 kputs(b->fmt, &s); kputc('\0', &s);
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;
83 int bcfview(int argc, char *argv[])
88 uint64_t n_processed = 0;
92 khash_t(set64) *hash = 0;
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) {
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;
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;
113 if (argc == optind) {
114 fprintf(stderr, "Usage: bcftools view [-cGPb] [-l list] <in.bcf> [reg]\n");
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);
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);
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) {
134 idx = bcf_idx_load(argv[optind]);
137 off = bcf_idx_query(idx, tid, begin, end);
138 bgzf_seek(bp->fp, off, SEEK_SET);
139 bcf_idx_destroy(idx);
143 while (bcf_read(bp, b) > 0) {
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;
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;
155 if (vc.flag & VC_CALL) {
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;
164 if (vc.flag & VC_BCF) bcf_write(bout, b);
167 if (vc.flag & VC_NO_GENO) {
171 vcf = bcf_fmt(bp, b);
177 if (vc.flag & VC_CALL) bcf_p1_dump_afs(p1);
178 bcf_close(bp); bcf_close(bout);
180 if (hash) kh_destroy(set64, hash);
181 if (vc.fn_list) free(vc.fn_list);
182 if (p1) bcf_p1_destroy(p1);