int bcf_close(bcf_t *b)
{
+ int ret;
if (b == 0) return 0;
- return bgzf_close(b->fp);
+ ret = bgzf_close(b->fp);
+ free(b);
+ return ret;
}
int bcf_hdr_write(bcf_t *b, const bcf_hdr_t *h)
return 0;
}
-#define char2int(s) (((int)s[0])<<8|s[1])
-
int bcf_sync(int n_smpl, bcf1_t *b)
{
- char *p, *tmp[5], *s;
+ char *p, *tmp[5];
int i, n;
+ ks_tokaux_t aux;
// set ref, alt, flt, info, fmt
b->ref = b->alt = b->flt = b->info = b->fmt = 0;
for (p = b->str, n = 0; p < b->str + b->l_str; ++p)
memset(b->gi + old_m, 0, (b->m_gi - old_m) * sizeof(bcf_ginfo_t));
}
b->n_gi = n;
- for (p = s = b->fmt, n = 0; *p; ++p) {
- if (*p == ':' || *(p+1) == 0) {
- char *q = *p == ':'? p : p + 1;
- if ((q - s) != 2) return -2;
- b->gi[n].fmt = char2int(s);
- s = q;
- }
- }
+ for (p = kstrtok(b->fmt, ":", &aux), n = 0; p; p = kstrtok(0, 0, &aux))
+ b->gi[n++].fmt = bcf_str2int(p, aux.p - p);
// set gi[i].len
for (i = 0; i < b->n_gi; ++i) {
- if (b->gi[i].fmt == char2int("PL")) {
+ if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
b->gi[i].len = b->n_alleles * (b->n_alleles + 1) / 2;
- } else if (b->gi[i].fmt == char2int("DP") || b->gi[i].fmt == char2int("HQ")) {
+ } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("HQ", 2)) {
b->gi[i].len = 2;
- } else if (b->gi[i].fmt == char2int("GQ") || b->gi[i].fmt == char2int("GT")) {
+ } else if (b->gi[i].fmt == bcf_str2int("GQ", 2) || b->gi[i].fmt == bcf_str2int("GT", 2)) {
b->gi[i].len = 1;
- } else if (b->gi[i].fmt == char2int("GL")) {
+ } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
b->gi[i].len = 4;
}
b->gi[i].data = realloc(b->gi[i].data, n_smpl * b->gi[i].len);
else kputs(p, s);
}
-char *bcf_fmt(const bcf_hdr_t *h, bcf1_t *b)
+void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s)
{
- kstring_t s;
int i, j, x;
- memset(&s, 0, sizeof(kstring_t));
- kputs(h->ns[b->tid], &s); kputc('\t', &s);
- kputw(b->pos + 1, &s); kputc('\t', &s);
- fmt_str(b->str, &s); kputc('\t', &s);
- fmt_str(b->ref, &s); kputc('\t', &s);
- fmt_str(b->alt, &s); kputc('\t', &s);
- kputw(b->qual, &s); kputc('\t', &s);
- fmt_str(b->flt, &s); kputc('\t', &s);
- fmt_str(b->info, &s);
+ s->l = 0;
+ if (h->n_ref) kputs(h->ns[b->tid], s);
+ else kputw(b->tid, s);
+ kputc('\t', s);
+ kputw(b->pos + 1, s); kputc('\t', s);
+ fmt_str(b->str, s); kputc('\t', s);
+ fmt_str(b->ref, s); kputc('\t', s);
+ fmt_str(b->alt, s); kputc('\t', s);
+ kputw(b->qual, s); kputc('\t', s);
+ fmt_str(b->flt, s); kputc('\t', s);
+ fmt_str(b->info, s);
if (b->fmt[0]) {
- kputc('\t', &s);
- fmt_str(b->fmt, &s);
+ kputc('\t', s);
+ fmt_str(b->fmt, s);
}
x = b->n_alleles * (b->n_alleles + 1) / 2;
for (j = 0; j < h->n_smpl; ++j) {
- kputc('\t', &s);
+ kputc('\t', s);
for (i = 0; i < b->n_gi; ++i) {
- if (i) kputc(':', &s);
- if (b->gi[i].fmt == char2int("PL")) {
+ if (i) kputc(':', s);
+ if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
uint8_t *d = (uint8_t*)b->gi[i].data + j * x;
int k;
for (k = 0; k < x; ++k) {
- if (k > 0) kputc(',', &s);
- kputw(d[k], &s);
+ if (k > 0) kputc(',', s);
+ kputw(d[k], s);
}
- } else if (b->gi[i].fmt == char2int("DP")) {
- kputw(((uint16_t*)b->gi[i].data)[j], &s);
- } else if (b->gi[i].fmt == char2int("GQ")) {
- kputw(((uint8_t*)b->gi[i].data)[j], &s);
+ } else if (b->gi[i].fmt == bcf_str2int("DP", 2)) {
+ kputw(((uint16_t*)b->gi[i].data)[j], s);
+ } else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
+ kputw(((uint8_t*)b->gi[i].data)[j], s);
+ } else if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
+ int y = ((uint8_t*)b->gi[i].data)[j];
+ kputc('0' + (y>>3&7), s);
+ kputc("/|"[y>>6&1], s);
+ kputc('0' + (y&7), s);
}
}
}
+}
+
+char *bcf_fmt(const bcf_hdr_t *h, bcf1_t *b)
+{
+ kstring_t s;
+ s.l = s.m = 0; s.s = 0;
+ bcf_fmt_core(h, b, &s);
return s.s;
}