-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
147 lines (127 loc) · 4.68 KB
/
utils.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
#import h5py
import numpy as np
from rdkit import Chem
def convert_to_smiles(vector, char):
list_char = list(char)
#list_char = char.tolist()
vector = vector.astype(int)
return "".join(map(lambda x: list_char[x], vector)).strip()
def stochastic_convert_to_smiles(vector, char):
list_char = char.tolist()
s = ""
for i in range(len(vector)):
prob = vector[i].tolist()
norm0 = sum(prob)
prob = [i/norm0 for i in prob]
index = np.random.choice(len(list_char), 1, p=prob)
s+=list_char[index[0]]
return s
def one_hot_array(i, n):
return list(map(int, [ix == i for ix in range(n)]))
def one_hot_index(vec, charset):
return list(map(charset.index, vec))
def from_one_hot_array(vec):
oh = np.where(vec == 1)
if oh[0].shape == (0, ):
return None
return int(oh[0][0])
def decode_smiles_from_indexes(vec, charset):
return "".join(map(lambda x: charset[x], vec)).strip()
def load_dataset(filename, split = True):
h5f = h5py.File(filename, 'r')
if split:
data_train = h5f['data_train'][:]
else:
data_train = None
data_test = h5f['data_test'][:]
charset = h5f['charset'][:]
h5f.close()
if split:
return data_train, data_test, charset
else:
return data_test, charset
def encode_smiles(smiles, model, charset):
cropped = list(smiles.ljust(120))
preprocessed = np.array([list(map(lambda x: one_hot_array(x, len(charset)), one_hot_index(cropped, charset)))])
latent = model.encoder.predict(preprocessed)
return latent
def smiles_to_onehot(smiles, charset):
cropped = list(smiles.ljust(120))
preprocessed = np.array([list(map(lambda x: one_hot_array(x, len(charset)), one_hot_index(cropped, charset)))])
return preprocessed
def smiles_to_vector(smiles, vocab, max_length):
while len(smiles)<max_length:
smiles +=" "
return [vocab.index(str(x)) for x in smiles]
def decode_latent_molecule(latent, model, charset, latent_dim):
decoded = model.decoder.predict(latent.reshape(1, latent_dim)).argmax(axis=2)[0]
smiles = decode_smiles_from_indexes(decoded, charset)
return smiles
def interpolate(source_smiles, dest_smiles, steps, charset, model, latent_dim):
source_latent = encode_smiles(source_smiles, model, charset)
dest_latent = encode_smiles(dest_smiles, model, charset)
step = (dest_latent - source_latent) / float(steps)
results = []
for i in range(steps):
item = source_latent + (step * i)
decoded = decode_latent_molecule(item, model, charset, latent_dim)
results.append(decoded)
return results
def get_unique_mols(mol_list):
inchi_keys = [Chem.InchiToInchiKey(Chem.MolToInchi(m)) for m in mol_list]
u, indices = np.unique(inchi_keys, return_index=True)
unique_mols = [[mol_list[i], inchi_keys[i]] for i in indices]
return unique_mols
def accuracy(arr1, arr2, length):
total = len(arr1)
count1=0
count2=0
count3=0
for i in range(len(arr1)):
if np.array_equal(arr1[i,:length[i]], arr2[i,:length[i]]):
count1+=1
for i in range(len(arr1)):
for j in range(length[i]):
if arr1[i][j]==arr2[i][j]:
count2+=1
count3+=1
return float(count1/float(total)), float(count2/count3)
def extract_vocab(filename, seq_length):
import collections
with open(filename) as f:
lines = f.read().split('\n')[:-1]
lines = [l.split() for l in lines]
lines = [l for l in lines if len(l[0])<seq_length-2]
smiles = [l[0] for l in lines]
total_string = ''
for s in smiles:
total_string+=s
counter = collections.Counter(total_string)
count_pairs = sorted(counter.items(), key=lambda x: -x[1])
chars, counts = zip(*count_pairs)
vocab = dict(zip(chars, range(len(chars))))
chars+=('E',) #End of smiles
chars+=('X',) #Start of smiles
vocab['E'] = len(chars)-2
vocab['X'] = len(chars)-1
return chars, vocab
def load_data(filename, seq_length, char, vocab):
with open(filename) as f:
lines = f.read().split('\n')[:-1]
smiles = [l for l in lines if len(l)<seq_length-2]
smiles_input = []
smiles_output = []
length = []
for s in smiles:
length.append(len(s)+1)
s1 = ('X'+s).ljust(seq_length, 'E')
s2 = s.ljust(seq_length, 'E')
list1 = list(map(vocab.get, s1))
list2 = list(map(vocab.get, s2))
if None in list1 or None in list2: continue
smiles_input.append(list1)
smiles_output.append(list2)
smiles_input = np.array(smiles_input)
smiles_output = np.array(smiles_output)
length = np.array(length)
return smiles_input, smiles_output, length