-
Notifications
You must be signed in to change notification settings - Fork 269
/
molecule.py
266 lines (208 loc) · 6.75 KB
/
molecule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import warnings
from typing import List, Optional, Union
import numpy as np
from spyrmsd import constants, graph, utils
class Molecule:
def __init__(
self,
atomicnums: Union[np.ndarray, List[int]],
coordinates: Union[np.ndarray, List[List[float]]],
adjacency_matrix: Optional[Union[np.ndarray, List[List[int]]]] = None,
) -> None:
"""
Molecule initialisation.
Parameters
----------
atomicnums: Union[np.ndarray, List[int]]
Atomic numbers
coordinates: Union[np.ndarray, List[List[float]]]
Atomic coordinates
adjacency_matrix: Union[np.ndarray, List[List[int]]], optional
Molecular graph adjacency matrix
Notes
-----
A molecule is built from atomic numbers and atomic coordinates only.
Optionally, a good representation of the molecular graph (obtained with
OpenBabel or RDKit) can be stored as adjacency matrix.
"""
atomicnums = np.asarray(atomicnums, dtype=int)
coordinates = np.asarray(coordinates, dtype=float)
self.natoms: int = len(atomicnums)
assert atomicnums.shape == (self.natoms,)
assert coordinates.shape == (self.natoms, 3)
self.atomicnums = atomicnums
self.coordinates = coordinates
self.stripped: bool = bool(np.all(atomicnums != 1))
if adjacency_matrix is not None:
self.adjacency_matrix: np.ndarray = np.asarray(adjacency_matrix, dtype=int)
# Molecular graph
self.G = None
self.masses: Optional[List[float]] = None
@classmethod
def from_obabel(cls, obmol, adjacency: bool = True):
"""
Constructor from OpenBabel molecule.
Parameters
----------
obmol:
OpenBabel molecule
adjacency:
Flag to compute the adjacency matrix
Returns
-------
spyrmsd.molecule.Molecule
:code:`spyrmsd` Molecule
"""
# TODO: Check if OpenBabel is available?
from spyrmsd.optional import obabel as ob
return ob.to_molecule(obmol, adjacency=adjacency)
@classmethod
def from_rdkit(cls, rdmol, adjacency: bool = True):
"""
Constructor from RDKit molecule.
Parameters
----------
rdmol:
RDKit molecule
adjacency:
Flag to compute the adjacency matrix
Returns
-------
spyrmsd.molecule.Molecule
:code:`spyrmsd` Molecule
"""
# TODO: Check if RDKit is available?
from spyrmsd.optional import rdkit as rd
return rd.to_molecule(rdmol, adjacency=adjacency)
def translate(self, vector: Union[np.ndarray, List[float]]) -> None:
"""
Translate molecule.
Parameters
----------
vector: np.ndarray
Translation vector (in 3D)
"""
assert len(vector) == 3
vector = np.asarray(vector)
self.coordinates += vector
def rotate(
self, angle: float, axis: Union[np.ndarray, List[float]], units: str = "rad"
) -> None:
"""
Rotate molecule.
Parameters
----------
angle: float
Rotation angle
axis: np.ndarray
Axis of rotation (in 3D)
units: {"rad", "deg"}
Units of the angle (radians `rad` or degrees `deg`)
"""
axis = np.asarray(axis)
self.coordinates = utils.rotate(self.coordinates, angle, axis, units)
def center_of_mass(self) -> np.ndarray:
"""
Center of mass.
Returns
-------
np.ndarray
Center of mass
Notes
-----
Atomic masses are cached.
"""
# Get masses and cache them
if self.masses is None:
self.masses = [constants.anum_to_mass[anum] for anum in self.atomicnums]
return np.average(self.coordinates, axis=0, weights=self.masses)
def center_of_geometry(self) -> np.ndarray:
"""
Center of geometry.
Returns
-------
np.ndarray
Center of geometry
"""
return utils.center_of_geometry(self.coordinates)
# TODO: Change name (to stripH)
def strip(self) -> None:
"""
Strip hydrogen atoms.
"""
if not self.stripped:
idx = self.atomicnums != 1 # Non-hydrogen atoms
# Strip
self.atomicnums = self.atomicnums[idx]
self.coordinates = self.coordinates[idx, :]
# Update number of atoms
self.natoms = len(self.atomicnums)
# Update adjacency matrix
if self.adjacency_matrix is not None:
self.adjacency_matrix = self.adjacency_matrix[np.ix_(idx, idx)]
# Reset molecular graph when stripping
self.G = None
self.stripped = True
def to_graph(self):
"""
Convert molecule to graph.
Returns
-------
Graph
Molecular graph.
Notes
-----
If the molecule does not have an associated adjacency matrix, a simple
bond perception is used.
The molecular graph is cached.
"""
if self.G is None:
try:
self.G = graph.graph_from_adjacency_matrix(
self.adjacency_matrix, self.atomicnums
)
except AttributeError:
warnings.warn(
"Molecule was not initialized with an adjacency matrix. "
+ "Using bond perception..."
)
# Automatic bond perception (with very simple rule)
self.adjacency_matrix = graph.adjacency_matrix_from_atomic_coordinates(
self.atomicnums, self.coordinates
)
self.G = graph.graph_from_adjacency_matrix(
self.adjacency_matrix, self.atomicnums
)
return self.G
def __len__(self) -> int:
"""
Molecule size.
Returns
-------
int
Number of atoms within the molecule
"""
return self.natoms
def coords_from_molecule(mol: Molecule, center: bool = False) -> np.ndarray:
"""
Atomic coordinates from molecule.
Parameters
----------
mol: molecule.Molecule
Molecule
center: bool
Center flag
Returns
-------
np.ndarray
Atomic coordinates (possibly centred)
Notes
-----
Atomic coordinates are centred according to the center of geometry, not the center
of mass.
"""
if center:
coords = mol.coordinates - mol.center_of_geometry()
else:
coords = mol.coordinates
return coords