Skip to content

Don't emit VCF header when filter expression is invalid #221

@tomwhite

Description

@tomwhite

In some cases filter expressions raise exceptions at eval time, not at initialization, so the header has already been written. For example:

vcztools view -i 'BLAH>0' vcz_test_cache/sample.vcf.vcz/
##fileformat=VCFv4.3
##source=bio2zarr-0.1.6
##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral Allele">
##INFO=<ID=AC,Number=2,Type=Integer,Description="Allele count in genotypes, for each ALT allele, in the same order as listed">
##INFO=<ID=AF,Number=2,Type=Float,Description="Allele Frequency">
##INFO=<ID=AN,Number=1,Type=Integer,Description="Total number of alleles in called genotypes">
##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership, build 129">
##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">
##INFO=<ID=H2,Number=0,Type=Flag,Description="HapMap2 membership">
##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data">
##FILTER=<ID=PASS,Description="All filters passed">
##FILTER=<ID=s50,Description="Less than 50% of samples have data">
##FILTER=<ID=q10,Description="Quality below 10">
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth">
##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality">
##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype Quality">
##contig=<ID=19>
##contig=<ID=20>
##contig=<ID=X>
##vcztools_viewCommand=view -i BLAH>0 vcz_test_cache/sample.vcf.vcz/; Date=2025-05-28 09:35:40.638678
##fileDate=20090805
##reference=1000GenomesPilot-NCBI36
##phasing=partial
##ALT=<ID=DEL:ME:ALU,Description="Deletion of ALU element">
##ALT=<ID=CNV,Description="Copy number variable region">
#CHROM	POS	ID	REF	ALT	QUAL	FILTER	INFO	FORMAT	NA00001	NA00002	NA00003
Traceback (most recent call last):
  File "/Users/tom/miniforge3/envs/vcztools-3.11-m1/bin/vcztools", line 8, in <module>
    sys.exit(vcztools_main())
             ^^^^^^^^^^^^^^^
  File "/Users/tom/miniforge3/envs/vcztools-3.11-m1/lib/python3.11/site-packages/click/core.py", line 1442, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/miniforge3/envs/vcztools-3.11-m1/lib/python3.11/site-packages/click/core.py", line 1363, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/tom/miniforge3/envs/vcztools-3.11-m1/lib/python3.11/site-packages/click/core.py", line 1830, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/miniforge3/envs/vcztools-3.11-m1/lib/python3.11/site-packages/click/core.py", line 1226, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/miniforge3/envs/vcztools-3.11-m1/lib/python3.11/site-packages/click/core.py", line 794, in invoke
    return callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/workspace/vcztools/vcztools/cli.py", line 43, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/workspace/vcztools/vcztools/cli.py", line 267, in view
    vcf_writer.write_vcf(
  File "/Users/tom/workspace/vcztools/vcztools/vcf_writer.py", line 161, in write_vcf
    for chunk_data in retrieval.variant_chunk_iter(
  File "/Users/tom/workspace/vcztools/vcztools/retrieval.py", line 189, in variant_chunk_iter
    for v_chunk, v_mask_chunk in variant_chunk_index_iter_with_filtering(
  File "/Users/tom/workspace/vcztools/vcztools/retrieval.py", line 160, in variant_chunk_index_iter_with_filtering
    filter_fields_reader = VariantChunkReader(root, fields=filter_fields)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/workspace/vcztools/vcztools/retrieval.py", line 32, in __init__
    self.arrays = {key: self.root[key] for key in fields}
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/workspace/vcztools/vcztools/retrieval.py", line 32, in <dictcomp>
    self.arrays = {key: self.root[key] for key in fields}
                        ~~~~~~~~~^^^^^
  File "/Users/tom/miniforge3/envs/vcztools-3.11-m1/lib/python3.11/site-packages/zarr/hierarchy.py", line 511, in __getitem__
    raise KeyError(item)
KeyError: 'variant_BLAH'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions