This is a test version of Biostars. For the public version, visit https://www.biostars.org.
add VAF or FREQ in vcf file from read depth

Hi,

I have a vcf file with these values GT:VR:RR:DP:GQ in the format field. I would like to add the Variant Allele frequency tag (Frequency of alternate allele). I know that I can calculate it manually from the read depth information FREQ=(DP-RR)100/DP where DP: read depth and RR: reference read depth. Do you know any way to do it with commands (bcftools,vcftools or awk)?

Thanks!

next-gen

1 answer

(not tested) using VcfFilterJdk http://lindenb.github.io/jvarkit/VcfFilterJdk.html

$ bcftools view in.vcf.gz |\
awk '/^#CHROM/ {printf("##FORMAT=<ID=FREQ,Number=1,Type=Float,Description=\"\">\n");} {print;}' |
java -jar dist/vcffilterjdk.jar -e 'final List<Genotype> gts = new ArrayList<>();for(final Genotype gt:variant.getGenotypes()) {final GenotypeBuilder gb=new GenotypeBuilder(gt);if(gt.hasDP() && gt.hasAnyAttribute("RR")){double rr =  gt.getAttributeAsDouble("RR",0.0);gb.attribute("FREQ",((gt.getDP()-rr)*100.0)/gt.getDP());}gts.add(gb.make());}return new VariantContextBuilder(variant).genotypes(gts).make();'

Log in to answer this question.