Skip to content

Commit

Permalink
Fix a bug in genotype filtering
Browse files Browse the repository at this point in the history
An empty genotype field (".") followed a non-empty field (e.g. "0/1") was
not fully reset on next site, causing the program to crash.

Resolves #2314
  • Loading branch information
pd3 committed Nov 16, 2024
1 parent 5d57c7b commit f602523
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
15 changes: 5 additions & 10 deletions filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1191,12 +1191,9 @@ static void filters_set_format_string(filter_t *flt, bcf1_t *line, token_t *tok)
}
static void _filters_set_genotype(filter_t *flt, bcf1_t *line, token_t *tok, int type)
{
tok->nvalues = tok->str_value.l = 0;
bcf_fmt_t *fmt = bcf_get_fmt(flt->hdr, line, "GT");
if ( !fmt )
{
tok->nvalues = tok->str_value.l = 0;
return;
}
if ( !fmt ) return;

int i,j, nsmpl = bcf_hdr_nsamples(flt->hdr), nvals1 = type==2 ? 3 : 4;
if ( tok->str_value.m <= nvals1*nsmpl )
Expand Down Expand Up @@ -1276,12 +1273,10 @@ static void filters_set_genotype4(filter_t *flt, bcf1_t *line, token_t *tok) { _

static void filters_set_genotype_string(filter_t *flt, bcf1_t *line, token_t *tok)
{
tok->nvalues = tok->str_value.l = 0;
bcf_fmt_t *fmt = bcf_get_fmt(flt->hdr, line, "GT");
if ( !fmt )
{
tok->nvalues = 0;
return;
}
if ( !fmt ) return;

int i, blen = 4, nsmpl = line->n_sample;

gt_length_too_big:
Expand Down
2 changes: 2 additions & 0 deletions test/query.filter.14.1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chr1:1 SM 0|1
chr1:2 SM .
1 change: 1 addition & 0 deletions test/query.filter.14.2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chr1:2 SM .
1 change: 1 addition & 0 deletions test/query.filter.14.3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chr1:1 SM 0|1
6 changes: 6 additions & 0 deletions test/query.filter.14.vcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
##fileformat=VCFv4.2
##contig=<ID=chr1>
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT SM
chr1 1 . A C . . . GT 0|1
chr1 2 . G T . . . . .
3 changes: 3 additions & 0 deletions test/test.pl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
run_test(\&test_vcf_merge,$opts,in=>['merge.gvcf.5.a','merge.gvcf.5.b'],out=>'merge.gvcf.5.1.out',args=>'--gvcf - --merge none');
run_test(\&test_vcf_merge,$opts,in=>['merge.gvcf.11.a','merge.gvcf.11.b','merge.gvcf.11.c'],out=>'merge.gvcf.11.1.out',args=>'--gvcf -');
# run_test(\&test_vcf_merge_big,$opts,in=>'merge_big.1',out=>'merge_big.1.1',nsmpl=>79000,nfiles=>79,nalts=>486,args=>''); # commented out for speed
run_test(\&test_vcf_query,$opts,in=>'query.filter.14',out=>'query.filter.14.1.out',args=>q[-f '%CHROM:%POS [ %SAMPLE %GT]']);
run_test(\&test_vcf_query,$opts,in=>'query.filter.14',out=>'query.filter.14.2.out',args=>q[-f '%CHROM:%POS [ %SAMPLE %GT]' -i'GT="."']);
run_test(\&test_vcf_query,$opts,in=>'query.filter.14',out=>'query.filter.14.3.out',args=>q[-f '%CHROM:%POS [ %SAMPLE %GT]' -i'GT="0|1"']);
run_test(\&test_vcf_query,$opts,in=>'query.func.1',out=>'query.func.1.1.out',args=>q[-f '%CHROM:%POS\\t%INFO/AD\\t%SUM(INFO/AD)']);
run_test(\&test_vcf_query,$opts,in=>'query.func.1',out=>'query.func.1.2.out',args=>q[-f '%CHROM:%POS\\t[%AD ]\\t%SUM(FORMAT/AD)']);
run_test(\&test_vcf_query,$opts,in=>'query.func.1',out=>'query.func.1.3.out',args=>q[-f '%CHROM:%POS\\t[%AD ]\\t[ %SUM(FORMAT/AD)]']);
Expand Down

0 comments on commit f602523

Please sign in to comment.