8 KSTREAM_INIT(gzFile, gzread, 0x10000)
10 int bcfview(int argc, char *argv[]);
11 int bcf_main_index(int argc, char *argv[]);
13 #define BUF_SIZE 0x10000
15 int bcf_cat(int n, char * const *fn)
20 buf = malloc(BUF_SIZE);
21 out = bcf_open("-", "w");
22 for (i = 0; i < n; ++i) {
27 in = bcf_open(fn[i], "r");
29 if (i == 0) bcf_hdr_write(out, h);
32 fstat(knet_fileno(in->fp->x.fpr), &s);
34 while (knet_tell(in->fp->x.fpr) < end) {
35 int size = knet_tell(in->fp->x.fpr) + BUF_SIZE < end? BUF_SIZE : end - knet_tell(in->fp->x.fpr);
36 knet_read(in->fp->x.fpr, buf, size);
37 fwrite(buf, 1, size, out->fp->x.fpw);
40 abort(); // FIXME: not implemented
49 extern double bcf_pair_freq(const bcf1_t *b0, const bcf1_t *b1, double f[4]);
51 int bcf_main_ldpair(int argc, char *argv[])
63 fprintf(stderr, "Usage: bcftools ldpair <in.bcf> <in.list>\n");
66 fplist = gzopen(argv[2], "rb");
68 memset(&str, 0, sizeof(kstring_t));
69 fp = bcf_open(argv[1], "rb");
71 str2id = bcf_build_refhash(h);
72 idx = bcf_idx_load(argv[1]);
74 fprintf(stderr, "[%s] No bcf index is found. Abort!\n", __func__);
77 b0 = calloc(1, sizeof(bcf1_t));
78 b1 = calloc(1, sizeof(bcf1_t));
79 while (ks_getuntil(ks, '\n', &str, &dret) >= 0) {
82 int tid0 = -1, tid1 = -1, pos0 = -1, pos1 = -1;
84 for (p = q = str.s, k = 0; *p; ++p) {
85 if (*p == ' ' || *p == '\t') {
87 if (k == 0) tid0 = bcf_str2id(str2id, q);
88 else if (k == 1) pos0 = atoi(q) - 1;
89 else if (k == 2) tid1 = strcmp(q, "=")? bcf_str2id(str2id, q) : tid0;
90 else if (k == 3) pos1 = atoi(q) - 1;
95 if (k == 3) pos1 = atoi(q) - 1;
96 if (tid0 >= 0 && tid1 >= 0 && pos0 >= 0 && pos1 >= 0) {
99 off = bcf_idx_query(idx, tid0, pos0);
100 bgzf_seek(fp->fp, off, SEEK_SET);
101 while (bcf_read(fp, h, b0) >= 0 && b0->pos != pos0);
102 off = bcf_idx_query(idx, tid1, pos1);
103 bgzf_seek(fp->fp, off, SEEK_SET);
104 while (bcf_read(fp, h, b1) >= 0 && b1->pos != pos1);
105 r = bcf_pair_freq(b0, b1, f);
107 printf("%s\t%d\t%s\t%d\t%.4g\t%.4g\t%.4g\t%.4g\t%.4g\n", h->ns[tid0], pos0+1, h->ns[tid1], pos1+1,
108 r, f[0], f[1], f[2], f[3]);
109 } //else fprintf(stderr, "[%s] Parse error at line %d.\n", __func__, lineno);
111 bcf_destroy(b0); bcf_destroy(b1);
112 bcf_idx_destroy(idx);
113 bcf_str2id_destroy(str2id);
122 int bcf_main_ld(int argc, char *argv[])
130 fprintf(stderr, "Usage: bcftools ld <in.bcf>\n");
133 fp = bcf_open(argv[1], "rb");
134 h = bcf_hdr_read(fp);
135 // read the entire BCF
137 b0 = calloc(1, sizeof(bcf1_t));
138 while (bcf_read(fp, h, b0) >= 0) {
141 b = realloc(b, sizeof(void*) * m);
143 b[n] = calloc(1, sizeof(bcf1_t));
147 // compute pair-wise r^2
148 printf("%d\n", n); // the number of loci
149 for (i = 0; i < n; ++i) {
150 printf("%s:%d", h->ns[b[i]->tid], b[i]->pos + 1);
151 for (j = 0; j < i; ++j) {
152 double r = bcf_pair_freq(b[i], b[j], f);
153 printf("\t%.3f", r*r);
158 for (i = 0; i < n; ++i) bcf_destroy(b[i]);
165 int main(int argc, char *argv[])
168 fprintf(stderr, "\n");
169 fprintf(stderr, "Usage: bcftools <command> <arguments>\n\n");
170 fprintf(stderr, "Command: view print, extract, convert and call SNPs from BCF\n");
171 fprintf(stderr, " index index BCF\n");
172 fprintf(stderr, " cat concatenate BCFs\n");
173 fprintf(stderr, " ld compute all-pair r^2\n");
174 fprintf(stderr, " ldpair compute r^2 between requested pairs\n");
175 fprintf(stderr, "\n");
178 if (strcmp(argv[1], "view") == 0) return bcfview(argc-1, argv+1);
179 else if (strcmp(argv[1], "index") == 0) return bcf_main_index(argc-1, argv+1);
180 else if (strcmp(argv[1], "ld") == 0) return bcf_main_ld(argc-1, argv+1);
181 else if (strcmp(argv[1], "ldpair") == 0) return bcf_main_ldpair(argc-1, argv+1);
182 else if (strcmp(argv[1], "cat") == 0) return bcf_cat(argc-2, argv+2); // cat is different ...
184 fprintf(stderr, "[main] Unrecognized command.\n");