|
1
|
|
|
#! /usr/bin/env python |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
|
|
""" |
|
7
|
|
|
# skchem.features.descriptors.information |
|
8
|
|
|
|
|
9
|
|
|
Information content indices. |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
|
|
from functools import partial |
|
13
|
|
|
|
|
14
|
|
|
import pandas as pd |
|
|
|
|
|
|
15
|
|
|
import numpy as np |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
from .decorators import requires_dmat, requires_h_filled |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def entropy(l): |
|
|
|
|
|
|
21
|
|
|
""" Entropy for a list. |
|
22
|
|
|
|
|
23
|
|
|
Examples: |
|
24
|
|
|
>>> entropy([1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3]) # doctest: +ELLIPSIS |
|
25
|
|
|
1.505... |
|
26
|
|
|
|
|
27
|
|
|
>>> entropy([1, 1, 1, 1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 8]) # doctest: +ELLIPSIS |
|
28
|
|
|
2.75... |
|
29
|
|
|
""" |
|
30
|
|
|
n_envs = pd.value_counts(l).values |
|
31
|
|
|
l = n_envs / len(l) |
|
32
|
|
|
return sum(- l * np.log2(l)) |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
@requires_h_filled |
|
36
|
|
|
@requires_dmat |
|
37
|
|
|
def _ic(mol, m): |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
""" neighbourhood information content of orders 0 to n """ |
|
40
|
|
|
|
|
41
|
|
|
res = np.zeros((m + 1,)) |
|
42
|
|
|
|
|
43
|
|
|
if len(mol.atoms) == 0: |
|
44
|
|
|
return res |
|
45
|
|
|
|
|
46
|
|
|
n_atoms = len(mol.atoms) |
|
47
|
|
|
atomic = mol.atoms.atomic_number |
|
48
|
|
|
env = atomic.copy() |
|
49
|
|
|
d_mat = mol._dMat |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
for i in range(m + 1): |
|
52
|
|
|
shell_mat = d_mat == i |
|
53
|
|
|
arr = np.zeros((n_atoms, n_atoms)) |
|
54
|
|
|
np.fill_diagonal(arr, env) |
|
55
|
|
|
arr[shell_mat] = np.tile(atomic, n_atoms)[shell_mat.flatten()] |
|
56
|
|
|
# hash the envs - string fastest immutable |
|
57
|
|
|
env = np.array([hash(np.sort(x).tostring()) for x in arr]) |
|
58
|
|
|
res[i] = entropy(env) |
|
59
|
|
|
|
|
60
|
|
|
return np.array(res) |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
def ic(mol, m): |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
""" Neighborhood Information Content of order m. |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
The $m$th order neighbourhood Information Content $IC_m$ is calculated as |
|
68
|
|
|
|
|
69
|
|
|
$$ IC_m = - \sum{g=1}{G}{\log_2{\frac{A_g}{A}}} = - \sum{g=1}{G}{p_g \cdot \log_2{p_g} $$ |
|
70
|
|
|
|
|
71
|
|
|
where $G$ is the number of equivalence classes and $A_g$ is the cardinality |
|
72
|
|
|
of the $g$th equivalence class, and $p_g$ is the probability of randomly |
|
73
|
|
|
selecting a vertex of the $g$th class. |
|
74
|
|
|
|
|
75
|
|
|
Args: |
|
76
|
|
|
mol (skchem.Mol): |
|
77
|
|
|
The molecule for which to calculate the descriptor. |
|
78
|
|
|
|
|
79
|
|
|
m (int): |
|
80
|
|
|
The order of the environments to use. |
|
81
|
|
|
|
|
82
|
|
|
Returns: |
|
83
|
|
|
float |
|
84
|
|
|
|
|
85
|
|
|
Note: |
|
86
|
|
|
The function memoizes orders below 7. |
|
87
|
|
|
|
|
88
|
|
|
Examples: |
|
89
|
|
|
|
|
90
|
|
|
From MDC: |
|
91
|
|
|
|
|
92
|
|
|
>>> import skchem |
|
93
|
|
|
>>> mol = skchem.Mol.from_smiles('CC(C)=CC') |
|
94
|
|
|
>>> ic(mol, 0) # doctest: +ELLIPSIS |
|
95
|
|
|
0.918... |
|
96
|
|
|
|
|
97
|
|
|
>>> ic(mol, 1) # doctest: +ELLIPSIS |
|
98
|
|
|
1.375... |
|
99
|
|
|
|
|
100
|
|
|
>>> ic(mol, 2) # doctest: +ELLIPSIS |
|
101
|
|
|
1.871... |
|
102
|
|
|
|
|
103
|
|
|
>>> ic(mol, 3) # doctest: +ELLIPSIS |
|
104
|
|
|
2.422... |
|
105
|
|
|
|
|
106
|
|
|
References: |
|
107
|
|
|
Molecular Descriptors for Chemoinformatics, pp 408-411 |
|
108
|
|
|
doi:10.1002/9783527628766 |
|
109
|
|
|
""" |
|
110
|
|
|
|
|
111
|
|
|
if m > 6: |
|
112
|
|
|
return _ic(mol, m)[m] |
|
113
|
|
|
|
|
114
|
|
|
if not hasattr(mol, '_ic'): |
|
115
|
|
|
mol._ic = _ic(mol, 6) |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
return mol._ic[m] |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
@requires_h_filled |
|
121
|
|
|
def tic(mol, m): |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
""" Neighborhood Total Information Content of order m. |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
The $m$th order total neighbourhood Information Content $TIC_m$ |
|
126
|
|
|
is defined as: |
|
127
|
|
|
|
|
128
|
|
|
$$ IC_m = A \cdot IC_m $$ |
|
129
|
|
|
|
|
130
|
|
|
where $A$ is the number of graph vertices. |
|
131
|
|
|
|
|
132
|
|
|
Args: |
|
133
|
|
|
mol (skchem.Mol): |
|
134
|
|
|
The molecule for which to calculate the descriptor. |
|
135
|
|
|
|
|
136
|
|
|
m (int): |
|
137
|
|
|
The order of the environments to use. |
|
138
|
|
|
|
|
139
|
|
|
Returns: |
|
140
|
|
|
float |
|
141
|
|
|
|
|
142
|
|
|
Examples: |
|
143
|
|
|
|
|
144
|
|
|
From MDC: |
|
145
|
|
|
|
|
146
|
|
|
>>> import skchem |
|
147
|
|
|
>>> mol = skchem.Mol.from_smiles('CC(C)=CC') |
|
148
|
|
|
>>> tic(mol, 0) # doctest: +ELLIPSIS |
|
149
|
|
|
13.774... |
|
150
|
|
|
|
|
151
|
|
|
>>> tic(mol, 1) # doctest: +ELLIPSIS |
|
152
|
|
|
20.629... |
|
153
|
|
|
|
|
154
|
|
|
>>> tic(mol, 2) # doctest: +ELLIPSIS |
|
155
|
|
|
28.074... |
|
156
|
|
|
|
|
157
|
|
|
>>> tic(mol, 3) # doctest: +ELLIPSIS |
|
158
|
|
|
36.338... |
|
159
|
|
|
|
|
160
|
|
|
References: |
|
161
|
|
|
Molecular Descriptors for Chemoinformatics, pp 408-411 |
|
162
|
|
|
doi:10.1002/9783527628766 |
|
163
|
|
|
""" |
|
164
|
|
|
|
|
165
|
|
|
return len(mol.atoms) * ic(mol, m) |
|
166
|
|
|
|
|
167
|
|
|
@requires_h_filled |
|
168
|
|
|
def sic(mol, m): |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
""" Structural Information Content of order *m*. |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
The m'th order SIC_m is defined in a normalized form of the information |
|
173
|
|
|
content to delete the influence of graph size. |
|
174
|
|
|
|
|
175
|
|
|
$$ SIC_m = \frac{IC_m}{\log_2{A}} $$ |
|
176
|
|
|
|
|
177
|
|
|
Args: |
|
178
|
|
|
mol (skchem.Mol): |
|
179
|
|
|
The molecule for which to calculate the descriptor. |
|
180
|
|
|
|
|
181
|
|
|
m (int): |
|
182
|
|
|
The order of sugraphs to use. |
|
183
|
|
|
|
|
184
|
|
|
Returns: |
|
185
|
|
|
float |
|
186
|
|
|
|
|
187
|
|
|
Examples: |
|
188
|
|
|
|
|
189
|
|
|
From MDC: |
|
190
|
|
|
|
|
191
|
|
|
>>> import skchem |
|
192
|
|
|
>>> mol = skchem.Mol.from_smiles('CC(C)=CC') |
|
193
|
|
|
>>> sic(mol, 0) # doctest: +ELLIPSIS |
|
194
|
|
|
0.235... |
|
195
|
|
|
|
|
196
|
|
|
>>> sic(mol, 1) # doctest: +ELLIPSIS |
|
197
|
|
|
0.352... |
|
198
|
|
|
|
|
199
|
|
|
>>> sic(mol, 2) # doctest: +ELLIPSIS |
|
200
|
|
|
0.479... |
|
201
|
|
|
|
|
202
|
|
|
>>> sic(mol, 3) # doctest: +ELLIPSIS |
|
203
|
|
|
0.620... |
|
204
|
|
|
|
|
205
|
|
|
References: |
|
206
|
|
|
Molecular Descriptors for Chemoinformatics, pp 408-411 |
|
207
|
|
|
doi:10.1002/9783527628766 |
|
208
|
|
|
|
|
209
|
|
|
""" |
|
210
|
|
|
|
|
211
|
|
|
n_atoms = len(mol.atoms) |
|
212
|
|
|
if n_atoms <= 1: |
|
213
|
|
|
return 0.0 |
|
214
|
|
|
return ic(mol, m) / np.log2(n_atoms) |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
@requires_h_filled |
|
218
|
|
|
def bic(mol, m): |
|
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
""" Bonding Information Content of order $m$. |
|
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
The $m$th order $BIC_m$ is defined in a normalized form as the $SIC_m$, but |
|
223
|
|
|
taking into account the number of edges and their multiplicity, |
|
224
|
|
|
|
|
225
|
|
|
$$ BIC_m = \frac{IC_m}{\log{2}{(\sum{b=1}{B} \pi_b^{*})}} $$ |
|
226
|
|
|
|
|
227
|
|
|
where B is the number of edges and $\pi_b^{*} is the conventional bond |
|
228
|
|
|
order of the edge $b$. In the original definition, the denominator was |
|
229
|
|
|
simply considered to be the edge number $B$. |
|
230
|
|
|
|
|
231
|
|
|
Args: |
|
232
|
|
|
mol (skchem.Mol): |
|
233
|
|
|
The molecule for which to calculate the descriptor. |
|
234
|
|
|
|
|
235
|
|
|
m (int): |
|
236
|
|
|
The order of the sugraphs to use. |
|
237
|
|
|
|
|
238
|
|
|
Returns: |
|
239
|
|
|
float |
|
240
|
|
|
|
|
241
|
|
|
Examples: |
|
242
|
|
|
|
|
243
|
|
|
From MDC: |
|
244
|
|
|
|
|
245
|
|
|
>>> import skchem |
|
246
|
|
|
>>> mol = skchem.Mol.from_smiles('CC(C)=CC') |
|
247
|
|
|
>>> bic(mol, 0) # doctest: +ELLIPSIS |
|
248
|
|
|
0.235... |
|
249
|
|
|
|
|
250
|
|
|
>>> bic(mol, 1) # doctest: +ELLIPSIS |
|
251
|
|
|
0.352... |
|
252
|
|
|
|
|
253
|
|
|
>>> bic(mol, 2) # doctest: +ELLIPSIS |
|
254
|
|
|
0.479... |
|
255
|
|
|
|
|
256
|
|
|
>>> bic(mol, 3) # doctest: +ELLIPSIS |
|
257
|
|
|
0.620... |
|
258
|
|
|
|
|
259
|
|
|
References: |
|
260
|
|
|
Molecular Descriptors for Chemoinformatics, pp 408-411 |
|
261
|
|
|
doi:10.1002/9783527628766 |
|
262
|
|
|
|
|
263
|
|
|
""" |
|
264
|
|
|
|
|
265
|
|
|
return ic(mol, m) / np.log2(sum(mol.bonds.order)) |
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
@requires_h_filled |
|
269
|
|
|
def cic(mol, m): |
|
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
""" Complementary Information Content of order $m$. |
|
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
The $m$th order $CIC_m$ measures the deviation of $IC_m$ from its maximum |
|
274
|
|
|
value, that corresponds to the vertex partition into equivalence classes |
|
275
|
|
|
containing one element each: |
|
276
|
|
|
|
|
277
|
|
|
$$ CIC_m = \log{2}{A} - IC_m $$ |
|
278
|
|
|
|
|
279
|
|
|
where $A$ is the number of graph vertices. |
|
280
|
|
|
|
|
281
|
|
|
Args: |
|
282
|
|
|
mol (skchem.Mol): |
|
283
|
|
|
The molecule for which to calculate the descriptor. |
|
284
|
|
|
|
|
285
|
|
|
m (int): |
|
286
|
|
|
The order of sugraphs to use. |
|
287
|
|
|
|
|
288
|
|
|
Returns: |
|
289
|
|
|
float |
|
290
|
|
|
|
|
291
|
|
|
Examples: |
|
292
|
|
|
|
|
293
|
|
|
From MDC: |
|
294
|
|
|
|
|
295
|
|
|
>>> import skchem |
|
296
|
|
|
>>> mol = skchem.Mol.from_smiles('CC(C)=CC') |
|
297
|
|
|
>>> cic(mol, 0) # doctest: +ELLIPSIS |
|
298
|
|
|
2.988... |
|
299
|
|
|
|
|
300
|
|
|
>>> cic(mol, 1) # doctest: +ELLIPSIS |
|
301
|
|
|
2.531... |
|
302
|
|
|
|
|
303
|
|
|
>>> cic(mol, 2) # doctest: +ELLIPSIS |
|
304
|
|
|
2.035... |
|
305
|
|
|
|
|
306
|
|
|
>>> cic(mol, 3) # doctest: +ELLIPSIS |
|
307
|
|
|
1.484... |
|
308
|
|
|
|
|
309
|
|
|
References: |
|
310
|
|
|
Molecular Descriptors for Chemoinformatics, pp 408-411 |
|
311
|
|
|
doi:10.1002/9783527628766 |
|
312
|
|
|
""" |
|
313
|
|
|
|
|
314
|
|
|
n_atoms = len(mol.atoms) |
|
315
|
|
|
if n_atoms <= 1: |
|
316
|
|
|
return 0.0 |
|
317
|
|
|
return np.log2(n_atoms) - ic(mol, m) |
|
318
|
|
|
|
|
319
|
|
|
|
|
320
|
|
|
def ric(mol, m): |
|
|
|
|
|
|
321
|
|
|
|
|
322
|
|
|
""" The redundant information content. |
|
|
|
|
|
|
323
|
|
|
|
|
324
|
|
|
A measure of relative redundancy of a graph obtained by normalizing the |
|
325
|
|
|
complementary information content, defined as: |
|
326
|
|
|
|
|
327
|
|
|
$$ R_m = \frac{CIC_m}{\log{2}{A}} = 1 - SIC_m $$ |
|
328
|
|
|
|
|
329
|
|
|
Args: |
|
330
|
|
|
mol (skchem.Mol): |
|
331
|
|
|
The molecule for which to calculate the descriptor. |
|
332
|
|
|
|
|
333
|
|
|
m (int): |
|
334
|
|
|
The order of sugraphs to use. |
|
335
|
|
|
|
|
336
|
|
|
Returns: |
|
337
|
|
|
float |
|
338
|
|
|
|
|
339
|
|
|
Examples: |
|
340
|
|
|
|
|
341
|
|
|
Extrapolated from MDC: |
|
342
|
|
|
|
|
343
|
|
|
>>> import skchem |
|
344
|
|
|
>>> mol = skchem.Mol.from_smiles('CC(C)=CC') |
|
345
|
|
|
>>> ric(mol, 0) # doctest: +ELLIPSIS |
|
346
|
|
|
-1.988... |
|
347
|
|
|
|
|
348
|
|
|
>>> ric(mol, 1) # doctest: +ELLIPSIS |
|
349
|
|
|
-1.531... |
|
350
|
|
|
|
|
351
|
|
|
>>> ric(mol, 2) # doctest: +ELLIPSIS |
|
352
|
|
|
-1.035... |
|
353
|
|
|
|
|
354
|
|
|
>>> ric(mol, 3) # doctest: +ELLIPSIS |
|
355
|
|
|
-0.484... |
|
356
|
|
|
|
|
357
|
|
|
References: |
|
358
|
|
|
Molecular Descriptors for Chemoinformatics, pp 408-411 |
|
359
|
|
|
doi:10.1002/9783527628766 |
|
360
|
|
|
|
|
361
|
|
|
""" |
|
362
|
|
|
|
|
363
|
|
|
return 1 - cic(mol, m) |
|
364
|
|
|
|
|
365
|
|
|
|
|
366
|
|
|
def ord_neigh(mol): |
|
367
|
|
|
|
|
368
|
|
|
""" The order of neighbourhood. |
|
|
|
|
|
|
369
|
|
|
|
|
370
|
|
|
The order of neighbourhood is defined as the order $m$ of the $IC_m$ index |
|
371
|
|
|
when it reaches the maximum value: |
|
372
|
|
|
|
|
373
|
|
|
\DeclareMathOperator*{\argmax}{arg\,max} |
|
374
|
|
|
|
|
375
|
|
|
$$ O = \min \Big( \argmax_m IC_m \Big) $$ |
|
376
|
|
|
|
|
377
|
|
|
Args: |
|
378
|
|
|
mol (skchem.Mol): |
|
379
|
|
|
The molecule for which to calculate the descriptor. |
|
380
|
|
|
|
|
381
|
|
|
Returns: |
|
382
|
|
|
int |
|
383
|
|
|
|
|
384
|
|
|
Examples: |
|
385
|
|
|
|
|
386
|
|
|
From MDC: |
|
387
|
|
|
|
|
388
|
|
|
>>> import skchem |
|
389
|
|
|
>>> mol = skchem.Mol.from_smiles('CC(C)=CC') |
|
390
|
|
|
>>> ord_neigh(mol) |
|
391
|
|
|
3 |
|
392
|
|
|
|
|
393
|
|
|
References: |
|
394
|
|
|
Molecular Descriptors for Chemoinformatics, pp 408-411 |
|
395
|
|
|
doi:10.1002/9783527628766 |
|
396
|
|
|
|
|
397
|
|
|
""" |
|
398
|
|
|
|
|
399
|
|
|
if not hasattr(mol, '_ic'): |
|
400
|
|
|
mol._ic = _ic(mol, 6) |
|
|
|
|
|
|
401
|
|
|
|
|
402
|
|
|
res = np.argmax(mol._ic) |
|
|
|
|
|
|
403
|
|
|
if res == 6: |
|
404
|
|
|
return np.argmax(_ic(mol, 20)) |
|
405
|
|
|
else: |
|
406
|
|
|
return res |
|
407
|
|
|
|
|
408
|
|
|
|
|
409
|
|
|
DESCRIPTORS = {'ic_{}'.format(i): partial(ic, m=i) for i in range(7)} |
|
410
|
|
|
DESCRIPTORS.update({'tic_{}'.format(i): partial(ic, m=i) for i in range(7)}) |
|
411
|
|
|
DESCRIPTORS.update({'sic_{}'.format(i): partial(sic, m=i) for i in range(7)}) |
|
412
|
|
|
DESCRIPTORS.update({'cic_{}'.format(i): partial(cic, m=i) for i in range(7)}) |
|
413
|
|
|
DESCRIPTORS.update({'bic_{}'.format(i): partial(bic, m=i) for i in range(7)}) |
|
414
|
|
|
DESCRIPTORS.update({'ric_{}'.format(i): partial(ric, m=i) for i in range(7)}) |
|
415
|
|
|
DESCRIPTORS['ord_neigh'] = ord_neigh |
|
416
|
|
|
|
|
417
|
|
|
__all__ = ['ic', 'tic','sic', 'cic', 'bic', 'ric', 'ord_neigh', 'DESCRIPTORS'] |
|
|
|
|
|
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.