/
make_vcf_referenceSites.py
executable file
·56 lines (37 loc) · 1.75 KB
/
make_vcf_referenceSites.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
import argparse
from common import yield_bedcoordinate
import bx.seq.twobit
from VcfRecord import *
from VcfGenotype import *
""" given a bedstring and bx.seq.twobit object, extract the sequence from the intervals parsed from the bedstring """
def twoBitExtract(bedstring, twobit):
(chr, start, end)=bedstring.split('\t')
start=int(start)
end=int(end)
def main():
usage = "usage: %prog [options] interval.bed"
parser = argparse.ArgumentParser(description='make reference vcf records based on non-variant interval bed file and pedfile')
parser.add_argument('bed', metavar='bed', type=str, help='interval.bed')
parser.add_argument("-ped", metavar='pedfile', type=str, help="file.ped")
parser.add_argument("-twobitfile", metavar='tbf', type=str, help="file.2bit")
args=parser.parse_args()
samples=[]
pedfh=open(args.ped)
bedfh=open(args.bed)
tbf=bx.seq.twobit.TwoBitFile( open( args.twobitfile ) )
for line in pedfh:
samples.append( line.split('\t')[1] )
print "##fileformat=VCFv4.1"
print "##INFO=<ID=NS,Number=1,Type=Integer,Description=\"Number of Samples With Data\">"
print "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">"
print "\t".join(["#CHROM", "POS" ,"ID", "REF", "ALT", "QUAL", "FILTER", "INFO","FORMAT"]+samples)
for (chrom,start,end) in yield_bedcoordinate(bedfh):
for pos in range(start+1,end+1):
sequence=tbf[chrom][pos-1:pos]
vrec=VcfRecord(chrom,str(pos), '.', sequence,info="NS=8")
genotypelist=[ VcfGenotype("GT","0/0") for z in range(len(samples)) ]
vrec.addGenotypeList(genotypelist)
print vrec.toStringwithGenotypes()
if __name__=="__main__":
main()