|
1
|
|
|
#! /usr/bin/env python |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2007-2009 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
|
|
""" |
|
7
|
|
|
skchem.descriptors.fingerprints |
|
8
|
|
|
|
|
9
|
|
|
Fingerprinting classes and associated functions are defined. |
|
10
|
|
|
|
|
11
|
|
|
""" |
|
12
|
|
|
|
|
13
|
|
|
from functools import wraps |
|
14
|
|
|
|
|
15
|
|
|
import pandas as pd |
|
|
|
|
|
|
16
|
|
|
from rdkit.Chem import DataStructs |
|
|
|
|
|
|
17
|
|
|
from rdkit.Chem.rdMolDescriptors import (GetMorganFingerprint, |
|
|
|
|
|
|
18
|
|
|
GetHashedMorganFingerprint, |
|
19
|
|
|
GetAtomPairFingerprint, |
|
20
|
|
|
GetHashedAtomPairFingerprint, |
|
21
|
|
|
GetTopologicalTorsionFingerprint, |
|
22
|
|
|
GetHashedTopologicalTorsionFingerprint, |
|
23
|
|
|
GetMACCSKeysFingerprint, |
|
24
|
|
|
GetFeatureInvariants, |
|
25
|
|
|
GetConnectivityInvariants) |
|
26
|
|
|
from rdkit.Chem.rdReducedGraphs import GetErGFingerprint |
|
|
|
|
|
|
27
|
|
|
from rdkit.Chem.rdmolops import RDKFingerprint |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
import numpy as np |
|
|
|
|
|
|
30
|
|
|
import skchem |
|
31
|
|
|
|
|
32
|
|
|
def skchemize(func, columns=None, *args, **kwargs): |
|
33
|
|
|
""" |
|
34
|
|
|
|
|
35
|
|
|
transform an RDKit fingerprinting function to work well with pandas |
|
36
|
|
|
|
|
37
|
|
|
>>> from rdkit import Chem |
|
38
|
|
|
>>> import skchem |
|
39
|
|
|
>>> from skchem.descriptors import skchemize |
|
40
|
|
|
>>> from skchem.core import Mol |
|
41
|
|
|
>>> f = skchemize(Chem.RDKFingerprint) |
|
42
|
|
|
>>> m = Mol.from_smiles('c1ccccc1') |
|
43
|
|
|
>>> f(m) |
|
44
|
|
|
0 0 |
|
45
|
|
|
1 0 |
|
46
|
|
|
2 0 |
|
47
|
|
|
3 0 |
|
48
|
|
|
4 0 |
|
49
|
|
|
5 0 |
|
50
|
|
|
6 0 |
|
51
|
|
|
7 0 |
|
52
|
|
|
8 0 |
|
53
|
|
|
9 0 |
|
54
|
|
|
10 0 |
|
55
|
|
|
11 0 |
|
56
|
|
|
12 0 |
|
57
|
|
|
13 0 |
|
58
|
|
|
14 0 |
|
59
|
|
|
15 0 |
|
60
|
|
|
16 0 |
|
61
|
|
|
17 0 |
|
62
|
|
|
18 0 |
|
63
|
|
|
19 0 |
|
64
|
|
|
20 0 |
|
65
|
|
|
21 0 |
|
66
|
|
|
22 0 |
|
67
|
|
|
23 0 |
|
68
|
|
|
24 0 |
|
69
|
|
|
25 0 |
|
70
|
|
|
26 0 |
|
71
|
|
|
27 0 |
|
72
|
|
|
28 0 |
|
73
|
|
|
29 0 |
|
74
|
|
|
.. |
|
75
|
|
|
2018 0 |
|
76
|
|
|
2019 0 |
|
77
|
|
|
2020 0 |
|
78
|
|
|
2021 0 |
|
79
|
|
|
2022 0 |
|
80
|
|
|
2023 0 |
|
81
|
|
|
2024 0 |
|
82
|
|
|
2025 0 |
|
83
|
|
|
2026 0 |
|
84
|
|
|
2027 0 |
|
85
|
|
|
2028 0 |
|
86
|
|
|
2029 0 |
|
87
|
|
|
2030 0 |
|
88
|
|
|
2031 0 |
|
89
|
|
|
2032 0 |
|
90
|
|
|
2033 0 |
|
91
|
|
|
2034 0 |
|
92
|
|
|
2035 0 |
|
93
|
|
|
2036 0 |
|
94
|
|
|
2037 0 |
|
95
|
|
|
2038 0 |
|
96
|
|
|
2039 0 |
|
97
|
|
|
2040 0 |
|
98
|
|
|
2041 0 |
|
99
|
|
|
2042 0 |
|
100
|
|
|
2043 0 |
|
101
|
|
|
2044 0 |
|
102
|
|
|
2045 0 |
|
103
|
|
|
2046 0 |
|
104
|
|
|
2047 0 |
|
105
|
|
|
dtype: int64 |
|
106
|
|
|
>>> from skchem.data import resource |
|
107
|
|
|
>>> df = skchem.read_sdf(resource('test_sdf', 'multi_molecule-simple.sdf')) |
|
108
|
|
|
>>> df.structure.apply(f) |
|
109
|
|
|
0 1 2 3 4 5 6 7 8 9 ... 2038 \\ |
|
110
|
|
|
name ... |
|
111
|
|
|
297 0 0 0 0 0 0 0 0 0 0 ... 0 |
|
112
|
|
|
6324 0 0 0 0 0 0 0 0 0 0 ... 0 |
|
113
|
|
|
6334 0 0 0 0 0 0 0 0 0 0 ... 0 |
|
114
|
|
|
<BLANKLINE> |
|
115
|
|
|
2039 2040 2041 2042 2043 2044 2045 2046 2047 |
|
116
|
|
|
name |
|
117
|
|
|
297 0 0 0 0 0 0 0 0 0 |
|
118
|
|
|
6324 0 0 0 0 0 0 0 0 0 |
|
119
|
|
|
6334 0 0 0 0 0 0 0 0 0 |
|
120
|
|
|
<BLANKLINE> |
|
121
|
|
|
[3 rows x 2048 columns] |
|
122
|
|
|
|
|
123
|
|
|
""" |
|
124
|
|
|
@wraps(func) |
|
125
|
|
|
def func_wrapper(m): |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
""" Function that wraps an rdkit function allowing it to produce dataframes. """ |
|
128
|
|
|
|
|
129
|
|
|
arr = np.array(0) |
|
130
|
|
|
DataStructs.ConvertToNumpyArray(func(m, *args, **kwargs), arr) |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
return pd.Series(arr, index=columns) |
|
133
|
|
|
|
|
134
|
|
|
return func_wrapper |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
class Fingerprinter(object): |
|
138
|
|
|
|
|
139
|
|
|
""" Fingerprinter Base class. """ |
|
140
|
|
|
|
|
141
|
|
|
def __init__(self, name, func): |
|
142
|
|
|
self.NAME = name |
|
|
|
|
|
|
143
|
|
|
self.func = func |
|
144
|
|
|
|
|
145
|
|
|
def __call__(self, obj): |
|
146
|
|
|
return self.transform(obj) |
|
147
|
|
|
|
|
148
|
|
|
def __add__(self, other): |
|
149
|
|
|
fpers = [] |
|
150
|
|
|
for fper in (self, other): |
|
151
|
|
|
if isinstance(fper, FusionFingerprinter): |
|
152
|
|
|
fpers += fper.fingerprinters |
|
|
|
|
|
|
153
|
|
|
else: |
|
154
|
|
|
fpers.append(fper) |
|
155
|
|
|
|
|
156
|
|
|
return FusionFingerprinter(fpers) |
|
157
|
|
|
|
|
158
|
|
|
def fit(self, X, y): |
|
|
|
|
|
|
159
|
|
|
return self |
|
160
|
|
|
|
|
161
|
|
|
def transform(self, obj): |
|
162
|
|
|
|
|
163
|
|
|
""" calculate the fingerprint for the given object. """ |
|
164
|
|
|
|
|
165
|
|
|
if isinstance(obj, skchem.Mol): |
|
166
|
|
|
return self._transform(obj) |
|
167
|
|
|
|
|
168
|
|
|
elif isinstance(obj, pd.DataFrame): |
|
169
|
|
|
return obj.structure.apply(self.transform) |
|
170
|
|
|
|
|
171
|
|
|
elif isinstance(obj, pd.Series): |
|
172
|
|
|
return obj.apply(self.transform) |
|
173
|
|
|
|
|
174
|
|
|
else: |
|
175
|
|
|
raise NotImplementedError |
|
176
|
|
|
|
|
177
|
|
|
def _transform(self, mol): |
|
178
|
|
|
|
|
179
|
|
|
""" Calculate the fingerprint on a molecule. """ |
|
180
|
|
|
|
|
181
|
|
|
return pd.Series(list(self.func(mol)), name=mol.name) |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
class FusionFingerprinter(Fingerprinter): |
|
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
def __init__(self, fingerprinters): |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
self.fingerprinters = fingerprinters |
|
189
|
|
|
|
|
190
|
|
|
def transform(self, obj): |
|
191
|
|
|
|
|
192
|
|
|
if isinstance(obj, skchem.Mol): |
|
193
|
|
|
return pd.concat([fp.transform(obj) for fp in self.fingerprinters], keys=[fp.NAME for fp in self.fingerprinters]) |
|
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
elif isinstance(obj, pd.DataFrame): |
|
196
|
|
|
return pd.concat([fp.transform(obj) for fp in self.fingerprinters], keys=[fp.NAME for fp in self.fingerprinters], axis=1) |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
elif isinstance(obj, pd.Series): |
|
199
|
|
|
return pd.concat([fp.transform(obj.structure) for fp in self.fingerprinters], keys=[fp.NAME for fp in self.fingerprinters], axis=1) |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
else: |
|
202
|
|
|
raise NotImplementedError |
|
203
|
|
|
|
|
204
|
|
|
def _transform(self, mol): |
|
205
|
|
|
|
|
206
|
|
|
return pd.concat([fp.transform(mol) for fp in self.fingerprinters]) |
|
207
|
|
|
|
|
208
|
|
View Code Duplication |
class MorganFingerprinter(Fingerprinter): |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
""" Morgan Fingerprint Transformer. """ |
|
211
|
|
|
|
|
212
|
|
|
NAME = 'morgan' |
|
213
|
|
|
|
|
214
|
|
|
def __init__(self, radius=2, n_feats=2048, as_bits=True, |
|
|
|
|
|
|
215
|
|
|
use_features=False, use_bond_types=True, use_chirality=False): |
|
216
|
|
|
|
|
217
|
|
|
""" |
|
218
|
|
|
@param radius |
|
219
|
|
|
@param n_feats |
|
220
|
|
|
@param as_bits |
|
221
|
|
|
@param use_features |
|
222
|
|
|
@param use_bond_types |
|
223
|
|
|
@param use_chirality |
|
224
|
|
|
""" |
|
225
|
|
|
self.radius = radius |
|
226
|
|
|
self.n_feats = n_feats |
|
227
|
|
|
self.as_bits = as_bits |
|
228
|
|
|
self.use_features = use_features |
|
229
|
|
|
self.use_bond_types = use_bond_types |
|
230
|
|
|
self.use_chirality = use_chirality |
|
231
|
|
|
|
|
232
|
|
|
def _transform(self, mol): |
|
233
|
|
|
|
|
234
|
|
|
""" |
|
235
|
|
|
@param mol |
|
236
|
|
|
|
|
237
|
|
|
@returns pd.Series |
|
238
|
|
|
""" |
|
239
|
|
|
if self.n_feats == -1: |
|
240
|
|
|
|
|
241
|
|
|
res = GetMorganFingerprint(mol, self.radius, |
|
242
|
|
|
useFeatures=self.use_features, |
|
243
|
|
|
useBondTypes=self.use_bond_types, |
|
244
|
|
|
useChirality=self.use_chirality) |
|
245
|
|
|
else: |
|
246
|
|
|
res = list(GetHashedMorganFingerprint(mol, self.radius, |
|
247
|
|
|
nBits=self.n_feats, |
|
|
|
|
|
|
248
|
|
|
useFeatures=self.use_features, |
|
|
|
|
|
|
249
|
|
|
useBondTypes=self.use_bond_types, |
|
|
|
|
|
|
250
|
|
|
useChirality=self.use_chirality)) |
|
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
res = pd.Series(res, name=mol.name) |
|
253
|
|
|
|
|
254
|
|
|
if self.as_bits: |
|
255
|
|
|
return (res > 0).astype(int) |
|
256
|
|
|
else: |
|
257
|
|
|
return res |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
View Code Duplication |
class AtomPairFingerprinter(Fingerprinter): |
|
|
|
|
|
|
261
|
|
|
|
|
262
|
|
|
""" Atom Pair Tranformer. """ |
|
263
|
|
|
|
|
264
|
|
|
NAME = 'atom_pair' |
|
265
|
|
|
|
|
266
|
|
|
def __init__(self, min_length=1, max_length=30, n_feats=2048, as_bits=False, use_chirality=False): |
|
|
|
|
|
|
267
|
|
|
self.min_length = min_length |
|
268
|
|
|
self.max_length = max_length |
|
269
|
|
|
self.n_feats = n_feats |
|
270
|
|
|
self.as_bits = as_bits |
|
271
|
|
|
self.use_chirality = use_chirality |
|
272
|
|
|
|
|
273
|
|
|
def _transform(self, mol): |
|
274
|
|
|
|
|
275
|
|
|
""" |
|
276
|
|
|
@param molecules |
|
277
|
|
|
|
|
278
|
|
|
@return pd.Series |
|
279
|
|
|
""" |
|
280
|
|
|
|
|
281
|
|
|
if self.n_feats == -1: |
|
282
|
|
|
|
|
283
|
|
|
res = GetAtomPairFingerprint(mol, minLength=self.min_length, |
|
284
|
|
|
maxLength=self.max_length, |
|
285
|
|
|
includeChirality=self.use_chirality) |
|
286
|
|
|
else: |
|
287
|
|
|
res = list(GetHashedAtomPairFingerprint(mol, minLength=self.min_length, |
|
288
|
|
|
maxLength=self.max_length, |
|
|
|
|
|
|
289
|
|
|
nBits=self.n_feats, |
|
|
|
|
|
|
290
|
|
|
includeChirality=self.use_chirality)) |
|
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
res = pd.Series(res, name=mol.name) |
|
293
|
|
|
|
|
294
|
|
|
if self.as_bits: |
|
295
|
|
|
return (res > 0).astype(int) |
|
296
|
|
|
else: |
|
297
|
|
|
return res |
|
298
|
|
|
|
|
299
|
|
|
class TopologicalTorsionFingerprinter(Fingerprinter): |
|
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
NAME = 'topological_torsion' |
|
302
|
|
|
|
|
303
|
|
|
def __init__(self, target_size=4, n_feats=2048, as_bits=False, |
|
|
|
|
|
|
304
|
|
|
use_chirality=False): |
|
305
|
|
|
self.target_size = target_size |
|
|
|
|
|
|
306
|
|
|
self.n_feats = n_feats |
|
|
|
|
|
|
307
|
|
|
self.as_bits = as_bits |
|
|
|
|
|
|
308
|
|
|
self.use_chirality = use_chirality |
|
|
|
|
|
|
309
|
|
|
|
|
310
|
|
|
def _transform(self, mol): |
|
311
|
|
|
|
|
312
|
|
|
if self.n_feats == -1: |
|
313
|
|
|
|
|
314
|
|
|
res = GetTopologicalTorsionFingerprint(mol, targetSize=self.targetSize, |
|
|
|
|
|
|
315
|
|
|
includeChirality=self.use_chirality) |
|
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
else: |
|
318
|
|
|
res = list(GetHashedTopologicalTorsionFingerprint(mol, |
|
319
|
|
|
targetSize=self.targetSize, |
|
|
|
|
|
|
320
|
|
|
nBits=self.n_feats)) |
|
|
|
|
|
|
321
|
|
|
|
|
322
|
|
|
res = pd.Series(res, name=mol.name) |
|
323
|
|
|
|
|
324
|
|
|
if self.as_bits: |
|
325
|
|
|
return (res > 0).astype(int) |
|
326
|
|
|
else: |
|
327
|
|
|
return res |
|
328
|
|
|
|
|
329
|
|
|
|
|
330
|
|
|
class MACCSKeysFingerprinter(Fingerprinter): |
|
331
|
|
|
|
|
332
|
|
|
""" MACCS Keys Fingerprints """ |
|
333
|
|
|
|
|
334
|
|
|
NAME = 'maccs' |
|
335
|
|
|
|
|
336
|
|
|
def __init__(self): |
|
|
|
|
|
|
337
|
|
|
pass |
|
338
|
|
|
|
|
339
|
|
|
def _transform(self, mol): |
|
340
|
|
|
|
|
341
|
|
|
return pd.Series(list(GetMACCSKeysFingerprint(mol))) |
|
342
|
|
|
|
|
343
|
|
|
class ErGFingerprinter(Fingerprinter): |
|
344
|
|
|
|
|
345
|
|
|
""" ErG Fingerprints """ |
|
346
|
|
|
|
|
347
|
|
|
NAME = 'erg' |
|
348
|
|
|
|
|
349
|
|
|
def __init__(self): |
|
|
|
|
|
|
350
|
|
|
pass |
|
351
|
|
|
|
|
352
|
|
|
def _transform(self, mol): |
|
353
|
|
|
|
|
354
|
|
|
return pd.Series(GetErGFingerprint(mol)) |
|
355
|
|
|
|
|
356
|
|
|
class FeatureInvariantsFingerprinter(Fingerprinter): |
|
357
|
|
|
|
|
358
|
|
|
""" Feature invariant fingerprints. """ |
|
359
|
|
|
|
|
360
|
|
|
NAME = 'feat_inv' |
|
361
|
|
|
|
|
362
|
|
|
def __init__(self): |
|
|
|
|
|
|
363
|
|
|
pass |
|
364
|
|
|
|
|
365
|
|
|
def _transform(self, mol): |
|
366
|
|
|
|
|
367
|
|
|
return pd.Series(GetFeatureInvariants(mol)) |
|
368
|
|
|
|
|
369
|
|
|
class ConnectivityInvariantsFingerprinter(Fingerprinter): |
|
370
|
|
|
|
|
371
|
|
|
""" Connectiity invariant fingerprints """ |
|
372
|
|
|
|
|
|
|
|
|
|
373
|
|
|
NAME = 'conn_inv' |
|
374
|
|
|
|
|
375
|
|
|
def __init__(self): |
|
|
|
|
|
|
376
|
|
|
pass |
|
377
|
|
|
|
|
378
|
|
|
def _transform(self, mol): |
|
379
|
|
|
|
|
380
|
|
|
return pd.Series(GetConnectivityInvariants(mol)) |
|
381
|
|
|
|
|
382
|
|
|
class RDKFingerprinter(Fingerprinter): |
|
|
|
|
|
|
383
|
|
|
|
|
384
|
|
|
""" RDKit fingerprint """ |
|
385
|
|
|
|
|
386
|
|
|
NAME = 'rdk' |
|
387
|
|
|
|
|
388
|
|
|
def __init__(self, min_path=1, max_path=7, n_feats=2048, n_bits_per_hash=2, |
|
|
|
|
|
|
389
|
|
|
use_hs=True, target_density=0.0, min_size=128, |
|
|
|
|
|
|
390
|
|
|
branched_paths=True, use_bond_types=True): |
|
|
|
|
|
|
391
|
|
|
self.min_path = 1 |
|
|
|
|
|
|
392
|
|
|
self.max_path = 7 |
|
|
|
|
|
|
393
|
|
|
self.n_feats = 2048 |
|
|
|
|
|
|
394
|
|
|
self.n_bits_per_hash = 2 |
|
|
|
|
|
|
395
|
|
|
self.use_hs = True |
|
|
|
|
|
|
396
|
|
|
self.target_density = 0.0 |
|
|
|
|
|
|
397
|
|
|
self.min_size = 128 |
|
|
|
|
|
|
398
|
|
|
self.branched_paths = True |
|
|
|
|
|
|
399
|
|
|
self.use_bond_types = True |
|
|
|
|
|
|
400
|
|
|
|
|
401
|
|
|
def _transform(self, mol): |
|
402
|
|
|
|
|
403
|
|
|
return pd.Series(list(RDKFingerprint(mol, minPath=self.min_path, |
|
404
|
|
|
maxPath=self.max_path, |
|
405
|
|
|
fpSize=self.n_feats, |
|
406
|
|
|
nBitsPerHash=self.n_bits_per_hash, |
|
407
|
|
|
useHs=self.use_hs, |
|
408
|
|
|
tgtDensity=self.target_density, |
|
409
|
|
|
minSize=self.min_size, |
|
410
|
|
|
branchedPaths=self.branched_paths, |
|
411
|
|
|
useBondOrder=self.use_bond_types)), |
|
412
|
|
|
name=mol.name) |
|
|
|
|
|
|
413
|
|
|
|
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.