-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_clustered.py
282 lines (236 loc) · 6.69 KB
/
filter_clustered.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
Filter clustered variants present in 10 percent of wellderly/inova samples
"""
import os, sys, gzip, datetime
counter_alleles_inova = 0
counter_alleles_wellderly = 0
well_001 = 0
well_005 = 0
well_005_plus = 0
inova_001 = 0
inova_005 = 0
inova_005_plus = 0
DID_start = 0
Inova_start = 0
total_DID = 0
total_ASM = 0
def count_AF(line):
tp_line = line.split("\t")
alleles = tp_line[4].split(",")
total_well_alleles = 0
total_inova_alleles = 0
total_well_alt = 0
total_inova_alt = 0
dict_of_alleles_well={}
dict_of_alleles_inova={}
well_AF = 0
inova_AF = 0
if len(alleles) > 1:
#print "len alleles > 1"
#create a dictionary that would count the number of alleles
for index, i in enumerate(alleles):
#add one to the index, alleles start at 1
ind = index + 1
dict_of_alleles_well[ind] = 0
dict_of_alleles_inova[ind] = 0
for index, gen in enumerate(tp_line[9:]):
index = index + 9
if len(alleles) == 1:
#tp_gen = gen[:3]
#First Allele
if gen[0] == "0":
if index < 529:
total_well_alleles += 1
else:
total_inova_alleles += 1
elif gen[0] == "1":
if index < 529:
total_well_alleles += 1
total_well_alt +=1
else:
total_inova_alleles += 1
total_inova_alt +=1
#Second Allele
if gen[2] == "0":
if index < 529:
total_well_alleles += 1
else:
total_inova_alleles += 1
elif gen[2] == "1":
if index < 529:
total_well_alleles += 1
total_well_alt +=1
else:
total_inova_alleles += 1
total_inova_alt +=1
else:
#print "Multi alleles"
#First Allele
if gen[0] != ".":
if gen[0] == "0":
if index < 529:
total_well_alleles += 1
else:
total_inova_alleles += 1
else:
if index < 529:
total_well_alleles += 1
#Increment the dictionary value for that allele
try:
dict_of_alleles_well[int(gen[0])] +=1
except:
print "gen[0] " + str(gen[0])
print "alleles " + tp_line[4]
else:
total_inova_alleles += 1
try:
dict_of_alleles_inova[int(gen[0])] +=1
except:
print "gen[0] " + str(gen[0])
print "alleles " + tp_line[4]
#Second Allele
if gen[2] != ".":
if gen[2] == "0":
if index < 529:
total_well_alleles += 1
else:
total_inova_alleles += 1
else:
if index < 529:
total_well_alleles += 1
#Increment the dictionary value for that allele
try:
dict_of_alleles_well[int(gen[2])] +=1
except:
print "gen[0] " + str(gen[2])
print "alleles " + tp_line[4]
else:
total_inova_alleles += 1
try:
dict_of_alleles_inova[int(gen[2])] +=1
except:
print "gen[0] " + str(gen[2])
print "alleles " + tp_line[4]
inova_max_value = max(dict_of_alleles_inova.values())
well_max_value = max(dict_of_alleles_well.values())
try:
if len(alleles) == 1:
well_AF = float(total_well_alt)/float(total_well_alleles)
else:
# For multi allelic values remove the highest AF from 1
well_AF = 1 - float(well_max_value)/float(total_well_alleles)
#print str(AF)
except:
well_AF = 0.0
try:
if len(alleles) == 1:
inova_AF = float(total_inova_alt)/float(total_inova_alleles)
else:
inova_AF = 1 - float(inova_max_value)/float(total_inova_alleles)
#print str(AF)
except:
inova_AF = 0.0
if well_AF < 0.01:
well_001 += 1
elif well_AF < 0.05:
well_005 += 1
else:
well_005_plus += 1
if inova_AF < 0.01:
inova_001 += 1
elif inova_AF < 0.05:
inova_005 += 1
else:
inova_005_plus +=1
def main(chrom):
infile = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_filtered_VQHIGH_whiteOnly/wellderly_inova.VQHIGH.0.95white." + chrom + ".vcf.gz"
outfile = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_filtered_VQHIGH_whiteOnly_clustered/clustered." + chrom + ".vcf.gz"
counter_file = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_filtered_VQHIGH_whiteOnly_clustered/cluster_filters_counter.txt"
filter_file = "/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_filtered_VQHIGH_whiteOnly_clustered/filters.clustered." + chrom + ".vcf.gz"
inf = gzip.open(infile)
o = gzip.open(outfile, 'w')
filt = gzip.open(filter_file, 'w')
counterf = open(counter_file, 'a')
#counterf.write("CHROM\tOriginalLines\tFilteredLines\n")
filt.write("#CHROM\tPOS\tID\tREF\tALT\tCluster\n")
counterf.write("\t\t\tWellderly\t\t\tInova\n")
counterf.write("AF\tTotal Var\t Filtered Var\t<0.01\t0.01-0.05\t>0.05\t<0.01\t0.01-0.05\t>0.05\n")
counter = 0
filter_counter = 0
buffe_filter = ""
buffe = ""
for line in inf:
if line[:2] == "##":
print "header"
o.write(line)
#Extract only the white individuals ID
elif line[0] == "#":
print "header"
o.write(line)
else:
counter += 1
line = line.strip()
tp_line = line.split("\t")
filter_line = "\t".join(tp_line[:5])
if line[-2:] == "::":
filter_line = filter_line + "\t\n"
else:
clusters = line.split(":")
well_clust = []
inova_clust = []
try:
count_elements = len(clusters)
#print "Elements counted " + str(count_elements)
well_clust = clusters[count_elements-1].split(",")
inova_clust = clusters[count_elements-2].split(",")
#print "Length clustered inova: " + str(len(inova_clust))
except:
print "Except"
#If more then 10% in either wellderly or inova clusetered together
if len(well_clust) > 60 or len(inova_clust) >150:
#print "found"
filter_counter = filter_counter + 1
filter_line = filter_line + "\tYES\n"
count_AF(line)
else:
filter_line = filter_line + "\t\n"
buffe = buffe + line
buffe_filter = buffe_filter + filter_line
if filter_counter%100 == 0:
filt.write(buffe_filter)
buffe_filter = ""
filt.flush()
o.write(buffe)
buffe = ""
o.flush()
if counter%10000 ==0:
print "Total lines"
print str(counter)
print "Good lines"
print str(filter_counter)
sys.stdout.flush()
filt.write(buffe_filter)
o.write(buffe)
#clus.close()
print "Total lines"
print str(counter)
print "Good lines"
print str(filter_counter)
print "End"
AF_counter = chrom + "\t" + str(counter) + "\t" + str(counter_unique_no_pass) + "\t" + str(well_001) + "\t" + str(well_005) + "\t" + str(well_005_plus) + \
"\t" + str(inova_001) + "\t" + str(inova_005) + "\t" + str(inova_005_plus)
print AF_counter
counterf.write(AF_counter)
counterf.close()
inf.close()
o.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)