forked from jku-vds-lab/cime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleSHAP.py
262 lines (198 loc) · 9.78 KB
/
ExampleSHAP.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
# %%
#####################################
### import the used functions
#####################################
from rdkit import Chem
from rdkit.Chem import AllChem, DataStructs, Descriptors
from rdkit.Chem import PandasTools
import pandas as pd
import catboost
import shap
import numpy as np
#######################################
###define the used functions
#######################################
def smiles_to_mols(smis):
"""Convert a list of smiles to a list of RDKit Mol objects"""
if not isinstance(smis, list):
raise TypeError("Please provide smiles as a list")
mols = []
successful_inds = []
for ind, smi in enumerate(smis):
# RDKit will sometimes 'fail' but instead of throwing an
# error it will (sometimes!) print a warning and return None.
m = Chem.MolFromSmiles(smi, sanitize=True)
if m is None:
print("Mol generation failed for", smi, "(index", ind, ")\n")
else:
mols.append(m)
successful_inds.append(ind)
return mols, successful_inds
# define a function to calculate count based morgan fingerprints
def calc_morgan_counts_for_smiles(mol, radius=2, nBits=2048):
try:
fpCounts = AllChem.GetHashedMorganFingerprint(
mol, radius, nBits=nBits, includeRedundantEnvironments=False)
array = np.zeros((0,), dtype=np.int8)
DataStructs.ConvertToNumpyArray(fpCounts, array)
except:
print("Failure in calc_morgan_counts_for_smiles()")
array = np.repeat(np.nan, nBits)
return array
def getSubstructIndex(mol,atomID,radius):
if radius>0:
env = Chem.FindAtomEnvironmentOfRadiusN(mol,radius,atomID)
atomsToUse=[]
for b in env:
atomsToUse.append(mol.GetBondWithIdx(b).GetBeginAtomIdx())
atomsToUse.append(mol.GetBondWithIdx(b).GetEndAtomIdx())
atomsToUse = list(set(atomsToUse))
else:
atomsToUse = [atomID]
env=None
return(atomsToUse)
def calc_indices_for_fingerprints(smiles,radius = 2,nBits=2048):
mol = Chem.MolFromSmiles(smiles,sanitize = True)
bi = {}
fp = AllChem.GetHashedMorganFingerprint(mol,radius = radius, nBits=nBits, includeRedundantEnvironments=False,bitInfo=bi)
fp = fp.GetNonzeroElements()
#weights = [0 for i in mol.GetAtoms()]
#weights[slice(mol.GetSubstructMatch(Chem.MolFromSmiles('CNC(=O)C(C)(C)C',sanitize = True)))] += 1
fp_indices = {}
for fragment_id, count in fp.items():
sub_indices = []
for i in range(count):
#print(i)
root, radius = bi[fragment_id][i]
subindex = getSubstructIndex(mol,root,radius)
sub_indices = sub_indices + subindex
sub_indices = list(set(sub_indices))
fp_indices[fragment_id] = sub_indices
fp_indices = {"fp_morgan_counts_" + str(k): v for k, v in fp_indices.items() }
return(fp_indices)
def atom_attributions(mol, indices, shap_vals, featureNames, direction="both"):
"""
Sum up the attributions to the model predictions (Shap values) for each atom in mol.
Parameters:
mol: rdkit molecule object
indices (dict): a dictionary with keys = feature names and values = list of atom positions in mol (output of calc_indices_for_fingerprints())
shap_vals: a numpy array of dimensions (len(featureNames))
featureNames (np.array): numpy array containing the feature names for shap_vals
direction (str): whether to consider positive (up), negative (down) or both shap_vals contributions
Returns:
np.array: each element = 1 atom of mol and the values are the summed contributions from shap_vals
"""
## TO DO: add support for MACCS keys
# get for each fingerprint bit the atom numbers mapping to that bit and initialise weights to 0
curWeights = np.array([0 for i in range(mol.GetNumAtoms())], 'float64')
# step through each index (dictionary)
for j in indices:
# retrieve index to subset into the featureNames and the corresponding shap values array
curFPIndex = [i for i, s in enumerate(featureNames) if s == j]
if len(curFPIndex) == 0:
continue
if len(curFPIndex) > 1:
raise IndexError("Found several matches for feature " + j)
# do something only if (i) the fingerprint bit was in the selected features
# and (ii) only if the retrieved shap value is positive/negative (direction argument)
if (direction == "up" and shap_vals[curFPIndex[0]] > 0) or (direction == "down" and shap_vals[curFPIndex[0]] < 0) or (direction == "both"):
# use the atom numbers ('positions') to update the curWeights array, where
# each atom of the current molecule is represented
# one atom may be part of several fingerprints so the summing has to be done across
# all fingerprints
for pos in indices[j]:
#value = (curWeights[pos] + shap_vals[curFPIndex[0]])
value = (curWeights[pos] + (shap_vals[curFPIndex[0]]/len(indices[j])))
curWeights[pos] = value
return curWeights
#######################################################
##define some smiles
#######################################################
smiles = ['Nc1nc(NC2CC2)c2ncn(C3C=CC(CO)C3)c2n1',
'CC(=O)NCCCS(=O)(=O)O',
'CCCC(=O)Nc1ccc(OCC(O)CNC(C)C)c(C(C)=O)c1',
'CC(=O)Nc1ccc(O)cc1',
'CC(=O)Nc1nnc(S(N)(=O)=O)s1',
'CC(=O)NO'
]
################################################################################
##make sure that the smiles are canonical to have full compatibility with CIME
################################################################################
#canonicalize smiles
mols, successful_inds = smiles_to_mols(smiles)
# get canonical smiles
smiles = [Chem.MolToSmiles(m) for m in mols]
#generate some names for the smiles
smiles_names = [i for i in range(len(smiles))]
################################################################################
##calculate the logp for each of them
################################################################################
logp = [Descriptors.MolLogP(i) for i in mols]
################################################################################
##calculate the morgan fingerprints
################################################################################
# calculate the morgan counts fingerprints
featuresMorganCounts = [calc_morgan_counts_for_smiles(
mol, radius=2, nBits=2048) for mol in mols]
columns = ["fp_morgan_counts_" + str(i) for i in range(2048)]
featuresMorganCounts = pd.DataFrame(featuresMorganCounts, columns=[
"fp_morgan_counts_" + str(i) for i in range(2048)])
##################################################
##train a model on the values
##################################################
model = catboost.CatBoostRegressor()
# Fit model
model.fit(featuresMorganCounts, logp)
##################################################
##calculate the shap values
##################################################
explainer = shap.TreeExplainer(model)
shap_vals = explainer.shap_values(featuresMorganCounts)
####################################################
#now assign weights based on the model
###################################################
indices = [calc_indices_for_fingerprints(smi,radius = 2,nBits=2048) for smi in smiles]
#indices = indices[1]
featureNames = featuresMorganCounts.columns
#shap_vals = shap_vals[1]
#mol = mols[1]
modelAttributions = [list(atom_attributions(mols[i], indices[i], shap_vals[i],featureNames , direction="down")) for i in range(len(smiles))]
# %%
###################################################
##Calculate the Gasteiger Charges
###################################################
for l in mols:
AllChem.ComputeGasteigerCharges(l)
charges_contribs = [
[mols[i].GetAtomWithIdx(j).GetDoubleProp('_GasteigerCharge') for j in range(mols[i].GetNumAtoms())] for i in
range(len(mols))]
##################################################
# put the smiles and info into a data frame
###################################################
sdfExportData = pd.DataFrame({'names': smiles_names,
'smiles': smiles})
####################################################################################
##Add the partial charges to the sdfExportDataFrame as one string for each compound
####################################################################################
sdfExportData["atom.Contribution.rep_modelAttributions"] = [' '.join([str(round(i, 4)) for i in modelAttributions[j]]) for
j in range(len(modelAttributions))]
####################################################################################
##Add the model attributions for logp to the data frame
####################################################################################
sdfExportData["atom.Contribution.rep_GasteigerCharges"] = [' '.join([str(round(i, 4)) for i in charges_contribs[j]]) for
j in range(len(charges_contribs))]
####################################################
# %%
#######################################################################################
#add the chemical info to the data frame
#######################################################################################
# smilesCol: in which column the smiles are located
PandasTools.AddMoleculeColumnToFrame(sdfExportData, smilesCol='smiles', molCol='ROMol', includeFingerprints=False)
# to avoid duplicate smiles column
del sdfExportData['smiles']
# %%
########################################################################
###Export as an sdf file you can read into CIME
########################################################################
PandasTools.WriteSDF(sdfExportData,"SDF_OUTPUT.sdf",
properties=list(sdfExportData.columns), idName="RowID")