-
Notifications
You must be signed in to change notification settings - Fork 11
/
bim_vcf.py
executable file
·56 lines (44 loc) · 1.52 KB
/
bim_vcf.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
from optparse import OptionParser
import subprocess
'''
bim_vcf.py
Convert variants in a Plink .bim file to .vcf
'''
################################################################################
# main
################################################################################
def main():
usage = 'usage: %prog [options] <in_bim> <out_vcf>'
parser = OptionParser(usage)
parser.add_option('-z', dest='zip', default=False, action='store_true')
(options,args) = parser.parse_args()
if len(args) != 2:
parser.error('Must provide input BIM and output VCF.')
else:
in_bim_file = args[0]
out_vcf_file = args[1]
# open out VCF
out_vcf_open = open(out_vcf_file, 'w')
# print header
print('##fileformat=VCFv4.2', file=out_vcf_open)
cols = ['#CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO']
print('\t'.join(cols), file=out_vcf_open)
# parse BIM
for line in open(in_bim_file):
a = line.split()
chrom = a[0]
snp_id = a[1]
pos = a[3]
a1 = a[4]
a2 = a[5]
cols = [chrom, pos, snp_id, a2, a1, '.', '.', '.']
print('\t'.join(cols), file=out_vcf_open)
out_vcf_open.close()
if options.zip:
subprocess.call('gzip -f %s' % out_vcf_file, shell=True)
################################################################################
# __main__
################################################################################
if __name__ == '__main__':
main()