8 KSTREAM_INIT(gzFile, gzread, 4096)
18 bcf_hdr_t *vcf_hdr_read(bcf_t *bp)
24 if (!bp->is_vcf) return 0;
25 h = calloc(1, sizeof(bcf_hdr_t));
28 memset(&meta, 0, sizeof(kstring_t));
29 memset(&smpl, 0, sizeof(kstring_t));
30 while (ks_getuntil(v->ks, '\n', &v->line, &dret) >= 0) {
31 if (v->line.l < 2) continue;
32 if (v->line.s[0] != '#') return 0; // no sample line
33 if (v->line.s[0] == '#' && v->line.s[1] == '#') {
34 kputsn(v->line.s, v->line.l, &meta); kputc('\n', &meta);
35 } else if (v->line.s[0] == '#') {
38 for (q = v->line.s, p = q + 1, k = 0; *p; ++p) {
39 if (*p == '\t' || *(p+1) == 0) {
40 r = *(p+1) == 0? p+1 : p;
42 kputsn(q, r - q, &smpl);
53 h->sname = smpl.s; h->l_smpl = smpl.l;
54 h->txt = meta.s; h->l_txt = meta.l;
59 bcf_t *vcf_open(const char *fn, const char *mode)
63 bp = calloc(1, sizeof(bcf_t));
64 v = calloc(1, sizeof(vcf_t));
67 if (strchr(mode, 'r')) {
68 v->fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
69 v->ks = ks_init(v->fp);
70 } else if (strchr(mode, 'w'))
71 v->fpout = strcmp(fn, "-")? fopen(fn, "w") : fdopen(fileno(stdout), "w");
75 void bcf_hdr_clear(bcf_hdr_t *b);
77 int vcf_close(bcf_t *bp)
80 if (bp == 0) return -1;
81 if (bp->v == 0) return -1;
87 if (v->fpout) fclose(v->fpout);
94 int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h)
96 vcf_t *v = (vcf_t*)bp->v;
98 if (v == 0 || v->fpout == 0) return -1;
99 fwrite(h->txt, 1, h->l_txt, v->fpout);
100 fprintf(v->fpout, "#CHROM\tPOS\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT");
101 for (i = 0; i < h->n_smpl; ++i)
102 fprintf(v->fpout, "\t%s", h->sns[i]);
103 fputc('\n', v->fpout);
107 int vcf_read(bcf_t *bp, bcf1_t *b)
110 vcf_t *v = (vcf_t*)bp->v;
112 if (ks_getuntil(v->ks, '\n', &v->line, &dret) < 0) return -1;
113 return v->line.l + 1;
116 int vcf_test(int argc, char *argv[])
120 bp = vcf_open(argv[1], "r");
121 bpout = vcf_open("-", "w");
122 h = vcf_hdr_read(bpout);
123 vcf_hdr_write(bpout, h);