bindview 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bindview/__init__.py +88 -0
- bindview/__main__.py +6 -0
- bindview/align.py +493 -0
- bindview/arrange.js +347 -0
- bindview/arrange.py +248 -0
- bindview/backends.py +290 -0
- bindview/basepairs.py +1711 -0
- bindview/chains.py +264 -0
- bindview/chem.py +57 -0
- bindview/clearance.py +123 -0
- bindview/cli.py +990 -0
- bindview/compare.py +1854 -0
- bindview/confidence.py +531 -0
- bindview/consensus.py +303 -0
- bindview/covalent.py +220 -0
- bindview/depict.py +790 -0
- bindview/detect.py +378 -0
- bindview/diagram.py +1891 -0
- bindview/distance_table.py +589 -0
- bindview/ensemble.py +787 -0
- bindview/errors.py +47 -0
- bindview/exposure.py +207 -0
- bindview/features.py +179 -0
- bindview/fingerprint.py +469 -0
- bindview/fragments.py +304 -0
- bindview/geometry.py +482 -0
- bindview/graph.py +611 -0
- bindview/graphbuild.py +431 -0
- bindview/interactions.py +418 -0
- bindview/interactive.py +334 -0
- bindview/interface.py +1329 -0
- bindview/ions.py +155 -0
- bindview/layout/__init__.py +15 -0
- bindview/layout/body.py +421 -0
- bindview/layout/engine.py +1084 -0
- bindview/layout/field.py +261 -0
- bindview/ligand.py +583 -0
- bindview/near.py +67 -0
- bindview/options.py +92 -0
- bindview/pae.py +991 -0
- bindview/peptide.py +610 -0
- bindview/plane.py +320 -0
- bindview/polymer.py +678 -0
- bindview/py.typed +0 -0
- bindview/report.py +1407 -0
- bindview/residues.py +296 -0
- bindview/resolve.py +493 -0
- bindview/scene.py +239 -0
- bindview/solvent.py +254 -0
- bindview/structure.py +1145 -0
- bindview/styles/__init__.py +2 -0
- bindview/styles/absence.py +180 -0
- bindview/styles/base.py +2128 -0
- bindview/styles/closeup.py +146 -0
- bindview/styles/detailed.py +690 -0
- bindview/styles/ligand_draw.py +289 -0
- bindview/styles/mixed.py +70 -0
- bindview/styles/polymer_draw.py +354 -0
- bindview/styles/rings.py +75 -0
- bindview/styles/schematic.py +311 -0
- bindview/styles/summary.py +147 -0
- bindview/svg.py +310 -0
- bindview/textmetrics.py +64 -0
- bindview/theme.py +317 -0
- bindview-0.1.0.dist-info/METADATA +270 -0
- bindview-0.1.0.dist-info/RECORD +70 -0
- bindview-0.1.0.dist-info/WHEEL +4 -0
- bindview-0.1.0.dist-info/entry_points.txt +2 -0
- bindview-0.1.0.dist-info/licenses/LICENSE +21 -0
- bindview-0.1.0.dist-info/licenses/NOTICE +48 -0
bindview/layout/field.py
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"""The ligand as an obstacle: a sampled distance field plus bond segments.
|
|
2
|
+
|
|
3
|
+
Every placement question about the ligand -- is this spot free, which way is
|
|
4
|
+
outward, does this line cross a bond -- is answered from here. The distance
|
|
5
|
+
field is sampled once on a regular grid (a few hundred thousand cheap NumPy
|
|
6
|
+
operations) so that the search over candidate poses can test thousands of
|
|
7
|
+
positions with array lookups.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import hashlib
|
|
13
|
+
from functools import cached_property
|
|
14
|
+
from typing import Sequence
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
|
|
18
|
+
from ..depict import Depiction
|
|
19
|
+
from ..geometry import marching_squares, points_segments_distance
|
|
20
|
+
|
|
21
|
+
__all__ = ["LigandField"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class LigandField:
|
|
25
|
+
"""Distance to the nearest ligand ink, sampled on a grid.
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
dep:
|
|
30
|
+
The ligand depiction.
|
|
31
|
+
reach:
|
|
32
|
+
How far beyond the ligand's bounding box the grid extends.
|
|
33
|
+
step:
|
|
34
|
+
Grid spacing in user units.
|
|
35
|
+
extra:
|
|
36
|
+
Additional obstacle discs ``(x, y, radius)``.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(self, dep: Depiction, reach: float, step: float = 2.0,
|
|
40
|
+
extra: "Sequence[tuple[float, float, float]]" = ()) -> None:
|
|
41
|
+
self.dep = dep
|
|
42
|
+
self.step = float(step)
|
|
43
|
+
m = dep.metrics
|
|
44
|
+
|
|
45
|
+
seg_a: list[tuple[float, float]] = []
|
|
46
|
+
seg_b: list[tuple[float, float]] = []
|
|
47
|
+
rad: list[float] = []
|
|
48
|
+
bond_a: list[tuple[float, float]] = []
|
|
49
|
+
bond_b: list[tuple[float, float]] = []
|
|
50
|
+
straight = dep.arranged == "plane" # ... where a link between two boxes is that straight line
|
|
51
|
+
for b in dep.bonds:
|
|
52
|
+
if b.kind in ("closure", "crosslink") and not straight:
|
|
53
|
+
continue # drawn as a bracket beside the chain, not as this straight line
|
|
54
|
+
pa, pb = dep.pos(b.a), dep.pos(b.b)
|
|
55
|
+
if b.kind in ("covalent", "chain", "gap", "closure", "crosslink"):
|
|
56
|
+
bond_a.append(pa)
|
|
57
|
+
bond_b.append(pb)
|
|
58
|
+
wide = m.double_offset if (b.order > 1 or b.aromatic) else (m.wedge_width / 2.0 if b.stereo != "none" else 0.0)
|
|
59
|
+
seg_a.append(pa)
|
|
60
|
+
seg_b.append(pb)
|
|
61
|
+
rad.append(m.bond_width / 2.0 + wide)
|
|
62
|
+
for a in dep.atoms:
|
|
63
|
+
if a.label is None:
|
|
64
|
+
# A bare vertex still occupies its point (single-atom fragments, chain ends).
|
|
65
|
+
seg_a.append(a.pos)
|
|
66
|
+
seg_b.append(a.pos)
|
|
67
|
+
rad.append(m.bond_width / 2.0)
|
|
68
|
+
continue
|
|
69
|
+
for box in a.label.boxes:
|
|
70
|
+
h = box[3] - box[1]
|
|
71
|
+
w = box[2] - box[0]
|
|
72
|
+
r = min(w, h) / 2.0
|
|
73
|
+
cy = (box[1] + box[3]) / 2.0
|
|
74
|
+
cx = (box[0] + box[2]) / 2.0
|
|
75
|
+
if w >= h:
|
|
76
|
+
seg_a.append((box[0] + r, cy))
|
|
77
|
+
seg_b.append((box[2] - r, cy))
|
|
78
|
+
else:
|
|
79
|
+
seg_a.append((cx, box[1] + r))
|
|
80
|
+
seg_b.append((cx, box[3] - r))
|
|
81
|
+
rad.append(r)
|
|
82
|
+
for x, y, r in extra:
|
|
83
|
+
# Ink the style will add on the ligand (contact spokes): keep bodies off it.
|
|
84
|
+
seg_a.append((x, y))
|
|
85
|
+
seg_b.append((x, y))
|
|
86
|
+
rad.append(r)
|
|
87
|
+
self.seg_a = np.array(seg_a, dtype=float).reshape(-1, 2)
|
|
88
|
+
self.seg_b = np.array(seg_b, dtype=float).reshape(-1, 2)
|
|
89
|
+
self.seg_r = np.array(rad, dtype=float)
|
|
90
|
+
self.bond_a = np.array(bond_a, dtype=float).reshape(-1, 2)
|
|
91
|
+
self.bond_b = np.array(bond_b, dtype=float).reshape(-1, 2)
|
|
92
|
+
self.atom_xy = np.array([a.pos for a in dep.atoms], dtype=float).reshape(-1, 2)
|
|
93
|
+
#: The residue boxes of a chain ligand, ``(U, 4)`` as x0, y0, x1, y1; empty for a ligand drawn as atoms.
|
|
94
|
+
self.unit_boxes = np.array([u.box for u in dep.units], dtype=float).reshape(-1, 4)
|
|
95
|
+
#: The ink of the atoms that are drawn beside such a box, as small rectangles (every atom, every
|
|
96
|
+
#: bond in two halves): a line that is another atom's does not run across them either. Residue by
|
|
97
|
+
#: residue, with one rectangle round all of a residue's: most lines come near none of them, and are
|
|
98
|
+
#: looked at against the small ones only where they come near.
|
|
99
|
+
self._pieces = [np.array(u.rects, dtype=float).reshape(-1, 4) for u in dep.units if u.rects]
|
|
100
|
+
self._piece_bounds = np.array([[g[:, 0].min(), g[:, 1].min(), g[:, 2].max(), g[:, 3].max()]
|
|
101
|
+
for g in self._pieces], dtype=float).reshape(-1, 4)
|
|
102
|
+
|
|
103
|
+
pts = np.vstack([self.seg_a, self.seg_b])
|
|
104
|
+
# (Rounded before it is cut to a whole number: a bound that lies on one but for a last bit lies on it.)
|
|
105
|
+
self.x0 = float(np.floor(np.round(pts[:, 0].min() - reach, 6)))
|
|
106
|
+
self.y0 = float(np.floor(np.round(pts[:, 1].min() - reach, 6)))
|
|
107
|
+
nx = int(np.ceil(np.round((pts[:, 0].max() + reach - self.x0) / self.step, 6))) + 1
|
|
108
|
+
ny = int(np.ceil(np.round((pts[:, 1].max() + reach - self.y0) / self.step, 6))) + 1
|
|
109
|
+
self.nx, self.ny = nx, ny
|
|
110
|
+
gx = self.x0 + np.arange(nx) * self.step
|
|
111
|
+
gy = self.y0 + np.arange(ny) * self.step
|
|
112
|
+
self.gx, self.gy = gx, gy
|
|
113
|
+
|
|
114
|
+
@cached_property
|
|
115
|
+
def values(self) -> np.ndarray:
|
|
116
|
+
"""The distance at the nodes of the grid, ``(ny, nx)``. Worked out when it is first asked for."""
|
|
117
|
+
grid = np.column_stack([np.tile(self.gx, self.ny), np.repeat(self.gy, self.nx)])
|
|
118
|
+
best = np.full(len(grid), np.inf)
|
|
119
|
+
# Chunk over segments to keep temporaries small.
|
|
120
|
+
for s in range(0, len(self.seg_a), 24):
|
|
121
|
+
d = points_segments_distance(grid, self.seg_a[s:s + 24], self.seg_b[s:s + 24]) - self.seg_r[None, s:s + 24]
|
|
122
|
+
best = np.minimum(best, d.min(axis=1))
|
|
123
|
+
return best.reshape(self.ny, self.nx)
|
|
124
|
+
|
|
125
|
+
@cached_property
|
|
126
|
+
def key(self) -> str:
|
|
127
|
+
"""A digest of all that is in the field: two with the same one are the same."""
|
|
128
|
+
h = hashlib.blake2b(digest_size=20)
|
|
129
|
+
for a in (self.seg_a, self.seg_b, self.seg_r, self.bond_a, self.bond_b, self.atom_xy, self.unit_boxes,
|
|
130
|
+
*self._pieces):
|
|
131
|
+
h.update(f"{a.dtype.str}{a.shape}".encode())
|
|
132
|
+
h.update(np.ascontiguousarray(a).tobytes())
|
|
133
|
+
h.update(repr((self.step, self.x0, self.y0, self.nx, self.ny)).encode())
|
|
134
|
+
return h.hexdigest()
|
|
135
|
+
|
|
136
|
+
# -- queries -----------------------------------------------------------
|
|
137
|
+
def sdf(self, pts: np.ndarray) -> np.ndarray:
|
|
138
|
+
"""Clearance to the ligand at ``pts`` (``(..., 2)``), bilinearly interpolated.
|
|
139
|
+
|
|
140
|
+
Points outside the grid are far from the ligand by construction; they
|
|
141
|
+
get the edge value plus their distance to the grid, which keeps the
|
|
142
|
+
field monotone.
|
|
143
|
+
"""
|
|
144
|
+
p = np.asarray(pts, dtype=float)
|
|
145
|
+
fx = (p[..., 0] - self.x0) / self.step
|
|
146
|
+
fy = (p[..., 1] - self.y0) / self.step
|
|
147
|
+
cx = np.clip(fx, 0.0, self.nx - 1.000001)
|
|
148
|
+
cy = np.clip(fy, 0.0, self.ny - 1.000001)
|
|
149
|
+
ix = cx.astype(int)
|
|
150
|
+
iy = cy.astype(int)
|
|
151
|
+
tx = cx - ix
|
|
152
|
+
ty = cy - iy
|
|
153
|
+
v = self.values
|
|
154
|
+
out = (v[iy, ix] * (1 - tx) * (1 - ty) + v[iy, ix + 1] * tx * (1 - ty)
|
|
155
|
+
+ v[iy + 1, ix] * (1 - tx) * ty + v[iy + 1, ix + 1] * tx * ty)
|
|
156
|
+
outside = np.hypot(fx - cx, fy - cy) * self.step
|
|
157
|
+
return out + outside
|
|
158
|
+
|
|
159
|
+
def outward(self, pts: np.ndarray) -> np.ndarray:
|
|
160
|
+
"""Unit vectors pointing away from the ligand at ``pts`` (the field gradient)."""
|
|
161
|
+
p = np.asarray(pts, dtype=float)
|
|
162
|
+
h = self.step
|
|
163
|
+
ex = np.array([h, 0.0])
|
|
164
|
+
ey = np.array([0.0, h])
|
|
165
|
+
gx = self.sdf(p + ex) - self.sdf(p - ex)
|
|
166
|
+
gy = self.sdf(p + ey) - self.sdf(p - ey)
|
|
167
|
+
n = np.hypot(gx, gy)
|
|
168
|
+
n = np.where(n < 1e-9, 1.0, n)
|
|
169
|
+
return np.stack([gx / n, gy / n], axis=-1)
|
|
170
|
+
|
|
171
|
+
def boxes_crossed(self, p: np.ndarray, q: np.ndarray, own: np.ndarray, margin: float = 1.0,
|
|
172
|
+
reach: float = 0.0) -> np.ndarray:
|
|
173
|
+
"""Does the segment ``p[i] -> q[i]`` pass through a residue box of a chain ligand?
|
|
174
|
+
|
|
175
|
+
The box that holds ``own`` (the point the link aims at) does not count:
|
|
176
|
+
the link ends on it. A contact is read off by the box its line reaches,
|
|
177
|
+
so a line that runs across another box on its way says the wrong thing.
|
|
178
|
+
Returns one boolean per segment; all ``False`` for a ligand drawn as atoms.
|
|
179
|
+
|
|
180
|
+
Where atoms are drawn beside a box, their ink counts as well: a line
|
|
181
|
+
does not run across an atom or a bond it does not end on. What lies
|
|
182
|
+
within ``reach`` of ``own`` is its own target's ink (the atoms of a
|
|
183
|
+
ring round its centre) and does not count.
|
|
184
|
+
"""
|
|
185
|
+
n = len(p)
|
|
186
|
+
if not len(self.unit_boxes) or not n:
|
|
187
|
+
return np.zeros(n, dtype=bool)
|
|
188
|
+
hit = self._crossed(p, q, own, self.unit_boxes, margin, 0.0)
|
|
189
|
+
if len(self._pieces):
|
|
190
|
+
near = self._through(p, q, self._piece_bounds, 0.0)
|
|
191
|
+
for g in np.nonzero(near.any(axis=0))[0]:
|
|
192
|
+
rows = np.nonzero(near[:, g] & ~hit)[0]
|
|
193
|
+
if len(rows):
|
|
194
|
+
hit[rows] = self._crossed(p[rows], q[rows], own, self._pieces[g], 0.0, reach)
|
|
195
|
+
return hit
|
|
196
|
+
|
|
197
|
+
@classmethod
|
|
198
|
+
def _crossed(cls, p: np.ndarray, q: np.ndarray, own: np.ndarray, boxes: np.ndarray, margin: float,
|
|
199
|
+
reach: float) -> np.ndarray:
|
|
200
|
+
"""One boolean per segment ``p[i] -> q[i]``: does it pass through one of ``boxes`` that is not
|
|
201
|
+
within ``reach`` of ``own``?"""
|
|
202
|
+
x0, y0 = boxes[:, 0] - margin, boxes[:, 1] - margin
|
|
203
|
+
x1, y1 = boxes[:, 2] + margin, boxes[:, 3] + margin
|
|
204
|
+
away = np.hypot(np.maximum(np.maximum(x0 - own[0], own[0] - x1), 0.0),
|
|
205
|
+
np.maximum(np.maximum(y0 - own[1], own[1] - y1), 0.0))
|
|
206
|
+
foreign = away > reach
|
|
207
|
+
return (cls._through(p, q, boxes, margin) & foreign[None, :]).any(axis=1)
|
|
208
|
+
|
|
209
|
+
@staticmethod
|
|
210
|
+
def _through(p: np.ndarray, q: np.ndarray, boxes: np.ndarray, margin: float) -> np.ndarray:
|
|
211
|
+
"""``(len(p), len(boxes))`` booleans: does the segment ``p[i] -> q[i]`` pass through ``boxes[j]``?"""
|
|
212
|
+
n = len(p)
|
|
213
|
+
x0, y0 = boxes[:, 0] - margin, boxes[:, 1] - margin
|
|
214
|
+
x1, y1 = boxes[:, 2] + margin, boxes[:, 3] + margin
|
|
215
|
+
lo = np.zeros((n, len(x0)))
|
|
216
|
+
hi = np.ones((n, len(x0)))
|
|
217
|
+
for axis, (b0, b1) in enumerate(((x0, x1), (y0, y1))):
|
|
218
|
+
start = p[:, axis][:, None]
|
|
219
|
+
d = (q[:, axis] - p[:, axis])[:, None]
|
|
220
|
+
still = np.abs(d) < 1e-12
|
|
221
|
+
safe = np.where(still, 1.0, d)
|
|
222
|
+
t0, t1 = (b0[None, :] - start) / safe, (b1[None, :] - start) / safe
|
|
223
|
+
inside = (start >= b0[None, :]) & (start <= b1[None, :])
|
|
224
|
+
lo = np.maximum(lo, np.where(still, np.where(inside, -np.inf, np.inf), np.minimum(t0, t1)))
|
|
225
|
+
hi = np.minimum(hi, np.where(still, np.where(inside, np.inf, -np.inf), np.maximum(t0, t1)))
|
|
226
|
+
return hi > lo
|
|
227
|
+
|
|
228
|
+
def bond_crossings(self, p: np.ndarray, q: np.ndarray) -> np.ndarray:
|
|
229
|
+
"""Number of covalent bonds properly crossed by each segment ``p[i]->q[i]``."""
|
|
230
|
+
p = np.asarray(p, dtype=float).reshape(-1, 2)
|
|
231
|
+
q = np.asarray(q, dtype=float).reshape(-1, 2)
|
|
232
|
+
if len(self.bond_a) == 0 or len(p) == 0:
|
|
233
|
+
return np.zeros(len(p), dtype=int)
|
|
234
|
+
return segment_cross_matrix(p, q, self.bond_a, self.bond_b).sum(axis=1)
|
|
235
|
+
|
|
236
|
+
def contours(self, level: float) -> list[np.ndarray]:
|
|
237
|
+
"""Closed curves at constant clearance ``level``, longest first."""
|
|
238
|
+
return marching_squares(self.values, level, self.x0, self.y0, self.step)
|
|
239
|
+
|
|
240
|
+
def cells(self, lo: float, hi: float, stride: int = 2) -> np.ndarray:
|
|
241
|
+
"""Grid nodes whose clearance lies in ``[lo, hi]`` (every ``stride``-th node)."""
|
|
242
|
+
v = self.values[::stride, ::stride]
|
|
243
|
+
iy, ix = np.nonzero((v >= lo) & (v <= hi))
|
|
244
|
+
return np.column_stack([self.gx[::stride][ix], self.gy[::stride][iy]])
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def segment_cross_matrix(p: np.ndarray, q: np.ndarray, a: np.ndarray, b: np.ndarray) -> np.ndarray:
|
|
248
|
+
"""``(len(p), len(a))`` booleans: does segment ``p[i]->q[i]`` properly cross ``a[j]->b[j]``."""
|
|
249
|
+
d1 = (q - p)[:, None, :] # (P, 1, 2)
|
|
250
|
+
pa = a[None, :, :] - p[:, None, :] # (P, A, 2)
|
|
251
|
+
pb = b[None, :, :] - p[:, None, :]
|
|
252
|
+
c1 = d1[..., 0] * pa[..., 1] - d1[..., 1] * pa[..., 0]
|
|
253
|
+
c2 = d1[..., 0] * pb[..., 1] - d1[..., 1] * pb[..., 0]
|
|
254
|
+
d2 = (b - a)[None, :, :] # (1, A, 2)
|
|
255
|
+
ap = -pa
|
|
256
|
+
aq = q[:, None, :] - a[None, :, :]
|
|
257
|
+
c3 = d2[..., 0] * ap[..., 1] - d2[..., 1] * ap[..., 0]
|
|
258
|
+
c4 = d2[..., 0] * aq[..., 1] - d2[..., 1] * aq[..., 0]
|
|
259
|
+
eps = 1e-9
|
|
260
|
+
return ((c1 > eps) & (c2 < -eps) | (c1 < -eps) & (c2 > eps)) & \
|
|
261
|
+
((c3 > eps) & (c4 < -eps) | (c3 < -eps) & (c4 > eps))
|