Skip to content

Commit 4dc2440

Browse files
author
zhangrui2018@picb.ac.cn
committed
vcf2hap.py
1 parent 1ff831c commit 4dc2440

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

vcf2hap.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import argparse
2+
from numpy import *
3+
4+
def vcf2hap(input, chr, output):
5+
f = open(input+".vcf", 'r')
6+
g1 = open(output+".hap", 'w')
7+
g2 = open(output+".ind", 'w')
8+
g3 = open(output+".pos", 'w')
9+
10+
A = []
11+
if chr != 'X':
12+
for a in f:
13+
if a[0] == "#" and a[1] == "C":
14+
a = a.split()
15+
for i in range(9, len(a)):
16+
g2.write(a[i]+"\n")
17+
elif a[0] != "#":
18+
a = a.split()
19+
g3.write(a[1]+"\t"+a[3]+"\t"+a[4]+"\n")
20+
A1 = []
21+
for i in range(9, len(a)):
22+
A1.append(a[i][0])
23+
A1.append(a[i][2])
24+
A.append(A1)
25+
else:
26+
for a in f:
27+
if a[0] == "#" and a[1] == "C":
28+
a = a.split()
29+
for i in range(9, len(a)):
30+
g2.write(a[i]+"\n")
31+
elif a[0] != "#":
32+
a = a.split()
33+
g3.write(a[1]+"\t"+a[3]+"\t"+a[4]+"\n")
34+
A1 = []
35+
for i in range(9, len(a)):
36+
A1.append(a[i][0])
37+
A1.append('9')
38+
A.append(A1)
39+
40+
AM = mat(A)
41+
AM = AM.T
42+
43+
44+
for i in range(0, AM.shape[0]):
45+
for j in range(0, AM.shape[1]):
46+
g1.write(str(AM[i,j]))
47+
g1.write("\n")
48+
49+
print("Done")
50+
51+
f.close()
52+
g1.close()
53+
g2.close()
54+
g3.close()
55+
56+
if __name__ == '__main__':
57+
parser = argparse.ArgumentParser()
58+
parser.add_argument("--input", type = str, required = True, \
59+
help = "Prefix of input vcf")
60+
parser.add_argument("--chr", type = str, required = True, \
61+
help = "chromosome label, 1,2,...,22,X")
62+
parser.add_argument("--output", type = str, required = True, \
63+
help = "Prefix of output file(.hap, .ind, .pos)")
64+
65+
args = parser.parse_args()
66+
if args.chr not in ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','X']:
67+
print("Error: not correct chr!")
68+
exit()
69+
70+
vcf2hap(args.input, args.chr, args.output)
71+

0 commit comments

Comments
 (0)