forked from ranjit58/NGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_all_vcf.py
executable file
·64 lines (52 loc) · 1.79 KB
/
merge_all_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
57
58
59
60
61
62
63
64
#!/usr/bin/python
# The program will take any number of vcf files and merge them based on similar coordintes. Each input file become a column and each row shows if a coordinate is present in that column or not
# usage : program.py vcf1 vcf2 vcf3 vcf4 (as many input file as you want)
# the program will produce a output "merged.vcf"
import re
import sys
dict = {}
tmp = 0
# list sys.argv[2] holds all the commandline arguments
# the code reads all the input files and create a merged dictionary of coordinates
for i in range(1,len(sys.argv)):
print "reading file", sys.argv[i]
input = open(sys.argv[i],"r")
for line in input:
if re.match('##|#',line):
tmp = 0
else:
line=line.rstrip('\n')
linesplit = re.split('\t',line)
dict[linesplit[1]] = ""
input.close()
# each file is individually read to create a temporary dictionary
# the merged dictionary is search for each temp dictionary again and output is stored/updated as the value of merged dictionary key
for i in range(1,len(sys.argv)):
print "reading file", sys.argv[i]
input = open(sys.argv[i],"r")
temp_dict = {}
for line in input:
if re.match('##|#',line):
tmp = 0
else:
line=line.rstrip('\n')
linesplit = re.split('\t',line)
temp_dict[linesplit[1]] = line
input.close()
for i in dict.keys():
if temp_dict.has_key(i):
#dict[i] = dict[i] + "@1@" + temp_dict[i]
dict[i] = dict[i] + "\t1"
else:
#dict[i] = dict[i] + "@0@"
dict[i] = dict[i] + "\t0"
input.close()
output = open("merged.vcf","w")
# write header
header = ""
for i in range(1,len(sys.argv)):
header = header + "\t" + str(sys.argv[i] )
output.write("Coordinate" + header +"\n")
for i in sorted(dict.keys()):
output.write( str(i) + "\t" + str(dict[str(i)]) + "\n")
# tmp = 0