8 KSTREAM_INIT(gzFile, gzread, 4096)
19 bcf_hdr_t *vcf_hdr_read(bcf_t *bp)
25 if (!bp->is_vcf) return bcf_hdr_read(bp);
26 h = calloc(1, sizeof(bcf_hdr_t));
29 memset(&meta, 0, sizeof(kstring_t));
30 memset(&smpl, 0, sizeof(kstring_t));
31 while (ks_getuntil(v->ks, '\n', &v->line, &dret) >= 0) {
32 if (v->line.l < 2) continue;
33 if (v->line.s[0] != '#') return 0; // no sample line
34 if (v->line.s[0] == '#' && v->line.s[1] == '#') {
35 kputsn(v->line.s, v->line.l, &meta); kputc('\n', &meta);
36 } else if (v->line.s[0] == '#') {
40 for (p = kstrtok(v->line.s, "\t\n", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) {
42 kputsn(p, aux.p - p, &smpl);
51 h->sname = smpl.s; h->l_smpl = smpl.l;
52 h->txt = meta.s; h->l_txt = meta.l;
57 bcf_t *vcf_open(const char *fn, const char *mode)
61 if (strchr(mode, 'b')) return bcf_open(fn, mode);
62 bp = calloc(1, sizeof(bcf_t));
63 v = calloc(1, sizeof(vcf_t));
66 v->refhash = bcf_str2id_init();
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") : stdout;
75 int vcf_close(bcf_t *bp)
78 if (bp == 0) return -1;
79 if (!bp->is_vcf) return bcf_close(bp);
85 if (v->fpout) fclose(v->fpout);
87 bcf_str2id_destroy(v->refhash);
93 int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h)
95 vcf_t *v = (vcf_t*)bp->v;
96 int i, has_ref = 0, has_ver = 0;
97 if (!bp->is_vcf) return bcf_hdr_write(bp, h);
99 if (strstr(h->txt, "##fileformat=")) has_ver = 1;
100 if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.0\n");
101 fwrite(h->txt, 1, h->l_txt - 1, v->fpout);
102 if (strstr(h->txt, "##SQ=")) has_ref = 1;
104 if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.0\n");
106 fprintf(v->fpout, "##SQ=");
107 for (i = 0; i < h->n_ref; ++i) {
108 fprintf(v->fpout, "%s", h->ns[i]);
109 fputc(i == h->n_ref - 1? '\n' : ',', v->fpout);
112 fprintf(v->fpout, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT");
113 for (i = 0; i < h->n_smpl; ++i)
114 fprintf(v->fpout, "\t%s", h->sns[i]);
115 fputc('\n', v->fpout);
119 int vcf_write(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
121 vcf_t *v = (vcf_t*)bp->v;
122 extern void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s);
123 if (!bp->is_vcf) return bcf_write(bp, h, b);
124 bcf_fmt_core(h, b, &v->line);
125 fwrite(v->line.s, 1, v->line.l, v->fpout);
126 fputc('\n', v->fpout);
127 return v->line.l + 1;
130 int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b)
132 int dret, k, i, sync = 0;
133 vcf_t *v = (vcf_t*)bp->v;
137 if (!bp->is_vcf) return bcf_read(bp, h, b);
139 str.l = 0; str.m = b->m_str; str.s = b->str;
140 rn.l = rn.m = h->l_nm; rn.s = h->name;
141 if (ks_getuntil(v->ks, '\n', &v->line, &dret) < 0) return -1;
142 for (p = kstrtok(v->line.s, "\t", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) {
145 int tid = bcf_str2id(v->refhash, p);
147 tid = bcf_str2id_add(v->refhash, p);
148 kputs(p, &rn); kputc('\0', &rn);
152 } else if (k == 1) { // pos
154 } else if (k == 5) { // qual
155 b->qual = (p[0] >= '0' && p[0] <= '9')? atoi(p) : 0;
156 } else if (k <= 8) { // variable length strings
157 kputs(p, &str); kputc('\0', &str);
158 b->l_str = str.l; b->m_str = str.m; b->str = str.s;
159 if (k == 8) bcf_sync(h->n_smpl, b);
161 for (q = kstrtok(p, ":", &a2), i = 0; q && i < b->n_gi; q = kstrtok(0, 0, &a2), ++i) {
162 if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
163 ((uint8_t*)b->gi[i].data)[k-9] = (q[0] - '0')<<3 | (q[2] - '0') | (q[1] == '/'? 0 : 1) << 6;
164 } else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
165 int x = strtol(q, &q, 10);
166 if (x > 255) x = 255;
167 ((uint8_t*)b->gi[i].data)[k-9] = x;
168 } else if (b->gi[i].fmt == bcf_str2int("DP", 2)) {
169 int x = strtol(q, &q, 10);
170 if (x > 0xffff) x = 0xffff;
171 ((uint16_t*)b->gi[i].data)[k-9] = x;
172 } else if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
174 uint8_t *data = (uint8_t*)b->gi[i].data;
175 for (j = 0; j < b->gi[i].len; ++j) {
176 x = strtol(q, &q, 10);
177 if (x > 255) x = 255;
178 data[i * b->gi[i].len + j] = x;
185 h->l_nm = rn.l; h->name = rn.s;
186 if (sync) bcf_hdr_sync(h);
187 return v->line.l + 1;