-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_36kmer.py
313 lines (228 loc) · 8.65 KB
/
remove_36kmer.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""
Remove ALL varinats from repeatMask
RepeatMasker
"""
import os, sys, gzip, datetime
class Overlap(object):
def __init__(self,arr1Start,arr2Start,arr1End,arr2End):
## Two sets of start and end sites. Each start and end define the
## various islands
self.a1s = arr1Start
self.a2s = arr2Start
self.a1e = arr1End
self.a2e = arr2End
def main(self):
a1s = self.a1s
a2s = self.a2s
a1e = self.a1e
a2e = self.a2e
overlap_array = self.overlapCounts(a1s,a2s,a1e,a2e)
return overlap_array
#Overlaping one begin and end with and array of overlaps (a2s and a2e)
def overlapCounts(self,a1s,a2s,a1e,a2e):
#AllOverlaps = []
#i = 0
#while i < len(a1s):
totalOverlap = 0
#beg1 = a1s[i]
#end1 = a1e[i]
beg1 = int(a1s)
end1 = int(a1e)
indx = self.insert_pos(a2s,a2s,beg1)
if indx == "ERROR":
return False
try:
beg2 = a2s[indx]
end2 = a2e[indx]
except IndexError:
indx = indx-1
beg2 = a2s[indx]
end2 = a2e[indx]
overlap = self.overlapNumber(beg1,end1,beg2,end2)
totalOverlap += overlap
flagF, indxF = True, indx
flagB, indxB = True, indx
while flagF:
overlap = False
indxF += 1
try:
beg2 = a2s[indxF]
end2 = a2e[indxF]
except IndexError:
flagF = False
break
overlap = self.overlapNumber(beg1,end1,beg2,end2)
if overlap:
return True
#totalOverlap += overlap
else:
flagF = False
while flagB:
overlap = False
indxB -= 1
if indxB < 0:
break
try:
beg2 = a2s[indxB]
end2 = a2e[indxB]
except IndexError:
flagB = False
break
overlap = self.overlapNumber(beg1,end1,beg2,end2)
if overlap:
return True
#totalOverlap += overlap
else:
flagB = False
#i += 1
#print (AllOverlaps)
return False
def insert_pos(self,arrI,arr,value):
## arrI - to get the final index
## arr - the array to work on, same as arrI
if len(arr) == 1:
if value > arr[0]:
return arrI.index(arr[0])+1
else:
return arrI.index(arr[0])
else:
arr_mid = len(arr)/2
arrLow = arr[:arr_mid]
arrHigh = arr[arr_mid:]
#print arrLow
#try:
if value>arrLow[-1]:
out = self.insert_pos(arrI,arrHigh,value)
else:
out = self.insert_pos(arrI,arrLow,value)
return out
def overlapPercent(self,s1,e1,s2,e2):
## The function which calculates the overlap between two isalnds
##
## seq1 s1---------------------e1
## seq2 s2---------------------e2
##
## seq1 s1----------------------------e1
## aeq2 s2--------------e2
##
## seq1 s1--------------------e1
## aeq2 s2----------------e2
##
## seq1 s1-----------e1
## aeq2 s2-----------------------------e2
##
if (s1<=s2) and (e1<e2) and (e1>=s2):
return True
elif (s1<=s2) and (e1>=e2):
return True
elif (s1>s2) and (s1<=e2) and (e1>=e2):
return True
elif (s1>s2) and (e1<e2):
return True
else:
return False
'''
seqL1 = float(e1-s1+1)
seqL2 = float(e2-s2+1)
if (s1<=s2) and (e1<e2) and (e1>=s2):
overlap = float(e1-s2+1)
ovrPerc = min((overlap/seqL1),(overlap/seqL2))*100.0
elif (s1<=s2) and (e1>=e2):
overlap = seqL2
ovrPerc = (overlap/seqL1)*100.0
elif (s1>s2) and (s1<=e2) and (e1>=e2):
overlap = float(e2-s1+1)
ovrPerc = min((overlap/seqL1),(overlap/seqL2))*100.0
elif (s1>s2) and (e1<e2):
overlap = seqL1
ovrPerc = (overlap/seqL2)*100.0
else:
ovrPerc = 0.0
return ovrPerc
'''
def overlapNumber(self,s1,e1,s2,e2):
seqL1 = float(e1-s1+1)
seqL2 = float(e2-s2+1)
if (s1<=s2) and (e1<e2) and (e1>=s2):
overlap = float(e1-s2+1)
elif (s1<=s2) and (e1>=e2):
overlap = seqL2
elif (s1>s2) and (s1<=e2) and (e1>=e2):
overlap = float(e2-s1+1)
elif (s1>s2) and (e1<e2):
overlap = seqL1
else:
overlap = 0.0
return overlap
def main(chrom):
ch = "1"
#RepeatMasker_filename="/gpfs/home/gerikson/wellderly/filter_data/RepeatMasker/byChrom_ALL/RepeatMasker."+chrom+".txt"
kmer_file="/gpfs/home/gerikson/wellderly/filter_data/36kmer/byChrom/unique_36mer" + str(chrom) + ".txt"
input_filename="/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_snps_AFmore0.01/vcf_snps_AF0.01."+str(chrom)+".vcf.gz"
output_filename="/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_allFilters_36kmer_snpsOnly_AF0.01/vcf_nokmer_snps_AF0.01."+str(chrom)+".vcf.gz"
counter_file="/gpfs/group/stsi/data/projects/wellderly/GenomeComb/vcf_allFilters_36kmer_snpsOnly_AF0.01/counter.txt"
c = open(counter_file, "a")
repeatMask_begin = []
repeatMask_end = []
print 'Prepping RepeatMask...'
count_rep = 0
print 'Prepping RepeatMask...'
with open(kmer_file) as f:
for line in f:
items = line.split('\t')
repeatMask_begin.append(int(items[1]))
repeatMask_end.append(int(items[2]))
print "Total repeats " + str(count_rep)
print 'Calculating...'
counter = 0
good_lines = 0
repeatMask_counter = 0
block = ""
f = gzip.open(input_filename)
o = gzip.open(output_filename, 'w')
#filter_block = ""
for line in f:
if line[:1] == "#":
print "header"
o.write(line)
continue
counter += 1
if counter%100 == 0:
o.write(block)
#filt.write(filter_block)
block = ""
#filter_block = ""
o.flush()
if counter%10000 == 0:
print datetime.datetime.now().time()
print "total lines " + str(counter)
print "Good lines " + str(good_lines)
sys.stdout.flush()
tp_line = line.strip().split("\t")
begin = int(tp_line[1])
end = int(tp_line[1]) + 1
#Add RepeatMask
repOverlap = Overlap(begin, repeatMask_begin, end, repeatMask_end)
overlapRep = repOverlap.main()
if overlapRep:
block = block + line
good_lines += 1
continue
#filt.write(filter_block)
o.write(block)
#print "Total g counter " + str(repeatMask_counter)
print "Total lines " + str(counter)
print "Total good lines " + str(good_lines)
c.write(chrom+"\t"+str(counter)+"\t"+str(good_lines)+"\n")
c.close()
f.close()
o.close()
#filt.close()
if __name__ == '__main__':
print "Python Version: " + sys.version
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)