forked from bio-ontology-research-group/deepgozero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_deepgopp.py
executable file
·306 lines (280 loc) · 10.2 KB
/
predict_deepgopp.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
#!/usr/bin/env python
import numpy as np
import pandas as pd
import click as ck
from sklearn.metrics import classification_report
from sklearn.metrics.pairwise import cosine_similarity
import sys
from collections import deque
import time
import logging
from sklearn.metrics import roc_curve, auc, matthews_corrcoef
from scipy.spatial import distance
from scipy import sparse
import math
from utils import FUNC_DICT, Ontology, NAMESPACES
from matplotlib import pyplot as plt
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
# def get_goplus_defs():
# plus_defs = {}
# with open('data/definitions_mf.txt') as f:
# for line in f:
# line = line.strip()
# go_id, definition = line.split(': ')
# go_id = go_id.replace('_', ':')
# definition = definition.replace('_', ':')
# plus_defs[go_id] = set(definition.split(' and '))
# return plus_defs
ont = 'mf'
@ck.command()
@ck.option(
'--train-data-file', '-trdf', default=f'data/{ont}/train_data.pkl',
help='Data file with training features')
@ck.option(
'--valid-data-file', '-vrdf', default=f'data/{ont}/valid_data.pkl',
help='Data file with training features')
@ck.option(
'--test-data-file', '-tsdf', default=f'data/{ont}/predictions_deepgoel.pkl',
help='Test data file')
@ck.option(
'--terms-file', '-tf', default=f'data/{ont}/terms.pkl',
help='Data file with sequences and complete set of annotations')
@ck.option(
'--diamond-scores-file', '-dsf', default=f'data/{ont}/test_diamond.res',
help='Diamond output')
@ck.option(
'--out_file', '-of', help='Output file')
def main(train_data_file, valid_data_file, test_data_file, terms_file,
diamond_scores_file, out_file):
go_rels = Ontology('data/go.obo', with_rels=True)
terms_df = pd.read_pickle(terms_file)
terms = terms_df['gos'].values.flatten()
terms_dict = {v: i for i, v in enumerate(terms)}
train_df = pd.read_pickle(train_data_file)
valid_df = pd.read_pickle(valid_data_file)
train_df = pd.concat([train_df, valid_df])
test_df = pd.read_pickle(test_data_file)
# test_df = test_df[test_df["orgs"] == "9606"]
annotations = train_df['prop_annotations'].values
annotations = list(map(lambda x: set(x), annotations))
test_annotations = test_df['prop_annotations'].values
test_annotations = list(map(lambda x: set(x), test_annotations))
go_rels.calculate_ic(annotations + test_annotations)
# Print IC values of terms
ics = {}
for term in terms:
ics[term] = go_rels.get_ic(term)
prot_index = {}
for i, row in enumerate(train_df.itertuples()):
prot_index[row.proteins] = i
# BLAST Similarity (Diamond)
diamond_scores = {}
with open(diamond_scores_file) as f:
for line in f:
it = line.strip().split()
if it[0] not in diamond_scores:
diamond_scores[it[0]] = {}
diamond_scores[it[0]][it[1]] = float(it[2])
blast_preds = []
print('Diamond preds')
for i, row in enumerate(test_df.itertuples()):
annots = {}
prot_id = row.proteins
# BlastKNN
if prot_id in diamond_scores:
sim_prots = diamond_scores[prot_id]
allgos = set()
total_score = 0.0
for p_id, score in sim_prots.items():
allgos |= annotations[prot_index[p_id]]
total_score += score
allgos = list(sorted(allgos))
sim = np.zeros(len(allgos), dtype=np.float32)
for j, go_id in enumerate(allgos):
s = 0.0
for p_id, score in sim_prots.items():
if go_id in annotations[prot_index[p_id]]:
s += score
sim[j] = s / total_score
ind = np.argsort(-sim)
for go_id, score in zip(allgos, sim):
annots[go_id] = score
blast_preds.append(annots)
# DeepGOPlus
go_set = go_rels.get_namespace_terms(NAMESPACES[ont])
go_set.remove(FUNC_DICT[ont])
labels = test_df['prop_annotations'].values
labels = list(map(lambda x: set(filter(lambda y: y in go_set, x)), labels))
# print(len(go_set))
deep_preds = []
# Load definitions
plus_defs = get_goplus_defs()
for i, row in enumerate(test_df.itertuples()):
annots_dict = {}
for j, score in enumerate(row.preds):
go_id = terms[j]
if not go_id.startswith('GO'):
continue
annots_dict[go_id] = score
# for go_id, defs in plus_defs.items():
# if go_id not in terms_dict:
# continue
# mscore = 1
# ok = True
# for t_id in defs:
# if t_id in terms_dict:
# mscore = min(mscore, row.preds[terms_dict[t_id]])
# else:
# ok = False
# break
# if ok:
# annots_dict[go_id] = max(annots_dict[go_id], mscore)
deep_preds.append(annots_dict)
# Combine scores for diamond and deepgo
alphas = {NAMESPACES['mf']: 0.5, NAMESPACES['bp']: 0.5, NAMESPACES['cc']: 0.5}
comb_preds = []
for i in range(len(blast_preds)):
gos = set(blast_preds[i]) | set(deep_preds[i])
annots_dict = {}
for go_id in gos:
if not go_rels.has_term(go_id):
continue
alpha = alphas[go_rels.get_namespace(go_id)]
if go_id in blast_preds[i] and go_id in deep_preds[i]:
annots_dict[go_id] = blast_preds[i][go_id] * alpha + deep_preds[i][go_id] * (1 - alpha)
elif go_id in blast_preds[i]:
annots_dict[go_id] = blast_preds[i][go_id] * alpha
else:
annots_dict[go_id] = deep_preds[i][go_id] * (1 - alpha)
comb_preds.append(annots_dict)
# print('AUTHOR DeepGOPlus')
# print('MODEL 1')
# print('KEYWORDS sequence alignment.')
# for i, row in enumerate(test_df.itertuples()):
# prot_id = row.proteins
# for go_id, score in deep_preds[i].items():
# print(f'{prot_id}\t{go_id}\t{score:.2f}')
# print('END')
# return
# Propagate scores
# deepgo_preds = []
# for annots_dict in deep_preds:
# annots = {}
# for go_id, score in annots_dict.items():
# for a_id in go_rels.get_anchestors(go_id):
# if a_id in annots:
# annots[a_id] = max(annots[a_id], score)
# else:
# annots[a_id] = score
# deepgo_preds.append(annots)
print('Computing Fmax')
fmax = 0.0
tmax = 0.0
precisions = []
recalls = []
smin = 1000000.0
rus = []
mis = []
for t in range(0, 101):
threshold = t / 100.0
preds = []
for i, row in enumerate(test_df.itertuples()):
annots = set()
for go_id, score in blast_preds[i].items():
if score >= threshold:
annots.add(go_id)
new_annots = set()
for go_id in annots:
new_annots |= go_rels.get_anchestors(go_id)
preds.append(new_annots)
# Filter classes
preds = list(map(lambda x: set(filter(lambda y: y in go_set, x)), preds))
allpreds = set()
for pred in preds:
allpreds |= pred
print(len(allpreds))
fscore, prec, rec, s, ru, mi, fps, fns = evaluate_annotations(go_rels, labels, preds)
avg_fp = sum(map(lambda x: len(x), fps)) / len(fps)
avg_ic = sum(map(lambda x: sum(map(lambda go_id: go_rels.get_ic(go_id), x)), fps)) / len(fps)
print(f'{avg_fp} {avg_ic}')
precisions.append(prec)
recalls.append(rec)
print(f'Fscore: {fscore}, Precision: {prec}, Recall: {rec} S: {s}, RU: {ru}, MI: {mi} threshold: {threshold}')
if fmax < fscore:
fmax = fscore
tmax = threshold
if smin > s:
smin = s
print(f'Fmax: {fmax:0.3f}, Smin: {smin:0.3f}, threshold: {tmax}')
precisions = np.array(precisions)
recalls = np.array(recalls)
sorted_index = np.argsort(recalls)
recalls = recalls[sorted_index]
precisions = precisions[sorted_index]
aupr = np.trapz(precisions, recalls)
print(f'AUPR: {aupr:0.3f}')
plt.figure()
lw = 2
plt.plot(recalls, precisions, color='darkorange',
lw=lw, label=f'AUPR curve (area = {aupr:0.2f})')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Area Under the Precision-Recall curve')
plt.legend(loc="lower right")
plt.savefig(f'aupr_{ont}.pdf')
df = pd.DataFrame({'precisions': precisions, 'recalls': recalls})
df.to_pickle(f'PR_{ont}.pkl')
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
def compute_mcc(labels, preds):
# Compute ROC curve and ROC area for each class
mcc = matthews_corrcoef(labels.flatten(), preds.flatten())
return mcc
def evaluate_annotations(go, real_annots, pred_annots):
total = 0
p = 0.0
r = 0.0
p_total= 0
ru = 0.0
mi = 0.0
fps = []
fns = []
for i in range(len(real_annots)):
if len(real_annots[i]) == 0:
continue
tp = set(real_annots[i]).intersection(set(pred_annots[i]))
fp = pred_annots[i] - tp
fn = real_annots[i] - tp
for go_id in fp:
mi += go.get_ic(go_id)
for go_id in fn:
ru += go.get_ic(go_id)
fps.append(fp)
fns.append(fn)
tpn = len(tp)
fpn = len(fp)
fnn = len(fn)
total += 1
recall = tpn / (1.0 * (tpn + fnn))
r += recall
if len(pred_annots[i]) > 0:
p_total += 1
precision = tpn / (1.0 * (tpn + fpn))
p += precision
ru /= total
mi /= total
r /= total
if p_total > 0:
p /= p_total
f = 0.0
if p + r > 0:
f = 2 * p * r / (p + r)
s = math.sqrt(ru * ru + mi * mi)
return f, p, r, s, ru, mi, fps, fns
if __name__ == '__main__':
main()