-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_all_candidates.py
95 lines (78 loc) · 1.94 KB
/
list_all_candidates.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
import sys
f_target=sys.argv[1]
with open(f_target, 'r') as myfile:
target=myfile.read().replace('\n', '')
PAM=str(sys.argv[2])
gRNA_len=int(sys.argv[3])
f2 = open(sys.argv[4], 'w')
#print target
#~ target = "AAAAAAAAA"
#~ PAM = "AA"
#~ gRNA_len = 2
class NFiller:
def __init__(self, PAM):
self.PAM = PAM
self.bases= ['A', 'C', 'G', 'T']
def findall(self, p, s):
'''Yields all the positions of
the pattern p in the string s.'''
i = s.find(p)
while i != -1:
yield i
i = s.find(p, i+1)
def recursive_N_filler(self, whole, lst, ipositions):
if (not ipositions) :
return lst
for c in self.bases:
lst2 = list(lst)
lst2[ipositions[-1]] = c
ip = ipositions[:-1]
whole.append(self.recursive_N_filler(whole, lst2, ip))
def get_list(self):
PAM = self.PAM
ipositions = [index for index in self.findall("N", PAM)]
if(not ipositions):
return [PAM]
PAMlist = list(PAM)
whole = []
self.recursive_N_filler(whole, PAMlist, ipositions)
w = [x for x in whole if x != None]
#print w
#print(len(w))
s = [''.join(x) for x in w]
return s
def findall(p, s):
'''Yields all the positions of
the pattern p in the string s.'''
i = s.find(p)
while i != -1:
yield i
i = s.find(p, i+1)
def find_candidates(target, PAM, gRNA_len):
"""
#~ Say,
#~ target: ATATATATGCATATAGCTATAGCATGCAT
#~ pAM: TGC
#~ gRNA_len: 5
"""
ww = [(i-gRNA_len, target[i-gRNA_len:i+len(PAM)]) for i in findall(PAM, target)]
return [x for x in ww if x[0] >= 0]
#[(i-gRNA_len, target[i-gRNA_len:i+len(PAM)]) for i in findall(PAM, target)]
PAMs = NFiller(PAM).get_list()
#print PAMs
candidates = []
for PAM in PAMs:
candidates.extend(find_candidates(target, PAM, gRNA_len))
#print candidates
dic = {}
for candidate in candidates:
key = candidate[1]
if key in dic:
dic[key] += 1
else:
dic[key] = 1
#print dic
#print candidates
for i in candidates:
f2.write(i[1]+" "+ str(dic[i[1]]) +'\n')
f2.close()