This is a test version of Biostars. For the public version, visit https://www.biostars.org.
VCF filtering by allele frequency

Hi, I am trying to filtering a vcf file by allele frequency. I'm using a file downloaded from the gnomad v2 liftover. I am using code as follows (python):

import vcf
vcf_reader = vcf.Reader(filename="gnomad.vcf.gz")
vcf_writer = vcf.Writer(open("filtered.vcf", "w"), vcf_reader)
for record in vcf_reader:
    if record.INFO['AF'][0] >= 0.5:
    vcf_writer.write_record(record)

And I have got error:

KeyError: 'AF'

But in column "INFO" in vcf has to be allele frequency. Any other ideas how to filter gnomad file by AF? Thanks in advance

af allele frequency gnomad vcf python

1 answer

because not all variants have a AF field.

$ wget -O - -q "https://storage.googleapis.com/gnomad-public/release/2.1.1/vcf/genomes/gnomad.genomes.r2.1.1.sites.1.vcf.bgz" | gunzip -c | grep -v "^#" | grep  -v ";AF="  | head -n 1 

1   11063   rs561109771 T   TG  79.39   AC0 AC=0;AN=0;rf_tp_probability=3.62584e-01;FS=0.00000e+00;Inbreedin

https://gnomad.broadinstitute.org/variant/1-11063-T-TG?dataset=gnomad_r2_1

you have to test for the presence of the AF key before using it.

Log in to answer this question.