|
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
|
|
|
|
|
14
|
|
|
class ChemicalObject(object): |
|
15
|
|
|
|
|
16
|
|
|
""" A mixin for each chemical object in scikit-chem """ |
|
17
|
|
|
|
|
18
|
|
|
@classmethod |
|
19
|
|
|
def from_super(cls, obj): |
|
20
|
|
|
|
|
21
|
|
|
"""A method that converts the class of an object of parent class to that of the child. """ |
|
22
|
|
|
|
|
23
|
|
|
obj.__class__ = cls |
|
24
|
|
|
return obj |
|
25
|
|
|
|
|
26
|
|
|
class AtomView(object): |
|
27
|
|
|
|
|
28
|
|
|
""" Atom interface wrapper """ |
|
29
|
|
|
|
|
30
|
|
|
def __init__(self, owner): |
|
31
|
|
|
self.owner = owner |
|
32
|
|
|
self._current = 0 |
|
33
|
|
|
self._high = self.owner.GetNumAtoms() |
|
34
|
|
|
|
|
35
|
|
|
def __getitem__(self, index): |
|
36
|
|
|
from .atom import Atom |
|
37
|
|
|
return Atom.from_super(self.owner.GetAtomWithIdx(index)) |
|
38
|
|
|
|
|
39
|
|
|
def __iter__(self): |
|
|
|
|
|
|
40
|
|
|
return AtomIterator(self.owner) |
|
41
|
|
|
|
|
42
|
|
|
class AtomIterator(AtomView): |
|
43
|
|
|
|
|
44
|
|
|
""" Atom iterator """ |
|
45
|
|
|
|
|
46
|
|
|
def __init__(self, owner): |
|
47
|
|
|
super(AtomIterator, self).__init__(owner) |
|
48
|
|
|
self._current = 0 |
|
49
|
|
|
self._high = self.owner.GetNumAtoms() |
|
50
|
|
|
|
|
51
|
|
|
def __next__(self): |
|
52
|
|
|
if self._current >= self._high: |
|
53
|
|
|
raise StopIteration |
|
54
|
|
|
else: |
|
55
|
|
|
self._current += 1 |
|
56
|
|
|
return self[self._current - 1] |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
class PropertyView(object): |
|
60
|
|
|
|
|
61
|
|
|
""" Property object wrapper """ |
|
62
|
|
|
|
|
63
|
|
|
def __init__(self, owner): |
|
64
|
|
|
self.owner = owner |
|
65
|
|
|
|
|
66
|
|
|
def __getitem__(self, index): |
|
67
|
|
|
return self.owner.GetProp(str(index)) |
|
68
|
|
|
|
|
69
|
|
|
def __setitem__(self, key, value): |
|
70
|
|
|
if not isinstance(value, str): |
|
71
|
|
|
warnings.warn("""RDKit property keys and values can only be |
|
72
|
|
|
`str`. Using `{value}` as a `str`.""".format(value=value)) |
|
73
|
|
|
self.owner.SetProp(str(key), str(value)) |
|
74
|
|
|
|
|
75
|
|
|
def __iter__(self): |
|
76
|
|
|
return iter(self.keys()) |
|
77
|
|
|
|
|
78
|
|
|
def get(self, index, default=None): |
|
|
|
|
|
|
79
|
|
|
if index in self.keys(): |
|
80
|
|
|
return self[index] |
|
81
|
|
|
else: |
|
82
|
|
|
return default |
|
83
|
|
|
|
|
84
|
|
|
def pop(self, index, default=None): |
|
|
|
|
|
|
85
|
|
|
if default: |
|
86
|
|
|
val = self.get(index, default) |
|
87
|
|
|
else: |
|
88
|
|
|
val = self[index] |
|
89
|
|
|
self.remove(index) |
|
90
|
|
|
return val |
|
91
|
|
|
|
|
92
|
|
|
def remove(self, index): |
|
|
|
|
|
|
93
|
|
|
self.owner.ClearProp(index) |
|
94
|
|
|
|
|
95
|
|
|
def clear(self): |
|
|
|
|
|
|
96
|
|
|
for idx in self.keys(): |
|
97
|
|
|
self.remove(idx) |
|
98
|
|
|
|
|
99
|
|
|
def keys(self): |
|
|
|
|
|
|
100
|
|
|
return list(k for k in self.owner.GetPropNames() if k[:1] != '_') |
|
101
|
|
|
|
|
102
|
|
|
def items(self): |
|
|
|
|
|
|
103
|
|
|
return list((k, self[k]) for k in self.keys()) |
|
104
|
|
|
|
|
105
|
|
|
def __str__(self): |
|
106
|
|
|
return str(dict(self)) |
|
107
|
|
|
|
|
108
|
|
|
def __repr__(self): |
|
109
|
|
|
return '<{klass} values="{values}" at {address}>'.format( |
|
110
|
|
|
klass=self.__class__.__name__, |
|
111
|
|
|
values=str(self), |
|
112
|
|
|
address=hex(id(self))) |
|
113
|
|
|
|