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

Drop duplicate FORMAT tags and fix an error with dropping large tags #1752

Merged
merged 2 commits into from
Mar 14, 2024
Merged
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
6 changes: 3 additions & 3 deletions test/noroundtrip-out.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##contig=<ID=3>
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT NA1
3 50 . A T 0 PASS . GT:GT 2,4:.
3 50 . A T 0 PASS . GT 0/1
3 60 . T C 0 PASS . GT 0/1
3 70 . G A 0 PASS . GT:GT 2,4:.
3 80 . C G 0 PASS . GT:GT 2,4:0/1
3 70 . G A 0 PASS . GT 0/1
3 80 . C G 0 PASS . GT 0/1
29 changes: 24 additions & 5 deletions vcf.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* vcf.c -- VCF/BCF API functions.

Copyright (C) 2012, 2013 Broad Institute.
Copyright (C) 2012-2023 Genome Research Ltd.
Copyright (C) 2012-2024 Genome Research Ltd.
Portions copyright (C) 2014 Intel Corporation.

Author: Heng Li <lh3@sanger.ac.uk>
Expand Down Expand Up @@ -3029,6 +3029,26 @@ static int vcf_parse_format_alloc4(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v,
fmt[j].buf = (uint8_t*)mem->s + fmt[j].offset;
}

// check for duplicate tags
int i;
for (i=1; i<v->n_fmt; i++)
{
fmt_aux_t *ifmt = &fmt[i];
if ( ifmt->size==-1 ) continue; // already marked for removal
for (j=0; j<i; j++)
{
fmt_aux_t *jfmt = &fmt[j];
if ( jfmt->size==-1 ) continue; // already marked for removal
if ( ifmt->key!=jfmt->key ) continue;
static int warned = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Htslib is meant to be thread safe, and also callable multiple times. We shouldn't be using static variables like this as they can lead to thread complaints with automated checking tools (such as two threads modifying the same variable without intervening locks) and also if we parse file A and file B then we'll never report warnings on file B if file A produced one.

Personally, I think we should warn for everything. If it gets too spammy, people can turn down the warning level with the appropriate command line options to set verbosity. (Or just fix their buggy inputs.)

We could however have it as a non-standard function level scope so we warn once for the function as a whole. This reduces spamming a little if the entire string is duplicated.

if ( !warned ) hts_log_warning("Duplicate FORMAT tag %s at %s:%"PRIhts_pos, bcf_hdr_int2id(h,BCF_DT_ID,ifmt->key), bcf_seqname_safe(h,v), v->pos+1);
warned = 1;
v->errcode |= BCF_ERR_TAG_INVALID;
ifmt->size = -1;
ifmt->offset = 0;
break;
}
}
return 0;
}

Expand Down Expand Up @@ -3072,7 +3092,7 @@ static int vcf_parse_format_fill5(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v,

if ( z->size==-1 )
{
// this field is to be ignored, it's too big
// this field is to be ignored, it's either too big or a duplicate
while ( *t != ':' && *t ) t++;
}
else if (htype == BCF_HT_STR) {
Expand Down Expand Up @@ -3274,18 +3294,17 @@ static int vcf_parse_format_gt6(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v,

}
if ( need_downsize ) {
i = 1;
i = 0;
while ( i < v->n_fmt ) {
if ( fmt[i].size==-1 )
{
memmove(&fmt[i-1],&fmt[i],sizeof(*fmt));
v->n_fmt--;
if ( i < v->n_fmt ) memmove(&fmt[i],&fmt[i+1],sizeof(*fmt)*(v->n_fmt-i));
}
else
i++;
}
}

return 0;
}

Expand Down