-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation_rare_terms.py
More file actions
359 lines (266 loc) · 13.2 KB
/
evaluation_rare_terms.py
File metadata and controls
359 lines (266 loc) · 13.2 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
from matplotlib import pyplot as plt, rcParams
from matplotlib.patches import Rectangle
import numpy as np
from sklearn.metrics import auc, roc_curve, roc_auc_score, average_precision_score
from Utils import pickle_save, pickle_load
from collections import Counter
import math
import CONSTANTS
import obonet
import networkx as nx
import collections
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from scipy import stats
def plot_distribution(results):
keys = {"mf": 0, "cc": 1, "bp": 2, "swissprot": 0, "trembl": 1}
titles = {"mf": "Molecular Function", "cc": "Cellular Component", "bp": "Biological Process",
"swissprot": "Swissprot", "trembl": "Trembl"}
nrows, ncols = 3, 2
fig, axs = plt.subplots(
nrows, ncols, figsize=(20, 16),
)
for ont, dbsubset in results.items():
for sptr, cnts in dbsubset.items():
im = axs[keys[ont]][keys[sptr]].bar(cnts.keys(), cnts.values());
axs[keys[ont]][keys[sptr]].set_title(titles[ont] + " --- " + titles[sptr], fontsize=18, fontweight="bold")
plt.suptitle(f'Distribution of rare terms in test set \n', fontsize=24, ha='center', fontweight="bold")
plt.tight_layout()
plt.savefig("plots/rare_distribution.png")
def plot_rare_distribution_bar(data, Xs, measure="auc"):
keys = {"mf": 1, "cc": 2, "bp": 0}
titles = {"mf": "Molecular Function", "cc": "Cellular Component", "bp": "Biological Process"}
labels = {"tale":"Tale", "netgo3":"NetGO3", "full_gcn":"TransFew", "sprof": "Sprof"}
Xs = ['_'.join(map(str, s)) for s in Xs]
x = np.arange(len(Xs)) # the label locations
width = 0.2 # the width of the bars
nrows, ncols = 3, 1
fig, axs = plt.subplots(
nrows, ncols, figsize=(12, 12),
gridspec_kw={'hspace': 0.4},
sharex=False, sharey=True,
#layout='constrained'
)
for ont, ont_values in data.items():
multiplier = 0
for method, values in ont_values.items():
offset = width * multiplier
bars = axs[keys[ont]].bar(x + offset, values, width, label=labels[method]);
multiplier += 1
axs[keys[ont]].set_title(titles[ont], fontsize=18, fontweight="bold")
axs[keys[ont]].set_xticks(x + width, Xs)
axs[keys[ont]].tick_params(axis='both', labelrotation=25, labelsize=14)
axs[keys[ont]].legend(loc='upper left', ncols=3, fontsize=16)
fig.text(0.5, 0.04, 'Annotation Frequency', ha='center', fontweight ='bold', fontsize=16)
fig.text(0.08, 0.5, 'Average AUC', va='center', rotation='vertical', fontweight ='bold', fontsize=16)
# plt.suptitle(f'Average AUCs of terms grouped by annotation size on the test dataset\n', x=0.5, y=0.9, fontsize=26, ha='center', fontweight="bold")
plt.tight_layout()
plt.savefig("plots/average_new_{}.png".format(measure), bbox_inches='tight')
def plot_rare_distribution_line(data, Xs):
# got these colors from the cafa-evaluator tool plot assignment
colors = {
"deepgose": (0.12156862745098039, 0.4666666666666667, 0.7058823529411765),
"netgo3": (0.6823529411764706, 0.7803921568627451, 0.9098039215686274),
"sprof": (0.596078431372549, 0.8745098039215686, 0.5411764705882353),
"tale": (1.0, 0.4980392156862745, 0.054901960784313725),
"transfew": (0.8392156862745098, 0.15294117647058825, 0.1568627450980392)
}
keys = {"mf": 1, "cc": 2, "bp": 0}
ontologies = ["cc", "mf", "bp"]
titles = {"mf": "Molecular Function", "cc": "Cellular Component", "bp": "Biological Process"}
Xs = ['_'.join(map(str, s)) for s in Xs]
x = np.arange(len(Xs))
nrows, ncols = 3, 1
fig, axs = plt.subplots(
nrows, ncols, figsize=(14, 12),
gridspec_kw={'hspace': 0.6},
sharex=False, sharey=True,
layout='constrained'
)
for ont in ontologies:
ont_values = data[ont]
for method, values in ont_values.items():
axs[keys[ont]].plot(Xs, values, color=colors[method], label=method)
axs[keys[ont]].plot(Xs, values, color=colors[method], marker='o', markersize=10, mfc='none')
axs[keys[ont]].plot(Xs, values, color=colors[method], marker='o', markersize=5)
axs[keys[ont]].set_title(titles[ont], fontsize=18)
axs[keys[ont]].set_xticks(x, Xs)
axs[keys[ont]].tick_params(axis='both', labelrotation=45, labelsize=14)
axs[keys[ont]].legend(loc='upper left', ncols=5, fontsize=16)
fig.text(0.5, 0.04, 'Annotation Frequency', ha='center', fontsize=16)
fig.text(0.08, 0.5, 'Average AUC', va='center', rotation='vertical', fontsize=16)
# plt.suptitle(f'Average AUCs of terms grouped by annotation size on the test dataset\n', x=0.5, y=0.9, fontsize=26, ha='center', fontweight="bold")
plt.tight_layout()
plt.savefig("plots/average_new.png", bbox_inches='tight')
def check_intersection_rare():
# rare terms
onts = ["mf", "cc", "bp"]
db_subset = ['swissprot', 'trembl']
results = {i: {} for i in onts}
for ont in onts:
for sptr in db_subset:
rare_terms = set()
term_stats = pickle_load(CONSTANTS.ROOT_DIR+"/{}/terms_stats".format(ont))
for term in term_stats:
if term_stats[term][0] <= 30 :
rare_terms.add(term)
all_terms = set()
test_groundtruth = pickle_load(CONSTANTS.ROOT_DIR + "test/output_t1_t2/groundtruth")[ont]
proteins = pickle_load(CONSTANTS.ROOT_DIR + "test/output_t1_t2/test_proteins")[ont][sptr]
for protein in proteins:
all_terms.update(test_groundtruth[protein])
rare_terms_in_test = all_terms.intersection(rare_terms)
tmp = []
for i in rare_terms_in_test:
tmp.append(term_stats[i][0])
w = collections.Counter(tmp)
results[ont][sptr] = w
plot_distribution(results)
def compute_roc(labels, preds):
# Compute ROC curve and ROC area for each class
fpr, tpr, _ = roc_curve(labels.flatten(), preds.flatten())
roc_auc = auc(fpr, tpr)
return roc_auc, fpr, tpr
def load_predictions(in_file):
data = {}
with open(in_file) as f:
lines = [line.strip('\n').split("\t") for line in f]
for line in lines:
if line[0] in data:
data[line[0]].append((line[1], line[2]))
else:
data[line[0]] = [(line[1], line[2]), ]
return data
def get_rare_terms(ont, bottom, top):
rare_terms = []
term_stats = pickle_load(CONSTANTS.ROOT_DIR+"/{}/terms_stats".format(ont))
for term in term_stats:
if term_stats[term][0] >= bottom and term_stats[term][0] <= top:
rare_terms.append(term)
return rare_terms
def compute_measure():
onts = ['cc', 'mf', 'bp']
results = {i: {} for i in onts}
test_proteins = pickle_load(CONSTANTS.ROOT_DIR + "test/output_t1_t2/test_proteins")
test_groundtruth = pickle_load(CONSTANTS.ROOT_DIR + "test/output_t1_t2/groundtruth")
thresholds = [(1, 5), (6, 10), (11, 15),(16, 20), (21, 25), (26, 30),
(31, 35), (36, 40), (41, 45), (46, 50), (51, 55), (56, 60),
(61, 65), (66, 70), (71, 75), (76, 80), (81, 85), (86, 90),
(91, 95), (96, 100)]
methods = ["tale", "netgo3", "sprof", "deepgose", "transfew"]
db_subset = "swissprot"
for ont in onts:
for method in methods:
results[ont][method] = []
for threshold in thresholds:
rare_terms = {term: pos for pos, term in enumerate(get_rare_terms(ont, threshold[0], threshold[1]))}
# get all predictions for particular method
pth = CONSTANTS.ROOT_DIR + "evaluation/predictions/full/{}_{}/{}.tsv".format(db_subset, ont, method)
predictions = load_predictions(pth)
# get all proteins in ontology
proteins = test_proteins[ont][db_subset]
preds = np.zeros((len(proteins), len(rare_terms)), dtype=np.float32)
labels = np.zeros((len(proteins), len(rare_terms)), dtype=np.float32)
# convert to numpy array
for pos, protein in enumerate(proteins):
_pred = {go_term: score for go_term, score in predictions[protein] if go_term in rare_terms}
_labs = test_groundtruth[ont][protein].intersection(rare_terms)
for go_id, go_pos in rare_terms.items():
if go_id in _pred:
preds[pos, go_pos] = _pred[go_id]
if go_id in _labs:
labels[pos, go_pos] = 1.0
count = 0
total = 0
for go_id, go_pos in rare_terms.items():
pos_n = np.sum(labels[:, go_pos])
if pos_n > 0:
# _score = roc_auc_score(labels[:, go_pos], preds[:, go_pos])
_score, _, _ = compute_roc(labels[:, go_pos], preds[:, go_pos])
total += _score
count += 1
score = round(total/count, 3)
results[ont][method].append(score)
pickle_save(results, "rest")
pickle_save(thresholds, "thres")
# plot_rare_distribution(results, Xs=thresholds, measure=measure)
# Just duplicated code:///
def compute_pearson():
onts = ['cc', 'mf', 'bp']
results = {'cc': {}, 'mf': {}, 'bp': {}}
test_proteins = pickle_load(CONSTANTS.ROOT_DIR + "test/output_t1_t2/test_proteins")
test_groundtruth = pickle_load(CONSTANTS.ROOT_DIR + "test/output_t1_t2/groundtruth")
thresholds = [(1, 5), (6, 10), (11, 15),(16, 20), (21, 25), (26, 30),
(31, 35), (36, 40), (41, 45), (46, 50), (51, 55), (56, 60),
(61, 65), (66, 70), (71, 75), (76, 80), (81, 85), (86, 90),
(91, 95), (96, 100)]
methods = ["tale", "netgo3", "sprof", "deepgose", "transfew"]
db_subset = "swissprot"
for ont in onts:
for method in methods:
print(method)
results[ont][method] = {'scores': [], 'thresholds': []}
for op, threshold in enumerate(thresholds):
rare_terms = {term: pos for pos, term in enumerate(get_rare_terms(ont, threshold[0], threshold[1]))}
# print(ont, threshold, len(rare_terms))
pth = CONSTANTS.ROOT_DIR + "evaluation/predictions/full/{}_{}/{}.tsv".format(db_subset, ont, method)
predictions = load_predictions(pth)
# get all proteins in ontology
proteins = test_proteins[ont][db_subset]
preds = np.zeros((len(proteins), len(rare_terms)), dtype=np.float32)
labels = np.zeros((len(proteins), len(rare_terms)), dtype=np.float32)
# convert to numpy array
# convert to numpy array
for pos, protein in enumerate(proteins):
_pred = {go_term: score for go_term, score in predictions[protein] if go_term in rare_terms}
_labs = test_groundtruth[ont][protein].intersection(rare_terms)
for go_id, go_pos in rare_terms.items():
if go_id in _pred:
preds[pos, go_pos] = _pred[go_id]
if go_id in _labs:
labels[pos, go_pos] = 1.0
count = 0
total = 0
for go_id, go_pos in rare_terms.items():
pos_n = np.sum(labels[:, go_pos])
if pos_n > 0:
# _score = roc_auc_score(labels[:, go_pos], preds[:, go_pos])
_score, _, _ = compute_roc(labels[:, go_pos], preds[:, go_pos])
total += _score
count += 1
score = round(total/count, 3)
results[ont][method]['scores'].append(round(score, 3))
results[ont][method]['thresholds'].append(op)
_compute_pearson(results)
def _compute_pearson(data):
for ont in data:
for method in data[ont]:
print(data[ont][method])
pearson = stats.pearsonr(data[ont][method]['thresholds'], data[ont][method]['scores'])
print(ont, method, pearson)
def compute_roc(labels, preds):
# Compute ROC curve and ROC area for each class
fpr, tpr, _ = roc_curve(labels.flatten(), preds.flatten())
roc_auc = auc(fpr, tpr)
return roc_auc, fpr, tpr
def get_graph(go_path=CONSTANTS.ROOT_DIR + "/obo/go-basic.obo"):
go_graph = obonet.read_obo(open(go_path, 'r'))
accepted_edges = set()
unaccepted_edges = set()
for edge in go_graph.edges:
if edge[2] == 'is_a' or edge[2] == 'part_of':
accepted_edges.add(edge)
else:
unaccepted_edges.add(edge)
go_graph.remove_edges_from(unaccepted_edges)
return go_graph
def add_anscetor_terms(data):
go_graph = get_graph()
new_annots = set()
for go_id in data:
if go_id in go_graph:
new_annots |= nx.descendants(go_graph, go_id) | {go_id}
return new_annots
# check_intersection_rare()
# compute_measure()
compute_pearson()