|
1
|
|
|
#! /usr/bin/env python |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
1 |
|
""" |
|
7
|
|
|
# skchem.features.descriptors.charge |
|
8
|
|
|
|
|
9
|
|
|
Charge descriptors for scikit-chem. |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
1 |
|
from collections import OrderedDict |
|
13
|
|
|
|
|
14
|
1 |
|
import numpy as np |
|
|
|
|
|
|
15
|
1 |
|
from rdkit.Chem import rdPartialCharges |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
1 |
|
from .caching import cache |
|
18
|
1 |
|
from .fundamentals import geometric_matrix, adjacency_matrix |
|
19
|
|
|
|
|
20
|
|
|
# TODO: consider using population analysis for 'proper' charge indices |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
1 |
|
def hstackable(res): |
|
|
|
|
|
|
24
|
|
|
return res if isinstance(res, np.ndarray) else np.array([res]) |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
1 |
|
@cache |
|
28
|
|
|
def gasteiger_charges(mol): |
|
29
|
|
|
|
|
30
|
|
|
""" The gasteiger partial charges for the atoms of the molecule. |
|
31
|
|
|
|
|
32
|
|
|
Args: |
|
33
|
|
|
mol (skchem.Mol): |
|
34
|
|
|
The molecule for which to calculate the descriptors. |
|
35
|
|
|
|
|
36
|
|
|
Returns: |
|
37
|
|
|
float |
|
38
|
|
|
""" |
|
39
|
|
|
|
|
40
|
|
|
rdPartialCharges.ComputeGasteigerCharges(mol) |
|
41
|
|
|
return mol.atoms.props.pop('_GasteigerCharge') |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
1 |
|
@cache.inject(gasteiger_charges) |
|
45
|
|
|
def max_partial_charge(mol, g_charges): |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
""" The maximum gasteiger partial charge of all atoms. |
|
48
|
|
|
|
|
49
|
|
|
Args: |
|
50
|
|
|
mol (skchem.Mol): |
|
51
|
|
|
The molecule for which to calculate the descriptor. |
|
52
|
|
|
|
|
53
|
|
|
Returns: |
|
54
|
|
|
float |
|
55
|
|
|
""" |
|
56
|
|
|
|
|
57
|
|
|
return g_charges.max() |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
1 |
|
@cache |
|
61
|
1 |
|
@cache.inject(gasteiger_charges) |
|
62
|
|
|
def min_partial_charge(mol, g_charges): |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
""" The minimum gasteiger partial charge of all atoms. |
|
65
|
|
|
|
|
66
|
|
|
Args: |
|
67
|
|
|
mol (skchem.Mol): |
|
68
|
|
|
The molecule for which to calculate the descriptor. |
|
69
|
|
|
|
|
70
|
|
|
Returns: |
|
71
|
|
|
float |
|
72
|
|
|
""" |
|
73
|
|
|
|
|
74
|
|
|
return g_charges.min() |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
1 |
|
@cache.inject(gasteiger_charges) |
|
78
|
|
|
def total_positive_charge(mol, g_charges): |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
""" The total positive gasteiger partial charges of all atoms. |
|
81
|
|
|
|
|
82
|
|
|
Args: |
|
83
|
|
|
mol (skchem.Mol): |
|
84
|
|
|
The molecule for which to calculate the descriptor. |
|
85
|
|
|
|
|
86
|
|
|
Returns: |
|
87
|
|
|
float |
|
88
|
|
|
""" |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
return g_charges[g_charges > 0].sum() |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
1 |
|
@cache.inject(gasteiger_charges) |
|
95
|
|
|
def total_negative_charge(mol, g_charges): |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
""" The total negative gasteiger partial charges of all atoms. |
|
98
|
|
|
|
|
99
|
|
|
Args: |
|
100
|
|
|
mol (skchem.Mol): |
|
101
|
|
|
The molecule for which to calculate the descriptor. |
|
102
|
|
|
|
|
103
|
|
|
Returns: |
|
104
|
|
|
float |
|
105
|
|
|
""" |
|
106
|
|
|
|
|
107
|
|
|
return g_charges[g_charges > 0].sum() |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
1 |
|
@cache.inject(gasteiger_charges) |
|
111
|
|
|
def total_absolute_charge(mol, g_charges): |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
""" The total absolute gasteiger partial charges of all atoms. |
|
114
|
|
|
|
|
115
|
|
|
This may be considered the electronic charge index (ECI). |
|
116
|
|
|
|
|
117
|
|
|
Args: |
|
118
|
|
|
mol (skchem.Mol): |
|
119
|
|
|
The molecule for which to calculate the descriptor. |
|
120
|
|
|
|
|
121
|
|
|
Returns: |
|
122
|
|
|
float |
|
123
|
|
|
""" |
|
124
|
|
|
return np.abs(g_charges).sum() |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
1 |
|
@cache.inject(gasteiger_charges) |
|
128
|
|
|
def mean_absolute_charge(mol, g_charges): |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
""" The mean absolute gasteiger partial charges of all atoms. |
|
131
|
|
|
|
|
132
|
|
|
This may be considered the 'charge polarization'. |
|
133
|
|
|
|
|
134
|
|
|
Args: |
|
135
|
|
|
mol (skchem.Mol): |
|
136
|
|
|
The molecule for which to calculate the descriptor. |
|
137
|
|
|
|
|
138
|
|
|
Returns: |
|
139
|
|
|
float |
|
140
|
|
|
""" |
|
141
|
|
|
|
|
142
|
|
|
return np.abs(g_charges).mean() |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
1 |
|
@cache.inject(gasteiger_charges) |
|
146
|
|
|
def total_squared_charge(mol, g_charges): |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
""" The total of the squared gasteiger partial charges of all atoms. |
|
149
|
|
|
|
|
150
|
|
|
Args: |
|
151
|
|
|
mol (skchem.Mol): |
|
152
|
|
|
The molecule for which to calculate the descriptor. |
|
153
|
|
|
|
|
154
|
|
|
Returns: |
|
155
|
|
|
float |
|
156
|
|
|
""" |
|
157
|
|
|
|
|
158
|
|
|
return np.power(g_charges, 2).sum() |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
1 |
|
@cache.inject(gasteiger_charges, adjacency_matrix) |
|
162
|
|
|
def submolecular_polarity_parameter(mol, g_charges, a_mat): |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
""" The submolecular polarity parameter. |
|
165
|
|
|
|
|
166
|
|
|
This is the greatest difference in partial charges of bonded atoms in the |
|
167
|
|
|
molecule. |
|
168
|
|
|
|
|
169
|
|
|
Args: |
|
170
|
|
|
mol (skchem.Mol): |
|
171
|
|
|
The molecule for which to calculate the descriptor. |
|
172
|
|
|
|
|
173
|
|
|
Returns: |
|
174
|
|
|
float |
|
175
|
|
|
""" |
|
176
|
|
|
|
|
177
|
|
|
return (np.abs(g_charges - g_charges[:, np.newaxis]) * a_mat).max() |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
1 |
|
@cache |
|
181
|
1 |
|
@cache.inject(gasteiger_charges, geometric_matrix) |
|
182
|
1 |
|
def _topographic_electron_matrix(mol, g_charges, g_mat, conformer=-1): |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
""" The matrix of topographic charge interactions. |
|
185
|
|
|
|
|
186
|
|
|
Args: |
|
187
|
|
|
mol (skchem.Mol): |
|
188
|
|
|
The molecule for which to calculate the descriptor. |
|
189
|
|
|
|
|
190
|
|
|
Returns: |
|
191
|
|
|
float |
|
192
|
|
|
""" |
|
193
|
|
|
g_mat = g_mat ** -2 |
|
194
|
|
|
np.fill_diagonal(g_mat, 0) |
|
195
|
|
|
return np.abs(g_charges - g_charges[:, np.newaxis]) * g_mat |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
1 |
|
@cache |
|
199
|
1 |
|
@cache.inject(_topographic_electron_matrix) |
|
200
|
|
|
def topographic_electronic_descriptor(mol, tem): |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
""" The topolographic electronic descriptor. |
|
203
|
|
|
|
|
204
|
|
|
The sum of the differences in charges of all atoms in the molecule, scaled |
|
205
|
|
|
by the inverse square of their distances, i.e. a measure of the coulombic |
|
206
|
|
|
interactions. |
|
207
|
|
|
|
|
208
|
|
|
Args: |
|
209
|
|
|
mol (skchem.Mol): |
|
210
|
|
|
The molecule for which to calculate the descriptor. |
|
211
|
|
|
|
|
212
|
|
|
Returns: |
|
213
|
|
|
float |
|
214
|
|
|
""" |
|
215
|
|
|
return 0.5 * tem.sum() |
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
1 |
|
@cache |
|
219
|
1 |
|
@cache.inject(_topographic_electron_matrix, adjacency_matrix) |
|
220
|
|
|
def br_topographic_electronic_descriptor(mol, tem, a_mat): |
|
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
""" The bond restricted topographic electronic descriptor. |
|
223
|
|
|
|
|
224
|
|
|
As the topographic electronic descriptor, except restricted only to bonded |
|
225
|
|
|
atoms. |
|
226
|
|
|
|
|
227
|
|
|
Args: |
|
228
|
|
|
mol (skchem.Mol): |
|
229
|
|
|
The molecule for which to calculate the descriptor. |
|
230
|
|
|
|
|
231
|
|
|
Returns: |
|
232
|
|
|
float |
|
233
|
|
|
|
|
234
|
|
|
See Also: |
|
235
|
|
|
skchem.features.descriptors.charge.topographic_electronic_descriptor |
|
236
|
|
|
""" |
|
237
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
return 0.5 * (tem * a_mat).sum() |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
1 |
|
@cache.inject(topographic_electronic_descriptor, min_partial_charge) |
|
243
|
|
|
def pcw_topological_electronic_index(mol, tem, q_min): |
|
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
""" Partial charge weighted topological electronic index. |
|
246
|
|
|
|
|
247
|
|
|
The topographic electronic index, scaled by the maximum negative partial |
|
248
|
|
|
charge in the molecule. |
|
249
|
|
|
|
|
250
|
|
|
Args: |
|
251
|
|
|
mol (skchem.Mol): |
|
252
|
|
|
The molecule for which to calculate the descriptor. |
|
253
|
|
|
|
|
254
|
|
|
Returns: |
|
255
|
|
|
float |
|
256
|
|
|
|
|
257
|
|
|
See Also: |
|
258
|
|
|
skchem.features.descriptors.charge.topographic_electronic_descriptor |
|
259
|
|
|
""" |
|
260
|
|
|
|
|
261
|
|
|
return np.abs(tem / q_min) |
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
1 |
|
@cache.inject(br_topographic_electronic_descriptor, min_partial_charge) |
|
265
|
|
|
def br_pcw_topological_electronic_index(mol, brtem, q_min): |
|
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
""" Bond restricted partial charge weighted topological electronic index. |
|
268
|
|
|
|
|
269
|
|
|
The partial charge weighted topological electronic index, with the |
|
270
|
|
|
summation restricted to bonded atom pairs. |
|
271
|
|
|
|
|
272
|
|
|
Args: |
|
273
|
|
|
mol (skchem.Mol): |
|
274
|
|
|
The molecule for which to calculate the descriptor. |
|
275
|
|
|
|
|
276
|
|
|
Returns: |
|
277
|
|
|
float |
|
278
|
|
|
|
|
279
|
|
|
See Also: |
|
280
|
|
|
skchem.features.descriptors.charge.topographic_electronic_descriptor |
|
281
|
|
|
""" |
|
282
|
|
|
return np.abs(brtem / q_min) |
|
283
|
|
|
|
|
284
|
|
|
|
|
285
|
1 |
|
@cache.inject(gasteiger_charges, adjacency_matrix) |
|
286
|
|
|
def local_dipole_index(mol, g_charges, a_mat): |
|
|
|
|
|
|
287
|
|
|
|
|
288
|
|
|
# cancel out 0.5s |
|
289
|
|
|
return (a_mat * np.abs(g_charges - g_charges[:, np.newaxis])).sum() / a_mat.sum() |
|
290
|
|
|
|
|
291
|
|
|
|
|
292
|
1 |
|
@cache.inject(gasteiger_charges) |
|
293
|
|
|
def min_abs_partial_charge(mol, g_charges): |
|
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
""" The minimum absolute gasteiger partial charge of all atoms. |
|
296
|
|
|
|
|
297
|
|
|
Args: |
|
298
|
|
|
mol (skchem.Mol): |
|
299
|
|
|
The molecule for which to calculate the descriptor. |
|
300
|
|
|
|
|
301
|
|
|
Returns: |
|
302
|
|
|
float |
|
303
|
|
|
""" |
|
304
|
|
|
|
|
305
|
|
|
return np.abs(g_charges).min() |
|
306
|
|
|
|
|
307
|
|
|
|
|
308
|
1 |
|
@cache.inject(gasteiger_charges) |
|
309
|
|
|
def max_abs_partial_charge(mol, g_charges): |
|
|
|
|
|
|
310
|
|
|
|
|
311
|
|
|
""" The maximum absolute gasteiger partial charge of all atoms. |
|
312
|
|
|
|
|
313
|
|
|
Args: |
|
314
|
|
|
mol (skchem.Mol): |
|
315
|
|
|
The molecule for which to calculate the descriptor. |
|
316
|
|
|
|
|
317
|
|
|
Returns: |
|
318
|
|
|
float |
|
319
|
|
|
""" |
|
320
|
|
|
|
|
321
|
|
|
return np.abs(g_charges).max() |
|
322
|
|
|
|
|
323
|
|
|
|
|
324
|
1 |
|
all_feats = OrderedDict( |
|
|
|
|
|
|
325
|
|
|
(('max_partial_charge', max_partial_charge), |
|
326
|
|
|
('min_partial_charge', min_partial_charge), |
|
327
|
|
|
('total_positive_charge', total_positive_charge), |
|
328
|
|
|
('total_negative_charge', total_negative_charge), |
|
329
|
|
|
('total_absolute_charge', total_absolute_charge), |
|
330
|
|
|
('total_squared_charge', total_squared_charge), |
|
331
|
|
|
('submolecular_polarity_parameter', submolecular_polarity_parameter), |
|
332
|
|
|
('topographic_electronic_descriptor', topographic_electronic_descriptor), |
|
333
|
|
|
('br_topographic_electronic_descriptor', |
|
334
|
|
|
br_topographic_electronic_descriptor), |
|
335
|
|
|
('pcw_topological_electronic_index', pcw_topological_electronic_index), |
|
336
|
|
|
('br_pcw_topological_electronic_index', |
|
337
|
|
|
br_pcw_topological_electronic_index), |
|
338
|
|
|
('local_dipole_index', local_dipole_index), |
|
339
|
|
|
('min_abs_partial_charge', min_abs_partial_charge), |
|
340
|
|
|
('max_abs_partial_charge', max_abs_partial_charge)) |
|
341
|
|
|
) |
|
342
|
|
|
|
|
343
|
|
|
|
|
344
|
1 |
|
__all__ = ['gasteiger_charges', 'max_partial_charge', 'min_partial_charge', |
|
345
|
|
|
'total_positive_charge', 'total_negative_charge', |
|
346
|
|
|
'total_absolute_charge', 'total_squared_charge', |
|
347
|
|
|
'submolecular_polarity_parameter', |
|
348
|
|
|
'topographic_electronic_descriptor', |
|
349
|
|
|
'br_topographic_electronic_descriptor', |
|
350
|
|
|
'pcw_topological_electronic_index', |
|
351
|
|
|
'br_pcw_topological_electronic_index', |
|
352
|
|
|
'local_dipole_index', |
|
353
|
|
|
'min_abs_partial_charge', 'max_abs_partial_charge'] |
|
|
|
|
|
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__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.