forked from devalab/SpectraToStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
223 lines (182 loc) · 6.95 KB
/
environment.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
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit import RDLogger
import numpy as np
import copy
import random
from collections import OrderedDict
from rdkit.Chem import rdMolDescriptors as rdDesc
from .molecular_graph import *
from .molecule_state import MolState
import torch
import warnings
from rdkit.Chem.QED import qed
from rdkit.Chem import AllChem
from rdkit import DataStructs
from .RPNMR import predictor_new as P
from scipy.stats import wasserstein_distance
import pickle
from copy import deepcopy
import ray
with open("../../data/10kMolSplit.pkl", "rb") as inFile:
dat = pickle.load(inFile)
dat = dat["test_df"]
lg = RDLogger.logger()
lg.setLevel(RDLogger.CRITICAL)
rdBase.DisableLog('rdApp.error')
warnings.filterwarnings("ignore")
class Env:
def __init__(self, molForm, targetSpectra,episode_actor):
self.molForm = molForm
self.state = MolState(molForm,targetSpectra)
self.targetSpectra = targetSpectra
self.episode_actor = episode_actor
def __del__(self) :
pass
def __str__(self):
return "Current Env State : " + str(self.state) + " MolForm : " + str(self.molForm)
def convertStrToList(self, string):
return [int(i) for i in string.strip('][').split(', ')]
def reset(self,index):
while True:
row = dat.iloc[index]
molForm = row.MolForm
self.molForm = self.convertStrToList(molForm)
self.targetSpectra = row.Spectrum
if self.molForm[0] == len(self.targetSpectra):
break
index = index + 1
self.targetmol = row.Smiles
self.targetrdmol = row.Rdmol
self.state = MolState(self.molForm, self.targetSpectra)
def reward(self, state = None, target = None):
if state is None:
state = self.state
if target is None:
target = self.targetSpectra
mol = deepcopy(self.state.rdmol)
Chem.SanitizeMol(mol)
target_nmr = self.targetSpectra[:]
S, D, T, Q = 0, 0, 0, 0
for atom in mol.GetAtoms():
if atom.GetAtomicNum() == 6:
if atom.GetNumImplicitHs() == 0:
S += 1
if atom.GetNumImplicitHs() == 1:
D += 1
if atom.GetNumImplicitHs() == 2:
T += 1
if atom.GetNumImplicitHs() == 3:
Q += 1
splits = [i[1] for i in target_nmr]
if splits.count('Q') > Q:
return 0
elif splits.count('Q') == Q and splits.count('T') > T:
return 0
elif splits.count('Q') == Q and splits.count('T') == T and splits.count('D') > D:
return 0
predicted_nmr = ray.get(self.episode_actor.predict.remote(Chem.MolToSmiles(mol)))
if predicted_nmr == -1:
return 0
predicted_nmr = np.array(predicted_nmr)
if(np.any(predicted_nmr<0)):
reward = 0
return 0
predicted_nmr /= 220
target_nmr = [i[0] for i in target_nmr]
target_nmr = np.array(target_nmr, dtype=np.float64)
target_nmr /= 220
reward = 1 - wasserstein_distance(predicted_nmr,target_nmr)
return 2*(reward-0.5)
def terminal_reward(self, state = None, target = None):
if state is None:
state = self.state
if target is None:
target = self.targetSpectra
mol = deepcopy(state.rdmol)
Chem.SanitizeMol(mol)
if state.numInRdmol < state.totalNumOfAtoms:
return 0
predicted_nmr = ray.get(self.episode_actor.predict.remote(Chem.MolToSmiles(mol)))
if predicted_nmr == -1:
return 0
predicted_nmr = np.array(predicted_nmr)
if(np.any(predicted_nmr<0)):
reward = 0
return 0
predicted_nmr /= 220
target_nmr = self.targetSpectra[:]
target_nmr = [i[0] for i in target_nmr]
if len(target_nmr) != self.molForm[0]:
print(self.targetSpectra)
print(target_nmr)
raise "Target NMR Error"
target_nmr = np.array(target_nmr, dtype=np.float64)
target_nmr /= 220
reward = 1 - wasserstein_distance(predicted_nmr,target_nmr)
return 2*(reward-0.5)
def invalidAction(self):
raise Exception("Invalid Action has been chosen :(")
def isTerminal(self, state: MolState = None):
try:
if state is None:
state = self.state
if sum(state.valid_actions()) == 0:
return True
if state.numInRdmol < state.totalNumOfAtoms:
return False
target_nmr = self.targetSpectra[:]
mol = deepcopy(state.rdmol)
Chem.SanitizeMol(mol)
# mol = Chem.AddHs(mol)
S, D, T, Q = 0, 0, 0, 0
for atom in mol.GetAtoms():
if atom.GetAtomicNum() == 6:
if atom.GetNumImplicitHs() == 0:
S += 1
if atom.GetNumImplicitHs() == 1:
D += 1
if atom.GetNumImplicitHs() == 2:
T += 1
if atom.GetNumImplicitHs() == 3:
Q += 1
splits = [i[1] for i in target_nmr]
if splits.count('Q') > Q:
return True
elif splits.count('Q') == Q and splits.count('T') > T:
return True
elif splits.count('Q') == Q and splits.count('T') == T and splits.count('D') > D:
return True
elif splits.count('Q') == Q and splits.count('T') == T and splits.count('D') == D and splits.count('S') == S:
return True
if S > splits.count('S'):
return True
elif S == splits.count('S') and D > splits.count('D'):
return True
elif S == splits.count('S') and D == splits.count('D') and T > splits.count('T'):
return True
if self.terminal_reward(state) > 0.99 :
return True
else:
return False
except Exception as e:
print(e)
print(Chem.MolToSmiles(self.state.rdmol))
def step(self, actionInt: int, state: MolState = None):
if state is None:
state = self.state
valid_actions = state.action_mask
if valid_actions[actionInt] == 0:
action = state._actionIntToList(actionInt)
print(state._actionIntToList(actionInt))
print(state)
return self.invalidAction()
state.doStep(state._actionIntToList(actionInt))
terminal = self.isTerminal(state)
_ = state.valid_actions()
if terminal:
reward = self.reward(state)
return state, reward,terminal
else:
return state, 0,terminal
# %%