How to calculate reference allele frequency in pyvcf and print for all line?
1
0
Entering edit mode
8.2 years ago
kirannbishwa01 ★ 1.6k

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 • 2.3k views
ADD COMMENT
0
Entering edit mode
8.2 years ago
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()`
ADD COMMENT

Login before adding your answer.

Traffic: 1981 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6