-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVcf_extract_white.py
137 lines (110 loc) · 3.49 KB
/
Vcf_extract_white.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Extract lines whites only
Remove variants that were not present in the whites
"""
import os, sys, gzip, datetime
def main(chrom):
infile = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_wellderly_inova_withCorrectGenotypes/wellderly_inova." + chrom + ".vcf.gz"
outfile = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_wellderly_inova_whiteOnly/wellderly_inova.0.95white." + chrom + ".vcf.gz"
whitesfile = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_wellderly_inova_withCorrectGenotypes/White_0.95.txt"
filterfile = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_wellderly_inova_whiteOnly/filter.0.95white." + chrom + ".vcf.gz"
counter_file = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_wellderly_inova_whiteOnly/Whites_only_counter.txt"
w = open(whitesfile)
whites_id = []
header = []
#index the whites
ln = w.readline()
ln = ln.strip()
#Create dictionary here instead
whites_id = ln.split()
i = gzip.open(infile)
o = gzip.open(outfile, 'w')
#clus = gzip.open(clusteredfile, 'w')
filt =gzip.open(filterfile, 'w')
counterf = open(counter_file, 'a')
#counterf.write("CHROM\tOriginalLines\tFilteredLines\n")
filt.write("#CHROM\tPOS\tID\tREF\tALT\tNot_white\n")
counter_white = 0
counter = 0
good_counter = 0
buffe = ""
buffe_filter = ""
for line in i:
if line[:2] == "##":
print "header"
o.write(line)
#Extract only the white individuals ID
elif line[0] == "#":
#o.write(line)
line = line.strip()
header = line.split("\t")
final_header = "\t".join(header[:9])
for index, gen in enumerate(header[9:]):
index = index +9
#Extract whites only
if gen in whites_id:
final_header = final_header + "\t" + gen
counter_white += 1
final_header = final_header + "\n"
o.write(final_header)
print "# of whites " + str(counter_white)
else:
counter += 1
line = line.strip()
tp_line = line.split("\t")
filter_line = "\t".join(tp_line[:5])
filter_line = filter_line + "\t"
white_line = "\t".join(tp_line[:9])
#count_id = 0
for index, gen in enumerate(tp_line[9:]):
index = index +9
indiv_name = header[index]
if indiv_name in whites_id:
#count_id += 1
white_line = white_line + "\t" + gen
#check if the new line with whites only have any alternate alleles
#(if there are any alternate alleles they have VQHIGH or VQLOW)
if 'VQHIGH' in white_line or 'VQLOW' in white_line:
good_counter += 1
buffe = buffe + white_line + "\n"
#Don't add anything
filter_line = filter_line + "\t\n"
else:
filter_line = filter_line + "\tYES\n"
buffe_filter = buffe_filter + filter_line
if good_counter%100 == 0:
o.write(buffe)
buffe = ""
filt.write(buffe_filter)
buffe_filter = ""
o.flush()
filt.flush()
if counter%10000 ==0:
print "Total lines"
print str(counter)
print "Good lines"
print str(good_counter)
sys.stdout.flush()
o.write(buffe)
i.close()
o.close()
filt.write(buffe_filter)
#clus.close()
print "Total lines"
print str(counter)
print "Good lines"
print str(good_counter)
print "End"
t = chrom + "\t" + str(counter) + "\t" + str(good_counter) + "\n"
counterf.write(t)
counterf.close()
filt.close()
if __name__ == '__main__':
print "Python Version: " + sys.version
#args = parse_args()
#import time
#start = time.time()
main(sys.argv[1])
#main(sys.argv[1], sys.argv[2], sys.argv[3])
#end = time.time()
#print 'This took {0} seconds'.format(end - start)