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/fragments.py
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"""2D geometry for the part of a residue that is drawn explicitly.
|
|
2
|
+
|
|
3
|
+
In the detailed style a residue that makes a directional contact is not a
|
|
4
|
+
bubble: the piece of it that makes the contact is drawn as a small skeletal
|
|
5
|
+
formula in a second colour, the way LigPlot shows H-bonded residues. This
|
|
6
|
+
module decides which atoms that piece consists of and lays them out. Rotation
|
|
7
|
+
and placement on the page are the layout engine's business.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from functools import lru_cache
|
|
14
|
+
from typing import Iterable
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
from rdkit import Chem
|
|
18
|
+
from rdkit.Chem import rdDepictor
|
|
19
|
+
|
|
20
|
+
from .chem import quiet_rdkit
|
|
21
|
+
from .geometry import Point
|
|
22
|
+
from .residues import (NUCLEOTIDE_BACKBONE, PHOSPHATE, SUGAR, TAIL, ResidueTemplate, canonical_atom,
|
|
23
|
+
canonical_resname, element_of, given_atom, template_bonds, template_for)
|
|
24
|
+
|
|
25
|
+
__all__ = ["Fragment", "fragment_atoms", "build_fragment", "conformations", "DETAILS", "check_detail"]
|
|
26
|
+
|
|
27
|
+
#: How much of a residue is drawn where it is drawn with atoms: nothing of it (it stays one labelled
|
|
28
|
+
#: thing), its backbone, its side chain, or every heavy atom. ``None`` is not among them: it asks for
|
|
29
|
+
#: no level, and a residue is then what its style makes of it.
|
|
30
|
+
DETAILS = ("box", "backbone", "sidechain", "atoms")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def check_detail(detail: "str | None", what: str = "detail") -> "str | None":
|
|
34
|
+
"""``detail`` if it is ``None`` or one of :data:`DETAILS`; a :class:`ValueError` that names them otherwise."""
|
|
35
|
+
if detail is None or detail in DETAILS:
|
|
36
|
+
return detail
|
|
37
|
+
raise ValueError(f"{what} is {', '.join(repr(d) for d in DETAILS[:-1])} or {DETAILS[-1]!r} "
|
|
38
|
+
f"(or None, for what the style makes of a residue by itself), not {detail!r}")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class Fragment:
|
|
43
|
+
resname: str
|
|
44
|
+
atoms: tuple[str, ...]
|
|
45
|
+
elements: dict[str, str]
|
|
46
|
+
xy: dict[str, Point] # local coordinates, y down, in user units
|
|
47
|
+
bonds: tuple[tuple[str, str, int], ...]
|
|
48
|
+
rings: tuple[tuple[str, ...], ...]
|
|
49
|
+
root: str # the residue label sits beyond this atom
|
|
50
|
+
|
|
51
|
+
def centroid(self, names: Iterable[str]) -> Point:
|
|
52
|
+
pts = [self.xy[n] for n in names]
|
|
53
|
+
return (sum(p[0] for p in pts) / len(pts), sum(p[1] for p in pts) / len(pts))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def fragment_atoms(resname: str, involved: Iterable[str], detail: "str | None" = None) -> tuple[str, ...] | None:
|
|
57
|
+
"""Which atoms of a residue to draw, given the atoms that make contacts.
|
|
58
|
+
|
|
59
|
+
The rule keeps fragments recognisable and small: the alpha carbon always;
|
|
60
|
+
the *whole* side chain if any side-chain atom is involved (half an arginine
|
|
61
|
+
is not an arginine); the backbone nitrogen and/or carbonyl only when they
|
|
62
|
+
are themselves involved. Returns ``None`` for residues without a template
|
|
63
|
+
or contacts through atoms the template does not have -- those are drawn as
|
|
64
|
+
a bare labelled atom instead.
|
|
65
|
+
|
|
66
|
+
``detail`` (one of :data:`DETAILS`) adds to that and takes nothing away:
|
|
67
|
+
``"backbone"`` the nitrogen and the carbonyl, ``"sidechain"`` the side
|
|
68
|
+
chain, ``"atoms"`` both. The part that makes the contact is drawn at
|
|
69
|
+
every level, so a line of the residue always ends on an atom.
|
|
70
|
+
|
|
71
|
+
A nucleotide has three parts and the same rule (:func:`_nucleotide_atoms`):
|
|
72
|
+
its base is its side chain, its sugar and its phosphate its backbone.
|
|
73
|
+
"""
|
|
74
|
+
t = template_for(resname)
|
|
75
|
+
if t is None:
|
|
76
|
+
return None
|
|
77
|
+
wanted = {canonical_atom(resname, a) for a in involved}
|
|
78
|
+
if not wanted <= set(t.atoms):
|
|
79
|
+
return None
|
|
80
|
+
if t.kind == "nucleotide":
|
|
81
|
+
return _nucleotide_atoms(t, wanted, detail)
|
|
82
|
+
side = set(t.sidechain)
|
|
83
|
+
proline = canonical_resname(resname) == "PRO"
|
|
84
|
+
keep = {"CA"}
|
|
85
|
+
if wanted & side:
|
|
86
|
+
keep |= side
|
|
87
|
+
if proline:
|
|
88
|
+
keep.add("N") # the ring closes on the backbone nitrogen
|
|
89
|
+
if "N" in wanted:
|
|
90
|
+
keep.add("N")
|
|
91
|
+
if wanted & {"C", "O"}:
|
|
92
|
+
keep |= {"C", "O"}
|
|
93
|
+
if len(keep) == 1:
|
|
94
|
+
keep |= {"N", "C", "O"} # a lone vertex would be invisible
|
|
95
|
+
# What the level adds to the part that makes the contact.
|
|
96
|
+
if detail in ("sidechain", "atoms"):
|
|
97
|
+
keep |= side
|
|
98
|
+
if proline:
|
|
99
|
+
keep.add("N")
|
|
100
|
+
if detail in ("backbone", "atoms"):
|
|
101
|
+
keep |= {"N", "C", "O"}
|
|
102
|
+
return tuple(a for a in t.atoms if a in keep)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _nucleotide_atoms(t: ResidueTemplate, wanted: "set[str]", detail: "str | None") -> tuple[str, ...]:
|
|
106
|
+
"""Which atoms of a nucleotide to draw, given those that make contacts (under the template's names).
|
|
107
|
+
|
|
108
|
+
The part that makes the contact, whole: the base with C1' if any atom of the base is involved; the
|
|
109
|
+
ring of the sugar, with the hydroxyl that is involved; the phosphate with the O5' and C5' that hang
|
|
110
|
+
it on the sugar. A phosphate alone is drawn without the sugar, as five atoms: most contacts of a
|
|
111
|
+
charged ligand are with phosphates, and a sugar on each would crowd the figure. Where both ends of a
|
|
112
|
+
nucleotide are involved the sugar between them is drawn, so that the fragment is one piece.
|
|
113
|
+
``detail`` adds the base (``"sidechain"``), sugar and phosphate (``"backbone"``) or all (``"atoms"``).
|
|
114
|
+
"""
|
|
115
|
+
base = set(t.atoms) - set(NUCLEOTIDE_BACKBONE)
|
|
116
|
+
ring = {"C1'", "C2'", "C3'", "C4'", "O4'"}
|
|
117
|
+
keep: set[str] = set()
|
|
118
|
+
if wanted & base:
|
|
119
|
+
keep |= base | {"C1'"}
|
|
120
|
+
if wanted & set(SUGAR):
|
|
121
|
+
keep |= ring | (wanted & {"O2'", "O3'"})
|
|
122
|
+
if wanted & set(PHOSPHATE):
|
|
123
|
+
keep |= set(PHOSPHATE) | set(TAIL)
|
|
124
|
+
if wanted & set(TAIL):
|
|
125
|
+
keep |= set(TAIL)
|
|
126
|
+
if detail in ("sidechain", "atoms"):
|
|
127
|
+
keep |= base | {"C1'"}
|
|
128
|
+
if detail in ("backbone", "atoms"):
|
|
129
|
+
keep |= set(NUCLEOTIDE_BACKBONE)
|
|
130
|
+
if keep & (base | ring) and keep & (set(PHOSPHATE) | set(TAIL)):
|
|
131
|
+
keep |= ring | set(TAIL) # the sugar, between a base or a hydroxyl and the phosphate
|
|
132
|
+
return tuple(a for a in t.atoms if a in keep)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@lru_cache(maxsize=None)
|
|
136
|
+
def _unit_layout(resname: str, atoms: tuple[str, ...]) -> tuple[tuple[tuple[float, float], ...],
|
|
137
|
+
tuple[tuple[str, ...], ...]]:
|
|
138
|
+
"""Unit-bond-length 2D coordinates (y up) and rings for a fragment."""
|
|
139
|
+
t = template_for(resname)
|
|
140
|
+
assert t is not None
|
|
141
|
+
with quiet_rdkit():
|
|
142
|
+
full = Chem.MolFromSmiles(t.smiles)
|
|
143
|
+
keep = [t.atoms.index(a) for a in atoms]
|
|
144
|
+
new_index = {old: new for new, old in enumerate(keep)}
|
|
145
|
+
rw = Chem.RWMol()
|
|
146
|
+
for old in keep:
|
|
147
|
+
atom = Chem.Atom(full.GetAtomWithIdx(old).GetSymbol())
|
|
148
|
+
atom.SetNoImplicit(True)
|
|
149
|
+
rw.AddAtom(atom)
|
|
150
|
+
for b in full.GetBonds():
|
|
151
|
+
i, j = b.GetBeginAtomIdx(), b.GetEndAtomIdx()
|
|
152
|
+
if i in new_index and j in new_index:
|
|
153
|
+
rw.AddBond(new_index[i], new_index[j], Chem.BondType.SINGLE)
|
|
154
|
+
mol = rw.GetMol()
|
|
155
|
+
mol.UpdatePropertyCache(strict=False)
|
|
156
|
+
Chem.FastFindRings(mol)
|
|
157
|
+
prefer = rdDepictor.GetPreferCoordGen()
|
|
158
|
+
rdDepictor.SetPreferCoordGen(False)
|
|
159
|
+
try:
|
|
160
|
+
rdDepictor.Compute2DCoords(mol, canonOrient=True)
|
|
161
|
+
finally:
|
|
162
|
+
rdDepictor.SetPreferCoordGen(prefer)
|
|
163
|
+
conf = mol.GetConformer()
|
|
164
|
+
xy = np.array([[conf.GetAtomPosition(i).x, conf.GetAtomPosition(i).y] for i in range(mol.GetNumAtoms())])
|
|
165
|
+
lengths = [float(np.linalg.norm(xy[b.GetBeginAtomIdx()] - xy[b.GetEndAtomIdx()])) for b in mol.GetBonds()]
|
|
166
|
+
scale = float(np.median(lengths)) if lengths else 1.0
|
|
167
|
+
xy = xy / scale if scale > 1e-9 else xy
|
|
168
|
+
rings = tuple(tuple(atoms[i] for i in ring) for ring in mol.GetRingInfo().AtomRings())
|
|
169
|
+
return tuple((float(x), float(y)) for x, y in xy), rings
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def build_fragment(resname: str, involved: Iterable[str], bond_length: float,
|
|
173
|
+
detail: "str | None" = None, like: "Iterable[str] | None" = None) -> Fragment | None:
|
|
174
|
+
"""The drawable fragment of ``resname`` for the given contact atoms, or ``None``; with ``detail``,
|
|
175
|
+
at least that much of the residue (:func:`fragment_atoms`).
|
|
176
|
+
|
|
177
|
+
The atoms of a nucleotide are named as a file names them, which is one of two ways (``OP1`` or
|
|
178
|
+
``O1P``, ``C1'`` or ``C1*``): ``like`` is the names the contacts of the whole figure use, so that
|
|
179
|
+
every nucleotide of a figure is named in one way; left out, the names of ``involved`` decide."""
|
|
180
|
+
involved = tuple(involved)
|
|
181
|
+
atoms = fragment_atoms(resname, involved, detail)
|
|
182
|
+
if atoms is None:
|
|
183
|
+
return None
|
|
184
|
+
unit, rings = _unit_layout(canonical_resname(resname), atoms)
|
|
185
|
+
xy = {a: (x * bond_length, -y * bond_length) for a, (x, y) in zip(atoms, unit)}
|
|
186
|
+
chosen = set(atoms)
|
|
187
|
+
bonds = tuple((a, b, order) for a, b, order in template_bonds(canonical_resname(resname))
|
|
188
|
+
if a in chosen and b in chosen)
|
|
189
|
+
elements = {a: element_of(a) for a in atoms}
|
|
190
|
+
t = template_for(resname)
|
|
191
|
+
if t is None or t.kind != "nucleotide":
|
|
192
|
+
return Fragment(resname=canonical_resname(resname), atoms=atoms, elements=elements, xy=xy,
|
|
193
|
+
bonds=bonds, rings=rings, root="CA")
|
|
194
|
+
# A nucleotide: its name stands beyond C1', or beyond C5' where the phosphate is drawn alone. And its
|
|
195
|
+
# atoms are named as the figure's contacts name them, so that a contact finds its atom under its name.
|
|
196
|
+
root = "C1'" if "C1'" in chosen else "C5'"
|
|
197
|
+
like = involved if like is None else tuple(like)
|
|
198
|
+
own = {canonical_atom(resname, a): a for a in involved}
|
|
199
|
+
|
|
200
|
+
def as_given(name: str) -> str:
|
|
201
|
+
return own.get(name) or given_atom(name, like)
|
|
202
|
+
|
|
203
|
+
return Fragment(resname=canonical_resname(resname), atoms=tuple(as_given(a) for a in atoms),
|
|
204
|
+
elements={as_given(a): e for a, e in elements.items()},
|
|
205
|
+
xy={as_given(a): p for a, p in xy.items()},
|
|
206
|
+
bonds=tuple((as_given(a), as_given(b), order) for a, b, order in bonds),
|
|
207
|
+
rings=tuple(tuple(as_given(a) for a in ring) for ring in rings), root=as_given(root))
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
#: A fragment has at most this many layouts (:func:`conformations`).
|
|
211
|
+
_MOST_LAYOUTS = 24
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _reflect(p: Point, a: Point, b: Point) -> Point:
|
|
215
|
+
"""Mirror ``p`` in the line through ``a`` and ``b``."""
|
|
216
|
+
dx, dy = b[0] - a[0], b[1] - a[1]
|
|
217
|
+
n2 = dx * dx + dy * dy
|
|
218
|
+
if n2 < 1e-12:
|
|
219
|
+
return p
|
|
220
|
+
t = ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / n2
|
|
221
|
+
fx, fy = a[0] + t * dx, a[1] + t * dy
|
|
222
|
+
return (2.0 * fx - p[0], 2.0 * fy - p[1])
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def conformations(frag: Fragment) -> list[dict[str, Point]]:
|
|
226
|
+
"""Alternative flat layouts of a fragment, the template one first.
|
|
227
|
+
|
|
228
|
+
A long side chain drawn fully extended cannot bring its tip and its
|
|
229
|
+
backbone near the same part of a ligand. LigPlot's answer, used here too, is
|
|
230
|
+
to flip the far part of the chain about a bond: bond lengths and angles are
|
|
231
|
+
untouched, the zig-zag gains a kink. Every layout with up to two such flips
|
|
232
|
+
that keeps non-bonded atoms apart is returned, :data:`_MOST_LAYOUTS` at most.
|
|
233
|
+
"""
|
|
234
|
+
nbrs: dict[str, list[str]] = {a: [] for a in frag.atoms}
|
|
235
|
+
for a, b, _ in frag.bonds:
|
|
236
|
+
nbrs[a].append(b)
|
|
237
|
+
nbrs[b].append(a)
|
|
238
|
+
ring_bond = {frozenset((r[i], r[(i + 1) % len(r)])) for r in frag.rings for i in range(len(r))}
|
|
239
|
+
# Depth-first from the root gives every atom its parent and its subtree.
|
|
240
|
+
parent: dict[str, str | None] = {frag.root: None}
|
|
241
|
+
order = [frag.root]
|
|
242
|
+
for a in order:
|
|
243
|
+
for b in nbrs[a]:
|
|
244
|
+
if b not in parent:
|
|
245
|
+
parent[b] = a
|
|
246
|
+
order.append(b)
|
|
247
|
+
children: dict[str, list[str]] = {a: [] for a in frag.atoms}
|
|
248
|
+
for a, par in parent.items():
|
|
249
|
+
if par is not None:
|
|
250
|
+
children[par].append(a)
|
|
251
|
+
|
|
252
|
+
def subtree(a: str) -> list[str]:
|
|
253
|
+
out, stack = [], list(children[a])
|
|
254
|
+
while stack:
|
|
255
|
+
x = stack.pop()
|
|
256
|
+
out.append(x)
|
|
257
|
+
stack.extend(children[x])
|
|
258
|
+
return out
|
|
259
|
+
|
|
260
|
+
flippable = []
|
|
261
|
+
for far in order[1:]:
|
|
262
|
+
near = parent[far]
|
|
263
|
+
assert near is not None
|
|
264
|
+
if frozenset((near, far)) in ring_bond or not children[far]:
|
|
265
|
+
continue # ring bonds do not flip; a terminal atom has nothing to move
|
|
266
|
+
flippable.append((near, far))
|
|
267
|
+
|
|
268
|
+
bonded = {frozenset((a, b)) for a, b, _ in frag.bonds}
|
|
269
|
+
lengths = [((frag.xy[a][0] - frag.xy[b][0]) ** 2 + (frag.xy[a][1] - frag.xy[b][1]) ** 2) ** 0.5
|
|
270
|
+
for a, b, _ in frag.bonds]
|
|
271
|
+
unit_len = sorted(lengths)[len(lengths) // 2] if lengths else 1.0
|
|
272
|
+
|
|
273
|
+
def clean(xy: dict[str, Point]) -> bool:
|
|
274
|
+
names = list(frag.atoms)
|
|
275
|
+
for i, a in enumerate(names):
|
|
276
|
+
for b in names[i + 1:]:
|
|
277
|
+
if frozenset((a, b)) in bonded:
|
|
278
|
+
continue
|
|
279
|
+
if ((xy[a][0] - xy[b][0]) ** 2 + (xy[a][1] - xy[b][1]) ** 2) ** 0.5 < 0.8 * unit_len:
|
|
280
|
+
return False
|
|
281
|
+
return True
|
|
282
|
+
|
|
283
|
+
def apply(flips: tuple[int, ...]) -> dict[str, Point]:
|
|
284
|
+
xy = dict(frag.xy)
|
|
285
|
+
for k in flips: # ``flippable`` is ordered root outward
|
|
286
|
+
a, b = flippable[k]
|
|
287
|
+
for x in subtree(b):
|
|
288
|
+
xy[x] = _reflect(xy[x], xy[a], xy[b])
|
|
289
|
+
return xy
|
|
290
|
+
|
|
291
|
+
out = [dict(frag.xy)]
|
|
292
|
+
seen = {tuple(round(v, 3) for a in frag.atoms for v in frag.xy[a])}
|
|
293
|
+
combos: list[tuple[int, ...]] = [(k,) for k in range(len(flippable))]
|
|
294
|
+
combos += [(i, j) for i in range(len(flippable)) for j in range(i + 1, len(flippable))]
|
|
295
|
+
for flips in combos:
|
|
296
|
+
xy = apply(flips)
|
|
297
|
+
sig = tuple(round(v, 3) for a in frag.atoms for v in xy[a])
|
|
298
|
+
if sig in seen or not clean(xy):
|
|
299
|
+
continue
|
|
300
|
+
seen.add(sig)
|
|
301
|
+
out.append(xy)
|
|
302
|
+
if len(out) >= _MOST_LAYOUTS:
|
|
303
|
+
break
|
|
304
|
+
return out
|