-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCas9blocker.py
More file actions
executable file
·402 lines (268 loc) · 15.6 KB
/
Copy pathCas9blocker.py
File metadata and controls
executable file
·402 lines (268 loc) · 15.6 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#This is the Cas9 Blocker!
#Determine sequence match
#Locate pam site frame
#Alter the overlapping PAM sequence without changing the codon-AA
import re
import collections
from collections import defaultdict
import os
import sys
sys.path.append('/home/harrison/Downloads/pipeline-master')
import utils
import math
#parses the tab delimited table
table=utils.parseTable('/home/harrison/PPscripts/HS_tools/codon_hg19.txt', '\t')
#make the first row the key and the next 2 rows the values for that key
hg19_bias = defaultdict()
for line in table[1:]:
hg19_bias[line[0]]=[line[1],float(line[2])]
#This checks if the input is a text
if os.path.isfile(sys.argv[1]) and os.path.isfile(sys.argv[2]):
#This opens the input files
with open(sys.argv[1]) as f, open(sys.argv[2]) as g:
#This reads the file and makes the text a usuable string
dna1 = f.read().rstrip()
gRNA = g.read().rstrip()
#If the input was not a file than continue with the code
else:
dna1 = sys.argv[1]
gRNA = sys.argv[2]
pass
#This will replace all 'T's with 'U's if DNA was inputted otherwise
dna1 = dna1.replace('T', 'U')
gRNA = gRNA.replace('T', 'U')
dna1 = dna1.upper()
gRNA = gRNA.upper()
def reverse_complement(dna):
complement = {'A': 'U', 'C': 'G', 'G': 'C', 'U': 'A',}
return ''.join([complement[base] for base in dna[::-1]])
#this gives you a reverse version of the target and guide
rev_gRNA = gRNA[::-1]
revcom_dna1 = reverse_complement(dna1)
#Checks if the match is unique in the original sequence
if dna1.count(gRNA) == 1 and revcom_dna1.count(rev_gRNA) == 0:
print 'This is the target:\n'+ sys.argv[1]
print 'This is the guide:\n' + sys.argv[2]
#Finds the location of the match
for a in re.finditer(gRNA, dna1):
#This makes sure the adjacent 3 bases make a PAM site
if dna1[a.end() + 1] == 'G' and dna1[a.end() + 2] == 'G':
#This makes the PAM site a PAM variable
PAM = dna1[a.end()], dna1[a.end() + 1], dna1[a.end() + 2]
PAM = ''.join(PAM)
print 'The guide matches the sequence once at: '
print (a.start(), a.end())
#This print's out the same codon/PAM in DNA if DNA was given or leaves it as RNA if not.
if sys.argv[1].find('U') == -1:
t = PAM.replace('U', 'T')
elif sys.argv[1].find('T') == -1:
t = PAM
print t + ' is the PAM sequence'
#remainder indirectly gives postion for base 1 in PAM telling us the frame its in
if (a.end()%3) == 0:
print'The PAM is in a 123 frame'
#if 123 frame, PAM is the same as the codon that needs to be changed
print t + ' Is the overlapping codon'
if hg19_bias[PAM][0] == 'W':
print 'Cannot alter tryptophan due to only one codon'
quit()
#print out the amino acid the codon makes
print t + ' produces ' + hg19_bias[PAM][0]
#make a list of other codons that have the same amino acid
codon_list = []
for i in hg19_bias:
if hg19_bias[i][0]==hg19_bias[PAM][0]:
codon_list.append(i)
#finds a different codon with the highest codon bias
for b in codon_list:
if b == t:
codon_list.remove(b)
f = hg19_bias[PAM]
del hg19_bias[PAM]
freq_diff = 0
freq_diff_list = []
for i in codon_list:
freq_diff = f[1] - hg19_bias[i][1]
freq_diff = math.fabs(freq_diff)
freq_diff_list.append(freq_diff)
freq_diff_list.sort()
x = freq_diff_list[0]
for i in codon_list:
if math.fabs(f[1] - hg19_bias[i][1]) == x and i != 'AGG':
if sys.argv[1].find('U') == -1:
i = i.replace('U', 'T')
elif sys.argv[1].find('T') == -1:
i = i
print 'Now swapping ' + t + ' for ' + i + ' because its frequencies are close and both produce ' + f[0]
#So it is easier to read in the seqeunce
i = i.lower()
dna2 = list(dna1)
#replaces old codon overlapping PAM with new similar codon
dna2[a.end()], dna2[a.end() + 1], dna2[a.end() + 2] = i
#list becomes a string again for legibility
dna2 = ''.join(dna2)
#print out the new DNA string
if sys.argv[1].find('U') == -1:
dna2 = dna2.replace('U', 'T')
print dna2
quit()
else:
print dna2
quit()
#This asks whether the end position is in a different frame based on the positions remainder
elif (a.end()%3) == 1:
#everything after this point is the same as above for a different frame or checking the reverse complement of the target
codon_PAM = dna1[a.end() - 1], dna1[a.end()], dna1[a.end() + 1]
codon_PAM = ''.join(codon_PAM)
print'The PAM is in a 231 frame'
if sys.argv[1].find('T') == -1:
t = codon_PAM.replace('T', 'U')
elif sys.argv[1].find('U') == -1:
t = codon_PAM
print t + ' Is the overlapping codon'
print t +' produces ' + hg19_bias[codon_PAM][0]
codon_list = []
for i in hg19_bias:
if hg19_bias[i][0]==hg19_bias[codon_PAM][0]:
codon_list.append(i)
for b in codon_list:
if b == t:
codon_list.remove(b)
f = hg19_bias[codon_PAM]
del hg19_bias[codon_PAM]
freq_diff = 0
freq_diff_list = []
for i in codon_list:
freq_diff = f[1] - hg19_bias[i][1]
freq_diff = math.fabs(freq_diff)
freq_diff_list.append(freq_diff)
freq_diff_list.sort()
x = freq_diff_list[0]
for i in codon_list:
if math.fabs(f[1] - hg19_bias[i][1]) == x:
if sys.argv[1].find('U') == -1:
i = i.replace('U', 'T')
elif sys.argv[1].find('T') == -1:
i = i
print 'Now swapping ' + t + ' for ' + i + ' because its frequencies are close and both produce ' + f[0]
i = i.lower()
dna2 = list(dna1)
dna2[a.end() - 1], dna2[a.end()], dna2[a.end() + 1] = i
dna2 = ''.join(dna2)
if sys.argv[1].find('U') == -1:
dna2 = dna2.replace('U', 'T')
print dna2
else:
print 'PAM is in 321 frame. You cannot alter the PAM without altering the future peptide'
else:
print 'Target not adjacent to a PAM site'
elif dna1.count(gRNA) == 0 and revcom_dna1.count(rev_gRNA) == 1:
print 'Reversing the guide and reverse-complementing the target:'
if sys.argv[1].find('U') == -1:
rD = revcom_dna1.replace('U', 'T')
rG = rev_gRNA.replace('U', 'T')
elif sys.argv[1].find('T') == -1:
rD = revcom_dna1
rG = rev_gRNA
print 'This is the target:\n'+ rD
print 'This is the guide:\n' + rG
dna1 = revcom_dna1
gRNA = rev_gRNA
for a in re.finditer(gRNA, dna1):
if dna1[a.end() + 1] == 'G' and dna1[a.end() + 2] == 'G':
PAM = dna1[a.end()], dna1[a.end() + 1], dna1[a.end() + 2]
PAM = ''.join(PAM)
print 'The guide matches the sequence once at: '
print (a.start(), a.end())
if sys.argv[1].find('U') == -1:
t = PAM.replace('U', 'T')
elif sys.argv[1].find('T') == -1:
t = PAM
print t + ' is the PAM sequence'
if (a.end()%3) == 0:
print'The PAM is in a 123 frame'
print t + ' Is the overlapping codon'
if hg19_bias[PAM][0] == 'W':
print 'Cannot alter tryptophan due to only one codon'
else:
print t + ' produces ' + hg19_bias[PAM][0]
codon_list = []
for i in hg19_bias:
if hg19_bias[i][0]==hg19_bias[PAM][0]:
codon_list.append(i)
for b in codon_list:
if b == t:
codon_list.remove(b)
f = hg19_bias[PAM]
del hg19_bias[PAM]
freq_diff = 0
freq_diff_list = []
for i in codon_list:
freq_diff = f[1] - hg19_bias[i][1]
freq_diff = math.fabs(freq_diff)
freq_diff_list.append(freq_diff)
freq_diff_list.sort()
x = freq_diff_list[0]
for i in codon_list:
if math.fabs(f[1] - hg19_bias[i][1]) == x and i != 'AGG':
if sys.argv[1].find('U') == -1:
i = i.replace('U', 'T')
elif sys.argv[1].find('T') == -1:
i = i
print 'Now swapping ' + t + ' for ' + i + ' because its frequencies are close and both produce ' + f[0]
i = i.lower()
dna2 = list(dna1)
dna2[a.end()], dna2[a.end() + 1], dna2[a.end() + 2] = i
dna2 = ''.join(dna2)
if sys.argv[1].find('U') == -1:
dna2 = dna2.replace('U', 'T')
print dna2
else:
print dna2
elif (a.end()%3) == 1:
codon_PAM = dna1[a.end() - 1], dna1[a.end()], dna1[a.end() + 1]
codon_PAM = ''.join(codon_PAM)
print'The PAM is in a 231 frame'
if sys.argv[1].find('T') == -1:
t = codon_PAM.replace('T', 'U')
elif sys.argv[1].find('U') == -1:
t = codon_PAM
print t + ' Is the overlapping codon'
print t +' produces ' + hg19_bias[codon_PAM][0]
codon_list = []
for i in hg19_bias:
if hg19_bias[i][0]==hg19_bias[codon_PAM][0]:
codon_list.append(i)
for b in codon_list:
if b == t:
codon_list.remove(b)
f = hg19_bias[codon_PAM]
del hg19_bias[codon_PAM]
freq_diff = 0
freq_diff_list = []
for i in codon_list:
freq_diff = f[1] - hg19_bias[i][1]
freq_diff = math.fabs(freq_diff)
freq_diff_list.append(freq_diff)
freq_diff_list.sort()
x = freq_diff_list[0]
for i in codon_list:
if math.fabs(f[1] - hg19_bias[i][1]) == x:
if sys.argv[1].find('U') == -1:
i = i.replace('U', 'T')
elif sys.argv[1].find('T') == -1:
i = i
print 'Now swapping ' + t + ' for ' + i + ' because its frequencies are close and both produce ' + f[0]
i = i.lower()
dna2 = list(dna1)
dna2[a.end() - 1], dna2[a.end()], dna2[a.end() + 1] = i
dna2 = ''.join(dna2)
if sys.argv[1].find('U') == -1:
dna2 = dna2.replace('U', 'T')
print dna2
else:
print 'PAM is in 321 frame. You cannot alter the PAM without altering the future peptide'
else:
print 'Target not adjacent to a PAM site'
else:
print 'Guide does not match or is not unique'