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/resolve.py
ADDED
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
"""Resolving each interaction to the thing that makes it.
|
|
2
|
+
|
|
3
|
+
Hard requirement of the library: an interaction attaches to a named atom, or to
|
|
4
|
+
a ring centroid / charged group -- never to "somewhere on the ligand".
|
|
5
|
+
|
|
6
|
+
Records come in two flavours:
|
|
7
|
+
|
|
8
|
+
* **named** -- the detector gave atom names (H-bonds, hydrophobic contacts,
|
|
9
|
+
halogen bonds). The ligand atom is looked up by name; nothing is inferred.
|
|
10
|
+
* **unnamed** -- pi-stacking, pi-cation, salt bridges and (in PLIP's tables)
|
|
11
|
+
metal coordination carry only a residue and a distance. The partners are
|
|
12
|
+
then identified from the structure: every chemically possible pair of
|
|
13
|
+
(ligand ring / charged group / metal, protein ring / charged group / atom) is
|
|
14
|
+
enumerated and the one whose centre-to-centre distance reproduces the
|
|
15
|
+
reported distance is taken. Reported distances are rounded to 0.01 A, which
|
|
16
|
+
in practice pins the pair uniquely; where two candidates fall inside the
|
|
17
|
+
tolerance the chemically more canonical one wins, and several records from
|
|
18
|
+
the same residue are assigned jointly so no pair is used twice.
|
|
19
|
+
|
|
20
|
+
Each result says how it was obtained (``method``) and how well the geometry
|
|
21
|
+
agrees (``delta``), so a caller can audit the attachment. A record that cannot
|
|
22
|
+
be resolved is returned as unresolved and is *not drawn*.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import itertools
|
|
28
|
+
from dataclasses import dataclass, field
|
|
29
|
+
from typing import Sequence
|
|
30
|
+
|
|
31
|
+
import numpy as np
|
|
32
|
+
|
|
33
|
+
from .chem import is_metal
|
|
34
|
+
from .features import LigandGroup, perceive_charged_groups, perceive_rings
|
|
35
|
+
from .interactions import NOT_GIVEN_OR_A_NAME, Interaction, InteractionType
|
|
36
|
+
from .ligand import Ligand
|
|
37
|
+
from .residues import aromatic_rings, charged_groups
|
|
38
|
+
from .structure import ResidueKey, Structure
|
|
39
|
+
|
|
40
|
+
__all__ = ["Anchor", "ProteinSite", "ResolvedInteraction", "resolve_interactions", "DISTANCE_TOLERANCE"]
|
|
41
|
+
|
|
42
|
+
#: Agreement (angstrom) required between a computed and a reported distance.
|
|
43
|
+
#: Reported values are rounded to 0.01, so 0.02 leaves room for that and for
|
|
44
|
+
#: coordinate rounding in format conversions.
|
|
45
|
+
DISTANCE_TOLERANCE = 0.02
|
|
46
|
+
|
|
47
|
+
#: Beyond this disagreement a geometric match is not even called approximate.
|
|
48
|
+
_LOOSE_TOLERANCE = 0.6
|
|
49
|
+
|
|
50
|
+
T = InteractionType
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True)
|
|
54
|
+
class Anchor:
|
|
55
|
+
"""Where an interaction attaches on the ligand.
|
|
56
|
+
|
|
57
|
+
``kind`` is ``"atom"`` (one atom), ``"ring"`` (attach to the centroid of
|
|
58
|
+
``atoms``) or ``"group"`` (a charged group; ``atoms`` are its members).
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
kind: str
|
|
62
|
+
atoms: tuple[int, ...]
|
|
63
|
+
group_kind: str = ""
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def atom(self) -> int:
|
|
67
|
+
return self.atoms[0]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True)
|
|
71
|
+
class ProteinSite:
|
|
72
|
+
"""Where an interaction attaches on the protein residue.
|
|
73
|
+
|
|
74
|
+
``kind`` is ``"atom"``, ``"ring"``, ``"group"`` or ``"residue"`` (unknown --
|
|
75
|
+
the residue as a whole). ``atoms`` holds atom names.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
kind: str
|
|
79
|
+
atoms: tuple[str, ...] = ()
|
|
80
|
+
charge: int = 0
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclass
|
|
84
|
+
class ResolvedInteraction:
|
|
85
|
+
record: Interaction
|
|
86
|
+
index: int
|
|
87
|
+
residue: ResidueKey | None
|
|
88
|
+
ligand: Anchor | None
|
|
89
|
+
protein: ProteinSite
|
|
90
|
+
method: str # named | supplied | distance-match | approximate | nearest | unresolved
|
|
91
|
+
delta: float | None = None # |computed - reported| in angstrom, when geometry was used
|
|
92
|
+
computed_distance: float | None = None
|
|
93
|
+
notes: list[str] = field(default_factory=list)
|
|
94
|
+
#: A contact that the structure the figure shows does not make, and other models of its ensemble
|
|
95
|
+
#: do (:meth:`bindview.ensemble.Ensemble.diagram`). It is drawn faint, with its share where a
|
|
96
|
+
#: distance would stand, and is never counted as a contact of the diagram.
|
|
97
|
+
absent: bool = False
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def resolved(self) -> bool:
|
|
101
|
+
return self.ligand is not None
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def type(self) -> InteractionType:
|
|
105
|
+
return self.record.type
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# ---------------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@dataclass(frozen=True)
|
|
112
|
+
class _Candidate:
|
|
113
|
+
ligand: Anchor
|
|
114
|
+
protein: ProteinSite
|
|
115
|
+
distance: float
|
|
116
|
+
rank: int
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _centroid(points: Sequence[np.ndarray]) -> np.ndarray:
|
|
120
|
+
return np.mean(np.asarray(points, dtype=float), axis=0)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _protein_points(structure: Structure, key: ResidueKey) -> dict[str, np.ndarray]:
|
|
124
|
+
return {a.name: np.array(a.xyz) for a in structure.residue_atoms(key) if a.element not in ("H", "D")}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _protein_metals(structure: Structure, key: ResidueKey) -> frozenset[str]:
|
|
128
|
+
"""The names of the atoms of a residue that are metals, by their element: the ion of a residue of one atom,
|
|
129
|
+
and the iron of a haem."""
|
|
130
|
+
return frozenset(a.name for a in structure.residue_atoms(key) if is_metal(a.element.capitalize()))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _protein_ring_sites(resname: str, pts: dict[str, np.ndarray]) -> list[tuple[ProteinSite, np.ndarray]]:
|
|
134
|
+
out = []
|
|
135
|
+
for ring in aromatic_rings(resname):
|
|
136
|
+
if all(n in pts for n in ring):
|
|
137
|
+
out.append((ProteinSite("ring", ring), _centroid([pts[n] for n in ring])))
|
|
138
|
+
return out
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _protein_charge_sites(resname: str, pts: dict[str, np.ndarray], sign: int | None = None
|
|
142
|
+
) -> list[tuple[ProteinSite, np.ndarray]]:
|
|
143
|
+
out = []
|
|
144
|
+
for charge, names in charged_groups(resname):
|
|
145
|
+
if sign is not None and charge != sign:
|
|
146
|
+
continue
|
|
147
|
+
present = tuple(n for n in names if n in pts)
|
|
148
|
+
if present:
|
|
149
|
+
out.append((ProteinSite("group", present, charge), _centroid([pts[n] for n in present])))
|
|
150
|
+
return out
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _anchor_for(group: LigandGroup) -> Anchor:
|
|
154
|
+
if group.kind == "ring":
|
|
155
|
+
return Anchor("ring", group.atoms)
|
|
156
|
+
if len(group.atoms) == 1:
|
|
157
|
+
return Anchor("atom", group.atoms, group.kind)
|
|
158
|
+
return Anchor("group", group.atoms, group.kind)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _candidates(rec: Interaction, lig: Ligand, rings: list[LigandGroup], charged: list[LigandGroup],
|
|
162
|
+
pts: dict[str, np.ndarray], protein_metals: frozenset[str]) -> list[_Candidate]:
|
|
163
|
+
"""Every chemically possible (ligand site, protein site) pair for an unnamed record. ``pts`` are the heavy
|
|
164
|
+
atoms of the residue and ``protein_metals`` the names of those that are metals."""
|
|
165
|
+
out: list[_Candidate] = []
|
|
166
|
+
xyz = lig.coords3d
|
|
167
|
+
resname = rec.residue_name
|
|
168
|
+
|
|
169
|
+
def add(group: LigandGroup, site: ProteinSite, centre: np.ndarray, extra_rank: int = 0) -> None:
|
|
170
|
+
d = float(np.linalg.norm(group.center3d(xyz) - centre))
|
|
171
|
+
out.append(_Candidate(_anchor_for(group), site, d, group.rank + extra_rank))
|
|
172
|
+
|
|
173
|
+
if rec.type is T.PI_STACKING:
|
|
174
|
+
for site, centre in _protein_ring_sites(resname, pts):
|
|
175
|
+
for g in rings:
|
|
176
|
+
add(g, site, centre)
|
|
177
|
+
elif rec.type is T.PI_CATION:
|
|
178
|
+
# Either partner may supply the ring.
|
|
179
|
+
for site, centre in _protein_ring_sites(resname, pts):
|
|
180
|
+
for g in charged:
|
|
181
|
+
if g.charge > 0:
|
|
182
|
+
add(g, site, centre)
|
|
183
|
+
for site, centre in _protein_charge_sites(resname, pts, sign=+1):
|
|
184
|
+
for g in rings:
|
|
185
|
+
add(g, site, centre)
|
|
186
|
+
elif rec.type is T.SALTBRIDGE:
|
|
187
|
+
sites = _protein_charge_sites(resname, pts)
|
|
188
|
+
for site, centre in sites:
|
|
189
|
+
for g in charged:
|
|
190
|
+
if g.charge == -site.charge:
|
|
191
|
+
add(g, site, centre)
|
|
192
|
+
if not sites:
|
|
193
|
+
# Non-standard residue: fall back to its individual hetero atoms.
|
|
194
|
+
for name, p in pts.items():
|
|
195
|
+
if name[:1] in ("N", "O", "S"):
|
|
196
|
+
for g in charged:
|
|
197
|
+
add(g, ProteinSite("atom", (name,)), p, extra_rank=3)
|
|
198
|
+
elif rec.type is T.METAL:
|
|
199
|
+
metals = [g for g in charged if g.kind == "metal"]
|
|
200
|
+
if metals:
|
|
201
|
+
for name, p in pts.items():
|
|
202
|
+
rank = 0 if name[:1] in ("N", "O", "S") else 3
|
|
203
|
+
for g in metals:
|
|
204
|
+
add(g, ProteinSite("atom", (name,)), p, extra_rank=rank)
|
|
205
|
+
else:
|
|
206
|
+
# The metal sits on the protein side; the ligand donates.
|
|
207
|
+
for name, p in pts.items():
|
|
208
|
+
if name not in protein_metals:
|
|
209
|
+
continue
|
|
210
|
+
for i in range(lig.n_atoms):
|
|
211
|
+
el = lig.element(i)
|
|
212
|
+
if el in ("N", "O", "S"):
|
|
213
|
+
d = float(np.linalg.norm(xyz[i] - p))
|
|
214
|
+
out.append(_Candidate(Anchor("atom", (i,)), ProteinSite("atom", (name,)), d, 0))
|
|
215
|
+
else:
|
|
216
|
+
# A normally named type that arrived without names: match atom pairs.
|
|
217
|
+
for name, p in pts.items():
|
|
218
|
+
for i in range(lig.n_atoms):
|
|
219
|
+
if rec.ligand_atom and lig.names[i] != rec.ligand_atom:
|
|
220
|
+
continue
|
|
221
|
+
if rec.protein_atom and name != rec.protein_atom:
|
|
222
|
+
continue
|
|
223
|
+
d = float(np.linalg.norm(xyz[i] - p))
|
|
224
|
+
out.append(_Candidate(Anchor("atom", (i,)), ProteinSite("atom", (name,)), d, 0))
|
|
225
|
+
return out
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _cost(cand: _Candidate, reported: float | None) -> tuple[float, float | None]:
|
|
229
|
+
"""Lower is better. Exact matches sort by chemical rank, then by agreement."""
|
|
230
|
+
if reported is None:
|
|
231
|
+
return 500.0 + cand.rank * 50.0 + cand.distance, None
|
|
232
|
+
delta = abs(cand.distance - reported)
|
|
233
|
+
if delta <= DISTANCE_TOLERANCE:
|
|
234
|
+
return cand.rank + 0.5 * delta / DISTANCE_TOLERANCE, delta
|
|
235
|
+
return 100.0 + delta * 10.0 + cand.rank, delta
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _least_total(cost: Sequence[Sequence[float]]) -> list[int]:
|
|
239
|
+
"""For every row a column of its own, such that the costs that are chosen add up to the least there is:
|
|
240
|
+
the assignment problem, by the Hungarian method, in a time of rows x rows x columns. There are not more
|
|
241
|
+
rows than columns."""
|
|
242
|
+
n, m = len(cost), len(cost[0])
|
|
243
|
+
inf = float("inf")
|
|
244
|
+
u, v = [0.0] * (n + 1), [0.0] * (m + 1)
|
|
245
|
+
row_of = [0] * (m + 1) # the row each column has (rows and columns count from 1; 0 is none)
|
|
246
|
+
for i in range(1, n + 1):
|
|
247
|
+
row_of[0] = i
|
|
248
|
+
least, came_from, used = [inf] * (m + 1), [0] * (m + 1), [False] * (m + 1)
|
|
249
|
+
j0 = 0
|
|
250
|
+
while True:
|
|
251
|
+
used[j0] = True
|
|
252
|
+
i0, step, j1 = row_of[j0], inf, 0
|
|
253
|
+
for j in range(1, m + 1):
|
|
254
|
+
if not used[j]:
|
|
255
|
+
reduced = cost[i0 - 1][j - 1] - u[i0] - v[j]
|
|
256
|
+
if reduced < least[j]:
|
|
257
|
+
least[j], came_from[j] = reduced, j0
|
|
258
|
+
if least[j] < step:
|
|
259
|
+
step, j1 = least[j], j
|
|
260
|
+
for j in range(m + 1):
|
|
261
|
+
if used[j]:
|
|
262
|
+
u[row_of[j]] += step
|
|
263
|
+
v[j] -= step
|
|
264
|
+
else:
|
|
265
|
+
least[j] -= step
|
|
266
|
+
j0 = j1
|
|
267
|
+
if row_of[j0] == 0:
|
|
268
|
+
break
|
|
269
|
+
while j0:
|
|
270
|
+
row_of[j0] = row_of[came_from[j0]]
|
|
271
|
+
j0 = came_from[j0]
|
|
272
|
+
chosen = [0] * n
|
|
273
|
+
for j in range(1, m + 1):
|
|
274
|
+
if row_of[j]:
|
|
275
|
+
chosen[row_of[j] - 1] = j - 1
|
|
276
|
+
return chosen
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
#: Up to this many records of one residue and type are assigned by trying every combination of their best
|
|
280
|
+
#: candidates, which takes a time that grows with (records + 1) to the power of records: 7 to the 6th, 117,649
|
|
281
|
+
#: combinations, at most. More records are assigned by :func:`_least_total`.
|
|
282
|
+
_TRY_EVERY_COMBINATION = 6
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def _assign(records: list[Interaction], cands: list[_Candidate]) -> list[tuple[_Candidate, float | None] | None]:
|
|
286
|
+
"""Jointly assign distinct candidates to the records of one residue and type."""
|
|
287
|
+
if not cands:
|
|
288
|
+
return [None] * len(records)
|
|
289
|
+
costs = [[_cost(c, rec.distance) for c in cands] for rec in records]
|
|
290
|
+
best: list[tuple[int, float | None]] | None = None
|
|
291
|
+
if len(records) <= _TRY_EVERY_COMBINATION:
|
|
292
|
+
per_record = [sorted((cost, ci, delta) for ci, (cost, delta) in enumerate(row))[: max(4, len(records) + 1)]
|
|
293
|
+
for row in costs]
|
|
294
|
+
best_total = float("inf")
|
|
295
|
+
for combo in itertools.product(*per_record):
|
|
296
|
+
idx = [c[1] for c in combo]
|
|
297
|
+
if len(set(idx)) != len(idx):
|
|
298
|
+
continue
|
|
299
|
+
total = sum(c[0] for c in combo)
|
|
300
|
+
if total < best_total - 1e-12:
|
|
301
|
+
best_total = total
|
|
302
|
+
best = [(c[1], c[2]) for c in combo]
|
|
303
|
+
elif len(cands) >= len(records):
|
|
304
|
+
chosen = _least_total([[cost for cost, _ in row] for row in costs])
|
|
305
|
+
best = [(ci, row[ci][1]) for ci, row in zip(chosen, costs)]
|
|
306
|
+
if best is None:
|
|
307
|
+
# More records than distinct candidates (duplicated rows): allow reuse.
|
|
308
|
+
cheapest = [min(range(len(cands)), key=lambda ci: (row[ci][0], ci)) for row in costs]
|
|
309
|
+
best = [(ci, row[ci][1]) for ci, row in zip(cheapest, costs)]
|
|
310
|
+
return [(cands[ci], delta) for ci, delta in best]
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _named_anchor(rec: Interaction, lig: Ligand) -> tuple[Anchor | None, str | None]:
|
|
314
|
+
idx = lig.index_of(rec.ligand_atom or "")
|
|
315
|
+
if idx is None:
|
|
316
|
+
return None, f"ligand atom {rec.ligand_atom!r} is not in the structure"
|
|
317
|
+
return Anchor("atom", (idx,)), None
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _supplied_group(rec: Interaction, lig: Ligand) -> tuple[Anchor | None, str | None]:
|
|
321
|
+
idx = [lig.index_of(n) for n in rec.ligand_atoms]
|
|
322
|
+
missing = [n for n, i in zip(rec.ligand_atoms, idx) if i is None]
|
|
323
|
+
if missing:
|
|
324
|
+
return None, f"ligand atoms not in the structure: {', '.join(missing)}"
|
|
325
|
+
members = tuple(int(i) for i in idx if i is not None)
|
|
326
|
+
if len(members) == 1:
|
|
327
|
+
return Anchor("atom", members), None
|
|
328
|
+
kind = "ring" if rec.type in (T.PI_STACKING, T.PI_CATION) and _is_ring(lig, members) else "group"
|
|
329
|
+
return Anchor(kind, members), None
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _is_ring(lig: Ligand, members: tuple[int, ...]) -> bool:
|
|
333
|
+
want = set(members)
|
|
334
|
+
return any(set(r) == want for r in lig.mol.GetRingInfo().AtomRings())
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def _protein_site_from_record(rec: Interaction) -> ProteinSite:
|
|
338
|
+
if rec.protein_atoms:
|
|
339
|
+
if len(rec.protein_atoms) == 1:
|
|
340
|
+
return ProteinSite("atom", rec.protein_atoms)
|
|
341
|
+
rings = aromatic_rings(rec.residue_name)
|
|
342
|
+
if any(set(r) == set(rec.protein_atoms) for r in rings):
|
|
343
|
+
return ProteinSite("ring", rec.protein_atoms)
|
|
344
|
+
charge = 0
|
|
345
|
+
for sign, names in charged_groups(rec.residue_name):
|
|
346
|
+
if set(rec.protein_atoms) <= set(names):
|
|
347
|
+
charge = sign
|
|
348
|
+
return ProteinSite("group", rec.protein_atoms, charge)
|
|
349
|
+
if rec.protein_atom:
|
|
350
|
+
return ProteinSite("atom", (rec.protein_atom,))
|
|
351
|
+
return ProteinSite("residue")
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def _locate(structure: Structure | None, rec: Interaction) -> ResidueKey | None:
|
|
355
|
+
if structure is None:
|
|
356
|
+
return None
|
|
357
|
+
return structure.find_residue(rec.chain or None, rec.residue_number, rec.insertion_code)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def _why_not_located(structure: Structure, rec: Interaction) -> str:
|
|
361
|
+
"""Why the residue of a record was not found: the structure has none, or the record names no chain and
|
|
362
|
+
several chains have a residue of that number."""
|
|
363
|
+
chains = [] if rec.chain else [k.chain for k in structure.residues()
|
|
364
|
+
if k.number == rec.residue_number and k.icode == rec.insertion_code]
|
|
365
|
+
if len(chains) > 1:
|
|
366
|
+
listed = ", ".join(chains[:8]) + (", ..." if len(chains) > 8 else "")
|
|
367
|
+
return (f"residue {rec.residue_number}{rec.insertion_code} is in {len(chains)} chains ({listed}) "
|
|
368
|
+
f"and the record names no chain")
|
|
369
|
+
return f"residue {rec.chain or '?'}:{rec.residue_number} is not in the structure"
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def _chain_that_is_there(rec: Interaction, structure: Structure | None) -> Interaction:
|
|
373
|
+
"""The record with ``NA`` in the place of the chain read as what it is: the name of a chain, where the
|
|
374
|
+
structure has one called so, and otherwise a chain that is not given."""
|
|
375
|
+
if rec.chain == NOT_GIVEN_OR_A_NAME and (structure is None
|
|
376
|
+
or all(k.chain != NOT_GIVEN_OR_A_NAME for k in structure.residues())):
|
|
377
|
+
return rec.with_(chain="")
|
|
378
|
+
return rec
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def _names_that_are_there(rec: Interaction, lig: Ligand, structure: Structure | None,
|
|
382
|
+
residue: ResidueKey | None) -> Interaction:
|
|
383
|
+
"""The record with ``NA`` in the place of an atom's name read as what it is: the name of an atom, where
|
|
384
|
+
the ligand (the residue) has one called so, and otherwise a name that is not given. The same where it is
|
|
385
|
+
all that stands for the atoms of a group."""
|
|
386
|
+
alone = (NOT_GIVEN_OR_A_NAME,)
|
|
387
|
+
if lig.index_of(NOT_GIVEN_OR_A_NAME) is None:
|
|
388
|
+
if rec.ligand_atom == NOT_GIVEN_OR_A_NAME:
|
|
389
|
+
rec = rec.with_(ligand_atom=None)
|
|
390
|
+
if rec.ligand_atoms == alone:
|
|
391
|
+
rec = rec.with_(ligand_atoms=())
|
|
392
|
+
if rec.protein_atom == NOT_GIVEN_OR_A_NAME or rec.protein_atoms == alone:
|
|
393
|
+
there = (NOT_GIVEN_OR_A_NAME in _protein_points(structure, residue) if structure is not None and residue is not None
|
|
394
|
+
else rec.residue_name == NOT_GIVEN_OR_A_NAME)
|
|
395
|
+
if not there:
|
|
396
|
+
rec = rec.with_(protein_atom=None if rec.protein_atom == NOT_GIVEN_OR_A_NAME else rec.protein_atom,
|
|
397
|
+
protein_atoms=() if rec.protein_atoms == alone else rec.protein_atoms)
|
|
398
|
+
return rec
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def resolve_interactions(interactions: Sequence[Interaction], lig: Ligand,
|
|
402
|
+
structure: Structure | None = None) -> list[ResolvedInteraction]:
|
|
403
|
+
"""Resolve every record to its ligand anchor and protein site.
|
|
404
|
+
|
|
405
|
+
``structure`` is needed only for records that do not name their atoms.
|
|
406
|
+
Results come back in input order.
|
|
407
|
+
"""
|
|
408
|
+
rings = perceive_rings(lig)
|
|
409
|
+
charged = perceive_charged_groups(lig)
|
|
410
|
+
results: list[ResolvedInteraction | None] = [None] * len(interactions)
|
|
411
|
+
pending: dict[tuple[tuple[str, int, str], InteractionType, str | None], list[int]] = {}
|
|
412
|
+
|
|
413
|
+
for n, given in enumerate(interactions):
|
|
414
|
+
given = _chain_that_is_there(given, structure)
|
|
415
|
+
residue = _locate(structure, given)
|
|
416
|
+
rec = _names_that_are_there(given, lig, structure, residue)
|
|
417
|
+
site = _protein_site_from_record(rec)
|
|
418
|
+
notes: list[str] = []
|
|
419
|
+
if residue is not None and structure is not None:
|
|
420
|
+
actual = structure.residue_name(residue)
|
|
421
|
+
if actual.upper() != rec.residue_name.upper():
|
|
422
|
+
notes.append(f"record says {rec.residue_name}{rec.residue_number}, "
|
|
423
|
+
f"the structure has {actual} at that position")
|
|
424
|
+
if rec.ligand_atoms:
|
|
425
|
+
anchor, problem = _supplied_group(rec, lig)
|
|
426
|
+
if problem:
|
|
427
|
+
notes.append(problem)
|
|
428
|
+
results[n] = ResolvedInteraction(rec, n, residue, anchor, site,
|
|
429
|
+
"supplied" if anchor else "unresolved", notes=notes)
|
|
430
|
+
continue
|
|
431
|
+
group_type = rec.type in (T.PI_STACKING, T.PI_CATION, T.SALTBRIDGE)
|
|
432
|
+
if rec.ligand_atom and not group_type:
|
|
433
|
+
anchor, problem = _named_anchor(rec, lig)
|
|
434
|
+
if problem:
|
|
435
|
+
notes.append(problem)
|
|
436
|
+
if anchor is not None and site.kind == "residue" and residue is not None and structure is not None:
|
|
437
|
+
# Ligand atom named, protein atom not: find it by distance.
|
|
438
|
+
pts = _protein_points(structure, residue)
|
|
439
|
+
cands = [c for c in _candidates(rec.with_(protein_atom=None), lig, rings, charged, pts,
|
|
440
|
+
_protein_metals(structure, residue))
|
|
441
|
+
if c.ligand.atoms == anchor.atoms]
|
|
442
|
+
picked = _assign([rec], cands)[0]
|
|
443
|
+
if picked is not None and picked[1] is not None and picked[1] <= DISTANCE_TOLERANCE:
|
|
444
|
+
site = picked[0].protein
|
|
445
|
+
results[n] = ResolvedInteraction(rec, n, residue, anchor, site,
|
|
446
|
+
"named" if anchor else "unresolved", notes=notes)
|
|
447
|
+
continue
|
|
448
|
+
if residue is None:
|
|
449
|
+
why = "no structure was supplied" if structure is None else _why_not_located(structure, rec)
|
|
450
|
+
notes.append(f"cannot resolve the {rec.type.value} partner: {why}")
|
|
451
|
+
results[n] = ResolvedInteraction(rec, n, None, None, site, "unresolved", notes=notes)
|
|
452
|
+
continue
|
|
453
|
+
results[n] = ResolvedInteraction(rec, n, residue, None, site, "unresolved", notes=notes)
|
|
454
|
+
# Records are assigned together with those that have the same candidates: of one residue and type and,
|
|
455
|
+
# where the candidates are pairs of atoms, with the same atom of the residue named (or none).
|
|
456
|
+
named = None if group_type or rec.type is T.METAL else rec.protein_atom
|
|
457
|
+
pending.setdefault((rec.residue_id, rec.type, named), []).append(n)
|
|
458
|
+
|
|
459
|
+
for idxs in pending.values():
|
|
460
|
+
first = results[idxs[0]]
|
|
461
|
+
assert first is not None and first.residue is not None and structure is not None
|
|
462
|
+
recs = [r.record for r in (results[i] for i in idxs) if r is not None]
|
|
463
|
+
pts = _protein_points(structure, first.residue)
|
|
464
|
+
cands = _candidates(recs[0], lig, rings, charged, pts, _protein_metals(structure, first.residue))
|
|
465
|
+
for i, picked in zip(idxs, _assign(recs, cands)):
|
|
466
|
+
res = results[i]
|
|
467
|
+
assert res is not None
|
|
468
|
+
if picked is None:
|
|
469
|
+
res.notes.append(f"no {res.record.type.value} partner could be identified on the ligand "
|
|
470
|
+
f"or on {res.record.residue_label}")
|
|
471
|
+
continue
|
|
472
|
+
cand, delta = picked
|
|
473
|
+
res.computed_distance = cand.distance
|
|
474
|
+
res.delta = delta
|
|
475
|
+
if delta is None:
|
|
476
|
+
res.method = "nearest"
|
|
477
|
+
res.notes.append("no distance reported; attached to the chemically most plausible nearest partner")
|
|
478
|
+
elif delta <= DISTANCE_TOLERANCE:
|
|
479
|
+
res.method = "distance-match"
|
|
480
|
+
elif delta <= _LOOSE_TOLERANCE:
|
|
481
|
+
res.method = "approximate"
|
|
482
|
+
res.notes.append(
|
|
483
|
+
f"closest geometric match is {cand.distance:.2f} A against a reported {res.record.distance:.2f} A")
|
|
484
|
+
else:
|
|
485
|
+
res.notes.append(
|
|
486
|
+
f"no partner reproduces the reported {res.record.distance:.2f} A "
|
|
487
|
+
f"(closest candidate {cand.distance:.2f} A); not drawn")
|
|
488
|
+
continue
|
|
489
|
+
res.ligand = cand.ligand
|
|
490
|
+
if res.protein.kind == "residue" or not res.record.protein_atoms:
|
|
491
|
+
res.protein = cand.protein
|
|
492
|
+
|
|
493
|
+
return [r for r in results if r is not None]
|