-
Notifications
You must be signed in to change notification settings - Fork 4
/
function_prediction.py
241 lines (202 loc) · 9.61 KB
/
function_prediction.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
from collections import defaultdict
from embedding_lookup import EmbeddingLookup
import numpy
import sys
class FunctionPrediction(object):
def __init__(self, embedding_db, go_db, go, go_type):
self.go = go
if go_type == 'all':
self.lookup_db = EmbeddingLookup(embedding_db)
self.go_db = go_db
elif go_type == 'mfo' or go_type == 'bpo' or go_type == 'cco':
# only use proteins in the annotation set which actually have an annotation in this ontology
self.go_db = defaultdict(set)
embedding_db_reduced = dict()
for k in embedding_db.keys():
terms = go_db[k]
go_terms = self.get_terms_by_go(terms)[go_type]
if len(go_terms) > 0:
embedding_db_reduced[k] = embedding_db[k]
self.go_db[k] = go_terms
self.lookup_db = EmbeddingLookup(embedding_db_reduced)
else:
sys.exit("{} is not a valid GO. Valid GOs are [all|mfo|bpo|cco]".format(go_type))
def get_terms_by_go(self, terms):
terms_by_go = {'mfo': set(), 'bpo': set(), 'cco': set()}
for t in terms:
onto = self.go.get_ontology(t)
if onto != '':
terms_by_go[onto].add(t)
return terms_by_go
def run_prediction_embedding_all(self, querys, distance, hits, criterion):
"""
Perform inference based on embedding-similarity
:param querys: proteins for which GO terms should be predicted
:param distance: distance measure to use [euclidean|cosine]
:param hits: hits to include (either by distance or by number as defined with criterion)
:param criterion: should k closest hits or all hits with distance <k be included?
:return:
"""
predictions = defaultdict(defaultdict)
hit_ids = defaultdict(defaultdict)
distances, query_ids = self.lookup_db.run_embedding_lookup_distance(querys, distance)
for i in range(0, len(query_ids)):
query = query_ids[i].split()[0]
dists = distances[i, :].squeeze()
for h in hits:
prediction = dict()
if criterion == 'dist': # extract hits within a certain distance
h = float(h)
indices = numpy.nonzero(dists <= h)
elif criterion == 'num': # extract h closest hits
h = int(h)
indices_tmp = numpy.argpartition(dists, h)[0:h]
dists_tmp = [dists[i] for i in indices_tmp]
max_dist = numpy.amax(dists_tmp)
indices = numpy.nonzero(dists <= max_dist)[0]
if len(indices) > h:
print("Multiple hits with same distance found, resulting in {} hits".format(len(indices)))
else:
sys.exit("No valid criterion defined, valid criterions are [dist|num]")
num_hits = len(indices)
for ind in indices:
lookup_id = self.lookup_db.ids[ind]
go_terms = self.go_db[lookup_id]
dist = dists[ind]
if distance == 'euclidean':
# scale distance to reflect a similarity [0;1]
dist = 0.5 / (0.5 + dist)
elif distance == 'cosine':
dist = 1 - dist
for g in go_terms:
if g in prediction.keys():
# if multiple hits are included RIs get smaller --> predictions retrieved for different
# numbers of hits are not directly comparable
prediction[g] += dist / num_hits
else:
prediction[g] = dist / num_hits
if query not in hit_ids[h].keys():
hit_ids[h][query] = dict()
hit_ids[h][query][lookup_id] = round(dist, 2)
# round ri and remove hits with ri == 0.00
keys_for_deletion = set()
for p in prediction:
ri = round(prediction[p], 2)
if ri == 0.00:
keys_for_deletion.add(p)
else:
prediction[p] = ri
for k in keys_for_deletion:
del prediction[k]
# reduce prediction to leaf terms
parent_terms = []
for p in prediction.keys():
parents = self.go.get_parent_terms(p)
parent_terms += parents
# exclude terms that are parent terms, i.e. there are more specific terms also part of this prediction
keys_for_deletion = set()
for p in prediction.keys():
if p in parent_terms:
keys_for_deletion.add(p)
for k in keys_for_deletion:
del prediction[k]
predictions[h][query] = prediction
return predictions, hit_ids
def run_prediction_one_target(self, query_embedding, distance, k, criterion):
"""
Perform inference based on embedding-similarity for one query embedding
:param query_embedding: query to calculate prediction for
:param distance: distance measure to use [euclidean|cosine]
:param k: hits to include (either by distance or by number as defined with criterion)
:param criterion: Should k closest hits or all hits with distance <k be included?
:return: GO term predictions with RI
"""
prediction = dict()
distances, _ = self.lookup_db.run_embedding_lookup_distance(query_embedding, distance)
dists = distances[0, :].squeeze().numpy()
if criterion == 'dist': # extract hits within a certain distance
k = float(k)
indices = numpy.nonzero(dists <= k)
elif criterion == 'num': # extract h closest hits
k = int(k)
indices_tmp = numpy.argpartition(dists, k)[0:k]
dists_tmp = [dists[i] for i in indices_tmp]
max_dist = numpy.amax(dists_tmp)
indices = numpy.nonzero(dists <= max_dist)[0]
else:
sys.exit("No valid criterion defined, valid criterions are [dist|num]")
num_hits = len(indices)
for ind in indices:
lookup_id = self.lookup_db.ids[ind]
go_terms = self.go_db[lookup_id]
dist = dists[ind]
if distance == 'euclidean':
# scale distance to reflect a similarity [0;1]
dist = 0.5 / (0.5 + dist)
elif distance == 'cosine':
dist = 1 - dist
for g in go_terms:
if g in prediction.keys():
# if multiple hits are included RIs get smaller --> predictions retrieved for different
# numbers of hits are not directly comparable
prediction[g] += dist / num_hits
else:
prediction[g] = dist / num_hits
# round ri and remove hits with ri == 0.00
keys_for_deletion = set()
for p in prediction:
ri = round(prediction[p], 2)
if ri == 0.00:
keys_for_deletion.add(p)
else:
prediction[p] = ri
for j in keys_for_deletion:
del prediction[j]
# reduce prediction to leaf terms
parent_terms = []
for p in prediction.keys():
parents = self.go.get_parent_terms(p)
parent_terms += parents
# exclude terms that are parent terms, i.e. there are more specific terms also part of this prediction
keys_for_deletion = set()
for p in prediction.keys():
if p in parent_terms:
keys_for_deletion.add(p)
for k in keys_for_deletion:
del prediction[k]
return prediction
@staticmethod
def write_predictions(predictions, out_file):
"""
Write prediictions
:param predictions: predictions to write
:param out_file: output file
:return:
"""
with open(out_file, 'w') as out:
for p in predictions.keys():
prediction = predictions[p]
for pred in prediction.keys():
ri = prediction[pred]
out.write('{}\t{}\t'.format(p, pred))
out.write('{:0.2f}\n'.format(float(ri)))
@staticmethod
def write_predictions_cafa(predictions, out_file, model_num, team_name):
"""
Write prediictions in CAFA format
:param predictions: predictions to write
:param out_file: output file
:param model_num: number of model that is used
:param team_name: Team name to use in output file
:return:
"""
with open(out_file, 'w') as out:
out.write('AUTHOR\t{}\nMODEL\t{}\nKEYWORDS\thomolog, machine learning, natural language processing.'
'\n'.format(team_name, model_num))
for p in predictions.keys():
prediction = predictions[p]
for pred in prediction.keys():
ri = prediction[pred]
out.write('{}\t{}\t'.format(p, pred))
out.write('{:0.2f}\n'.format(float(ri)))
out.write('END')