-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgnn_data.py
328 lines (262 loc) · 12 KB
/
gnn_data.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
import os
import json
import numpy as np
import copy
import torch
import random
from tqdm import tqdm
from utils import UnionFindSet, get_bfs_sub_graph, get_dfs_sub_graph
from torch_geometric.data import Data, Dataset, InMemoryDataset, DataLoader
class GNN_DATA:
def __init__(self, ppi_path, exclude_protein_path=None, max_len=2000, skip_head=True, p1_index=0, p2_index=1, label_index=2, graph_undirection=True, bigger_ppi_path=None):
self.ppi_list = []
self.ppi_emb = []
self.ppi_dict = {}
self.ppi_label_list = []
self.protein_dict = {}
self.esm_dict = {}
self.protein_name = {}
self.ppi_path = ppi_path
self.bigger_ppi_path = bigger_ppi_path
self.max_len = max_len
name = 0
ppi_name = 0
# maxlen = 0
self.node_num = 0
self.edge_num = 0
if exclude_protein_path != None:
with open(exclude_protein_path, 'r') as f:
ex_protein = json.load(f)
f.close()
ex_protein = {p:i for i, p in enumerate(ex_protein)}
else:
ex_protein = {}
class_map = {'reaction':0, 'binding':1, 'ptmod':2, 'activation':3, 'inhibition':4, 'catalysis':5, 'expression':6}
for line in tqdm(open(ppi_path)):
if skip_head:
skip_head = False
continue
line = line.strip().split('\t')
if line[p1_index] in ex_protein.keys() or line[p2_index] in ex_protein.keys():
continue
# get node and node name
if line[p1_index] not in self.protein_name.keys():
self.protein_name[line[p1_index]] = name
name += 1
if line[p2_index] not in self.protein_name.keys():
self.protein_name[line[p2_index]] = name
name += 1
# get edge and its label
temp_data = ""
if line[p1_index] < line[p2_index]:
temp_data = line[p1_index] + "__" + line[p2_index]
else:
temp_data = line[p2_index] + "__" + line[p1_index]
if temp_data not in self.ppi_dict.keys():
self.ppi_dict[temp_data] = ppi_name
temp_label = [0, 0, 0, 0, 0, 0, 0]
temp_label[class_map[line[label_index]]] = 1
self.ppi_label_list.append(temp_label)
ppi_name += 1
else:
index = self.ppi_dict[temp_data]
temp_label = self.ppi_label_list[index]
temp_label[class_map[line[label_index]]] = 1
self.ppi_label_list[index] = temp_label
if bigger_ppi_path != None:
skip_head = True
for line in tqdm(open(bigger_ppi_path)):
if skip_head:
skip_head = False
continue
line = line.strip().split('\t')
if line[p1_index] not in self.protein_name.keys():
self.protein_name[line[p1_index]] = name
name += 1
if line[p2_index] not in self.protein_name.keys():
self.protein_name[line[p2_index]] = name
name += 1
temp_data = ""
if line[p1_index] < line[p2_index]:
temp_data = line[p1_index] + "__" + line[p2_index]
else:
temp_data = line[p2_index] + "__" + line[p1_index]
if temp_data not in self.ppi_dict.keys():
self.ppi_dict[temp_data] = ppi_name
temp_label = [0, 0, 0, 0, 0, 0, 0]
temp_label[class_map[line[label_index]]] = 1
self.ppi_label_list.append(temp_label)
ppi_name += 1
else:
index = self.ppi_dict[temp_data]
temp_label = self.ppi_label_list[index]
temp_label[class_map[line[label_index]]] = 1
self.ppi_label_list[index] = temp_label
i = 0
for ppi in tqdm(self.ppi_dict.keys()):
name = self.ppi_dict[ppi]
assert name == i
i += 1
temp = ppi.strip().split('__')
self.ppi_list.append(temp)
ppi_num = len(self.ppi_list)
self.origin_ppi_list = copy.deepcopy(self.ppi_list)
assert len(self.ppi_list) == len(self.ppi_label_list)
for i in tqdm(range(ppi_num)):
seq1_name = self.ppi_list[i][0]
seq2_name = self.ppi_list[i][1]
# print(len(self.protein_name))
self.ppi_list[i][0] = self.protein_name[seq1_name]
self.ppi_list[i][1] = self.protein_name[seq2_name]
if graph_undirection:
for i in tqdm(range(ppi_num)):
temp_ppi = self.ppi_list[i][::-1]
temp_ppi_label = self.ppi_label_list[i]
# if temp_ppi not in self.ppi_list:
self.ppi_list.append(temp_ppi)
self.ppi_label_list.append(temp_ppi_label)
self.node_num = len(self.protein_name)
self.edge_num = len(self.ppi_list)
def get_protein_aac(self, pseq_path):
# aac: amino acid sequences
self.pseq_path = pseq_path
self.pseq_dict = {}
self.protein_len = []
for line in tqdm(open(self.pseq_path)):
line = line.strip().split('\t')
if line[0] not in self.pseq_dict.keys():
self.pseq_dict[line[0]] = line[1]
self.protein_len.append(len(line[1]))
print("protein num: {}".format(len(self.pseq_dict)))
print("protein average length: {}".format(np.average(self.protein_len)))
print("protein max & min length: {}, {}".format(np.max(self.protein_len), np.min(self.protein_len)))
def embed_normal(self, seq, dim):
if len(seq) > self.max_len:
return seq[:self.max_len]
elif len(seq) < self.max_len:
less_len = self.max_len - len(seq)
return np.concatenate((seq, np.zeros((less_len, dim))))
return seq
def embed_normal2(self, seq):
if len(seq) > 1000:
return seq[:1000]
return seq
def vectorize(self, vec_path):
self.acid2vec = {}
self.dim = None
for line in open(vec_path):
line = line.strip().split('\t')
temp = np.array([float(x) for x in line[1].split()])
self.acid2vec[line[0]] = temp
if self.dim is None:
self.dim = len(temp)
print("acid vector dimension: {}".format(self.dim))
self.pvec_dict = {}
self.pvec_esm = {}
for p_name in tqdm(self.pseq_dict.keys()):
temp_seq = self.pseq_dict[p_name]
# print(p_name)
# print(temp_seq)
# print(1)
temp_vec = []
temp_esm = []
temp_seq_esm=self.embed_normal2(temp_seq)
temp_esm.append([p_name,temp_seq_esm])
# batch_labels, batch_strs, batch_tokens = self.batch_converter(temp_esm)
# results = self.esm_model(batch_tokens, repr_layers=[33], return_contacts=True)
# token_representations = results["representations"][33]
# token_representations = token_representations.detach().numpy()
for acid in temp_seq:
temp_vec.append(self.acid2vec[acid])
temp_vec = np.array(temp_vec)
temp_vec = self.embed_normal(temp_vec, self.dim)
self.pvec_dict[p_name] = temp_vec
# self.pvec_esm[p_name] = token_representations
def get_feature_origin(self, pseq_path, vec_path):
self.get_protein_aac(pseq_path)
self.vectorize(vec_path)
self.protein_dict = {}
# self.esm_dict = {}
for name in tqdm(self.protein_name.keys()):
self.protein_dict[name] = self.pvec_dict[name]
# self.esm_dict[name] = self.pvec_esm[name]
def get_connected_num(self):
self.ufs = UnionFindSet(self.node_num)
ppi_ndary = np.array(self.ppi_list)
for edge in ppi_ndary:
start, end = edge[0], edge[1]
self.ufs.union(start, end)
def generate_data(self):
self.get_connected_num()
print("Connected domain num: {}".format(self.ufs.count))
ppi_list = np.array(self.ppi_list)
ppi_label_list = np.array(self.ppi_label_list)
self.edge_index = torch.tensor(ppi_list, dtype=torch.long)
self.edge_attr = torch.tensor(ppi_label_list, dtype=torch.long)
self.x = []
# self.emb=[]
i = 0
for name in self.protein_name:
assert self.protein_name[name] == i
# print(self.protein_name)
i += 1
self.x.append(self.protein_dict[name])
# self.emb.append(self.esm_dict[name])
# self.emb = np.array(self.emb)
self.x = np.array(self.x)
self.x = torch.tensor(self.x, dtype=torch.float)
# print(self.x.shape)
# print(type(self.x))
# self.emb = torch.tensor(self.emb, dtype=torch.float)
# print(self.emb.shape)
# print(type(self.emb))
# print(self.x)
self.data = Data(x=self.x, edge_index=self.edge_index.T, edge_attr_1=self.edge_attr)
def split_dataset(self, train_valid_index_path, test_size=0.2, random_new=False, mode='random'):
if random_new:
if mode == 'random':
ppi_num = int(self.edge_num // 2)
random_list = [i for i in range(ppi_num)]
random.shuffle(random_list)
self.ppi_split_dict = {}
self.ppi_split_dict['train_index'] = random_list[: int(ppi_num * (1-test_size))]
self.ppi_split_dict['valid_index'] = random_list[int(ppi_num * (1-test_size)) :]
jsobj = json.dumps(self.ppi_split_dict)
with open(train_valid_index_path, 'w') as f:
f.write(jsobj)
f.close()
elif mode == 'bfs' or mode == 'dfs':
print("use {} methed split train and valid dataset".format(mode))
node_to_edge_index = {}
edge_num = int(self.edge_num // 2)
for i in range(edge_num):
edge = self.ppi_list[i]
if edge[0] not in node_to_edge_index.keys():
node_to_edge_index[edge[0]] = []
node_to_edge_index[edge[0]].append(i)
if edge[1] not in node_to_edge_index.keys():
node_to_edge_index[edge[1]] = []
node_to_edge_index[edge[1]].append(i)
node_num = len(node_to_edge_index)
sub_graph_size = int(edge_num * test_size)
if mode == 'bfs':
selected_edge_index = get_bfs_sub_graph(self.ppi_list, node_num, node_to_edge_index, sub_graph_size)
elif mode == 'dfs':
selected_edge_index = get_dfs_sub_graph(self.ppi_list, node_num, node_to_edge_index, sub_graph_size)
all_edge_index = [i for i in range(edge_num)]
unselected_edge_index = list(set(all_edge_index).difference(set(selected_edge_index)))
self.ppi_split_dict = {}
self.ppi_split_dict['train_index'] = unselected_edge_index
self.ppi_split_dict['valid_index'] = selected_edge_index
assert len(unselected_edge_index) + len(selected_edge_index) == edge_num
jsobj = json.dumps(self.ppi_split_dict)
with open(train_valid_index_path, 'w') as f:
f.write(jsobj)
f.close()
else:
print("your mode is {}, you should use bfs, dfs or random".format(mode))
return
else:
with open(train_valid_index_path, 'r') as f:
self.ppi_split_dict = json.load(f)
f.close()