-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_mine.py
373 lines (297 loc) · 13.8 KB
/
utils_mine.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import csv
import os
import random
import sys
from rdkit import Chem
import networkx as nx
from dgl import DGLGraph
import numpy as np
import torch
import dgl
from esm_model import *
def cut_long_preds(seq1, seq2):
if len(seq1) > len(seq2):
# seq1 = [seq1[i] for i in range(len(seq2))]
seq1 = random.sample(seq1, len(seq2))
else:
# seq2 = [seq2[i] for i in range(len(seq1))]
seq2 = random.sample(seq2, len(seq1))
seq = seq1 + seq2
seq.sort(key=lambda x: x[1], reverse=True)
return seq
def read_triple(file_path, entity2id, relation2id):
"""
Read triples and map them into ids.
"""
triples = []
with open(file_path) as fin:
for line in fin:
h, r, t = line.strip().split('\t')
etth = entity2id[h]
rlt = relation2id[r]
ettt = entity2id[t]
triples.append((etth, rlt, ettt))
return triples
def write_triples2file(triples, filepath, id2entity, id2relation):
with open(filepath, "w") as fout:
csv_writer = csv.writer(fout, delimiter="\t")
for h, r, t in triples:
csv_writer.writerow([id2entity[h], id2relation[r], id2entity[t]])
def prepare_self_data(ent_id2seq_path, ent_id2smiles_path):
def load_id2feat(filepath):
result = dict()
with open(filepath, "r") as fin:
csv_rdr = csv.reader(fin)
for line in csv_rdr:
result[int(line[0])] = line[1]
return result
def split_grams(seq, n=3):
one = zip(*[iter(seq)] * n) # handle first seq
two = zip(*[iter(seq[1:])] * n)
three = zip(*[iter(seq[2:])] * n)
total = [one, two, three]
str_ngram = set()
for ngrams in total:
for ngram in ngrams:
str_ngram.add(''.join(ngram))
# str_ngram.append(x)
return list(str_ngram)
def store_words_into_dict(sequences):
file_path = "data_rev/protein_words_dict_full_1_3.npy"
if os.path.isfile(file_path):
words = np.load(file_path, allow_pickle=True)
return words.item()
else:
words = dict()
words['-+-'] = 0
max_length = 0
print('process sequence')
count = 0
for seq in sequences:
count += 1
# print('{}/{}'.format(count, lens))
ngram_words = split_grams(seq)
if max_length < len(ngram_words):
max_length = len(ngram_words)
for w in ngram_words:
if w not in words:
words[w] = len(words)
np.save(file_path, words)
print('max words length of protein is {}'.format(max_length))
return words
def label_sequence_by_words(seq, words_dict, max_lenght=1200):
ngrams_words = split_grams(seq)
x = np.zeros(max_lenght, dtype=int)
for i, word in enumerate(ngrams_words[:max_lenght]):
if word in words_dict:
x[i] = words_dict[word]
else:
x[i] = words_dict['-+-']
return x
def smiles2graph(smile):
def atom_features(atom):
def one_of_k_encoding_unk(x, allowable_set):
"""Maps inputs not in the allowable set to the last element."""
if x not in allowable_set:
x = allowable_set[-1]
return list(map(lambda s: x == s, allowable_set))
def one_of_k_encoding(x, allowable_set):
if x not in allowable_set:
raise Exception("input {0} not in allowable set{1}:".format(x, allowable_set))
return list(map(lambda s: x == s, allowable_set))
return np.array(one_of_k_encoding_unk(atom.GetSymbol(),
['C', 'N', 'O', 'S', 'F', 'Si', 'P', 'Cl', 'Br', 'Mg', 'Na', 'Ca',
'Fe', 'As', 'Al', 'I', 'B', 'V', 'K', 'Tl', 'Yb', 'Sb', 'Sn',
'Ag', 'Pd', 'Co', 'Se', 'Ti', 'Zn', 'H', 'Li', 'Ge', 'Cu', 'Au',
'Ni', 'Cd', 'In', 'Mn', 'Zr', 'Cr', 'Pt', 'Hg', 'Pb',
'Unknown']) +
one_of_k_encoding(atom.GetDegree(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
one_of_k_encoding_unk(atom.GetTotalNumHs(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
one_of_k_encoding_unk(atom.GetImplicitValence(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
[atom.GetIsAromatic()])
mol = Chem.MolFromSmiles(smile)
if mol is None:
return None, None, None
c_size = mol.GetNumAtoms()
features = []
for atom in mol.GetAtoms():
feature = atom_features(atom)
features.append(feature / sum(feature))
edges = []
for bond in mol.GetBonds():
edges.append([bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()])
g = nx.Graph(edges).to_directed()
edge_index = []
for e1, e2 in g.edges:
edge_index.append([e1, e2])
return c_size, features, edge_index
def get_graph_features(c_id, smiles2graph):
h = list()
c_size, features, edge_index = smiles2graph[c_id]
g = DGLGraph()
g.add_nodes(c_size)
if edge_index:
edge_index = np.array(edge_index)
g.add_edges(edge_index[:, 0], edge_index[:, 1])
for f in features:
h.append(f)
g.ndata['x'] = torch.from_numpy(np.array(features)).float()
g = dgl.add_self_loop(g)
return g, h
ent_id2seq = load_id2feat(ent_id2seq_path)
proteins_list = list(set(ent_id2seq.values()))
words_dict = store_words_into_dict(proteins_list)
for ent_id, seq in ent_id2seq.items():
ent_id2seq[ent_id] = label_sequence_by_words(seq, words_dict)
word_length = len(words_dict)
ent_id2smiles = load_id2feat(ent_id2smiles_path)
invalid_smiles_drug_ids = list()
for drug_id, smiles in ent_id2smiles.items():
c_size, features, edge_index = smiles2graph(smiles)
if c_size is None and features is None and edge_index is None:
invalid_smiles_drug_ids.append(
drug_id) # the iteration mechanism of python does not support del element in loop
else:
ent_id2smiles[drug_id] = (c_size, features, edge_index)
for drug_id in invalid_smiles_drug_ids:
ent_id2smiles.pop(drug_id)
for drug_id in ent_id2smiles:
smiles_g, smiles_h = get_graph_features(
drug_id, ent_id2smiles)
ent_id2smiles[drug_id] = (smiles_g, smiles_h)
return ent_id2seq, ent_id2smiles, word_length
def prepare_self_esm_data(ent_id2seq_path, ent_id2smiles_path, esm_converter):
def load_id2seq(filepath):
data = list()
long_seq_index = list()
seq_1023_index = list()
with open(filepath, "r") as fin:
csv_rdr = csv.reader(fin)
for i, line in enumerate(csv_rdr):
seq = line[1]
length = len(line[1])
label = line[0]
if length > 1022:
data.append((label, seq[int(length / 2) - 511:int(length / 2) + 511]))
if length == 1023:
seq_1023_index.append(i)
else:
long_seq_index.append(i)
else:
data.append((label, seq))
_, _, batch_tokens = esm_converter(data)
# if the sequence has its length exceeding the max length, no start and finish token should be added
for index in seq_1023_index:
batch_tokens[index, -1] = 1
for index in long_seq_index:
batch_tokens[index, -1] = 1
batch_tokens[index, 0] = 1
ent_id2seq = dict()
for i, (idx, _) in enumerate(data):
ent_id2seq[int(idx)] = batch_tokens[i].tolist()
return ent_id2seq
def load_id2smiles(filepath):
result = dict()
with open(filepath, "r") as fin:
csv_rdr = csv.reader(fin)
for line in csv_rdr:
result[int(line[0])] = line[1]
return result
def smiles2graph(smile):
def atom_features(atom):
def one_of_k_encoding_unk(x, allowable_set):
"""Maps inputs not in the allowable set to the last element."""
if x not in allowable_set:
x = allowable_set[-1]
return list(map(lambda s: x == s, allowable_set))
def one_of_k_encoding(x, allowable_set):
if x not in allowable_set:
raise Exception("input {0} not in allowable set{1}:".format(x, allowable_set))
return list(map(lambda s: x == s, allowable_set))
return np.array(one_of_k_encoding_unk(atom.GetSymbol(),
['C', 'N', 'O', 'S', 'F', 'Si', 'P', 'Cl', 'Br', 'Mg', 'Na', 'Ca',
'Fe', 'As', 'Al', 'I', 'B', 'V', 'K', 'Tl', 'Yb', 'Sb', 'Sn',
'Ag', 'Pd', 'Co', 'Se', 'Ti', 'Zn', 'H', 'Li', 'Ge', 'Cu', 'Au',
'Ni', 'Cd', 'In', 'Mn', 'Zr', 'Cr', 'Pt', 'Hg', 'Pb',
'Unknown']) +
one_of_k_encoding(atom.GetDegree(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
one_of_k_encoding_unk(atom.GetTotalNumHs(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
one_of_k_encoding_unk(atom.GetImplicitValence(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
[atom.GetIsAromatic()])
mol = Chem.MolFromSmiles(smile)
if mol is None:
return None, None, None
c_size = mol.GetNumAtoms()
features = []
for atom in mol.GetAtoms():
feature = atom_features(atom)
features.append(feature / sum(feature))
edges = []
for bond in mol.GetBonds():
edges.append([bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()])
g = nx.Graph(edges).to_directed()
edge_index = []
for e1, e2 in g.edges:
edge_index.append([e1, e2])
return c_size, features, edge_index
def get_graph_features(c_id, smiles2graph):
h = list()
c_size, features, edge_index = smiles2graph[c_id]
g = DGLGraph()
g.add_nodes(c_size)
if edge_index:
edge_index = np.array(edge_index)
g.add_edges(edge_index[:, 0], edge_index[:, 1])
for f in features:
h.append(f)
# g.ndata['x'] = torch.from_numpy(np.array(features)).float() # CHGH
g = dgl.add_self_loop(g)
deg = g.in_degrees().float().clamp(min=1) # in_degree is 入度. clamp is 夹紧
norm = torch.pow(deg, -0.5) # for Equ6
g.ndata['d'] = norm # set features called "d" for all nodes
return g, h
ent_id2seq = load_id2seq(ent_id2seq_path)
ent_id2smiles = load_id2smiles(ent_id2smiles_path)
invalid_smiles_drug_ids = list()
for drug_id, smiles in ent_id2smiles.items():
c_size, features, edge_index = smiles2graph(smiles)
if c_size is None and features is None and edge_index is None:
invalid_smiles_drug_ids.append(
drug_id) # the iteration mechanism of python does not support del element in loop
else:
ent_id2smiles[drug_id] = (c_size, features, edge_index)
for drug_id in invalid_smiles_drug_ids:
ent_id2smiles.pop(drug_id)
for drug_id in ent_id2smiles:
smiles_g, smiles_h = get_graph_features(
drug_id, ent_id2smiles)
ent_id2smiles[drug_id] = (smiles_g, smiles_h)
return ent_id2seq, ent_id2smiles
def save2file(obj, save_directory, start_time, info=""):
file_path = start_time
for i in range(0, len(sys.argv)):
file_path += f"__{sys.argv[i]}"
file_path = (file_path.replace("/", "SLH").replace("__--", "--"))[0:80]
file_path = f"{save_directory}/{file_path}"
file_path = file_path + "_" + info
if isinstance(obj, torch.nn.Module):
file_path += ".pth"
else:
file_path += ".pickle"
print(file_path)
with open(file_path, "wb") as fout:
if isinstance(obj, torch.nn.Module):
st_dict = obj.state_dict()
if isinstance(obj, SelfModel):
self_states = dict()
for name, value in st_dict.items():
if "esm_model." not in name:
self_states[name] = value
st_dict = self_states
torch.save((obj.kwargs, st_dict), fout)
else:
pickle.dump(obj, fout)
return file_path