-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motifs.py
356 lines (244 loc) · 7.18 KB
/
Motifs.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
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
import random
"""
Input: A set of kmers Motifs
Output: The count matrix of Motifs, as a dictionary
"""
def Count(Motifs):
count = {}
k = len(Motifs[0])
for symbol in "ACGT":
count[symbol] = []
for j in range(k):
count[symbol].append(0)
t = len(Motifs)
for i in range(t):
for j in range(k):
symbol = Motifs[i][j]
count[symbol][j] += 1
return count
"""
Input: A list of kmers Motifs
Output: the profile matrix of Motifs, as a dictionary of lists.
"""
def Profile(Motifs):
t = len(Motifs)
k = len(Motifs[0])
profile = {}
count = Count(Motifs)
profile = count
for symbol in "ACGT":
for j in range(k):
profile[symbol][j] = count[symbol][j] / float(t)
return profile
"""
Input: A set of kmers Motifs
Output: A consensus string of Motifs.
"""
def Consensus(Motifs):
k = len(Motifs[0])
count = Count(Motifs)
consensus = ""
for j in range(k):
m = 0
frequentSymbol = ""
for symbol in "ACGT":
if count[symbol][j] > m:
m = count[symbol][j]
frequentSymbol = symbol
consensus += frequentSymbol
return consensus
"""
Input: A set of k-mers Motifs
Output: The score of these k-mers.
"""
def Score(Motifs):
t = len(Motifs)
k = len(Motifs[0])
consensus = Consensus(Motifs)
count = Count(Motifs)
score = 0
for j in range(k):
symbol = consensus[j]
value = count[symbol][j]
score += t - value
return score
"""
Input: String Text and profile matrix Profile
Output: Pr(Text, Profile)
"""
def Pr(Text, Profile):
prob = 1
for i, symbol in enumerate(Text):
prob *= Profile[symbol][i]
return prob
"""
Profile-most Probable k-mer Problem: Find a Profile-most probable k-mer in a string.
Input: A string Text, an integer k, and a 4 x k matrix Profile.
Output: A Profile-most probable k-mer in Text.
"""
def ProfileMostProbableKmer(Text, k, Profile):
pr = {}
most_prob = []
n = len(Text)
for i in range(n-k+1):
k_mer = Text[i:i+k]
probability = Pr(k_mer, Profile)
pr[k_mer] = probability
m = max(pr.values())
for i, value in pr.items():
if value == m:
most_prob.append(i)
return most_prob[0]
"""
Input: A list of kmers Dna, and integers k and t (where t is the number of kmers in Dna)
Output: GreedyMotifSearch(Dna, k, t)
"""
def GreedyMotifSearch(Dna, k, t):
BestMotifs = []
for i in range(0, t):
BestMotifs.append(Dna[i][0:k])
n = len(Dna[0])
for i in range(n-k+1):
Motifs = []
Motifs.append(Dna[0][i:i+k])
for j in range(1, t):
P = Profile(Motifs[0:j])
Motifs.append(ProfileMostProbableKmer(Dna[j], k, P))
if Score(Motifs) < Score(BestMotifs):
BestMotifs = Motifs
return BestMotifs
"""
Input: A set of kmers Motifs
Output: CountWithPseudocounts(Motifs)
"""
def CountWithPseudocounts(Motifs):
count = {}
t = len(Motifs)
k = len(Motifs[0])
for symbol in "ACGT":
count[symbol] = []
for j in range(k):
count[symbol].append(0)
for i in range(t):
for j in range(k):
symbol = Motifs[i][j]
count[symbol][j] += 1
for symbol in "ACGT":
for j in range(k):
count[symbol][j] += 1
return count
"""
Input: A set of kmers Motifs
Output: ProfileWithPseudocounts(Motifs)
"""
def ProfileWithPseudocounts(Motifs):
k = len(Motifs[0])
total = 0
profile = {}
count = CountWithPseudocounts(Motifs)
profile = count
for symbol in "ACGT":
total += count[symbol][0]
for symbol in "ACGT":
for j in range(k):
profile[symbol][j] /= float(total)
return profile
"""
Input: A list of kmers Dna, and integers k and t (where t is the number of kmers in Dna)
Output: GreedyMotifSearch(Dna, k, t)
"""
# Include these lines to have WithPseudocounts versions be used:
Count = CountWithPseudocounts
Profile = ProfileWithPseudocounts
def GreedyMotifSearchWithPseudocounts(Dna, k, t):
return GreedyMotifSearch(Dna, k, t)
"""
Input: A profile matrix Profile and a list of strings Dna
Output: Motifs(Profile, Dna)
"""
def Motifs(Profile, Dna):
t = len(Dna)
most_prob = []
for i in range(t):
most_prob.append(ProfileMostProbableKmer(
Dna[i], len(Profile['A']), Profile))
return most_prob
"""
Input: A list of strings Dna, and integers k and t
Output: RandomMotifs(Dna, k, t)
"""
def RandomMotifs(Dna, k, t):
s = len(Dna[0])
random_mer = []
for i in range(t):
r = random.randint(1, s - k)
random_mer.append(Dna[i][r:r+k])
return random_mer
"""
Input: Positive integers k and t, followed by a list of strings Dna
Output: RandomizedMotifSearch(Dna, k, t)
"""
def RandomizedMotifSearch(Dna, k, t):
M = RandomMotifs(Dna, k, t)
BestMotifs = M
while True:
Profile = ProfileWithPseudocounts(M)
M = Motifs(Profile, Dna)
if Score(M) < Score(BestMotifs):
BestMotifs = M
else:
return BestMotifs
"""
Input: A dictionary Probabilities, where keys are k-mers and values are the probabilities of these k-mers (which do not necessarily sum up to 1)
Output: A normalized dictionary where the probability of each k-mer was divided by the sum of all k-mers' probabilities
"""
def Normalize(Probabilities):
total = 0
normalized = {}
for symbol in Probabilities:
total += Probabilities[symbol]
for symbol in Probabilities:
normalized[symbol] = Probabilities[symbol] / total
return normalized
"""
Input: A dictionary Probabilities whose keys are k-mers and whose values are the probabilities of these kmers
Output: A randomly chosen k-mer with respect to the values in Probabilities
"""
def WeightedDie(Probabilities):
kmer = ''
r = random.uniform(0, 1)
count = 0
for key in Probabilities:
count += Probabilities[key]
if r < count:
kmer = key
return kmer
return kmer
"""
Input: A string Text, a profile matrix Profile, and an integer k
Output: ProfileGeneratedString(Text, profile, k)
"""
def ProfileGeneratedString(Text, profile, k):
n = len(Text)
probabilities = {}
for i in range(0, n-k+1):
probabilities[Text[i:i+k]] = Pr(Text[i:i+k], profile)
probabilities = Normalize(probabilities)
return WeightedDie(probabilities)
"""
Input: Integers k, t, and N, followed by a collection of strings Dna
Output: GibbsSampler(Dna, k, t, N)
"""
def GibbsSampler(Dna, k, t, N):
BestMotifs = []
motifs = RandomMotifs(Dna, k, t)
BestMotifs = motifs
for j in range(N):
i = random.randint(0, t - 1)
del motifs[i]
prof = ProfileWithPseudocounts(motifs)
prof_string = ProfileGeneratedString(Dna[i], prof, k)
motifs.insert(i, prof_string)
if Score(motifs) < Score(BestMotifs):
BestMotifs = motifs
return BestMotifs