-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgg.py
285 lines (235 loc) · 9.68 KB
/
dgg.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
import click as ck
import pandas as pd
from utils import Ontology
import torch as th
import numpy as np
from torch import nn
from torch.nn import functional as F
from torch import optim
from sklearn.metrics import roc_curve, auc, matthews_corrcoef
import copy
from torch.utils.data import DataLoader, IterableDataset, TensorDataset
from itertools import cycle
import math
from aminoacids import to_onehot, MAXLEN
from dgl.nn import GraphConv
import dgl
from torch_utils import FastTensorDataLoader
import csv
from torch.optim.lr_scheduler import MultiStepLR
@ck.command()
@ck.option(
'--data-root', '-dr', default='data',
help='Prediction model')
@ck.option(
'--ont', '-ont', default='mf',
help='Prediction model')
@ck.option(
'--batch-size', '-bs', default=37,
help='Batch size for training')
@ck.option(
'--epochs', '-ep', default=256,
help='Training epochs')
@ck.option(
'--load', '-ld', is_flag=True, help='Load Model?')
@ck.option(
'--device', '-d', default='cuda:1',
help='Device')
def main(data_root, ont, batch_size, epochs, load, device):
go_file = f'{data_root}/go.obo'
model_file = f'{data_root}/{ont}/dgg.th'
terms_file = f'{data_root}/{ont}/terms.pkl'
out_file = f'{data_root}/{ont}/predictions_nextprot_dgg.pkl'
go = Ontology(go_file, with_rels=True)
loss_func = nn.BCELoss()
iprs_dict, terms_dict, graph, train_nids, valid_nids, test_nids, data, labels, test_df = load_data(data_root, ont)
n_terms = len(terms_dict)
n_iprs = len(iprs_dict)
valid_labels = labels[valid_nids].numpy()
test_labels = labels[test_nids].numpy()
labels = labels.to(device)
print(valid_labels.shape)
graph = graph.to(device)
train_nids = train_nids.to(device)
valid_nids = valid_nids.to(device)
test_nids = test_nids.to(device)
net = DeepGraphGOModel(n_iprs, n_terms, device).to(device)
sampler = dgl.dataloading.MultiLayerFullNeighborSampler(2)
train_dataloader = dgl.dataloading.DataLoader(
graph, train_nids, sampler,
batch_size=batch_size,
shuffle=False,
drop_last=False,
num_workers=0)
valid_dataloader = dgl.dataloading.DataLoader(
graph, valid_nids, sampler,
batch_size=batch_size,
shuffle=False,
drop_last=False,
num_workers=0)
test_dataloader = dgl.dataloading.DataLoader(
graph, test_nids, sampler,
batch_size=batch_size,
shuffle=False,
drop_last=False,
num_workers=0)
optimizer = th.optim.Adam(net.parameters(), lr=1e-3)
scheduler = MultiStepLR(optimizer, milestones=[1, 3,], gamma=0.1)
best_loss = 10000.0
if not load:
print('Training the model')
log_file = open(f'{data_root}/train_logs.tsv', 'w')
logger = csv.writer(log_file, delimiter='\t')
for epoch in range(epochs):
net.train()
train_loss = 0
train_steps = int(math.ceil(len(train_nids) / batch_size))
with ck.progressbar(length=train_steps, show_pos=True) as bar:
for input_nodes, output_nodes, blocks in train_dataloader:
bar.update(1)
logits = net(input_nodes, output_nodes, blocks)
batch_labels = labels[output_nodes]
loss = F.binary_cross_entropy(logits, batch_labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss.detach().item()
train_loss /= train_steps
print('Validation')
net.eval()
with th.no_grad():
valid_steps = int(math.ceil(len(valid_nids) / batch_size))
valid_loss = 0
preds = []
with ck.progressbar(length=valid_steps, show_pos=True) as bar:
for input_nodes, output_nodes, blocks in valid_dataloader:
bar.update(1)
logits = net(input_nodes, output_nodes, blocks)
batch_labels = labels[output_nodes]
batch_loss = F.binary_cross_entropy(logits, batch_labels)
valid_loss += batch_loss.detach().item()
preds = np.append(preds, logits.detach().cpu().numpy())
valid_loss /= valid_steps
roc_auc = compute_roc(valid_labels, preds)
print(f'Epoch {epoch}: Loss - {train_loss}, Valid loss - {valid_loss}, AUC - {roc_auc}')
logger.writerow([epoch, train_loss, valid_loss, roc_auc])
if valid_loss < best_loss:
best_loss = valid_loss
print('Saving model')
th.save(net.state_dict(), model_file)
scheduler.step()
log_file.close()
# Loading best model
print('Loading the best model')
net.load_state_dict(th.load(model_file))
net.eval()
with th.no_grad():
test_steps = int(math.ceil(len(test_nids) / batch_size))
test_loss = 0
preds = []
with ck.progressbar(length=test_steps, show_pos=True) as bar:
for input_nodes, output_nodes, blocks in test_dataloader:
bar.update(1)
logits = net(input_nodes, output_nodes, blocks)
batch_labels = labels[output_nodes]
batch_loss = F.binary_cross_entropy(logits, batch_labels)
test_loss += batch_loss.detach().cpu().item()
preds = np.append(preds, logits.detach().cpu().numpy())
test_loss /= test_steps
preds = preds.reshape(-1, n_terms)
roc_auc = compute_roc(test_labels, preds)
print(f'Test Loss - {test_loss}, AUC - {roc_auc}')
preds = list(preds)
# Propagate scores using ontology structure
for i in range(len(preds)):
prop_annots = {}
for go_id, j in terms_dict.items():
score = preds[i][j]
for sup_go in go.get_anchestors(go_id):
if sup_go in prop_annots:
prop_annots[sup_go] = max(prop_annots[sup_go], score)
else:
prop_annots[sup_go] = score
for go_id, score in prop_annots.items():
if go_id in terms_dict:
preds[i][terms_dict[go_id]] = score
test_df['preds'] = preds
test_df.to_pickle(out_file)
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
class Residual(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
def forward(self, x):
return x + self.fn(x)
class MLPBlock(nn.Module):
def __init__(self, in_features, out_features, bias=True, layer_norm=False, dropout=0.5, activation=nn.ReLU):
super().__init__()
self.linear = nn.Linear(in_features, out_features, bias)
self.activation = activation()
self.layer_norm = nn.LayerNorm(out_features) if layer_norm else None
self.dropout = nn.Dropout(dropout) if dropout else None
def forward(self, x):
x = self.activation(self.linear(x))
if self.layer_norm:
x = self.layer_norm(x)
if self.dropout:
x = self.dropout(x)
return x
class DeepGraphGOModel(nn.Module):
def __init__(self, nb_iprs, nb_gos, device, hidden_dim=1024):
super().__init__()
self.nb_gos = nb_gos
self.net1 = MLPBlock(nb_iprs, hidden_dim)
self.conv1 = GraphConv(hidden_dim, hidden_dim)
self.conv2 = GraphConv(hidden_dim, hidden_dim)
input_length = hidden_dim
self.net2 = nn.Sequential(
nn.Linear(hidden_dim, nb_gos),
nn.Sigmoid())
def forward(self, input_nodes, output_nodes, blocks, residual=True):
g1 = blocks[0]
g2 = blocks[1]
features = g1.ndata['feat']['_N']
x = self.net1(features)
x = self.conv1(g1, x)
x = self.conv2(g2, x)
logits = self.net2(x)
return logits
def load_data(data_root, ont):
terms_df = pd.read_pickle(f'{data_root}/{ont}/terms.pkl')
terms = terms_df['gos'].values.flatten()
terms_dict = {v: i for i, v in enumerate(terms)}
print('Terms', len(terms))
ipr_df = pd.read_pickle(f'{data_root}/{ont}/interpros.pkl')
iprs = ipr_df['interpros'].values
iprs_dict = {v:k for k, v in enumerate(iprs)}
train_df = pd.read_pickle(f'{data_root}/{ont}/train_data_int.pkl')
valid_df = pd.read_pickle(f'{data_root}/{ont}/valid_data_int.pkl')
test_df = pd.read_pickle(f'{data_root}/{ont}/nextprot_data.pkl')
df = pd.concat([train_df, valid_df, test_df])
graphs, nids = dgl.load_graphs(f'{data_root}/{ont}/ppi_nextprot.bin')
data, labels = get_data(df, iprs_dict, terms_dict)
graph = graphs[0]
graph.ndata['feat'] = data
graph.ndata['labels'] = labels
train_nids, valid_nids, test_nids = nids['train_nids'], nids['valid_nids'], nids['test_nids']
return iprs_dict, terms_dict, graph, train_nids, valid_nids, test_nids, data, labels, test_df
def get_data(df, iprs_dict, terms_dict):
data = th.zeros((len(df), len(iprs_dict)), dtype=th.float32)
labels = th.zeros((len(df), len(terms_dict)), dtype=th.float32)
for i, row in enumerate(df.itertuples()):
for ipr in row.interpros:
if ipr in iprs_dict:
data[i, iprs_dict[ipr]] = 1
for go_id in row.prop_annotations:
if go_id in terms_dict:
g_id = terms_dict[go_id]
labels[i, g_id] = 1
return data, labels
if __name__ == '__main__':
main()