X-Git-Url: https://git.donarmstrong.com/?p=samtools.git;a=blobdiff_plain;f=bcftools%2Fvcf.c;h=e8526a38706357c4aa2d067fcf9c64dadd8978f6;hp=23aad3c0c97b02c12d9643d18c87e68ef4d87dc2;hb=2fa4cde9b8806faceffb8677440b54ca4c90b6f7;hpb=39eb14826a5ef0de7f5982390e7e2b0d671848ee diff --git a/bcftools/vcf.c b/bcftools/vcf.c index 23aad3c..e8526a3 100644 --- a/bcftools/vcf.c +++ b/bcftools/vcf.c @@ -30,7 +30,12 @@ bcf_hdr_t *vcf_hdr_read(bcf_t *bp) memset(&smpl, 0, sizeof(kstring_t)); while (ks_getuntil(v->ks, '\n', &v->line, &dret) >= 0) { if (v->line.l < 2) continue; - if (v->line.s[0] != '#') return 0; // no sample line + if (v->line.s[0] != '#') { + free(meta.s); + free(smpl.s); + free(h); + return 0; // no sample line + } if (v->line.s[0] == '#' && v->line.s[1] == '#') { kputsn(v->line.s, v->line.l, &meta); kputc('\n', &meta); } else if (v->line.s[0] == '#') { @@ -72,6 +77,33 @@ bcf_t *vcf_open(const char *fn, const char *mode) return bp; } +int vcf_dictread(bcf_t *bp, bcf_hdr_t *h, const char *fn) +{ + vcf_t *v; + gzFile fp; + kstream_t *ks; + kstring_t s, rn; + int dret; + if (bp == 0) return -1; + if (!bp->is_vcf) return 0; + s.l = s.m = 0; s.s = 0; + rn.m = rn.l = h->l_nm; rn.s = h->name; + v = (vcf_t*)bp->v; + fp = gzopen(fn, "r"); + ks = ks_init(fp); + while (ks_getuntil(ks, 0, &s, &dret) >= 0) { + bcf_str2id_add(v->refhash, strdup(s.s)); + kputs(s.s, &rn); kputc('\0', &rn); + if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret); + } + ks_destroy(ks); + gzclose(fp); + h->l_nm = rn.l; h->name = rn.s; + bcf_hdr_sync(h); + free(s.s); + return 0; +} + int vcf_close(bcf_t *bp) { vcf_t *v; @@ -84,7 +116,7 @@ int vcf_close(bcf_t *bp) } if (v->fpout) fclose(v->fpout); free(v->line.s); - bcf_str2id_destroy(v->refhash); + bcf_str2id_thorough_destroy(v->refhash); free(v); free(bp); return 0; @@ -93,22 +125,14 @@ int vcf_close(bcf_t *bp) int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h) { vcf_t *v = (vcf_t*)bp->v; - int i, has_ref = 0, has_ver = 0; + int i, has_ver = 0; if (!bp->is_vcf) return bcf_hdr_write(bp, h); if (h->l_txt > 0) { if (strstr(h->txt, "##fileformat=")) has_ver = 1; - if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.0\n"); + if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.1\n"); fwrite(h->txt, 1, h->l_txt - 1, v->fpout); - if (strstr(h->txt, "##SQ=")) has_ref = 1; - } - if (has_ver == 0) fprintf(v->fpout, "##fileformat=VCFv4.0\n"); - if (!has_ref) { - fprintf(v->fpout, "##SQ="); - for (i = 0; i < h->n_ref; ++i) { - fprintf(v->fpout, "%s", h->ns[i]); - fputc(i == h->n_ref - 1? '\n' : ',', v->fpout); - } } + if (h->l_txt == 0) fprintf(v->fpout, "##fileformat=VCFv4.1\n"); fprintf(v->fpout, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT"); for (i = 0; i < h->n_smpl; ++i) fprintf(v->fpout, "\t%s", h->sns[i]); @@ -139,12 +163,13 @@ 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 int tid = bcf_str2id(v->refhash, p); if (tid < 0) { - tid = bcf_str2id_add(v->refhash, p); + tid = bcf_str2id_add(v->refhash, strdup(p)); kputs(p, &rn); kputc('\0', &rn); sync = 1; } @@ -152,11 +177,11 @@ int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b) } else if (k == 1) { // pos b->pos = atoi(p) - 1; } else if (k == 5) { // qual - b->qual = (p[0] >= '0' && p[0] <= '9')? atoi(p) : 0; + b->qual = (p[0] >= '0' && p[0] <= '9')? atof(p) : 0; } 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) { @@ -164,7 +189,9 @@ int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b) ((uint8_t*)b->gi[i].data)[k-9] = 1<<7; } else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) { ((uint8_t*)b->gi[i].data)[k-9] = 0; - } else if (b->gi[i].fmt == bcf_str2int("DP", 2)) { + } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) { + ((int32_t*)b->gi[i].data)[k-9] = 0; + } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) { ((uint16_t*)b->gi[i].data)[k-9] = 0; } else if (b->gi[i].fmt == bcf_str2int("PL", 2)) { int y = b->n_alleles * (b->n_alleles + 1) / 2; @@ -184,7 +211,11 @@ int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b) int x = (int)(_x + .499); if (x > 255) x = 255; ((uint8_t*)b->gi[i].data)[k-9] = x; - } else if (b->gi[i].fmt == bcf_str2int("DP", 2)) { + } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) { + int x = strtol(q, &q, 10); + if (x > 0xffff) x = 0xffff; + ((uint32_t*)b->gi[i].data)[k-9] = x; + } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) { int x = strtol(q, &q, 10); if (x > 0xffff) x = 0xffff; ((uint16_t*)b->gi[i].data)[k-9] = x; @@ -204,7 +235,7 @@ int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b) y = b->n_alleles * (b->n_alleles + 1) / 2; for (j = 0; j < y; ++j) { x = strtod(q, &q); - data[(k-9) * y + j] = x; + data[(k-9) * y + j] = x > 0? -x/10. : x; ++q; } }