From: Heng Li Date: Fri, 17 Sep 2010 13:15:53 +0000 (+0000) Subject: * put n_smpl to "bcf1_t" to simplify API a little X-Git-Url: https://git.donarmstrong.com/?p=samtools.git;a=commitdiff_plain;h=4fe4b27b44d067c21948870d9e48976662dec397 * put n_smpl to "bcf1_t" to simplify API a little --- diff --git a/bam2bcf.c b/bam2bcf.c index 9553225..e55212c 100644 --- a/bam2bcf.c +++ b/bam2bcf.c @@ -158,6 +158,7 @@ int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b) { kstring_t s; int i; + b->n_smpl = bc->n; b->tid = tid; b->pos = pos; b->qual = 0; s.s = b->str; s.m = b->m_str; s.l = 0; kputc('\0', &s); @@ -179,7 +180,7 @@ int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b) // FMT kputs("PL", &s); kputc('\0', &s); b->m_str = s.m; b->str = s.s; b->l_str = s.l; - bcf_sync(bc->n, b); + bcf_sync(b); memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n); return 0; } diff --git a/bcftools/bcf.c b/bcftools/bcf.c index 3393aad..2fd1696 100644 --- a/bcftools/bcf.c +++ b/bcftools/bcf.c @@ -100,10 +100,10 @@ int bcf_hdr_sync(bcf_hdr_t *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; @@ -172,6 +172,7 @@ int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b) int i, l = 0; 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, &b->qual, 4); bgzf_read(bp->fp, &b->l_str, 4); @@ -182,7 +183,7 @@ int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b) } bgzf_read(bp->fp, b->str, b->l_str); l = 12 + b->l_str; - bcf_sync(h->n_smpl, b); + bcf_sync(b); 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; @@ -219,7 +220,7 @@ void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_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); - ksprintf(s, "%.3g", b->qual); /*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]) { diff --git a/bcftools/bcf.h b/bcftools/bcf.h index b52f367..92b2c03 100644 --- a/bcftools/bcf.h +++ b/bcftools/bcf.h @@ -19,7 +19,7 @@ typedef struct { char *str, *ref, *alt, *flt, *info, *fmt; // fmt, ref, alt and info point to str int n_gi, m_gi; bcf_ginfo_t *gi; - int n_alleles; + int n_alleles, n_smpl; // derived info: ref, alt, flt, info, fmt, n_gi, n_alleles } bcf1_t; @@ -49,7 +49,7 @@ extern "C" { bcf_t *bcf_open(const char *fn, const char *mode); int bcf_close(bcf_t *b); int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b); - int bcf_sync(int n_smpl, bcf1_t *b); + int bcf_sync(bcf1_t *b); int bcf_write(bcf_t *bp, const bcf_hdr_t *h, const bcf1_t *b); bcf_hdr_t *bcf_hdr_read(bcf_t *b); int bcf_hdr_write(bcf_t *b, const bcf_hdr_t *h); @@ -65,8 +65,8 @@ extern "C" { int vcf_write(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b); int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b); - int bcf_shrink_alt(int n_smpl, bcf1_t *b, int n); - int bcf_gl2pl(int n_smpl, bcf1_t *b); + int bcf_shrink_alt(bcf1_t *b, int n); + int bcf_gl2pl(bcf1_t *b); void *bcf_build_refhash(bcf_hdr_t *h); void bcf_str2id_destroy(void *_hash); diff --git a/bcftools/bcfutils.c b/bcftools/bcfutils.c index dd1e89f..4d6835d 100644 --- a/bcftools/bcfutils.c +++ b/bcftools/bcfutils.c @@ -48,11 +48,10 @@ int bcf_str2id_add(void *_hash, const char *str) return kh_val(hash, k); } -int bcf_shrink_alt(int n_smpl, bcf1_t *b, int n) +int bcf_shrink_alt(bcf1_t *b, int n) { char *p; - int i, j, k; - int *z; + int i, j, k, *z, n_smpl = b->n_smpl; if (b->n_alleles <= n) return -1; if (n > 1) { for (p = b->alt, k = 1; *p; ++p) @@ -79,14 +78,14 @@ int bcf_shrink_alt(int n_smpl, bcf1_t *b, int n) } // FIXME: to add GL } b->n_alleles = n; - bcf_sync(n_smpl, b); + bcf_sync(b); return 0; } -int bcf_gl2pl(int n_smpl, bcf1_t *b) +int bcf_gl2pl(bcf1_t *b) { char *p; - int i; + int i, n_smpl = b->n_smpl; bcf_ginfo_t *g; float *d0; uint8_t *d1; diff --git a/bcftools/call1.c b/bcftools/call1.c index 6b5c47b..5c6e477 100644 --- a/bcftools/call1.c +++ b/bcftools/call1.c @@ -133,7 +133,7 @@ static int test16(bcf1_t *b, anno16_t *a) return test16_core(anno, a); } -static void rm_info(int n_smpl, bcf1_t *b, const char *key) +static void rm_info(bcf1_t *b, const char *key) { char *p, *q; if ((p = strstr(b->info, key)) == 0) return; @@ -141,7 +141,7 @@ static void rm_info(int n_smpl, bcf1_t *b, const char *key) if (p > b->info && *(p-1) == ';') --p; memmove(p, q, b->l_str - (q - b->str)); b->l_str -= q - p; - bcf_sync(n_smpl, b); + bcf_sync(b); } static int update_bcf1(int n_smpl, bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p1rst_t *pr, double pref, int flag) @@ -153,7 +153,7 @@ static int update_bcf1(int n_smpl, bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p p_hwe = pr->g[0] >= 0.? test_hwe(pr->g) : 1.0; // only do HWE g[] is calculated test16(b, &a); - rm_info(n_smpl, b, "I16="); + rm_info(b, "I16="); memset(&s, 0, sizeof(kstring_t)); kputc('\0', &s); kputs(b->ref, &s); kputc('\0', &s); @@ -174,10 +174,10 @@ static int update_bcf1(int n_smpl, bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p b->m_str = s.m; b->l_str = s.l; b->str = s.s; b->qual = r < 1e-100? 99 : -4.343 * log(r); if (b->qual > 99) b->qual = 99; - bcf_sync(n_smpl, b); - if (!is_var) bcf_shrink_alt(n_smpl, b, 1); + bcf_sync(b); + if (!is_var) bcf_shrink_alt(b, 1); else if (!(flag&VC_KEEPALT)) - bcf_shrink_alt(n_smpl, b, pr->rank0 < 2? 2 : pr->rank0+1); + bcf_shrink_alt(b, pr->rank0 < 2? 2 : pr->rank0+1); return is_var; } @@ -314,7 +314,7 @@ int bcfview(int argc, char *argv[]) } if (vc.flag & VC_CALL) { // call variants bcf_p1rst_t pr; - bcf_gl2pl(h->n_smpl, b); + bcf_gl2pl(b); bcf_p1_cal(b, p1, &pr); // pr.g[3] is not calculated here if (vc.flag&VC_HWE) bcf_p1_cal_g3(p1, pr.g); if (n_processed % 100000 == 0) { diff --git a/bcftools/vcf.c b/bcftools/vcf.c index d441d7f..51ccb74 100644 --- a/bcftools/vcf.c +++ b/bcftools/vcf.c @@ -139,6 +139,7 @@ int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b) str.l = 0; str.m = b->m_str; str.s = b->str; rn.l = rn.m = h->l_nm; rn.s = h->name; if (ks_getuntil(v->ks, '\n', &v->line, &dret) < 0) return -1; + b->n_smpl = h->n_smpl; for (p = kstrtok(v->line.s, "\t", &aux), k = 0; p; p = kstrtok(0, 0, &aux), ++k) { *(char*)aux.p = 0; if (k == 0) { // ref @@ -156,7 +157,7 @@ int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b) } else if (k <= 8) { // variable length strings kputs(p, &str); kputc('\0', &str); b->l_str = str.l; b->m_str = str.m; b->str = str.s; - if (k == 8) bcf_sync(h->n_smpl, b); + if (k == 8) bcf_sync(b); } else { // k > 9 if (strncmp(p, "./.", 3) == 0) { for (i = 0; i < b->n_gi; ++i) {