1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Copyright (C) 2015 Rich Lewis <[email protected]> |
4
|
|
|
# License: 3-clause BSD |
5
|
|
|
|
6
|
|
|
""" |
7
|
|
|
## skchem.descriptors.atom |
8
|
|
|
|
9
|
|
|
Module specifying atom based descriptor generators. |
10
|
|
|
""" |
11
|
|
|
|
12
|
|
|
import pandas as pd |
|
|
|
|
13
|
|
|
import numpy as np |
|
|
|
|
14
|
|
|
import progressbar |
|
|
|
|
15
|
|
|
|
16
|
|
|
from rdkit import Chem |
|
|
|
|
17
|
|
|
from rdkit.Chem import Crippen |
|
|
|
|
18
|
|
|
from rdkit.Chem import Lipinski |
|
|
|
|
19
|
|
|
from rdkit.Chem import rdMolDescriptors, rdPartialCharges |
|
|
|
|
20
|
|
|
from rdkit.Chem.rdchem import HybridizationType |
|
|
|
|
21
|
|
|
from rdkit.Chem.rdDistGeom import EmbedMolecule |
|
|
|
|
22
|
|
|
|
23
|
|
|
import functools |
24
|
|
|
import warnings |
25
|
|
|
|
26
|
|
|
from .. import core |
27
|
|
|
from ..data import PERIODIC_TABLE |
28
|
|
|
from ..filters import OrganicFilter |
29
|
|
|
from .. import forcefields |
30
|
|
|
|
31
|
|
|
def element(a): |
|
|
|
|
32
|
|
|
|
33
|
|
|
""" Return the element """ |
34
|
|
|
|
35
|
|
|
return a.GetSymbol() |
36
|
|
|
|
37
|
|
|
def is_element(a, symbol='C'): |
|
|
|
|
38
|
|
|
|
39
|
|
|
""" Is the atom of a given element """ |
40
|
|
|
return element(a) == symbol |
41
|
|
|
|
42
|
|
|
element_features = {'is_{}'.format(e): functools.partial(is_element, symbol=e) for e in OrganicFilter.organic} |
|
|
|
|
43
|
|
|
|
44
|
|
|
def is_h_acceptor(a): |
|
|
|
|
45
|
|
|
|
46
|
|
|
""" Is an H acceptor? """ |
47
|
|
|
|
48
|
|
|
m = a.GetOwningMol() |
|
|
|
|
49
|
|
|
idx = a.GetIdx() |
50
|
|
|
return idx in [i[0] for i in Lipinski._HAcceptors(m)] |
|
|
|
|
51
|
|
|
|
52
|
|
|
def is_h_donor(a): |
|
|
|
|
53
|
|
|
|
54
|
|
|
""" Is an H donor? """ |
55
|
|
|
|
56
|
|
|
m = a.GetOwningMol() |
|
|
|
|
57
|
|
|
idx = a.GetIdx() |
58
|
|
|
return idx in [i[0] for i in Lipinski._HDonors(m)] |
|
|
|
|
59
|
|
|
|
60
|
|
|
def is_hetero(a): |
|
|
|
|
61
|
|
|
|
62
|
|
|
""" Is a heteroatom? """ |
63
|
|
|
|
64
|
|
|
m = a.GetOwningMol() |
|
|
|
|
65
|
|
|
idx = a.GetIdx() |
66
|
|
|
return idx in [i[0] for i in Lipinski._Heteroatoms(m)] |
|
|
|
|
67
|
|
|
|
68
|
|
|
def atomic_number(a): |
|
|
|
|
69
|
|
|
|
70
|
|
|
""" Atomic number of atom """ |
71
|
|
|
|
72
|
|
|
return a.GetAtomicNum() |
73
|
|
|
|
74
|
|
|
def atomic_mass(a): |
|
|
|
|
75
|
|
|
|
76
|
|
|
""" Atomic mass of atom """ |
77
|
|
|
|
78
|
|
|
return a.mass |
79
|
|
|
|
80
|
|
|
def explicit_valence(a): |
|
|
|
|
81
|
|
|
|
82
|
|
|
""" Explicit valence of atom """ |
83
|
|
|
return a.GetExplicitValence() |
84
|
|
|
|
85
|
|
|
def implicit_valence(a): |
|
|
|
|
86
|
|
|
|
87
|
|
|
""" Implicit valence of atom """ |
88
|
|
|
|
89
|
|
|
return a.GetImplicitValence() |
90
|
|
|
|
91
|
|
|
def valence(a): |
|
|
|
|
92
|
|
|
|
93
|
|
|
""" returns the valence of the atom """ |
94
|
|
|
|
95
|
|
|
return explicit_valence(a) + implicit_valence(a) |
96
|
|
|
|
97
|
|
|
def formal_charge(a): |
|
|
|
|
98
|
|
|
|
99
|
|
|
""" Formal charge of atom """ |
100
|
|
|
|
101
|
|
|
return a.GetFormalCharge() |
102
|
|
|
|
103
|
|
|
def is_aromatic(a): |
|
|
|
|
104
|
|
|
|
105
|
|
|
""" Boolean if atom is aromatic""" |
106
|
|
|
|
107
|
|
|
return a.GetIsAromatic() |
108
|
|
|
|
109
|
|
|
def num_implicit_hydrogens(a): |
|
|
|
|
110
|
|
|
|
111
|
|
|
""" Number of implicit hydrogens """ |
112
|
|
|
|
113
|
|
|
return a.GetNumImplicitHs() |
114
|
|
|
|
115
|
|
|
def num_explicit_hydrogens(a): |
|
|
|
|
116
|
|
|
|
117
|
|
|
""" Number of explicit hydrodgens """ |
118
|
|
|
|
119
|
|
|
return a.GetNumExplicitHs() |
120
|
|
|
|
121
|
|
|
def num_hydrogens(a): |
|
|
|
|
122
|
|
|
|
123
|
|
|
""" Number of hydrogens """ |
124
|
|
|
|
125
|
|
|
return num_implicit_hydrogens(a) + num_explicit_hydrogens(a) |
126
|
|
|
|
127
|
|
|
def is_in_ring(a): |
|
|
|
|
128
|
|
|
|
129
|
|
|
""" Whether the atom is in a ring """ |
130
|
|
|
|
131
|
|
|
return a.IsInRing() |
132
|
|
|
|
133
|
|
|
def crippen_log_p_contrib(a): |
|
|
|
|
134
|
|
|
|
135
|
|
|
""" Hacky way of getting logP contribution. """ |
136
|
|
|
|
137
|
|
|
idx = a.GetIdx() |
138
|
|
|
m = a.GetOwningMol() |
|
|
|
|
139
|
|
|
return Crippen._GetAtomContribs(m)[idx][0] |
|
|
|
|
140
|
|
|
|
141
|
|
|
def crippen_molar_refractivity_contrib(a): |
|
|
|
|
142
|
|
|
|
143
|
|
|
""" Hacky way of getting molar refractivity contribution. """ |
144
|
|
|
|
145
|
|
|
idx = a.GetIdx() |
146
|
|
|
m = a.GetOwningMol() |
|
|
|
|
147
|
|
|
return Crippen._GetAtomContribs(m)[idx][1] |
|
|
|
|
148
|
|
|
|
149
|
|
|
def tpsa_contrib(a): |
|
|
|
|
150
|
|
|
|
151
|
|
|
""" Hacky way of getting total polar surface area contribution. """ |
152
|
|
|
|
153
|
|
|
idx = a.GetIdx() |
154
|
|
|
m = a.GetOwningMol() |
|
|
|
|
155
|
|
|
return rdMolDescriptors._CalcTPSAContribs(m)[idx] |
|
|
|
|
156
|
|
|
|
157
|
|
|
def labute_asa_contrib(a): |
|
|
|
|
158
|
|
|
|
159
|
|
|
""" Hacky way of getting accessible surface area contribution. """ |
160
|
|
|
|
161
|
|
|
idx = a.GetIdx() |
162
|
|
|
m = a.GetOwningMol() |
|
|
|
|
163
|
|
|
return rdMolDescriptors._CalcLabuteASAContribs(m)[0][idx] |
|
|
|
|
164
|
|
|
|
165
|
|
|
def gasteiger_charge(a, force_calc=False): |
|
|
|
|
166
|
|
|
|
167
|
|
|
""" Hacky way of getting gasteiger charge """ |
168
|
|
|
|
169
|
|
|
res = a.props.get('_GasteigerCharge', None) |
170
|
|
|
if res and not force_calc: |
171
|
|
|
return float(res) |
172
|
|
|
else: |
173
|
|
|
idx = a.GetIdx() |
|
|
|
|
174
|
|
|
m = a.GetOwningMol() |
|
|
|
|
175
|
|
|
rdPartialCharges.ComputeGasteigerCharges(m) |
176
|
|
|
return float(a.props['_GasteigerCharge']) |
177
|
|
|
|
178
|
|
|
def electronegativity(a): |
|
|
|
|
179
|
|
|
|
180
|
|
|
return PERIODIC_TABLE.loc[a.atomic_number, 'pauling_electronegativity'] |
181
|
|
|
|
182
|
|
|
def first_ionization(a): |
|
|
|
|
183
|
|
|
|
184
|
|
|
return PERIODIC_TABLE.loc[a.atomic_number, 'first_ionisation_energy'] |
185
|
|
|
|
186
|
|
|
def group(a): |
|
|
|
|
187
|
|
|
|
188
|
|
|
return PERIODIC_TABLE.loc[a.atomic_number, 'group'] |
189
|
|
|
|
190
|
|
|
def period(a): |
|
|
|
|
191
|
|
|
|
192
|
|
|
return PERIODIC_TABLE.loc[a.atomic_number, 'period'] |
193
|
|
|
|
194
|
|
|
def is_hybridized(a, hybrid_type=HybridizationType.SP3): |
|
|
|
|
195
|
|
|
|
196
|
|
|
""" Hybridized as type hybrid_type, default SP3 """ |
197
|
|
|
|
198
|
|
|
return str(a.GetHybridization()) is hybrid_type |
199
|
|
|
|
200
|
|
|
hybridization_features = {'is_' + n + '_hybridized': functools.partial(is_hybridized, hybrid_type=n) for n in HybridizationType.names} |
|
|
|
|
201
|
|
|
|
202
|
|
|
atom_features = { |
|
|
|
|
203
|
|
|
'atomic_number': atomic_number, |
204
|
|
|
'atomic_mass': atomic_mass, |
205
|
|
|
'formal_charge': formal_charge, |
206
|
|
|
'gasteiger_charge': gasteiger_charge, |
207
|
|
|
'electronegativity': electronegativity, |
208
|
|
|
'first_ionisation': first_ionization, |
209
|
|
|
'group': group, |
210
|
|
|
'period': period, |
211
|
|
|
'valence': valence, |
212
|
|
|
'is_aromatic': is_aromatic, |
213
|
|
|
'num_hydrogens': num_hydrogens, |
214
|
|
|
'is_in_ring': is_in_ring, |
215
|
|
|
'log_p_contrib': crippen_log_p_contrib, |
216
|
|
|
'molar_refractivity_contrib': crippen_molar_refractivity_contrib, |
217
|
|
|
'is_h_acceptor': is_h_acceptor, |
218
|
|
|
'is_h_donor': is_h_donor, |
219
|
|
|
'is_heteroatom': is_hetero, |
220
|
|
|
'total_polar_surface_area_contrib': tpsa_contrib, |
221
|
|
|
'total_labute_accessible_surface_area': labute_asa_contrib, |
222
|
|
|
} |
223
|
|
|
atom_features.update(element_features) |
224
|
|
|
atom_features.update(hybridization_features) |
225
|
|
|
|
226
|
|
|
class AtomFeatureCalculator(object): |
|
|
|
|
227
|
|
|
|
228
|
|
|
def __init__(self, features='all', max_atoms=75): |
229
|
|
|
if features == 'all': |
230
|
|
|
features = atom_features |
231
|
|
|
self.features = pd.Series(features) |
232
|
|
|
self.max_atoms = max_atoms |
233
|
|
|
|
234
|
|
|
@property |
235
|
|
|
def feature_names(self): |
|
|
|
|
236
|
|
|
return self.features.index |
237
|
|
|
|
238
|
|
|
def transform(self, obj): |
|
|
|
|
239
|
|
|
if isinstance(obj, core.Atom): |
240
|
|
|
return self._transform_atom(obj) |
241
|
|
|
elif isinstance(obj, core.Mol): |
242
|
|
|
return self._transform_mol(obj) |
243
|
|
|
elif isinstance(obj, pd.Series): |
244
|
|
|
return self._transform_series(obj) |
245
|
|
|
elif isinstance(obj, pd.DataFrame): |
246
|
|
|
return self._transform_series(obj.structure) |
247
|
|
|
elif isinstance(obj, (tuple, list)): |
248
|
|
|
return self._transform_series(obj) |
249
|
|
|
else: |
250
|
|
|
raise NotImplementedError |
251
|
|
|
|
252
|
|
|
def _transform_atom(self, atom): |
253
|
|
|
return self.features.apply(lambda f: f(atom)) |
254
|
|
|
|
255
|
|
|
def _transform_mol(self, mol): |
256
|
|
|
return pd.DataFrame(self.transform(a) for a in mol.atoms) |
257
|
|
|
|
258
|
|
|
def _transform_series(self, data): |
259
|
|
|
X = np.repeat(np.nan, |
|
|
|
|
260
|
|
|
len(self.feature_names) * self.max_atoms * len(data))\ |
261
|
|
|
.reshape((len(data), self.max_atoms, len(self.feature_names))) |
262
|
|
|
for i, mol in enumerate(data): |
263
|
|
|
X[i, :len(mol.atoms), :] = self._transform_mol(mol) |
264
|
|
|
res = pd.Panel(X, items=data.index) |
265
|
|
|
res.minor_axis = self.features.index |
266
|
|
|
res.major_axis.name = 'atom_idx' |
267
|
|
|
return res |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
def __call__(self, *args, **kwargs): |
271
|
|
|
return self.transform(*args, **kwargs) |
272
|
|
|
|
273
|
|
|
class DistanceCalculator(object): |
|
|
|
|
274
|
|
|
def __init__(self, max_atoms=75): |
275
|
|
|
self.max_atoms = max_atoms |
276
|
|
|
|
277
|
|
|
def _transform_series(self, ser): |
278
|
|
|
bar = progressbar.ProgressBar() |
|
|
|
|
279
|
|
|
res = pd.Panel(np.array([self.transform(mol) for mol in bar(ser)]), items=ser.index) |
280
|
|
|
res.major_axis.name = res.minor_axis.name = 'atom_idx' |
281
|
|
|
return res |
282
|
|
|
|
283
|
|
|
def transform(self, obj): |
|
|
|
|
284
|
|
|
if isinstance(obj, core.Atom): |
285
|
|
|
return self._transform_atom(obj) |
|
|
|
|
286
|
|
|
elif isinstance(obj, core.Mol): |
287
|
|
|
return self._transform_mol(obj) |
|
|
|
|
288
|
|
|
elif isinstance(obj, pd.Series): |
289
|
|
|
return self._transform_series(obj) |
290
|
|
|
elif isinstance(obj, pd.DataFrame): |
291
|
|
|
return self._transform_series(obj.structure) |
292
|
|
|
elif isinstance(obj, (tuple, list)): |
293
|
|
|
return self._transform_series(obj) |
294
|
|
|
else: |
295
|
|
|
raise NotImplementedError |
296
|
|
|
|
297
|
|
|
def __call__(self, *args, **kwargs): |
298
|
|
|
return self.transform(*args, **kwargs) |
299
|
|
|
|
300
|
|
|
class GraphDistanceCalculator(DistanceCalculator): |
|
|
|
|
301
|
|
|
|
302
|
|
|
def _transform_mol(self, mol): |
303
|
|
|
res = np.repeat(np.nan, self.max_atoms ** 2).reshape(self.max_atoms, self.max_atoms) |
304
|
|
|
res[:len(mol.atoms), :len(mol.atoms)] = Chem.GetDistanceMatrix(mol) |
305
|
|
|
return res |
306
|
|
|
|
307
|
|
|
class SpaceDistanceCalculator(DistanceCalculator): |
|
|
|
|
308
|
|
|
|
309
|
|
|
def __init__(self, max_atoms=75, warn_on_fail=True, error_on_fail=False, forcefield='uff'): |
310
|
|
|
self.forcefield = forcefields.get(forcefield) |
311
|
|
|
self.warn_on_fail = warn_on_fail |
312
|
|
|
self.error_on_fail = error_on_fail |
313
|
|
|
super(SpaceDistanceCalculator, self).__init__(max_atoms=max_atoms) |
314
|
|
|
|
315
|
|
|
def _transform_mol(self, mol): |
316
|
|
|
res = np.repeat(np.nan, self.max_atoms ** 2).reshape(self.max_atoms, self.max_atoms) |
317
|
|
|
success = EmbedMolecule(mol) |
318
|
|
|
if success == -1: |
319
|
|
|
msg = 'Failed to Embed Molecule {}'.format(mol.name) |
320
|
|
|
if self.error_on_fail: |
321
|
|
|
raise RuntimeError(msg) |
322
|
|
|
elif self.warn_on_fail: |
323
|
|
|
warnings.warn(msg) |
324
|
|
|
return res |
325
|
|
|
else: |
326
|
|
|
pass |
327
|
|
|
m_new = Chem.AddHs(mol, addCoords=True) |
|
|
|
|
328
|
|
|
self.forcefield.optimize(mol) |
329
|
|
|
res[:len(mol.atoms), :len(mol.atoms)] = Chem.Get3DDistanceMatrix(mol) |
330
|
|
|
return res |
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.