|
1
|
|
|
#! /usr/bin/env python |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2015-2016 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
|
|
""" |
|
7
|
|
|
## skchem.core.mol |
|
8
|
|
|
|
|
9
|
|
|
Defining molecules in scikit-chem. |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
|
|
import rdkit.Chem |
|
|
|
|
|
|
13
|
|
|
import rdkit.Chem.inchi |
|
|
|
|
|
|
14
|
|
|
from rdkit.Chem.rdDepictor import Compute2DCoords |
|
|
|
|
|
|
15
|
|
|
from rdkit.Chem.rdMolDescriptors import CalcMolFormula, CalcExactMolWt |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
import json |
|
18
|
|
|
|
|
19
|
|
|
from . import Atom, Bond, Conformer |
|
|
|
|
|
|
20
|
|
|
from .base import ChemicalObject, AtomView, PropertyView |
|
21
|
|
|
from ..utils import Suppressor |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class Mol(rdkit.Chem.rdchem.Mol, ChemicalObject): |
|
24
|
|
|
|
|
25
|
|
|
"""Class representing a Molecule in scikit-chem. |
|
26
|
|
|
|
|
27
|
|
|
Mol objects inherit directly from rdkit Mol objects. Therefore, they |
|
28
|
|
|
contain atom and bond information, and may also include properties and |
|
29
|
|
|
atom bookmarks. |
|
30
|
|
|
|
|
31
|
|
|
Example: |
|
32
|
|
|
Constructors are implemented as class methods with the `from_` prefix. |
|
33
|
|
|
|
|
34
|
|
|
```python |
|
35
|
|
|
m = Mol.from_smiles('c1ccccc1') |
|
36
|
|
|
``` |
|
37
|
|
|
|
|
38
|
|
|
Serializers are implemented as instance methods with the `to_` prefix. |
|
39
|
|
|
|
|
40
|
|
|
```python |
|
41
|
|
|
m.to_smiles() |
|
42
|
|
|
``` |
|
43
|
|
|
|
|
44
|
|
|
""" |
|
45
|
|
|
|
|
46
|
|
|
def __init__(self, *args, **kwargs): |
|
47
|
|
|
|
|
48
|
|
|
""" |
|
49
|
|
|
The default constructor. |
|
50
|
|
|
|
|
51
|
|
|
Note: |
|
52
|
|
|
This will be rarely used, as it can only create an empty molecule. |
|
53
|
|
|
|
|
54
|
|
|
Args: |
|
55
|
|
|
*args: Arguments to be passed to the rdkit Mol constructor. |
|
56
|
|
|
**kwargs: Arguments to be passed to the rdkit Mol constructor. |
|
57
|
|
|
""" |
|
58
|
|
|
super(Mol, self).__init__(*args, **kwargs) |
|
59
|
|
|
self.__two_d = None #set in constructor |
|
60
|
|
|
|
|
61
|
|
|
@property |
|
62
|
|
|
def name(self): |
|
63
|
|
|
|
|
64
|
|
|
""" str: The name of the molecule. |
|
65
|
|
|
|
|
66
|
|
|
Raises: |
|
67
|
|
|
KeyError""" |
|
68
|
|
|
|
|
69
|
|
|
try: |
|
70
|
|
|
return self.GetProp('_Name') |
|
|
|
|
|
|
71
|
|
|
except KeyError: |
|
72
|
|
|
return None |
|
73
|
|
|
|
|
74
|
|
|
@name.setter |
|
75
|
|
|
def name(self, value): |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if value is None: |
|
78
|
|
|
self.ClearProp('_Name') |
|
|
|
|
|
|
79
|
|
|
else: |
|
80
|
|
|
self.SetProp('_Name', value) |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
@property |
|
83
|
|
|
def atoms(self): |
|
84
|
|
|
|
|
85
|
|
|
""" List[skchem.Atom]: An iterable over the atoms of the molecule. """ |
|
86
|
|
|
|
|
87
|
|
|
if not hasattr(self, '_atoms'): |
|
88
|
|
|
self._atoms = AtomView(self) |
|
|
|
|
|
|
89
|
|
|
return self._atoms |
|
90
|
|
|
|
|
91
|
|
|
@property |
|
92
|
|
|
def bonds(self): |
|
93
|
|
|
|
|
94
|
|
|
""" List[skchem.Bond]: An iterable over the bonds of the molecule. """ |
|
95
|
|
|
|
|
96
|
|
|
return [Bond.from_super(self.GetBondWithIdx(i)) \ |
|
|
|
|
|
|
97
|
|
|
for i in range(self.GetNumBonds())] |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
@property |
|
100
|
|
|
def mass(self): |
|
101
|
|
|
|
|
102
|
|
|
""" float: the mass of the molecule. """ |
|
103
|
|
|
|
|
104
|
|
|
return CalcExactMolWt(self) |
|
105
|
|
|
|
|
106
|
|
|
@property |
|
107
|
|
|
def props(self): |
|
108
|
|
|
|
|
109
|
|
|
""" PropertyView: A dictionary of the properties of the molecule. """ |
|
110
|
|
|
|
|
111
|
|
|
if not hasattr(self, '_props'): |
|
112
|
|
|
self._props = PropertyView(self) |
|
|
|
|
|
|
113
|
|
|
return self._props |
|
114
|
|
|
|
|
115
|
|
|
@property |
|
116
|
|
|
def conformers(self): |
|
117
|
|
|
|
|
118
|
|
|
""" List[Conformer]: conformers of the molecule. """ |
|
119
|
|
|
|
|
120
|
|
|
return [Conformer.from_super(self.GetConformer(i)) \ |
|
|
|
|
|
|
121
|
|
|
for i in range(len(self.GetConformers()))] |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
def to_formula(self): |
|
124
|
|
|
|
|
125
|
|
|
""" str: the chemical formula of the molecule. |
|
126
|
|
|
|
|
127
|
|
|
Raises: |
|
128
|
|
|
RuntimeError""" |
|
129
|
|
|
|
|
130
|
|
|
# formula may be undefined if atoms are uncertainly typed |
|
131
|
|
|
# e.g. if the molecule was initialize through SMARTS |
|
132
|
|
|
try: |
|
133
|
|
|
return CalcMolFormula(self) |
|
134
|
|
|
except RuntimeError: |
|
135
|
|
|
raise ValueError('Formula is undefined for {}'.format(self)) |
|
136
|
|
|
|
|
137
|
|
|
def _two_d(self): |
|
138
|
|
|
|
|
139
|
|
|
""" Return a conformer with coordinates in two dimension. """ |
|
140
|
|
|
|
|
141
|
|
|
if not hasattr(self, '__two_d'): |
|
142
|
|
|
self.__two_d = Compute2DCoords(self) |
|
143
|
|
|
return self.conformers[self.__two_d] |
|
144
|
|
|
|
|
145
|
|
|
def to_dict(self, kind="chemdoodle"): |
|
146
|
|
|
|
|
147
|
|
|
""" A dictionary representation of the molecule. |
|
148
|
|
|
|
|
149
|
|
|
Args: |
|
150
|
|
|
kind (str): |
|
151
|
|
|
The type of representation to use. Only `chemdoodle` is |
|
152
|
|
|
currently supported. |
|
153
|
|
|
Defaults to 'Chemdoodle'. |
|
154
|
|
|
|
|
155
|
|
|
Returns: |
|
156
|
|
|
dict: |
|
157
|
|
|
dictionary representation of the molecule.""" |
|
158
|
|
|
|
|
159
|
|
|
if kind == "chemdoodle": |
|
160
|
|
|
return self._to_dict_chemdoodle() |
|
161
|
|
|
|
|
162
|
|
|
else: |
|
163
|
|
|
raise NotImplementedError |
|
164
|
|
|
|
|
165
|
|
|
def _to_dict_chemdoodle(self): |
|
166
|
|
|
|
|
167
|
|
|
""" Chemdoodle dict representation of the molecule. |
|
168
|
|
|
|
|
169
|
|
|
Documentation of the format may be found on the [chemdoodle website](https://web.chemdoodle.com/docs/chemdoodle-json-format/)""" |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
atom_positions = [p.to_dict() for p in self._two_d().atom_positions] |
|
172
|
|
|
atom_elements = [a.element for a in self.atoms] |
|
173
|
|
|
|
|
174
|
|
|
for i, atom_position in enumerate(atom_positions): |
|
175
|
|
|
atom_position['l'] = atom_elements[i] |
|
176
|
|
|
|
|
177
|
|
|
bonds = [b.to_dict() for b in self.bonds] |
|
178
|
|
|
|
|
179
|
|
|
return {"m": [{"a": atom_positions, "b": bonds}]} |
|
180
|
|
|
|
|
181
|
|
|
def to_json(self, kind='chemdoodle'): |
|
182
|
|
|
|
|
183
|
|
|
""" Serialize a molecule using JSON. |
|
184
|
|
|
|
|
185
|
|
|
Args: |
|
186
|
|
|
kind (str): |
|
187
|
|
|
The type of serialization to use. Only `chemdoodle` is |
|
188
|
|
|
currently supported. |
|
189
|
|
|
|
|
190
|
|
|
Returns: |
|
191
|
|
|
str: the json string. """ |
|
192
|
|
|
|
|
193
|
|
|
return json.dumps(self.to_dict(kind=kind)) |
|
194
|
|
|
|
|
195
|
|
|
def to_inchi_key(self): |
|
196
|
|
|
|
|
197
|
|
|
""" The InChI key of the molecule. |
|
198
|
|
|
|
|
199
|
|
|
Returns: |
|
200
|
|
|
str: the InChI key. |
|
201
|
|
|
|
|
202
|
|
|
Raises: |
|
203
|
|
|
RuntimeError""" |
|
204
|
|
|
|
|
205
|
|
|
if not rdkit.Chem.inchi.INCHI_AVAILABLE: |
|
206
|
|
|
raise ImportError("InChI module not available.") |
|
207
|
|
|
|
|
208
|
|
|
res = rdkit.Chem.InchiToInchiKey(self.to_inchi()) |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
if res is None: |
|
211
|
|
|
raise RuntimeError("The molecule could not be encoded as InChI key.") |
|
212
|
|
|
|
|
213
|
|
|
return res |
|
214
|
|
|
|
|
215
|
|
|
def to_binary(self): |
|
216
|
|
|
|
|
217
|
|
|
""" Serialize the molecule to binary encoding. |
|
218
|
|
|
|
|
219
|
|
|
Args: |
|
220
|
|
|
None |
|
221
|
|
|
|
|
222
|
|
|
Returns: |
|
223
|
|
|
bytes: the molecule in bytes. |
|
224
|
|
|
|
|
225
|
|
|
Notes: |
|
226
|
|
|
Due to limitations in RDKit, not all data is serialized. Notably, |
|
227
|
|
|
properties are not, so e.g. compound names are not saved.""" |
|
228
|
|
|
|
|
229
|
|
|
return self.ToBinary() |
|
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
@classmethod |
|
232
|
|
|
def from_binary(cls, binary): |
|
233
|
|
|
|
|
234
|
|
|
""" Decode a molecule from a binary serialization. |
|
235
|
|
|
|
|
236
|
|
|
Args: |
|
237
|
|
|
binary: The bytes string to decode. |
|
238
|
|
|
|
|
239
|
|
|
Returns: |
|
240
|
|
|
skchem.Mol: The molecule encoded in the binary.""" |
|
241
|
|
|
|
|
242
|
|
|
return cls(binary) |
|
243
|
|
|
|
|
244
|
|
|
def __repr__(self): |
|
245
|
|
|
try: |
|
246
|
|
|
formula = self.to_formula() |
|
247
|
|
|
except ValueError: |
|
248
|
|
|
# if we can't generate the formula, just say it is unknown |
|
249
|
|
|
formula = 'unknown' |
|
250
|
|
|
|
|
251
|
|
|
return '<{klass} name="{name}" formula="{formula}" at {address}>'.format( |
|
252
|
|
|
klass=self.__class__.__name__, |
|
253
|
|
|
name=self.name, |
|
254
|
|
|
formula=formula, |
|
255
|
|
|
address=hex(id(self))) |
|
256
|
|
|
|
|
257
|
|
|
def __contains__(self, item): |
|
258
|
|
|
if isinstance(item, Mol): |
|
259
|
|
|
return self.HasSubstructMatch(item) |
|
|
|
|
|
|
260
|
|
|
else: |
|
261
|
|
|
raise NotImplementedError('No way to check if {} contains {}'.format(self, item)) |
|
262
|
|
|
|
|
263
|
|
|
def __eq__(self, item): |
|
264
|
|
|
if isinstance(item, self.__class__): |
|
265
|
|
|
return (self in item) and (item in self) |
|
266
|
|
|
else: |
|
267
|
|
|
return False |
|
268
|
|
|
|
|
269
|
|
|
def _repr_javascript(self): |
|
270
|
|
|
|
|
271
|
|
|
""" Rich printing in javascript. """ |
|
272
|
|
|
|
|
273
|
|
|
return self.to_json() |
|
274
|
|
|
|
|
275
|
|
|
def __str__(self): |
|
276
|
|
|
return '<Mol: {}>'.format(self.to_smiles()) |
|
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
def bind_constructor(constructor_name, name_to_bind=None): |
|
279
|
|
|
|
|
280
|
|
|
""" Bind an (rdkit) constructor to the class """ |
|
281
|
|
|
|
|
282
|
|
|
@classmethod |
|
283
|
|
|
def constructor(_, in_arg, name=None, *args, **kwargs): |
|
284
|
|
|
|
|
285
|
|
|
""" The constructor to be bound. """ |
|
286
|
|
|
|
|
287
|
|
|
m = getattr(rdkit.Chem, 'MolFrom' + constructor_name)(in_arg, *args, **kwargs) |
|
|
|
|
|
|
288
|
|
|
if m is None: |
|
289
|
|
|
raise ValueError('Failed to parse molecule, {}'.format(in_arg)) |
|
290
|
|
|
m = Mol.from_super(m) |
|
|
|
|
|
|
291
|
|
|
m.name = name |
|
292
|
|
|
return m |
|
293
|
|
|
|
|
294
|
|
|
setattr(Mol, 'from_{}'.format(constructor_name).lower() \ |
|
295
|
|
|
if name_to_bind is None else name_to_bind, constructor) |
|
296
|
|
|
|
|
297
|
|
|
def bind_serializer(serializer_name, name_to_bind=None): |
|
298
|
|
|
|
|
299
|
|
|
""" Bind an (rdkit) serializer to the class """ |
|
300
|
|
|
|
|
301
|
|
|
def serializer(self, *args, **kwargs): |
|
302
|
|
|
|
|
303
|
|
|
""" The serializer to be bound. """ |
|
304
|
|
|
return getattr(rdkit.Chem, 'MolTo' + serializer_name)(self, *args, **kwargs) |
|
305
|
|
|
|
|
306
|
|
|
setattr(Mol, 'to_{}'.format(serializer_name).lower() \ |
|
307
|
|
|
if name_to_bind is None else name_to_bind, serializer) |
|
308
|
|
|
|
|
309
|
|
|
CONSTRUCTORS = ['Inchi', 'Smiles', 'Mol2Block', 'Mol2File', 'MolBlock', \ |
|
310
|
|
|
'MolFile', 'PDBBlock', 'PDBFile', 'Smarts', 'TPLBlock', 'TPLFile'] |
|
311
|
|
|
SERIALIZERS = ['Inchi', 'Smiles', 'MolBlock', 'MolFile', 'PDBBlock', 'Smarts', 'TPLBlock', 'TPLFile'] |
|
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
list(map(bind_constructor, CONSTRUCTORS)) |
|
|
|
|
|
|
314
|
|
|
list(map(bind_serializer, SERIALIZERS)) |
|
|
|
|
|
|
315
|
|
|
|
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.