Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vcf int64 part2 #1004

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/longrefs/index.expected2.vcf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1 10010000110 . G <DEL> 0 . SVTYPE=DEL;SVLEN=-890;END=10010001000 PL 0,1,45
1 10010000110 . G <DEL> 0 . SVTYPE=DEL;SVLEN=-890;END=10010001000;QS=1,0 PL 0,1,45
2 changes: 1 addition & 1 deletion test/longrefs/index.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,4 @@
1 10010000107 . G <*> 0 . DP=1;I16=0,1,0,0,33,1089,0,0,29,841,0,0,2,4,0,0;QS=1,0;MQ0F=0 PL 0,3,29
1 10010000108 . C <*> 0 . DP=1;I16=0,1,0,0,32,1024,0,0,29,841,0,0,1,1,0,0;QS=1,0;MQ0F=0 PL 0,3,29
1 10010000109 . A <*> 0 . DP=1;I16=0,1,0,0,35,1225,0,0,29,841,0,0,0,0,0,0;QS=1,0;MQ0F=0 PL 0,3,29
1 10010000110 . G <DEL> 0 . SVTYPE=DEL;SVLEN=-890;END=10010001000 PL 0,1,45
1 10010000110 . G <DEL> 0 . SVTYPE=DEL;SVLEN=-890;END=10010001000;QS=1,0 PL 0,1,45
31 changes: 28 additions & 3 deletions vcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ DEALINGS IN THE SOFTWARE. */
#include "htslib/kstring.h"
#include "htslib/sam.h"

// A flag for bcf1_t->unpacked to indicate this contains 64-bit data.
// Prevents us accidentally attempting to write invalid BCF records.
#define BCF_IS_64BIT (1<<30)

#include "htslib/khash.h"
KHASH_MAP_INIT_STR(vdict, bcf_idinfo_t)
typedef khash_t(vdict) vdict_t;
Expand All @@ -59,7 +63,7 @@ HTSLIB_EXPORT
uint32_t bcf_float_vector_end = 0x7F800002;

HTSLIB_EXPORT
uint8_t bcf_type_shift[] = { 0, 0, 1, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
uint8_t bcf_type_shift[] = { 0, 0, 1, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

static bcf_idinfo_t bcf_idinfo_def = { .info = { 15, 15, 15 }, .hrec = { NULL, NULL, NULL}, .id = -1 };

Expand Down Expand Up @@ -1728,6 +1732,11 @@ int bcf_write(htsFile *hfp, bcf_hdr_t *h, bcf1_t *v)
}
bcf1_sync(v); // check if the BCF record was modified

if (v->unpacked & BCF_IS_64BIT) {
hts_log_error("Data contains 64-bit values not representable in BCF. Please use VCF instead");
return -1;
}

BGZF *fp = hfp->fp.bgzf;
union {
uint32_t i;
Expand Down Expand Up @@ -2526,6 +2535,8 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v)
} else {
v->pos -= 1;
}
if (v->pos >= INT32_MAX)
v->unpacked |= BCF_IS_64BIT;
jkbonfield marked this conversation as resolved.
Show resolved Hide resolved
} else if (i == 2) { // ID
if (strcmp(p, ".")) bcf_enc_vchar(str, q - p, p);
else bcf_enc_size(str, 0, BCF_BT_CHAR);
Expand Down Expand Up @@ -2679,7 +2690,16 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v)
val_a[0] = bcf_int32_missing;
v64 = bcf_int64_missing;
} else {
val_a[0] = v64 >= BCF_MIN_BT_INT32 && v64 <= BCF_MAX_BT_INT32 ? v64 : bcf_int32_missing;
if (v64 >= BCF_MIN_BT_INT32 && v64 <= BCF_MAX_BT_INT32) {
val_a[0] = v64;
} else {
val_a[0] = bcf_int32_missing;
// Only permit 64-bit INFO values for END tag
if (strcmp(key, "END") != 0) {
hts_log_warning("INFO key %s has out of range value %"PRId64, key, v64);
v64 = bcf_int64_missing;
}
}
}
for (t = te; *t && *t != ','; t++);
if (*t == ',') ++t;
Expand All @@ -2691,12 +2711,17 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v)
for (t = te; *t && *t != ','; t++);
}
if (n_val == 1) {
if (val_a[0] != v64 && v64 != bcf_int64_missing)
v->unpacked |= BCF_IS_64BIT;
bcf_enc_long1(str, v64);
} else {
bcf_enc_vint(str, n_val, val_a, -1);
}
if (strcmp(key, "END") == 0)
if (strcmp(key, "END") == 0) {
v->rlen = v64 - v->pos;
if (v->rlen >= INT32_MAX)
v->unpacked |= BCF_IS_64BIT;
}
} else if ((y>>4&0xf) == BCF_HT_REAL) {
float *val_f = (float *)val_a;
for (i = 0, t = val; i < n_val; ++i, ++t)
Expand Down