-
Notifications
You must be signed in to change notification settings - Fork 269
/
rmsd.py
382 lines (321 loc) · 10 KB
/
rmsd.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from typing import Any, List, Optional, Tuple, Union
import numpy as np
from spyrmsd import graph, hungarian, molecule, qcp, utils
def rmsd(
coords1: np.ndarray,
coords2: np.ndarray,
atomicn1: np.ndarray,
atomicn2: np.ndarray,
center: bool = False,
minimize: bool = False,
atol: float = 1e-9,
) -> float:
"""
Compute RMSD
Parameters
----------
coords1: np.ndarray
Coordinate of molecule 1
coords2: np.ndarray
Coordinates of molecule 2
atomicn1: np.ndarray
Atomic numbers for molecule 1
atomicn2: np.ndarray
Atomic numbers for molecule 2
center: bool
Center molecules at origin
minimize: bool
Compute minimum RMSD (with QCP method)
atol: float
Absolute tolerance parameter for QCP method (see :func:`qcp_rmsd`)
Returns
-------
float
RMSD
Notes
-----
When `minimize=True`, the QCP method is used. [1]_ The molecules are
centred at the origin according to the center of geometry and superimposed
in order to minimize the RMSD.
.. [1] D. L. Theobald, *Rapid calculation of RMSDs using a quaternion-based
characteristic polynomial*, Acta Crys. A **61**, 478-480 (2005).
"""
assert np.all(atomicn1 == atomicn2)
assert coords1.shape == coords2.shape
# Center coordinates if required
c1 = utils.center(coords1) if center or minimize else coords1
c2 = utils.center(coords2) if center or minimize else coords2
if minimize:
rmsd = qcp.qcp_rmsd(c1, c2, atol)
else:
n = coords1.shape[0]
rmsd = np.sqrt(np.sum((c1 - c2) ** 2) / n)
return rmsd
def hrmsd(
coords1: np.ndarray,
coords2: np.ndarray,
atomicn1: np.ndarray,
atomicn2: np.ndarray,
center=False,
):
"""
Compute minimum RMSD using the Hungarian method.
Parameters
----------
coords1: np.ndarray
Coordinate of molecule 1
coords2: np.ndarray
Coordinates of molecule 2
atomicn1: np.ndarray
Atomic numbers for molecule 1
atomicn2: np.ndarray
Atomic numbers for molecule 2
Returns
-------
float
Minimum RMSD (after assignment)
Notes
-----
The Hungarian algorithm is used to solve the linear assignment problem, which is
a minimum weight matching of the molecular graphs (bipartite). [2]_
The linear assignment problem is solved for every element separately.
.. [2] W. J. Allen and R. C. Rizzo, *Implementation of the Hungarian Algorithm to
Account for Ligand Symmetry and Similarity in Structure-Based Design*,
J. Chem. Inf. Model. **54**, 518-529 (2014)
"""
assert atomicn1.shape == atomicn2.shape
assert coords1.shape == coords2.shape
# Center coordinates if required
c1 = utils.center(coords1) if center else coords1
c2 = utils.center(coords2) if center else coords2
return hungarian.hungarian_rmsd(c1, c2, atomicn1, atomicn2)
def _rmsd_isomorphic_core(
coords1: np.ndarray,
coords2: np.ndarray,
aprops1: np.ndarray,
aprops2: np.ndarray,
am1: np.ndarray,
am2: np.ndarray,
center: bool = False,
minimize: bool = False,
isomorphisms: Optional[List[Tuple[List[int], List[int]]]] = None,
atol: float = 1e-9,
) -> Tuple[float, List[Tuple[List[int], List[int]]], Tuple[List[int], List[int]]]:
"""
Compute RMSD using graph isomorphism.
Parameters
----------
coords1: np.ndarray
Coordinate of molecule 1
coords2: np.ndarray
Coordinates of molecule 2
aprops1: np.ndarray
Atomic properties for molecule 1
aprops2: np.ndarray
Atomic properties for molecule 2
am1: np.ndarray
Adjacency matrix for molecule 1
am2: np.ndarray
Adjacency matrix for molecule 2
center: bool
Centering flag
minimize: bool
Compute minized RMSD
isomorphisms: Optional[List[Dict[int,int]]]
Previously computed graph isomorphism
atol: float
Absolute tolerance parameter for QCP (see :func:`qcp_rmsd`)
Returns
-------
Tuple[float, List[Dict[int, int]]]
RMSD (after graph matching) and graph isomorphisms
"""
assert coords1.shape == coords2.shape
n = coords1.shape[0]
# Center coordinates if required
c1 = utils.center(coords1) if center or minimize else coords1
c2 = utils.center(coords2) if center or minimize else coords2
# No cached isomorphisms
if isomorphisms is None:
# Convert molecules to graphs
G1 = graph.graph_from_adjacency_matrix(am1, aprops1)
G2 = graph.graph_from_adjacency_matrix(am2, aprops2)
# Get all the possible graph isomorphisms
isomorphisms = graph.match_graphs(G1, G2)
# Minimum result
# Squared displacement (not minimize) or RMSD (minimize)
min_result = np.inf
min_isomorphisms = None
# Loop over all graph isomorphisms to find the lowest RMSD
for idx1, idx2 in isomorphisms:
# Use the isomorphism to shuffle coordinates around (from original order)
c1i = c1[idx1, :]
c2i = c2[idx2, :]
if not minimize:
# Compute square displacement
# Avoid dividing by n and an expensive sqrt() operation
result = np.sum((c1i - c2i) ** 2)
else:
# Compute minimized RMSD using QCP
result = qcp.qcp_rmsd(c1i, c2i, atol)
if result < min_result:
min_result = result
min_isomorphisms = (idx1, idx2)
if not minimize:
# Compute actual RMSD from square displacement
min_result = np.sqrt(min_result / n)
# Return the actual RMSD
return min_result, isomorphisms, min_isomorphisms
def symmrmsd(
coordsref: np.ndarray,
coords: Union[np.ndarray, List[np.ndarray]],
apropsref: np.ndarray,
aprops: np.ndarray,
amref: np.ndarray,
am: np.ndarray,
center: bool = False,
minimize: bool = False,
cache: bool = True,
atol: float = 1e-9,
return_permutation: bool = False,
) -> Any:
"""
Compute RMSD using graph isomorphism for multiple coordinates.
Parameters
----------
coordsref: np.ndarray
Coordinate of reference molecule
coords: List[np.ndarray]
Coordinates of other molecule
apropsref: np.ndarray
Atomic properties for reference
aprops: np.ndarray
Atomic properties for other molecule
amref: np.ndarray
Adjacency matrix for reference molecule
am: np.ndarray
Adjacency matrix for other molecule
center: bool
Centering flag
minimize: bool
Minimum RMSD
cache: bool
Cache graph isomorphisms
atol: float
Absolute tolerance parameter for QCP (see :func:`qcp_rmsd`)
Returns
-------
float: Union[float, List[float]]
Symmetry-corrected RMSD(s) and graph isomorphisms
Notes
-----
Graph isomorphism is introduced for symmetry corrections. However, it is also
useful when two molecules do not have the atoms in the same order since atom
matching according to atomic numbers and the molecular connectivity is
performed. If atoms are in the same order and there is no symmetry, use the
`rmsd` function.
"""
if isinstance(coords, list): # Multiple RMSD calculations
RMSD: Any = []
isomorphism = None
min_iso = []
for c in coords:
if not cache:
# Reset isomorphism
isomorphism = None
srmsd, isomorphism, min_i = _rmsd_isomorphic_core(
coordsref,
c,
apropsref,
aprops,
amref,
am,
center=center,
minimize=minimize,
isomorphisms=isomorphism,
atol=atol,
)
min_iso.append(min_i)
RMSD.append(srmsd)
else: # Single RMSD calculation
RMSD, isomorphism, min_iso = _rmsd_isomorphic_core(
coordsref,
coords,
apropsref,
aprops,
amref,
am,
center=center,
minimize=minimize,
isomorphisms=None,
atol=atol,
)
if return_permutation:
return RMSD, min_iso
return RMSD
def rmsdwrapper(
molref: molecule.Molecule,
mols: Union[molecule.Molecule, List[molecule.Molecule]],
symmetry: bool = True,
center: bool = False,
minimize: bool = False,
strip: bool = True,
cache: bool = True,
) -> Any:
"""
Compute RMSD between two molecule.
Parameters
----------
molref: molecule.Molecule
Reference molecule
mols: Union[molecule.Molecule, List[molecule.Molecule]]
Molecules to compare to reference molecule
symmetry: bool, optional
Symmetry-corrected RMSD (using graph isomorphism)
center: bool, optional
Center molecules at origin
minimize: bool, optional
Minimised RMSD (using the quaternion polynomial method)
strip: bool, optional
Strip hydrogen atoms
Returns
-------
List[float]
RMSDs
"""
if not isinstance(mols, list):
mols = [mols]
if strip:
molref.strip()
for mol in mols:
mol.strip()
if minimize:
center = True
cref = molecule.coords_from_molecule(molref, center)
cmols = [molecule.coords_from_molecule(mol, center) for mol in mols]
RMSDlist = []
if symmetry:
RMSDlist = symmrmsd(
cref,
cmols,
molref.atomicnums,
mols[0].atomicnums,
molref.adjacency_matrix,
mols[0].adjacency_matrix,
center=center,
minimize=minimize,
cache=cache,
)
else: # No symmetry
for c in cmols:
RMSDlist.append(
rmsd(
cref,
c,
molref.atomicnums,
mols[0].atomicnums,
center=center,
minimize=minimize,
)
)
return RMSDlist