forked from gcorso/DiffDock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
94 lines (74 loc) · 2.24 KB
/
graph.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
try:
from spyrmsd.graphs.gt import (
cycle,
graph_from_adjacency_matrix,
lattice,
match_graphs,
num_edges,
num_vertices,
vertex_property,
)
except ImportError:
try:
from spyrmsd.graphs.nx import (
cycle,
graph_from_adjacency_matrix,
lattice,
match_graphs,
num_edges,
num_vertices,
vertex_property,
)
except ImportError:
raise ImportError("graph_tool or NetworkX libraries not found.")
__all__ = [
"graph_from_adjacency_matrix",
"match_graphs",
"vertex_property",
"num_vertices",
"num_edges",
"lattice",
"cycle",
"adjacency_matrix_from_atomic_coordinates",
]
import numpy as np
from spyrmsd import constants
def adjacency_matrix_from_atomic_coordinates(
aprops: np.ndarray, coordinates: np.ndarray
) -> np.ndarray:
"""
Compute adjacency matrix from atomic coordinates.
Parameters
----------
aprops: numpy.ndarray
Atomic properties
coordinates: numpy.ndarray
Atomic coordinates
Returns
-------
numpy.ndarray
Adjacency matrix
Notes
-----
This function is based on an automatic bond perception algorithm: two
atoms are considered to be bonded when their distance is smaller than
the sum of their covalent radii plus a tolerance value. [3]_
.. warning::
The automatic bond perceptron rule implemented in this functions
is very simple and only depends on atomic coordinates. Use
with care!
.. [3] E. C. Meng and R. A. Lewis, *Determination of molecular topology and atomic
hybridization states from heavy atom coordinates*, J. Comp. Chem. **12**, 891-898
(1991).
"""
n = len(aprops)
assert coordinates.shape == (n, 3)
A = np.zeros((n, n))
for i in range(n):
r_i = constants.anum_to_covalentradius[aprops[i]]
for j in range(i + 1, n):
r_j = constants.anum_to_covalentradius[aprops[j]]
distance = np.sqrt(np.sum((coordinates[i] - coordinates[j]) ** 2))
if distance < (r_i + r_j + constants.connectivity_tolerance):
A[i, j] = A[j, i] = 1
return A