This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to calculate reference allele frequency in pyvcf and print for all line?

I want to read the vcf file using pyvcf and output the values in a table format.

This is what I have done:

!/home/everestial007/anaconda2/bin/python
import vcf
pop1_data = vcf.Reader(open('raw01_variantsMY.vcf', 'r'))
pop2_data = vcf.Reader(open('raw01_variantsSP.vcf', 'r'))
record1 = next(pop1_data)
record2 = next(pop2_data)
alt_freqPop1 = record1.INFO['AF']
alt_freqPop2 = record1.INFO['AF']
ref_freqPop1 = 1-[alt_freqPop1]
ref_freqPop2 = 1-[alt_freqPop2]
print alt_freqPop1  **# this line prints**
print ref_freqPop1  **# this line isnot printing and receiving an error message**

Another problem:

I also want to print the values for all the lines in vcf

*print record1.CHROM, record1.POS, record1.ID, record1.REF, record1.ALT, record1.INFO['AF']*

But, this is printing only one line.

pyvcf vcf python print

1 answer

import pysam,sys

myvcf = pysam.VariantFile(sys.argv[1],'r')

for var in myvcf:
        print var.chrom, var.pos, var.id, var.ref, var.alts, var.info['AF'] # Whatever. look at `dir(var)` and `var.info.keys()`

Log in to answer this question.