|
1
|
|
|
#! /usr/bin/env python |
|
|
|
|
|
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2015 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
|
|
import pandas as pd |
|
|
|
|
|
|
7
|
|
|
from rdkit import Chem |
|
|
|
|
|
|
8
|
|
|
from rdkit.Chem import Crippen |
|
|
|
|
|
|
9
|
|
|
from rdkit.Chem import Lipinski |
|
|
|
|
|
|
10
|
|
|
from rdkit.Chem import rdMolDescriptors |
|
|
|
|
|
|
11
|
|
|
from rdkit.Chem.rdchem import HybridizationType |
|
|
|
|
|
|
12
|
|
|
import skchem |
|
13
|
|
|
|
|
14
|
|
|
import functools |
|
15
|
|
|
from skchem.data import periodic_table |
|
16
|
|
|
|
|
17
|
|
|
from .filters import organic |
|
18
|
|
|
|
|
19
|
|
|
def is_element(a, element=0): |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
""" Is the atom of a given element """ |
|
22
|
|
|
return a.GetSymbol() == element |
|
23
|
|
|
|
|
24
|
|
|
element_features = {'is_{}'.format(e): functools.partial(is_element, element=e) for e in organic} |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def is_h_acceptor(a): |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
""" Is an H acceptor? """ |
|
29
|
|
|
|
|
30
|
|
|
m = a.GetOwningMol() |
|
31
|
|
|
idx = a.GetIdx() |
|
32
|
|
|
return idx in [i[0] for i in Lipinski._HAcceptors(m)] |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def is_h_donor(a): |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
""" Is an H donor? """ |
|
37
|
|
|
|
|
38
|
|
|
m = a.GetOwningMol() |
|
39
|
|
|
idx = a.GetIdx() |
|
40
|
|
|
return idx in [i[0] for i in Lipinski._HDonors(m)] |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def is_hetero(a): |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
""" Is a heteroatom? """ |
|
45
|
|
|
|
|
46
|
|
|
m = a.GetOwningMol() |
|
47
|
|
|
idx = a.GetIdx() |
|
48
|
|
|
return idx in [i[0] for i in Lipinski._Heteroatoms(m)] |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def atomic_number(a): |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
""" Atomic number of atom """ |
|
53
|
|
|
|
|
54
|
|
|
return a.GetAtomicNum() |
|
55
|
|
|
|
|
56
|
|
|
def atomic_mass(a): |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
""" Atomic mass of atom """ |
|
59
|
|
|
|
|
60
|
|
|
return a.GetMass() |
|
61
|
|
|
|
|
62
|
|
|
def explicit_valence(a): |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
""" Explicit valence of atom """ |
|
65
|
|
|
return a.GetExplicitValence() |
|
66
|
|
|
|
|
67
|
|
|
def implicit_valence(a): |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
""" Implicit valence of atom """ |
|
70
|
|
|
|
|
71
|
|
|
return a.GetImplicitValence() |
|
72
|
|
|
|
|
73
|
|
|
def valence(a): |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
""" returns the valence of the atom """ |
|
76
|
|
|
|
|
77
|
|
|
return explicit_valence(a) + implicit_valence(a) |
|
78
|
|
|
|
|
79
|
|
|
def formal_charge(a): |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
""" Formal charge of atom """ |
|
82
|
|
|
|
|
83
|
|
|
return a.GetFormalCharge() |
|
84
|
|
|
|
|
85
|
|
|
def is_aromatic(a): |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
""" Boolean if atom is aromatic""" |
|
88
|
|
|
|
|
89
|
|
|
return a.GetIsAromatic() |
|
90
|
|
|
|
|
91
|
|
|
def num_implicit_hydrogens(a): |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
""" Number of implicit hydrogens """ |
|
94
|
|
|
|
|
95
|
|
|
return a.GetNumImplicitHs() |
|
96
|
|
|
|
|
97
|
|
|
def num_explicit_hydrogens(a): |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
""" Number of explicit hydrodgens """ |
|
100
|
|
|
|
|
101
|
|
|
return a.GetNumExplicitHs() |
|
102
|
|
|
|
|
103
|
|
|
def num_hydrogens(a): |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
""" Number of hydrogens """ |
|
106
|
|
|
|
|
107
|
|
|
return num_implicit_hydrogens(a) + num_explicit_hydrogens(a) |
|
108
|
|
|
|
|
109
|
|
|
def is_in_ring(a): |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
""" Whether the atom is in a ring """ |
|
112
|
|
|
|
|
113
|
|
|
return a.IsInRing() |
|
114
|
|
|
|
|
115
|
|
|
def crippen_log_p_contrib(a): |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
""" Hacky way of getting logP contribution. """ |
|
118
|
|
|
|
|
119
|
|
|
idx = a.GetIdx() |
|
120
|
|
|
m = a.GetOwningMol() |
|
121
|
|
|
return Crippen._GetAtomContribs(m)[idx][0] |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
def crippen_molar_refractivity_contrib(a): |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
""" Hacky way of getting molar refractivity contribution. """ |
|
126
|
|
|
|
|
127
|
|
|
idx = a.GetIdx() |
|
128
|
|
|
m = a.GetOwningMol() |
|
129
|
|
|
return Crippen._GetAtomContribs(m)[idx][1] |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
def tpsa_contrib(a): |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
""" Hacky way of getting total polar surface area contribution. """ |
|
134
|
|
|
|
|
135
|
|
|
idx = a.GetIdx() |
|
136
|
|
|
m = a.GetOwningMol() |
|
137
|
|
|
return rdMolDescriptors._CalcTPSAContribs(m)[idx] |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
def labute_asa_contrib(a): |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
""" Hacky way of getting accessible surface area contribution. """ |
|
142
|
|
|
|
|
143
|
|
|
idx = a.GetIdx() |
|
144
|
|
|
m = a.GetOwningMol() |
|
145
|
|
|
return rdMolDescriptors._CalcLabuteASAContribs(m)[0][idx] |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
def is_hybridized(a, hybrid_type=HybridizationType.SP3): |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
""" Hybridized as type hybrid_type, default SP3 """ |
|
150
|
|
|
|
|
151
|
|
|
return a.GetHybridization() is hybrid_type |
|
152
|
|
|
|
|
153
|
|
|
pt = periodic_table() |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
def electronegativity(a): |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
return a.GetSymbol() |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
hybridization_features = {'is_' + n + '_hybridized': functools.partial(is_hybridized, hybrid_type=n) for n in HybridizationType.names} |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
atom_features = { |
|
|
|
|
|
|
163
|
|
|
'atomic_number': atomic_number, |
|
164
|
|
|
'atomic_mass': atomic_mass, |
|
165
|
|
|
'formal_charge': formal_charge, |
|
166
|
|
|
'valence': valence, |
|
167
|
|
|
'is_aromatic': is_aromatic, |
|
168
|
|
|
'num_hydrogens': num_hydrogens, |
|
169
|
|
|
'is_in_ring': is_in_ring, |
|
170
|
|
|
'log_p_contrib': crippen_log_p_contrib, |
|
171
|
|
|
'molar_refractivity_contrib': crippen_molar_refractivity_contrib, |
|
172
|
|
|
'is_h_acceptor': is_h_acceptor, |
|
173
|
|
|
'is_h_donor': is_h_donor, |
|
174
|
|
|
'is_heteroatom': is_hetero, |
|
175
|
|
|
'total_polar_surface_area_contrib': tpsa_contrib, |
|
176
|
|
|
'total_labute_accessible_surface_area': labute_asa_contrib, |
|
177
|
|
|
} |
|
178
|
|
|
atom_features.update(element_features) |
|
179
|
|
|
atom_features.update(hybridization_features) |
|
180
|
|
|
|
|
181
|
|
|
class AtomFeatureCalculator(object): |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
def __init__(self, features=atom_features): |
|
|
|
|
|
|
184
|
|
|
self.features = pd.Series(features) |
|
185
|
|
|
|
|
186
|
|
|
@property |
|
187
|
|
|
def feature_names(self): |
|
|
|
|
|
|
188
|
|
|
return self.features.index |
|
189
|
|
|
|
|
190
|
|
|
def calculate(self, obj): |
|
|
|
|
|
|
191
|
|
|
if isinstance(obj, skchem.core.Atom): |
|
192
|
|
|
return self._calculate_atom(obj) |
|
193
|
|
|
elif isinstance(obj, skchem.core.Mol): |
|
194
|
|
|
return self._calculate_mol(obj) |
|
195
|
|
|
|
|
196
|
|
|
def _calculate_atom(self, atom): |
|
|
|
|
|
|
197
|
|
|
return self.features.apply(lambda f: f(atom)) |
|
198
|
|
|
|
|
199
|
|
|
def _calculate_mol(self, mol): |
|
|
|
|
|
|
200
|
|
|
return pd.DataFrame(self(a) for a in mol.atoms) |
|
201
|
|
|
|
|
202
|
|
|
def __call__(self, *args, **kwargs): |
|
203
|
|
|
return self.calculate(*args, **kwargs) |
|
204
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.