-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdebreak_genotype.py
More file actions
272 lines (248 loc) · 6.67 KB
/
Copy pathdebreak_genotype.py
File metadata and controls
272 lines (248 loc) · 6.67 KB
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
import time
import pysam
def genotype_filter_del(samfile,readpath,chrom,highcov):
samfile=pysam.AlignmentFile(samfile,"rb")
alldel=open(readpath,'r').read().split('\n')[:-1]
filt='-gt'
if chrom!='all':
alldel=[c for c in alldel if c.split('\t')[0]==chrom]
if alldel==[]:
return 0
filt='-gt-'+chrom
f=open(readpath+filt,'w')
for c in alldel:
chrom=c.split('\t')[0]
start=int(c.split('\t')[1])
stop=int(c.split('\t')[2])+start
svsize=int(c.split('\t')[2])
numsupp=int(c.split('\t')[3])
leftcov=samfile.count(chrom,start-150,start-50)
if leftcov>highcov*2:
continue
rightcov=samfile.count(chrom,stop+50,stop+150)
if rightcov>highcov*2:
continue
localcov=max(leftcov,rightcov)
'''
if localcov > highcov:
f.write(c+'\tHighCov\n')
'''
if localcov<=highcov:
f.write(c+'\tPASS\n')
f.close()
return True
def genotype_del(samfile,readpath,chrom,highcov):
samfile=pysam.AlignmentFile(samfile,"rb")
alldel=open(readpath,'r').read().split('\n')[:-1]
filt='-gt'
if chrom!='all':
alldel=[c for c in alldel if c.split('\t')[0]==chrom]
if alldel==[]:
return 0
filt='-gt-'+chrom
f=open(readpath+filt,'w')
for c in alldel:
chrom=c.split('\t')[0]
start=int(c.split('\t')[1])
stop=int(c.split('\t')[2])+start
svsize=int(c.split('\t')[2])
numsupp=int(c.split('\t')[3])
if numsupp > highcov:
#f.write(c+'\tGT=1/0\tHighCov\n')
continue
leftcov=samfile.count(chrom,max(0,start-150),start-50)
if leftcov>highcov*2:
continue
rightcov=samfile.count(chrom,max(0,stop+50),stop+150)
if rightcov>highcov*2:
continue
localcov=max(leftcov,rightcov)
'''
if localcov > highcov:
if numsupp>=0.6*localcov:
f.write(c+'\tGT=1/1\tHighCov\n')
if numsupp<0.6*localcov and numsupp>=0.2*localcov:
f.write(c+'\tGT=1/0\tHighCov\n')
'''
if localcov<=highcov:
if numsupp>=0.6*localcov:
f.write(c+'\tGT=1/1\n')
else:
f.write(c+'\tGT=1/0\n')
f.close()
return True
def genotype_filter_ins(samfile,readpath,chrom,highcov):
samfile=pysam.AlignmentFile(samfile,"rb")
alldel=open(readpath,'r').read().split('\n')[:-1]
filt='-gt'
if chrom!='all':
alldel=[c for c in alldel if c.split('\t')[0]==chrom]
if alldel==[]:
return 0
filt='-gt-'+chrom
f=open(readpath+filt,'w')
for c in alldel:
chrom=c.split('\t')[0]
start=int(c.split('\t')[1])
numsupp=int(c.split('\t')[3])
#if numsupp > highcov:
#f.write(c+'\tHighCov\n')
# continue
leftcov=samfile.count(chrom,start-150,start-50)
if leftcov>highcov*2:
continue
rightcov=samfile.count(chrom,start+50,start+150)
if rightcov>highcov*2:
continue
localcov=max(leftcov,rightcov)
'''
if localcov > highcov:
f.write(c+'\tHighCov\n')
'''
if localcov<=highcov:
f.write(c+'\tPASS\n')
f.close()
return True
def genotype_ins(samfile,readpath,chrom,highcov):
samfile=pysam.AlignmentFile(samfile,"rb")
alldel=open(readpath,'r').read().split('\n')[:-1]
filt='-gt'
if chrom!='all':
alldel=[c for c in alldel if c.split('\t')[0]==chrom]
if alldel==[]:
return 0
filt='-gt-'+chrom
f=open(readpath+filt,'w')
for c in alldel:
chrom=c.split('\t')[0]
start=int(c.split('\t')[1])
numsupp=int(c.split('\t')[3])
svsize=int(c.split('\t')[2])
if numsupp > highcov:
#f.write(c+'\tGT=0/1\tHighCov\n')
continue
leftcov=samfile.count(chrom,start-150,start-50)
if leftcov>highcov*2:
continue
rightcov=samfile.count(chrom,start+50,start+150)
if rightcov>highcov*2:
continue
localcov=max(leftcov,rightcov)
if localcov<=highcov:
if numsupp>=0.6*localcov:
f.write(c+'\tGT=1/1\n')
continue
allalignment=samfile.fetch(chrom,start-1,start+1)
numsupp2=0
localcov2=0
for align in allalignment:
if align.flag!=0:
continue
if int(svsize)<500 and abs(align.reference_start-start)<=100 or abs(start-align.reference_end)<=100:
continue
localcov2+=1
cigar=align.cigartuples
readstart=align.reference_start
readlen=0
for pair in cigar:
if pair[0] in [0,2]:
readlen+=pair[1]; continue
if pair[0] == 1:
if 0.7<=pair[1]/float(svsize)<=1.43 and abs(readstart+readlen-start)<=500:
numsupp2+=1; break
if int(svsize)>=500:
if abs(readstart-start)<=250 and cigar[0][0] in [4,5]:
numsupp2+=1; continue
if abs(start-align.reference_end)<=250 and cigar[-1][0] in [4,5]:
numsupp2+=1
if localcov<= highcov and localcov2<= highcov:
if numsupp2>=0.6*localcov2 or ( svsize>=500 and numsupp2>=0.5*localcov2):
f.write(c+'\tGT=1/1\n')
else:
f.write(c+'\tGT=1/0\n')
f.close()
return True
def genotype_filter_tra(samfile,readpath,chrom,highcov):
samfile=pysam.AlignmentFile(samfile,"rb")
alldel=open(readpath,'r').read().split('\n')[:-1]
filt='-gt'
if chrom!='all':
filt='-gt-'+chrom
alldel=[c for c in alldel if c.split('\t')[0]==chrom]
if alldel==[]:
return 0
f=open(readpath+filt,'w')
for c in alldel:
chr1=c.split('\t')[0]
bp1=int(c.split('\t')[1])
chr2=c.split('\t')[2]
bp2=int(c.split('\t')[3])
supp=int(c.split('\t')[4])
#if supp >highcov:
#f.write(c+'\tHighCov\n')
# continue
leftcov1=samfile.count(chr1,bp1-150,bp1-50)
if leftcov1>highcov*2:
continue
rightcov1=samfile.count(chr1,bp1+50,bp1+150)
if rightcov1>2*highcov:
continue
leftcov2=samfile.count(chr2,bp2-150,bp2-50)
if leftcov2>2*highcov:
continue
rightcov2=samfile.count(chr2,bp2+50,bp2+150)
if rightcov2>2*highcov:
continue
local_cov=max(leftcov1,rightcov1,leftcov2,rightcov2)
'''
if local_cov>highcov:
f.write(c+'\tHighCov\n')
'''
if local_cov<=highcov:
f.write(c+'\tPASS\n')
f.close()
return True
def genotype_tra(samfile,readpath,chrom,highcov):
samfile=pysam.AlignmentFile(samfile,"rb")
alldel=open(readpath,'r').read().split('\n')[:-1]
filt='-gt'
if chrom!='all':
alldel=[c for c in alldel if c.split('\t')[0]==chrom]
filt='-gt-'+chrom
if alldel==[]:
return 0
f=open(readpath+filt,'w')
for c in alldel:
chr1=c.split('\t')[0]
bp1=int(c.split('\t')[1])
chr2=c.split('\t')[2]
bp2=int(c.split('\t')[3])
supp=int(c.split('\t')[4])
if supp >highcov:
#f.write(c+'\tGT=1/0HighCov\n')
continue
leftcov=samfile.count(chr1,bp1-150,bp1-50)
if leftcov>2*highcov:
continue
rightcov=samfile.count(chr1,bp1+50,bp1+150)
if rightcov>2*highcov:
continue
local_cov=max(rightcov,leftcov)
leftcov=samfile.count(chr2,bp2-150,bp2-50)
if leftcov>2*highcov:
continue
rightcov=samfile.count(chr2,bp2+50,bp2+150)
if rightcov>2*highcov:
continue
local_cov=max(local_cov,rightcov,leftcov)
filt=''
'''
if local_cov>highcov:
filt='\tHighCov'
'''
if local_cov<=highcov:
if supp>=0.6*local_cov:
f.write(c+'\tGT=1/1'+filt+'\n')
else:
f.write(c+'\tGT=1/0'+filt+'\n')
return True