-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProbabilisticGenome.py
358 lines (323 loc) · 13.9 KB
/
ProbabilisticGenome.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
357
358
from operator import indexOf
import os
import operator
import json
import datetime
from math import log10, inf, isinf, ceil
from itertools import repeat
from collections import defaultdict
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
# Very small valued number for log(0) equivalent
zero = 10**-200
def parseGenome():
probability_file = "Probabilities.txt"
genome_file = "Genome.txt"
if os.path.exists(os.path.join(__location__, "Genome_probabilities.json")):
with open(os.path.join(__location__, "Genome_probabilities.json"), "r") as my_file:
probability_dictionary = json.load(my_file)
with open(os.path.join(__location__, genome_file), "r") as my_file:
genome = my_file.readline().strip()
return genome, probability_dictionary
# Extract data from files
with open(os.path.join(__location__, genome_file), "r") as my_file:
genome = my_file.readline().strip()
with open(os.path.join(__location__, probability_file), "r") as my_file:
probabilities = my_file.readline().strip().split(" ")
probability_dictionary = {}
nucleotides = set(("A", "C", "G", "T"))
total_score = 0
# Convert data to dictionary of probabilities based on index value
for i in range(len(genome)):
probability = float(probabilities[i])
total_score += probability
remaining_prob = (1-probability)/3
nucleotide = genome[i].upper()
prob_dict = {}
prob_dict[nucleotide] = probability
for nucleotide in nucleotides.difference(set((nucleotide))):
prob_dict[nucleotide] = remaining_prob
probability_dictionary[str(i)] = prob_dict
gap_score = total_score / len(genome)
probability_dictionary["GAP"] = gap_score
# Convert dictionary to json for easy transfer
prob_dict_json = json.dumps(probability_dictionary)
with open(os.path.join(__location__, "Genome_probabilities.json"), "w") as out_file:
out_file.write(prob_dict_json)
print("JSON file of probabilistic genome created at: ",
os.path.join(__location__, "Genome_probabilities.json"))
return genome, probability_dictionary
def preprocess():
if os.path.exists(os.path.join(__location__, "Genome_8mers.json")):
with open(os.path.join(__location__, "Genome_8mers.json"), "r") as my_file:
size_8_mers = json.load(my_file)
with open(os.path.join(__location__, "Genome_11mers.json"), "r") as my_file:
size_11_mers = json.load(my_file)
with open(os.path.join(__location__, "Genome_15mers.json"), "r") as my_file:
size_15_mers = json.load(my_file)
return defaultdict(list, size_8_mers), defaultdict(list, size_11_mers), defaultdict(list, size_15_mers)
size_15_mers = defaultdict(list)
size_11_mers = defaultdict(list)
size_8_mers = defaultdict(list)
for i in range(len(genome)-8):
seq8 = genome[i:i+8]
max_prob8 = 0
score8 = 0
for j in range(i, i+8):
val = probability_dictionary[str(j)][genome[j]]
max_prob8 += Log(val)
score8 += val
size_8_mers[seq8].append([i, max_prob8, score8])
if (i < len(genome) - 11):
seq11 = genome[i:i+11]
max_prob11 = 0
score11 = 0
for j in range(i, i+11):
val = probability_dictionary[str(j)][genome[j]]
max_prob11 += Log(val)
score11 += val
size_11_mers[seq11].append([i, max_prob11, score11])
if (i < len(genome) - 15):
seq15 = genome[i:i+15]
max_prob15 = 0
score15 = 0
for j in range(i, i+15):
val = probability_dictionary[str(j)][genome[j]]
max_prob15 += Log(val)
score15 += val
size_15_mers[seq15].append([i, max_prob15, score15])
with open(os.path.join(__location__, "Genome_8mers.json"), "w") as out_file:
out_file.write(json.dumps(size_8_mers))
with open(os.path.join(__location__, "Genome_11mers.json"), "w") as out_file:
out_file.write(json.dumps(size_11_mers))
with open(os.path.join(__location__, "Genome_15mers.json"), "w") as out_file:
out_file.write(json.dumps(size_15_mers))
return (size_8_mers, size_11_mers, size_15_mers)
def shortPerfectMatch(query, w=11):
if w == 8:
genome_matches = size_8_mers
elif w == 15:
genome_matches = size_15_mers
else:
genome_matches = size_11_mers
matches = defaultdict(list)
for i in range(len(query)-w):
seq = query[i:i+w]
if seq in matches.keys():
matches[seq][0].append(i)
elif len(genome_matches[seq]) > 0:
matches[seq] = [[i], genome_matches[seq]]
return matches
def Log(x):
if x <= 0:
return log10(zero)
else:
return log10(x)
def score(nucleotide, index):
vals = probability_dictionary[str(index)]
best_nucleotide = max(vals.items(), key=operator.itemgetter(1))[0]
if nucleotide == best_nucleotide:
return vals[nucleotide], Log(vals[nucleotide])
else:
return vals[nucleotide] - vals[best_nucleotide], Log(vals[nucleotide])
def ungappedExtension(query, matches):
# Need another threshold value for determining what moves on to gapped phase
ungapped_matches = defaultdict(list)
for k, v in matches.items():
for match in v[1]:
for start_index in v[0]:
right_index, right_genome_index, right_probability, right_maxima = extension(
k, start_index, query, match[0], 0)
left_index, left_genome_index, left_probability, left_maxima = extension(k,
start_index, query, match[0], 1)
extended_sequence = query[left_index: right_index+1]
score = right_maxima + match[-1] + left_maxima
probability = left_probability + match[1] + right_probability
# if score > threshold_t:
info = [left_index, left_genome_index, probability, score]
if all(map(lambda x, y: x != y, [v[1] for v in ungapped_matches[extended_sequence]], repeat(left_genome_index))):
ungapped_matches[extended_sequence].append(info)
return ungapped_matches
def extension(sequence, start, query, genome_start, dir):
# dir variable for determining right vs left extension (0 = right, 1 = left)
if dir == 0:
genome_start += len(sequence) - 1
start += len(sequence) - 1
# Need a threshold value still. Use 5 for now as a default
threshold = 5
drop = 0
max_score = 0
max_index = start
max_genome_index = genome_start
cur_score = 0
cur_prob = 0
prob_at_max = 0
while drop < threshold:
if dir == 0:
start += 1
genome_start += 1
if start > len(query)-1 or genome_start > len(genome) - 1:
break
else:
start -= 1
genome_start -= 1
if start < 0 or genome_start < 0:
break
vals = score(query[start], genome_start)
cur_score += vals[0]
cur_prob += vals[1]
if cur_score > max_score:
max_score = cur_score
max_index = start
max_genome_index = genome_start
prob_at_max = cur_prob
drop = 0
else:
drop = max_score - cur_score
return max_index, max_genome_index, prob_at_max, max_score
def gappedExtension(query, ungapped_matches):
# Need to perform NW variant on the right and left sides of the remaining query sequence.
alignment_info = []
for k, v in ungapped_matches.items():
for i, match in enumerate(v):
# print(k, i)
left_index, genome_index, cur_prob, cur_score = tuple(match)
left_query = query[:left_index]
left_genome_index = genome_index - 1
left_info = align(left_query, left_genome_index, True)
if isinf(left_info[1]):
continue
right_query = query[left_index + len(k):]
right_genome_index = genome_index + len(k)
right_info = align(right_query, right_genome_index)
if isinf(right_info[1]):
continue
total_score = left_info[2] + cur_score + right_info[2]
total_prob = left_info[1] + cur_prob + right_info[1]
genome_start_index = genome_index - len(left_info[0])
alignment = left_info[0] + k + right_info[0]
alignment_info.append(
(alignment, total_score, total_prob, genome_start_index))
return sorted(alignment_info, key=lambda info: info[1], reverse=True)[:5]
def align(query, index, reverse=False):
n = len(query)
if reverse:
s1 = query[::-1]
new_index = max(index -
(2*ceil(float(probability_dictionary["GAP"]) * n) + 1), 0)
s2 = genome[index: new_index: -1]
index = new_index
else:
s1 = query
s2 = genome[index: index + 2 *
ceil(float(probability_dictionary["GAP"]) * n)+1]
m = len(s2)
if m < n:
print("Not possible")
return ("", -inf, 0)
# Create empty dp array. Structured as (score, probability, prev_x, prev_y)
M = [[(0, 0, 0, 0) for _ in range(m + 1)] for _ in range(n + 1)]
# Can't align query to gaps in genome so those indices are marked as impossible
for i in range(1, n + 1):
for j in range(i):
M[i][j] = (-inf, 0, 0, 0)
for i in range(m - n):
for j in range(i+n+1, m + 1):
M[i][j] = (-inf, 0, 0, 0)
for i in range(1, n + 1):
for j in range(i, m-n+i+1):
s_diag = score(s1[i-1], index+j-1)
diagonal = M[i-1][j-1][0] + s_diag[0]
left = M[i][j-1][0] - probability_dictionary["GAP"]
if diagonal >= left:
M[i][j] = (diagonal, M[i-1][j-1][1] + s_diag[1], i-1, j-1)
else:
# Temporarily set probability to s_diag[1], but it needs to change
M[i][j] = (left, M[i][j-1][1] + s_diag[1], i, j-1)
# Now we need the highest score in the bottom row.
index = M[-1].index(max(M[-1]))
max_score = M[-1][index][0]
max_prob = M[-1][index][1]
s1_final = []
i = n
j = index
while i > 0 and j > 0:
vals = M[i][j]
if vals[-2] == i-1 and vals[-1] == j-1:
if reverse:
s1_final.append(s1[i-1])
else:
s1_final.insert(0, s1[i-1])
i -= 1
j -= 1
else:
if reverse:
s1_final.append("_")
else:
s1_final.insert(0, "_")
j -= 1
s1_final = "".join(s1_final)
return (s1_final, max_prob, max_score)
print(datetime.datetime.now())
genome, probability_dictionary = parseGenome()
size_8_mers, size_11_mers, size_15_mers = preprocess()
# print(len(probability_dictionary))
# sequence_files = ["query_seq100.txt", "query_seq300.txt",
# "query_seq500.txt", "query_seq1000.txt"]
# for file in sequence_files:
# new_file = file.split(".")[0]+"_8" + ".json"
# with open(os.path.join(__location__, "Query Seqs", file), "r") as seq_file:
# print(file)
# seqs = []
# for line in seq_file:
# seqs.append(line.strip().split(" "))
# seq_alignments = {}
# for i in range(len(seqs)):
# print(datetime.datetime.now())
# print("i=", i)
# alignments = {}
# for j in range(3):
# print("j=", j)
# query = seqs[i][j+1]
# stripped_seq = "".join(query.split("_"))
# matches = shortPerfectMatch(stripped_seq, 8)
# # # print(matches)
# ungapped_matches = ungappedExtension(stripped_seq, matches)
# # print(ungapped_matches)
# final_matches = gappedExtension(stripped_seq, ungapped_matches)
# # print(final_matches)
# alignments[query] = final_matches
# seq_alignments[seqs[i][0]] = alignments
# with open(os.path.join(__location__, "Query Predictions", new_file), "w") as align_file:
# align_file.write(json.dumps(seq_alignments))
# for i, file in enumerate([f for f in os.listdir(os.path.join(__location__, "Query Predictions")) if os.path.isfile(os.path.join(__location__, "Query Predictions", f))]):
# with open(os.path.join(__location__, "Query Predictions", file), "r+") as myfile:
# print(file)
# predictions = json.load(myfile)
# k = list(predictions.keys())[0]
# if len(k) == 100:
# with open(os.path.join(__location__, "Query Seqs", "query_seq100.txt"), "r") as seq_file:
# seqs = []
# for line in seq_file:
# seqs.append(line.strip().split(" "))
# elif len(k) == 300:
# with open(os.path.join(__location__, "Query Seqs", "query_seq300.txt"), "r") as seq_file:
# seqs = []
# for line in seq_file:
# seqs.append(line.strip().split(" "))
# elif len(k) == 500:
# with open(os.path.join(__location__, "Query Seqs", "query_seq500.txt"), "r") as seq_file:
# seqs = []
# for line in seq_file:
# seqs.append(line.strip().split(" "))
# else:
# with open(os.path.join(__location__, "Query Seqs", "query_seq1000.txt"), "r") as seq_file:
# seqs = []
# for line in seq_file:
# seqs.append(line.strip().split(" "))
# og_seqs = [seq for seq in [seqs[i][0] for i in range(len(seqs))]]
# for k, v in predictions.items():
# index = og_seqs.index(k)
# v["Correct"] = int(seqs[index][-2])
# myfile.write(json.dumps(predictions))
print(datetime.datetime.now())