-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_node_type.py
266 lines (209 loc) · 7.53 KB
/
add_node_type.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
from subprocess import Popen, PIPE
from math import *
import random
import numpy as np
from copy import deepcopy
#from types import IntType, ListType, TupleType, StringTypes
import itertools
import time
import math
import argparse
import subprocess
from load_model import loaded_model
from keras.preprocessing import sequence
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import Descriptors
from rdkit.Chem import MolFromSmiles, MolToSmiles
import sys
from rdkit.Chem import AllChem
from make_smile import zinc_data_with_bracket_original, zinc_processed_with_bracket
import sascorer
import pickle
import gzip
import networkx as nx
from rdkit.Chem import rdmolops
def expanded_node(model,state,val):
exp=[]
st_time=time.time()
all_nodes=[]
end="\n"
#position=[]
position=[]
position.extend(state)
total_generated=[]
new_compound=[]
get_int_old=[]
for j in range(len(position)):
get_int_old.append(val.index(position[j]))
get_int=get_int_old
x=np.reshape(get_int,(1,len(get_int)))
x_pad= sequence.pad_sequences(x, maxlen=82, dtype='int32',
padding='post', truncating='pre', value=0.)
#for i in range(1):
predictions=model.predict(x_pad)
#print "shape of RNN",predictions.shape
preds=np.asarray(predictions[0][len(get_int)-1]).astype('float64')
#print ("softmax?:",preds)
preds = np.log(preds) / 1.0
preds = np.exp(preds) / np.sum(np.exp(preds))
#sort_preds=preds.sort(reverse = True)
sort_index = np.argsort(-preds)
#print (sort_index)
i=0
sum_preds=preds[sort_index[i]]
all_nodes.append(sort_index[i])
#print (sum_preds)
while sum_preds<=0.95:
i+=1
all_nodes.append(sort_index[i])
sum_preds+=preds[sort_index[i]]
#print ("preds:",preds)
#for i in range(30):
# next_probas = np.random.multinomial(1, preds, 1)
#print ("next_probas:",next_probas)
# next_int=np.argmax(next_probas)
#get_int.append(next_int)
# all_nodes.append(next_int)
all_nodes=list(set(all_nodes))
fi_time=time.time()-st_time
print ("exp time:",fi_time)
print (all_nodes)
#total_generated.append(get_int)
#all_posible.extend(total_generated)
return all_nodes,fi_time
def node_to_add(all_nodes,val):
added_nodes=[]
for i in range(len(all_nodes)):
added_nodes.append(val[all_nodes[i]])
#print added_nodes
return added_nodes
def chem_kn_simulation(model,state,val,added_nodes):
all_posible=[]
print ("added_nodes:",added_nodes)
end="\n"
#val2=['C', '(', ')', 'c', '1', '2', 'o', '=', 'O', 'N', '3', 'F', '[C@@H]', 'n', '-', '#', 'S', 'Cl', '[O-]', '[C@H]', '[NH+]', '[C@]', 's', 'Br', '/', '[nH]', '[NH3+]', '4', '[NH2+]', '[C@@]', '[N+]', '[nH+]', '\\', '[S@]', '5', '[N-]', '[n+]', '[S@@]', '[S-]', '6', '7', 'I', '[n-]', 'P', '[OH+]', '[NH-]', '[P@@H]', '[P@@]', '[PH2]', '[P@]', '[P+]', '[S+]', '[o+]', '[CH2-]', '[CH-]', '[SH+]', '[O+]', '[s+]', '[PH+]', '[PH]', '8', '[S@@+]']
for i in range(len(added_nodes)):
#position=[]
position=[]
position.extend(state)
position.append(added_nodes[i])
#print state
#print position
#print len(val2)
total_generated=[]
new_compound=[]
get_int_old=[]
for j in range(len(position)):
get_int_old.append(val.index(position[j]))
get_int=get_int_old
x=np.reshape(get_int,(1,len(get_int)))
x_pad= sequence.pad_sequences(x, maxlen=82, dtype='int32',
padding='post', truncating='pre', value=0.)
while not get_int[-1] == val.index(end):
predictions=model.predict(x_pad)
#print "shape of RNN",predictions.shape
preds=np.asarray(predictions[0][len(get_int)-1]).astype('float64')
preds = np.log(preds) / 1.0
preds = np.exp(preds) / np.sum(np.exp(preds))
next_probas = np.random.multinomial(1, preds, 1)
#print predictions[0][len(get_int)-1]
#print "next probas",next_probas
#next_int=np.argmax(predictions[0][len(get_int)-1])
next_int=np.argmax(next_probas)
a=predictions[0][len(get_int)-1]
next_int_test=sorted(range(len(a)), key=lambda i: a[i])[-10:]
get_int.append(next_int)
x=np.reshape(get_int,(1,len(get_int)))
x_pad = sequence.pad_sequences(x, maxlen=82, dtype='int32',
padding='post', truncating='pre', value=0.)
if len(get_int)>82:
break
total_generated.append(get_int)
all_posible.extend(total_generated)
return all_posible
def predict_smile(all_posible,val):
new_compound=[]
for i in range(len(all_posible)):
total_generated=all_posible[i]
generate_smile=[]
for j in range(len(total_generated)-1):
generate_smile.append(val[total_generated[j]])
generate_smile.remove("&")
new_compound.append(generate_smile)
return new_compound
def make_input_smile(generate_smile):
new_compound=[]
for i in range(len(generate_smile)):
middle=[]
for j in range(len(generate_smile[i])):
middle.append(generate_smile[i][j])
com=''.join(middle)
new_compound.append(com)
#print new_compound
#print len(new_compound)
return new_compound
def check_node_type(new_compound,SA_mean,SA_std,logP_mean,logP_std,cycle_mean,cycle_std):
node_index=[]
valid_compound=[]
logp_value=[]
all_smile=[]
distance=[]
#print "SA_mean:",SA_mean
#print "SA_std:",SA_std
#print "logP_mean:",logP_mean
#print "logP_std:",logP_std
#print "cycle_mean:",cycle_mean
#print "cycle_std:",cycle_std
activity=[]
score=[]
for i in range(len(new_compound)):
try:
m = Chem.MolFromSmiles(str(new_compound[i]))
except:
print (None)
if m!=None and len(new_compound[i])<=81:
try:
logp=Descriptors.MolLogP(m)
except:
logp=-1000
node_index.append(i)
valid_compound.append(new_compound[i])
SA_score = -sascorer.calculateScore(MolFromSmiles(new_compound[i]))
cycle_list = nx.cycle_basis(nx.Graph(rdmolops.GetAdjacencyMatrix(MolFromSmiles(new_compound[i]))))
if len(cycle_list) == 0:
cycle_length = 0
else:
cycle_length = max([ len(j) for j in cycle_list ])
if cycle_length <= 6:
cycle_length = 0
else:
cycle_length = cycle_length - 6
cycle_score = -cycle_length
#print cycle_score
#print SA_score
#print logp
SA_score_norm=(SA_score-SA_mean)/SA_std
logp_norm=(logp-logP_mean)/logP_std
cycle_score_norm=(cycle_score-cycle_mean)/cycle_std
score_one = SA_score_norm+ logp_norm + cycle_score_norm
score.append(score_one)
all_smile.append(new_compound[i])
return node_index,score,valid_compound,all_smile
def logp_calculation(new_compound):
#print new_compound[0]
logp_value=[]
valid_smile=[]
all_smile=[]
distance=[]
m = Chem.MolFromSmiles(str(new_compound[0]))
try:
if m is not None:
logp=Descriptors.MolLogP(m)
valid_smile.append(new_compound)
else:
logp=-100
except:
logp=-100
all_smile.append(str(new_compound[0]))
return logp,valid_smile,all_smile