#include "kstring.h"
#include "bcf.h"
-void bcf_hdr_clear(bcf_hdr_t *b)
-{
- free(b->name); free(b->sname); free(b->txt); free(b->ns); free(b->sns);
- memset(b, 0, sizeof(bcf_hdr_t));
-}
-
bcf_t *bcf_open(const char *fn, const char *mode)
{
bcf_t *b;
} else {
b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdin), mode);
}
+#ifndef BCF_LITE
b->fp->owned_file = 1;
+#endif
return b;
}
return 0;
}
-int bcf_sync(int n_smpl, bcf1_t *b)
+int bcf_sync(bcf1_t *b)
{
char *p, *tmp[5];
- int i, n;
+ int i, n, n_smpl = b->n_smpl;
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)
- if (*p == 0 && p+1 != b->str + b->l_str) tmp[n++] = p + 1;
- if (n != 5) return -1;
+ for (p = b->str, n = 0; p < b->str + b->l_str; ++p) {
+ if (*p == 0 && p+1 != b->str + b->l_str) {
+ if (n == 5) {
+ ++n;
+ break;
+ } else tmp[n++] = p + 1;
+ }
+ }
+ if (n != 5) {
+ fprintf(stderr, "[%s] incorrect number of fields (%d != 5) at %d:%d\n", __func__, n, b->tid, b->pos);
+ return -1;
+ }
b->ref = tmp[0]; b->alt = tmp[1]; b->flt = tmp[2]; b->info = tmp[3]; b->fmt = tmp[4];
// set n_alleles
- for (p = b->alt, n = 1; *p; ++p)
- if (*p == ',') ++n;
- b->n_alleles = n + 1;
+ if (*b->alt == 0) b->n_alleles = 1;
+ else {
+ for (p = b->alt, n = 1; *p; ++p)
+ if (*p == ',') ++n;
+ b->n_alleles = n + 1;
+ }
// set n_gi and gi[i].fmt
for (p = b->fmt, n = 1; *p; ++p)
if (*p == ':') ++n;
for (i = 0; i < b->n_gi; ++i) {
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 == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("HQ", 2)) {
+ } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("HQ", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
b->gi[i].len = 2;
} 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 == bcf_str2int("GL", 2)) {
+ } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
b->gi[i].len = 4;
+ } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
+ b->gi[i].len = b->n_alleles * (b->n_alleles + 1) / 2 * 4;
}
b->gi[i].data = realloc(b->gi[i].data, n_smpl * b->gi[i].len);
}
int bcf_write(bcf_t *bp, const bcf_hdr_t *h, const bcf1_t *b)
{
- uint32_t x;
int i, l = 0;
if (b == 0) return -1;
bgzf_write(bp->fp, &b->tid, 4);
bgzf_write(bp->fp, &b->pos, 4);
- x = b->qual<<24 | b->l_str;
- bgzf_write(bp->fp, &x, 4);
+ bgzf_write(bp->fp, &b->qual, 4);
+ bgzf_write(bp->fp, &b->l_str, 4);
bgzf_write(bp->fp, b->str, b->l_str);
l = 12 + b->l_str;
for (i = 0; i < b->n_gi; ++i) {
int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b)
{
int i, l = 0;
- uint32_t x;
if (b == 0) return -1;
if (bgzf_read(bp->fp, &b->tid, 4) == 0) return -1;
+ b->n_smpl = h->n_smpl;
bgzf_read(bp->fp, &b->pos, 4);
- bgzf_read(bp->fp, &x, 4);
- b->qual = x >> 24; b->l_str = x << 8 >> 8;
+ bgzf_read(bp->fp, &b->qual, 4);
+ bgzf_read(bp->fp, &b->l_str, 4);
if (b->l_str > b->m_str) {
b->m_str = b->l_str;
kroundup32(b->m_str);
}
bgzf_read(bp->fp, b->str, b->l_str);
l = 12 + b->l_str;
- bcf_sync(h->n_smpl, b);
+ if (bcf_sync(b) < 0) return -2;
for (i = 0; i < b->n_gi; ++i) {
bgzf_read(bp->fp, b->gi[i].data, b->gi[i].len * h->n_smpl);
l += b->gi[i].len * h->n_smpl;
int i;
if (b == 0) return -1;
free(b->str);
- for (i = 0; i < b->n_gi; ++i)
+ for (i = 0; i < b->m_gi; ++i)
free(b->gi[i].data);
free(b->gi);
free(b);
{
int i, j, x;
s->l = 0;
- kputs(h->ns[b->tid], s); kputc('\t', s);
+ 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);
+ ksprintf(s, "%.3g", b->qual); kputc('\t', s);
fmt_str(b->flt, s); kputc('\t', s);
fmt_str(b->info, s);
if (b->fmt[0]) {
fmt_str(b->fmt, s);
}
x = b->n_alleles * (b->n_alleles + 1) / 2;
+ if (b->n_gi == 0) return;
+ int iPL = -1;
+ if ( b->n_alleles > 2 ) {
+ for (i=0; i<b->n_gi; i++) {
+ if ( b->gi[i].fmt == bcf_str2int("PL", 2) ) {
+ iPL = i;
+ break;
+ }
+ }
+ }
for (j = 0; j < h->n_smpl; ++j) {
+
+ // Determine GT with maximum PL (multiple ALT sites only)
+ int imax=-1;
+ if ( iPL!=-1 ) {
+ uint8_t *d = (uint8_t*)b->gi[iPL].data + j * x;
+ int k,identical=1;
+ imax=0;
+ for (k=1; k<x; k++)
+ {
+ if ( identical && d[k]!=d[k-1] ) identical = 0;
+ if ( d[k]<d[imax] ) imax = k;
+ }
+ // If all lks are identical, leave GT untouched
+ if ( identical ) imax = -1;
+ }
kputc('\t', s);
for (i = 0; i < b->n_gi; ++i) {
if (i) kputc(':', s);
if (k > 0) kputc(',', s);
kputw(d[k], s);
}
- } else if (b->gi[i].fmt == bcf_str2int("DP", 2)) {
+ } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 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("SP", 2)) {
+ kputw(((int32_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);
- }
+ int y = ((uint8_t*)b->gi[i].data)[j];
+ if ( y>>7&1 )
+ kputsn("./.", 3, s);
+ else if ( imax==-1 )
+ {
+ kputc('0' + (y>>3&7), s);
+ kputc("/|"[y>>6&1], s);
+ kputc('0' + (y&7), s);
+ }
+ else
+ {
+ // Arguably, the while loop will be faster than two sqrts
+ int n = 0;
+ int row = 1;
+ while ( n<imax )
+ {
+ row++;
+ n += row;
+ }
+ row--;
+ kputw(imax-n+row, s);
+ kputc("/|"[y>>6&1], s);
+ kputw(row, s);
+ }
+ } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
+ float *d = (float*)b->gi[i].data + j * x;
+ int k;
+ //printf("- %lx\n", d);
+ for (k = 0; k < x; ++k) {
+ if (k > 0) kputc(',', s);
+ ksprintf(s, "%.2f", d[k]);
+ }
+ } else kputc('.', s); // custom fields
}
}
}
bcf_fmt_core(h, b, &s);
return s.s;
}
+
+int bcf_append_info(bcf1_t *b, const char *info, int l)
+{
+ int shift = b->fmt - b->str;
+ int l_fmt = b->l_str - shift;
+ char *ori = b->str;
+ if (b->l_str + l > b->m_str) { // enlarge if necessary
+ b->m_str = b->l_str + l;
+ kroundup32(b->m_str);
+ b->str = realloc(b->str, b->m_str);
+ }
+ memmove(b->str + shift + l, b->str + shift, l_fmt); // move the FORMAT field
+ memcpy(b->str + shift - 1, info, l); // append to the INFO field
+ b->str[shift + l - 1] = '\0';
+ b->fmt = b->str + shift + l;
+ b->l_str += l;
+ if (ori != b->str) bcf_sync(b); // synchronize when realloc changes the pointer
+ return 0;
+}
+
+int bcf_cpy(bcf1_t *r, const bcf1_t *b)
+{
+ char *t1 = r->str;
+ bcf_ginfo_t *t2 = r->gi;
+ int i, t3 = r->m_str, t4 = r->m_gi;
+ *r = *b;
+ r->str = t1; r->gi = t2; r->m_str = t3; r->m_gi = t4;
+ if (r->m_str < b->m_str) {
+ r->m_str = b->m_str;
+ r->str = realloc(r->str, r->m_str);
+ }
+ memcpy(r->str, b->str, r->m_str);
+ bcf_sync(r); // calling bcf_sync() is simple but inefficient
+ for (i = 0; i < r->n_gi; ++i)
+ memcpy(r->gi[i].data, b->gi[i].data, r->n_smpl * r->gi[i].len);
+ return 0;
+}
+
+int bcf_is_indel(const bcf1_t *b)
+{
+ char *p;
+ if (strlen(b->ref) > 1) return 1;
+ for (p = b->alt; *p; ++p)
+ if (*p != ',' && p[1] != ',' && p[1] != '\0')
+ return 1;
+ return 0;
+}