1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Copyright (C) 2015-2016 Rich Lewis <[email protected]> |
4
|
|
|
# License: 3-clause BSD |
5
|
|
|
|
6
|
1 |
|
""" |
7
|
|
|
## skchem.descriptors.atom |
8
|
|
|
|
9
|
|
|
Module specifying atom based descriptor generators. |
10
|
|
|
""" |
11
|
|
|
|
12
|
1 |
|
import functools |
13
|
1 |
|
from abc import ABCMeta |
14
|
|
|
|
15
|
1 |
|
import pandas as pd |
|
|
|
|
16
|
1 |
|
import numpy as np |
|
|
|
|
17
|
|
|
|
18
|
1 |
|
from rdkit import Chem |
|
|
|
|
19
|
1 |
|
from rdkit.Chem import Crippen |
|
|
|
|
20
|
1 |
|
from rdkit.Chem import Lipinski |
|
|
|
|
21
|
1 |
|
from rdkit.Chem import rdMolDescriptors, rdPartialCharges |
|
|
|
|
22
|
1 |
|
from rdkit.Chem.rdchem import HybridizationType |
|
|
|
|
23
|
|
|
|
24
|
1 |
|
from ..core import Mol |
25
|
1 |
|
from ..resource import PERIODIC_TABLE, ORGANIC |
26
|
1 |
|
from ..base import AtomTransformer, Featurizer |
27
|
1 |
|
from ..utils import nanarray |
28
|
|
|
|
29
|
|
|
|
30
|
1 |
|
def element(a): |
|
|
|
|
31
|
|
|
|
32
|
|
|
""" Return the element """ |
33
|
|
|
|
34
|
1 |
|
return a.GetSymbol() |
35
|
|
|
|
36
|
|
|
|
37
|
1 |
|
def is_element(a, symbol='C'): |
|
|
|
|
38
|
|
|
|
39
|
|
|
""" Is the atom of a given element """ |
40
|
1 |
|
return element(a) == symbol |
41
|
|
|
|
42
|
1 |
|
element_features = {'is_{}'.format(e): functools.partial(is_element, symbol=e) |
|
|
|
|
43
|
|
|
for e in ORGANIC} |
44
|
|
|
|
45
|
|
|
|
46
|
1 |
|
def is_h_acceptor(a): |
|
|
|
|
47
|
|
|
|
48
|
|
|
""" Is an H acceptor? """ |
49
|
|
|
|
50
|
1 |
|
m = a.GetOwningMol() |
|
|
|
|
51
|
1 |
|
idx = a.GetIdx() |
52
|
1 |
|
return idx in [i[0] for i in Lipinski._HAcceptors(m)] |
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
1 |
|
def is_h_donor(a): |
|
|
|
|
56
|
|
|
|
57
|
|
|
""" Is an H donor? """ |
58
|
|
|
|
59
|
1 |
|
m = a.GetOwningMol() |
|
|
|
|
60
|
1 |
|
idx = a.GetIdx() |
61
|
1 |
|
return idx in [i[0] for i in Lipinski._HDonors(m)] |
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
1 |
|
def is_hetero(a): |
|
|
|
|
65
|
|
|
|
66
|
|
|
""" Is a heteroatom? """ |
67
|
|
|
|
68
|
1 |
|
m = a.GetOwningMol() |
|
|
|
|
69
|
1 |
|
idx = a.GetIdx() |
70
|
1 |
|
return idx in [i[0] for i in Lipinski._Heteroatoms(m)] |
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
1 |
|
def atomic_number(a): |
|
|
|
|
74
|
|
|
|
75
|
|
|
""" Atomic number of atom """ |
76
|
|
|
|
77
|
1 |
|
return a.GetAtomicNum() |
78
|
|
|
|
79
|
|
|
|
80
|
1 |
|
def atomic_mass(a): |
|
|
|
|
81
|
|
|
|
82
|
|
|
""" Atomic mass of atom """ |
83
|
|
|
|
84
|
1 |
|
return a.atomic_mass |
85
|
|
|
|
86
|
|
|
|
87
|
1 |
|
def explicit_valence(a): |
|
|
|
|
88
|
|
|
|
89
|
|
|
""" Explicit valence of atom """ |
90
|
1 |
|
return a.GetExplicitValence() |
91
|
|
|
|
92
|
|
|
|
93
|
1 |
|
def implicit_valence(a): |
|
|
|
|
94
|
|
|
|
95
|
|
|
""" Implicit valence of atom """ |
96
|
|
|
|
97
|
1 |
|
return a.GetImplicitValence() |
98
|
|
|
|
99
|
|
|
|
100
|
1 |
|
def valence(a): |
|
|
|
|
101
|
|
|
|
102
|
|
|
""" returns the valence of the atom """ |
103
|
|
|
|
104
|
1 |
|
return explicit_valence(a) + implicit_valence(a) |
105
|
|
|
|
106
|
|
|
|
107
|
1 |
|
def formal_charge(a): |
|
|
|
|
108
|
|
|
|
109
|
|
|
""" Formal charge of atom """ |
110
|
|
|
|
111
|
1 |
|
return a.GetFormalCharge() |
112
|
|
|
|
113
|
|
|
|
114
|
1 |
|
def is_aromatic(a): |
|
|
|
|
115
|
|
|
|
116
|
|
|
""" Boolean if atom is aromatic""" |
117
|
|
|
|
118
|
1 |
|
return a.GetIsAromatic() |
119
|
|
|
|
120
|
|
|
|
121
|
1 |
|
def num_implicit_hydrogens(a): |
|
|
|
|
122
|
|
|
|
123
|
|
|
""" Number of implicit hydrogens """ |
124
|
|
|
|
125
|
1 |
|
return a.GetNumImplicitHs() |
126
|
|
|
|
127
|
|
|
|
128
|
1 |
|
def num_explicit_hydrogens(a): |
|
|
|
|
129
|
|
|
|
130
|
|
|
""" Number of explicit hydrodgens """ |
131
|
|
|
|
132
|
1 |
|
return a.GetNumExplicitHs() |
133
|
|
|
|
134
|
|
|
|
135
|
1 |
|
def num_hydrogens(a): |
|
|
|
|
136
|
|
|
|
137
|
|
|
""" Number of hydrogens """ |
138
|
|
|
|
139
|
1 |
|
return num_implicit_hydrogens(a) + num_explicit_hydrogens(a) |
140
|
|
|
|
141
|
|
|
|
142
|
1 |
|
def is_in_ring(a): |
|
|
|
|
143
|
|
|
|
144
|
|
|
""" Whether the atom is in a ring """ |
145
|
|
|
|
146
|
1 |
|
return a.IsInRing() |
147
|
|
|
|
148
|
|
|
|
149
|
1 |
|
def crippen_log_p_contrib(a): |
|
|
|
|
150
|
|
|
|
151
|
|
|
""" Hacky way of getting logP contribution. """ |
152
|
|
|
|
153
|
1 |
|
idx = a.GetIdx() |
154
|
1 |
|
m = a.GetOwningMol() |
|
|
|
|
155
|
1 |
|
return Crippen._GetAtomContribs(m)[idx][0] |
|
|
|
|
156
|
|
|
|
157
|
|
|
|
158
|
1 |
|
def crippen_molar_refractivity_contrib(a): |
|
|
|
|
159
|
|
|
|
160
|
|
|
""" Hacky way of getting molar refractivity contribution. """ |
161
|
|
|
|
162
|
1 |
|
idx = a.GetIdx() |
163
|
1 |
|
m = a.GetOwningMol() |
|
|
|
|
164
|
1 |
|
return Crippen._GetAtomContribs(m)[idx][1] |
|
|
|
|
165
|
|
|
|
166
|
|
|
|
167
|
1 |
|
def tpsa_contrib(a): |
|
|
|
|
168
|
|
|
|
169
|
|
|
""" Hacky way of getting total polar surface area contribution. """ |
170
|
|
|
|
171
|
1 |
|
idx = a.GetIdx() |
172
|
1 |
|
m = a.GetOwningMol() |
|
|
|
|
173
|
1 |
|
return rdMolDescriptors._CalcTPSAContribs(m)[idx] |
|
|
|
|
174
|
|
|
|
175
|
|
|
|
176
|
1 |
|
def labute_asa_contrib(a): |
|
|
|
|
177
|
|
|
|
178
|
|
|
""" Hacky way of getting accessible surface area contribution. """ |
179
|
|
|
|
180
|
1 |
|
idx = a.GetIdx() |
181
|
1 |
|
m = a.GetOwningMol() |
|
|
|
|
182
|
1 |
|
return rdMolDescriptors._CalcLabuteASAContribs(m)[0][idx] |
|
|
|
|
183
|
|
|
|
184
|
|
|
|
185
|
1 |
|
def gasteiger_charge(a, force_calc=False): |
|
|
|
|
186
|
|
|
|
187
|
|
|
""" Hacky way of getting gasteiger charge """ |
188
|
|
|
|
189
|
1 |
|
res = a.props.get('_GasteigerCharge', None) |
190
|
1 |
|
if res and not force_calc: |
191
|
|
|
return float(res) |
192
|
|
|
else: |
193
|
1 |
|
m = a.GetOwningMol() |
|
|
|
|
194
|
1 |
|
rdPartialCharges.ComputeGasteigerCharges(m) |
195
|
1 |
|
return float(a.props['_GasteigerCharge']) |
196
|
|
|
|
197
|
|
|
|
198
|
1 |
|
def pauling_electronegativity(a): |
|
|
|
|
199
|
|
|
|
200
|
1 |
|
return a.pauling_electronegativity |
201
|
|
|
|
202
|
|
|
|
203
|
1 |
|
def first_ionization(a): |
|
|
|
|
204
|
|
|
|
205
|
1 |
|
return PERIODIC_TABLE.loc[a.atomic_number, 'first_ionisation_energy'] |
206
|
|
|
|
207
|
|
|
|
208
|
1 |
|
def group(a): |
|
|
|
|
209
|
|
|
|
210
|
1 |
|
return PERIODIC_TABLE.loc[a.atomic_number, 'group'] |
211
|
|
|
|
212
|
|
|
|
213
|
1 |
|
def period(a): |
|
|
|
|
214
|
|
|
|
215
|
1 |
|
return PERIODIC_TABLE.loc[a.atomic_number, 'period'] |
216
|
|
|
|
217
|
|
|
|
218
|
1 |
|
def is_hybridized(a, hybrid_type=HybridizationType.SP3): |
|
|
|
|
219
|
|
|
|
220
|
|
|
""" Hybridized as type hybrid_type, default SP3 """ |
221
|
|
|
|
222
|
1 |
|
return str(a.GetHybridization()) == str(hybrid_type) |
223
|
|
|
|
224
|
1 |
|
hybridization_features = {'is_' + n + '_hybridized': functools.partial( |
|
|
|
|
225
|
|
|
is_hybridized, hybrid_type=n) |
226
|
|
|
for n in HybridizationType.names} |
227
|
|
|
|
228
|
1 |
|
ATOM_FEATURES = { |
229
|
|
|
'atomic_number': atomic_number, |
230
|
|
|
'atomic_mass': atomic_mass, |
231
|
|
|
'formal_charge': formal_charge, |
232
|
|
|
'gasteiger_charge': gasteiger_charge, |
233
|
|
|
'pauling_electronegativity': pauling_electronegativity, |
234
|
|
|
'first_ionisation': first_ionization, |
235
|
|
|
'group': group, |
236
|
|
|
'period': period, |
237
|
|
|
'valence': valence, |
238
|
|
|
'is_aromatic': is_aromatic, |
239
|
|
|
'num_hydrogens': num_hydrogens, |
240
|
|
|
'is_in_ring': is_in_ring, |
241
|
|
|
'log_p_contrib': crippen_log_p_contrib, |
242
|
|
|
'molar_refractivity_contrib': crippen_molar_refractivity_contrib, |
243
|
|
|
'is_h_acceptor': is_h_acceptor, |
244
|
|
|
'is_h_donor': is_h_donor, |
245
|
|
|
'is_heteroatom': is_hetero, |
246
|
|
|
'total_polar_surface_area_contrib': tpsa_contrib, |
247
|
|
|
'total_labute_accessible_surface_area': labute_asa_contrib, |
248
|
|
|
} |
249
|
1 |
|
ATOM_FEATURES.update(element_features) |
250
|
1 |
|
ATOM_FEATURES.update(hybridization_features) |
251
|
|
|
|
252
|
|
|
|
253
|
1 |
|
class AtomFeaturizer(AtomTransformer, Featurizer): |
|
|
|
|
254
|
|
|
|
255
|
1 |
|
def __init__(self, features='all', n_jobs=1, verbose=True): |
256
|
1 |
|
self._features = None |
257
|
1 |
|
self.features = features |
258
|
|
|
|
259
|
1 |
|
super(AtomFeaturizer, self).__init__(n_jobs=n_jobs, verbose=verbose) |
260
|
|
|
|
261
|
1 |
|
@property |
262
|
|
|
def name(self): |
|
|
|
|
263
|
|
|
return 'atom_feat' |
264
|
|
|
|
265
|
1 |
|
@property |
266
|
|
|
def features(self): |
|
|
|
|
267
|
1 |
|
return self._features |
268
|
|
|
|
269
|
1 |
View Code Duplication |
@features.setter |
|
|
|
|
270
|
|
|
def features(self, features): |
|
|
|
|
271
|
1 |
|
if isinstance(features, str): |
272
|
1 |
|
if features == 'all': |
273
|
1 |
|
features = ATOM_FEATURES |
274
|
|
|
else: |
275
|
|
|
features = {features: ATOM_FEATURES[features]} |
276
|
|
|
elif isinstance(features, list): |
277
|
|
|
features = {feature: ATOM_FEATURES[feature] |
278
|
|
|
for feature in features} |
279
|
|
|
elif isinstance(features, (dict, pd.Series)): |
280
|
|
|
features = features |
281
|
|
|
else: |
282
|
|
|
raise NotImplementedError('Cannot use features {}'.format( |
283
|
|
|
features)) |
284
|
|
|
|
285
|
1 |
|
self._features = pd.Series(features) |
286
|
1 |
|
self._features.index.name = 'atom_features' |
287
|
|
|
|
288
|
1 |
|
@property |
289
|
|
|
def minor_axis(self): |
290
|
1 |
|
return self.features.index |
291
|
|
|
|
292
|
1 |
|
def _transform_atom(self, atom): |
293
|
1 |
|
return self.features.apply(lambda f: f(atom)).values |
|
|
|
|
294
|
|
|
|
295
|
1 |
|
def _transform_mol(self, mol): |
296
|
1 |
|
return np.array([self.transform(a) for a in mol.atoms]) |
297
|
|
|
|
298
|
|
|
|
299
|
1 |
|
class DistanceTransformer(AtomTransformer, Featurizer): |
300
|
|
|
|
301
|
|
|
""" Base class implementing Distance Matrix transformers. |
302
|
|
|
|
303
|
|
|
Concrete classes inheriting from this should implement `_transform_mol`. |
304
|
|
|
""" |
305
|
|
|
|
306
|
1 |
|
__metaclass__ = ABCMeta |
307
|
|
|
|
308
|
1 |
|
@property |
309
|
|
|
def minor_axis(self): |
310
|
|
|
return pd.RangeIndex(self.max_atoms, name='atom_idx') |
311
|
|
|
|
312
|
1 |
|
def _transform_atom(self, atom): |
313
|
|
|
return NotImplemented |
314
|
|
|
|
315
|
1 |
|
def transform(self, mols): |
316
|
|
|
res = super(DistanceTransformer, self).transform(mols) |
317
|
|
|
if isinstance(mols, Mol): |
318
|
|
|
res = res.iloc[:len(mols.atoms), :len(mols.atoms)] |
319
|
|
|
return res |
320
|
|
|
|
321
|
|
|
|
322
|
1 |
View Code Duplication |
class SpacialDistanceTransformer(DistanceTransformer): |
|
|
|
|
323
|
|
|
|
324
|
|
|
""" Transformer class for generating 3D distance matrices. """ |
325
|
|
|
|
326
|
|
|
# TODO: handle multiple conformers |
|
|
|
|
327
|
|
|
|
328
|
1 |
|
def __init__(self, n_jobs=1, verbose=True): |
329
|
|
|
|
330
|
|
|
""" Initialize a SpacialDistanceTransformer. |
331
|
|
|
|
332
|
|
|
Args: |
333
|
|
|
n_jobs (int): |
334
|
|
|
The number of processes to run the featurizer in. |
335
|
|
|
verbose (bool): |
336
|
|
|
Whether to output a progress bar. |
337
|
|
|
""" |
338
|
|
|
super(SpacialDistanceTransformer, self).__init__(n_jobs=n_jobs, |
339
|
|
|
verbose=verbose) |
340
|
|
|
|
341
|
1 |
|
@property |
342
|
|
|
def name(self): |
|
|
|
|
343
|
|
|
return 'spacial_dist' |
344
|
|
|
|
345
|
1 |
|
def _transform_mol(self, mol): |
346
|
|
|
res = nanarray((len(mol.atoms), self.max_atoms)) |
347
|
|
|
res[:, :len(mol.atoms)] = Chem.Get3DDistanceMatrix(mol) |
348
|
|
|
return res |
349
|
|
|
|
350
|
|
|
|
351
|
1 |
View Code Duplication |
class GraphDistanceTransformer(DistanceTransformer): |
|
|
|
|
352
|
|
|
|
353
|
|
|
""" Transformer class for generating Graph distance matrices. """ |
354
|
|
|
|
355
|
1 |
|
def __init__(self, n_jobs=1, verbose=True): |
356
|
|
|
|
357
|
|
|
""" Initialize a GraphDistanceTransformer. |
358
|
|
|
|
359
|
|
|
Args: |
360
|
|
|
n_jobs (int): |
361
|
|
|
The number of processes to run the featurizer in. |
362
|
|
|
verbose (bool): |
363
|
|
|
Whether to output a progress bar. |
364
|
|
|
""" |
365
|
|
|
|
366
|
|
|
super(GraphDistanceTransformer, self).__init__(n_jobs=n_jobs, |
367
|
|
|
verbose=verbose) |
368
|
|
|
|
369
|
1 |
|
@property |
370
|
|
|
def name(self): |
|
|
|
|
371
|
|
|
return 'graph_dist' |
372
|
|
|
|
373
|
1 |
|
def _transform_mol(self, mol): |
374
|
|
|
res = nanarray((len(mol.atoms), self.max_atoms)) |
375
|
|
|
res[:len(mol.atoms), :len(mol.atoms)] = Chem.GetDistanceMatrix(mol) |
376
|
|
|
return res |
377
|
|
|
|
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.