|
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.base |
|
8
|
|
|
|
|
9
|
|
|
Define base classes for scikit chem objects |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
|
|
import warnings |
|
13
|
|
|
import numpy as np |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class ChemicalObject(object): |
|
17
|
|
|
|
|
18
|
|
|
""" A mixin for each chemical object in scikit-chem """ |
|
19
|
|
|
|
|
20
|
|
|
@classmethod |
|
21
|
|
|
def from_super(cls, obj): |
|
22
|
|
|
|
|
23
|
|
|
"""A method that converts the class of an object of parent class to that of the child. """ |
|
24
|
|
|
|
|
25
|
|
|
obj.__class__ = cls |
|
26
|
|
|
return obj |
|
27
|
|
|
|
|
28
|
|
|
class AtomView(object): |
|
29
|
|
|
|
|
30
|
|
|
""" Atom interface wrapper """ |
|
31
|
|
|
|
|
32
|
|
|
def __init__(self, owner): |
|
33
|
|
|
self.owner = owner |
|
34
|
|
|
self.props = AtomPropertyView(self) |
|
35
|
|
|
|
|
36
|
|
|
def __getitem__(self, index): |
|
37
|
|
|
from .atom import Atom |
|
38
|
|
|
return Atom.from_super(self.owner.GetAtomWithIdx(index)) |
|
39
|
|
|
|
|
40
|
|
|
def __len__(self): |
|
41
|
|
|
return self.owner.GetNumAtoms() |
|
42
|
|
|
|
|
43
|
|
|
def __iter__(self): |
|
|
|
|
|
|
44
|
|
|
return AtomIterator(self.owner) |
|
45
|
|
|
|
|
46
|
|
|
def __str__(self): |
|
47
|
|
|
return str(list(str(atom) for atom in self)) |
|
48
|
|
|
|
|
49
|
|
|
def __repr__(self): |
|
50
|
|
|
return '<{klass} values="{values}" at {address}>'.format( |
|
51
|
|
|
klass=self.__class__.__name__, |
|
52
|
|
|
values=str(self), |
|
53
|
|
|
address=hex(id(self))) |
|
54
|
|
|
|
|
55
|
|
|
class AtomIterator(AtomView): |
|
56
|
|
|
|
|
57
|
|
|
""" Atom iterator """ |
|
58
|
|
|
|
|
59
|
|
|
def __init__(self, owner): |
|
60
|
|
|
super(AtomIterator, self).__init__(owner) |
|
61
|
|
|
self._current = 0 |
|
62
|
|
|
self._high = self.owner.GetNumAtoms() |
|
63
|
|
|
|
|
64
|
|
|
def __next__(self): |
|
65
|
|
|
if self._current >= self._high: |
|
66
|
|
|
raise StopIteration |
|
67
|
|
|
else: |
|
68
|
|
|
self._current += 1 |
|
69
|
|
|
return self[self._current - 1] |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
class View(object): |
|
73
|
|
|
|
|
74
|
|
|
""" View wrapper interface """ |
|
75
|
|
|
|
|
76
|
|
|
def keys(self): |
|
|
|
|
|
|
77
|
|
|
raise NotImplemented |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
def remove(self): |
|
|
|
|
|
|
80
|
|
|
raise NotImplemented |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def get(self, index, default=None): |
|
|
|
|
|
|
83
|
|
|
if index in self.keys(): |
|
84
|
|
|
return self[index] |
|
85
|
|
|
else: |
|
86
|
|
|
return default |
|
87
|
|
|
|
|
88
|
|
|
def pop(self, index, default=None): |
|
|
|
|
|
|
89
|
|
|
if default: |
|
90
|
|
|
val = self.get(index, default) |
|
91
|
|
|
else: |
|
92
|
|
|
val = self[index] |
|
93
|
|
|
self.remove(index) |
|
94
|
|
|
return val |
|
95
|
|
|
|
|
96
|
|
|
def clear(self): |
|
|
|
|
|
|
97
|
|
|
for idx in self.keys(): |
|
98
|
|
|
self.remove(idx) |
|
99
|
|
|
|
|
100
|
|
|
def items(self): |
|
|
|
|
|
|
101
|
|
|
return list((k, self[k]) for k in self.keys()) |
|
102
|
|
|
|
|
103
|
|
|
def remove(self, key): |
|
|
|
|
|
|
104
|
|
|
self.__delitem__(key) |
|
105
|
|
|
|
|
106
|
|
|
def __getitem__(self, key): |
|
107
|
|
|
raise NotImplemented |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
def __setitem__(self, key, value): |
|
110
|
|
|
raise NotImplemented |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
def __delitem__(self, key): |
|
113
|
|
|
raise NotImplemented |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
def __iter__(self): |
|
116
|
|
|
return iter(self.keys()) |
|
117
|
|
|
|
|
118
|
|
|
def __str__(self): |
|
119
|
|
|
return str(dict(self)) |
|
120
|
|
|
|
|
121
|
|
|
def __len__(self): |
|
122
|
|
|
return len(self.keys()) |
|
123
|
|
|
|
|
124
|
|
|
def __repr__(self): |
|
125
|
|
|
return '<{klass} values="{values}" at {address}>'.format( |
|
126
|
|
|
klass=self.__class__.__name__, |
|
127
|
|
|
values=str(self), |
|
128
|
|
|
address=hex(id(self))) |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
class PropertyView(View): |
|
132
|
|
|
|
|
133
|
|
|
""" Property object wrapper """ |
|
134
|
|
|
|
|
135
|
|
|
def __init__(self, owner): |
|
136
|
|
|
self._owner = owner |
|
137
|
|
|
|
|
138
|
|
|
def keys(self): |
|
139
|
|
|
return list(k for k in self._owner.GetPropNames() if k[:1] != '_') |
|
140
|
|
|
|
|
141
|
|
|
def __getitem__(self, key): |
|
142
|
|
|
|
|
143
|
|
|
# we manually work out if it was a float that was stored, as GetProp |
|
144
|
|
|
# returns floats and ints set by SetDoubleProp and SetIntProp as strings |
|
145
|
|
|
value = self._owner.GetProp(str(key)) |
|
146
|
|
|
try: |
|
147
|
|
|
return int(value) |
|
148
|
|
|
except ValueError: |
|
149
|
|
|
try: |
|
150
|
|
|
return float(value) |
|
151
|
|
|
except ValueError: |
|
152
|
|
|
return value |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
def __setitem__(self, key, value): |
|
156
|
|
|
|
|
157
|
|
|
if not isinstance(key, str): |
|
158
|
|
|
warnings.warn("""RDKit property keys can only be of type `str`. Using `{key}` as a `str`.""".format(key=key)) |
|
|
|
|
|
|
159
|
|
|
key = str(key) |
|
160
|
|
|
|
|
161
|
|
|
if key[0] == '_': |
|
162
|
|
|
warnings.warn("""`{value}` is a private RDKit property key. Using this may have unintended consequences.""".format(value=value)) |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
if isinstance(value, str): |
|
165
|
|
|
self._owner.SetProp(key, value) |
|
166
|
|
|
elif isinstance(value, (int, np.int64, np.int32)): |
|
167
|
|
|
self._owner.SetIntProp(key, value) |
|
168
|
|
|
elif isinstance(value, (float, np.float64, np.float32)): |
|
169
|
|
|
self._owner.SetDoubleProp(key, value) |
|
170
|
|
|
else: |
|
171
|
|
|
warnings.warn("""RDKit property keys can only be `str`, `int` or `float`. |
|
172
|
|
|
Using `{value}` as a `str`.""".format(value=value)) |
|
173
|
|
|
self._owner.SetProp(key, str(value)) |
|
174
|
|
|
|
|
175
|
|
|
def __delitem__(self, index): |
|
176
|
|
|
self._owner.ClearProp(index) |
|
177
|
|
|
|
|
178
|
|
|
class AtomPropertyView(View): |
|
179
|
|
|
|
|
180
|
|
|
""" Atom property wrapper """ |
|
181
|
|
|
|
|
182
|
|
|
def __init__(self, atom_view): |
|
183
|
|
|
self._atom_view = atom_view |
|
184
|
|
|
|
|
185
|
|
|
def keys(self): |
|
186
|
|
|
res = set() |
|
187
|
|
|
for atom in self._atom_view: |
|
188
|
|
|
res = res.union(set(atom.props.keys())) |
|
189
|
|
|
return list(res) |
|
190
|
|
|
|
|
191
|
|
|
def get(self, key, default=None): |
|
192
|
|
|
return [a.props.get(key, default) for a in self._atom_view] |
|
193
|
|
|
|
|
194
|
|
|
def __getitem__(self, key): |
|
195
|
|
|
if key not in self.keys(): |
|
196
|
|
|
raise KeyError('No atoms have the property set.') |
|
197
|
|
|
return self.get(key, None) |
|
198
|
|
|
|
|
199
|
|
|
def __setitem__(self, key, value): |
|
200
|
|
|
assert len(self._atom_view) == len(value), "Must pass same number of values as atoms." |
|
201
|
|
|
for atom, val in zip(self._atom_view, value): |
|
202
|
|
|
atom.props[key] = val |
|
203
|
|
|
|
|
204
|
|
|
def __delitem__(self, key): |
|
205
|
|
|
for atom in self._atom_view: |
|
206
|
|
|
atom.props.remove(key) |
|
207
|
|
|
|
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.