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/chains.py
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"""A peptide ligand that is a polymer chain of its own in the structure file.
|
|
2
|
+
|
|
3
|
+
A co-folding model writes a peptide ligand as one component with one residue
|
|
4
|
+
record, and its residues have to be perceived (:mod:`bindview.peptide`). A
|
|
5
|
+
deposited structure writes it as what it is: a chain of residues, each with
|
|
6
|
+
its name and its number. That is the easier case and the one a crystal
|
|
7
|
+
structure is uploaded in, and both end in the same two things:
|
|
8
|
+
|
|
9
|
+
* one :class:`~bindview.ligand.Ligand`, a lump of named atoms. The chain's atoms
|
|
10
|
+
get names that are unique in it (``63/OE1``:
|
|
11
|
+
:meth:`bindview.structure.Structure.with_chain_as_ligand`), and since the file
|
|
12
|
+
has no SMILES for a chain, :func:`chain_smiles` writes one from the names of
|
|
13
|
+
the residues and their atoms, so that bond orders are known;
|
|
14
|
+
* one :class:`~bindview.peptide.Peptide`, the decomposition into residues. Here
|
|
15
|
+
nothing of it is perceived (:func:`given_peptide`): a residue is what the
|
|
16
|
+
file says it is, with all the atoms the file gives it, under the name and
|
|
17
|
+
the number the file gives it. A component of the chain without a backbone of
|
|
18
|
+
its own, an acetyl cap for one, is not a residue but something that hangs on
|
|
19
|
+
one, exactly as the perceived route has it.
|
|
20
|
+
|
|
21
|
+
The numbers are then the deposited ones, and the figure says so; for a
|
|
22
|
+
perceived chain they are a count, and the figure says that.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
from typing import Sequence
|
|
28
|
+
|
|
29
|
+
from rdkit import Chem
|
|
30
|
+
|
|
31
|
+
from .chem import quiet_rdkit
|
|
32
|
+
from .peptide import _ONE, _OTHERS, Appendage, Crosslink, Peptide, PeptideResidue, _stereo
|
|
33
|
+
from .residues import _NUCLEOTIDE_TEMPLATES, _TEMPLATES, canonical_atom, canonical_resname
|
|
34
|
+
from .structure import Atom, ChainResidue
|
|
35
|
+
|
|
36
|
+
__all__ = ["chain_smiles", "given_peptide", "KNOWN_COMPONENTS"]
|
|
37
|
+
|
|
38
|
+
#: Components a chain may hold beside the twenty and the residues :mod:`bindview.peptide` can name:
|
|
39
|
+
#: the common modified residues, and caps. SMILES atom order as in :mod:`bindview.residues`
|
|
40
|
+
#: (N, CA, C, O, then the side chain) for a residue; a cap has no backbone.
|
|
41
|
+
_MODIFIED: tuple[tuple[str, str, tuple[str, ...]], ...] = (
|
|
42
|
+
("PTR", "NC(C=O)Cc1ccc(OP(=O)(O)O)cc1",
|
|
43
|
+
("N", "CA", "C", "O", "CB", "CG", "CD1", "CE1", "CZ", "OH", "P", "O1P", "O2P", "O3P", "CE2", "CD2")),
|
|
44
|
+
("TYS", "NC(C=O)Cc1ccc(OS(=O)(=O)O)cc1",
|
|
45
|
+
("N", "CA", "C", "O", "CB", "CG", "CD1", "CE1", "CZ", "OH", "S", "O1", "O2", "O3", "CE2", "CD2")),
|
|
46
|
+
("SEP", "NC(C=O)COP(=O)(O)O", ("N", "CA", "C", "O", "CB", "OG", "P", "O1P", "O2P", "O3P")),
|
|
47
|
+
("TPO", "NC(C=O)C(C)OP(=O)(O)O", ("N", "CA", "C", "O", "CB", "CG2", "OG1", "P", "O1P", "O2P", "O3P")),
|
|
48
|
+
("CSO", "NC(C=O)CSO", ("N", "CA", "C", "O", "CB", "SG", "OD")),
|
|
49
|
+
("ALY", "NC(C=O)CCCCNC(C)=O", ("N", "CA", "C", "O", "CB", "CG", "CD", "CE", "NZ", "CH", "CH3", "OH")),
|
|
50
|
+
("MLY", "NC(C=O)CCCCN(C)C", ("N", "CA", "C", "O", "CB", "CG", "CD", "CE", "NZ", "CH1", "CH2")),
|
|
51
|
+
("M3L", "NC(C=O)CCCC[N+](C)(C)C", ("N", "CA", "C", "O", "CB", "CG", "CD", "CE", "NZ", "CM1", "CM2", "CM3")),
|
|
52
|
+
("ACE", "CC=O", ("CH3", "C", "O")),
|
|
53
|
+
("NH2", "N", ("N",)),
|
|
54
|
+
("NME", "NC", ("N", "C")),
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _components() -> dict[str, tuple[str, tuple[str, ...]]]:
|
|
59
|
+
out = {t.name: (t.smiles, t.atoms) for t in _TEMPLATES.values()}
|
|
60
|
+
out.update({name: (smiles, atoms) for name, smiles, atoms in _OTHERS})
|
|
61
|
+
out.update({name: (smiles, atoms) for name, smiles, atoms in _MODIFIED})
|
|
62
|
+
out.update({name: (t.smiles, t.atoms) for name, t in _NUCLEOTIDE_TEMPLATES.items()})
|
|
63
|
+
return out
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
#: Every component whose bonds are known here by the names of its atoms.
|
|
67
|
+
KNOWN_COMPONENTS = _components()
|
|
68
|
+
_COVALENT = 1.9
|
|
69
|
+
_COVALENT_BIG = 2.3 # where sulfur, phosphorus or selenium takes part
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _local(name: str) -> str:
|
|
73
|
+
"""``OE1`` from ``63/OE1``: an atom's name within its residue."""
|
|
74
|
+
return name.split("/", 1)[1] if "/" in name else name
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def chain_smiles(atoms: Sequence[Atom], residues: Sequence[ChainResidue]) -> tuple[str | None, list[str]]:
|
|
78
|
+
"""A SMILES for a chain ligand, written from the names of its residues and atoms.
|
|
79
|
+
|
|
80
|
+
``atoms`` are the atoms of the merged ligand, ``residues`` what the file
|
|
81
|
+
said about them. Within a residue the bonds and their orders are those of
|
|
82
|
+
the component of that name, between the atoms that are present (a side
|
|
83
|
+
chain modelled only to its beta carbon is that much shorter). Between
|
|
84
|
+
residues every pair of atoms at bonding distance is bonded, singly: the
|
|
85
|
+
peptide bond, a disulfide, the bond of a cap.
|
|
86
|
+
|
|
87
|
+
Returns ``(None, notes)`` when a residue's component is not known here,
|
|
88
|
+
one of its atoms is not an atom of that component, or the file gives an
|
|
89
|
+
atom no element: bond orders are then not invented, and the ligand is
|
|
90
|
+
built from its connectivity alone.
|
|
91
|
+
"""
|
|
92
|
+
notes: list[str] = []
|
|
93
|
+
by_name = {a.name: a for a in atoms if a.element not in ("H", "D")}
|
|
94
|
+
rw = Chem.RWMol()
|
|
95
|
+
index: dict[str, int] = {}
|
|
96
|
+
with quiet_rdkit():
|
|
97
|
+
for res in residues:
|
|
98
|
+
known = KNOWN_COMPONENTS.get(res.name.upper())
|
|
99
|
+
nucleotide = canonical_resname(res.name) in _NUCLEOTIDE_TEMPLATES
|
|
100
|
+
if nucleotide: # under whatever name a force field gives it
|
|
101
|
+
known = KNOWN_COMPONENTS[canonical_resname(res.name)]
|
|
102
|
+
if known is None:
|
|
103
|
+
notes.append(f"{res.name} {res.number} is not a component whose bonds are known here; "
|
|
104
|
+
f"bond orders of the ligand are not known")
|
|
105
|
+
return None, notes
|
|
106
|
+
smiles, names = known
|
|
107
|
+
template = Chem.MolFromSmiles(smiles)
|
|
108
|
+
Chem.Kekulize(template, clearAromaticFlags=True)
|
|
109
|
+
order = {n: k for k, n in enumerate(names)}
|
|
110
|
+
if nucleotide:
|
|
111
|
+
# A file may name the two free oxygens of the phosphate, or the primes of the sugar, its own
|
|
112
|
+
# way. A phosphate that ends a chain has a third oxygen, as a carboxyl end has its OXT.
|
|
113
|
+
given = {canonical_atom(res.name, _local(n)): n for n in res.atoms}
|
|
114
|
+
order = {given[n].split("/", 1)[-1]: k for n, k in order.items() if n in given}
|
|
115
|
+
names = tuple(given[n].split("/", 1)[-1] if n in given else n for n in names)
|
|
116
|
+
present = [n for n in res.atoms if _local(n) in order]
|
|
117
|
+
ends = ("OXT", "OP3", "O3P") if nucleotide else ("OXT",)
|
|
118
|
+
extra = [n for n in res.atoms if _local(n) not in order and _local(n) not in ends]
|
|
119
|
+
if extra:
|
|
120
|
+
notes.append(f"{res.name} {res.number} has an atom {_local(extra[0])} that the component of that "
|
|
121
|
+
f"name does not have; bond orders of the ligand are not known")
|
|
122
|
+
return None, notes
|
|
123
|
+
for n in res.atoms:
|
|
124
|
+
element = by_name[n].element
|
|
125
|
+
try:
|
|
126
|
+
index[n] = rw.AddAtom(Chem.Atom(element[:1].upper() + element[1:].lower()))
|
|
127
|
+
except RuntimeError: # RDKit has no such element: the file does not say what the atom is
|
|
128
|
+
notes.append(f"{res.name} {res.number} has an atom {_local(n)} of no element that is known "
|
|
129
|
+
f"({element!r}); bond orders of the ligand are not known")
|
|
130
|
+
return None, notes
|
|
131
|
+
for n in present:
|
|
132
|
+
atom = template.GetAtomWithIdx(order[_local(n)])
|
|
133
|
+
rw.GetAtomWithIdx(index[n]).SetFormalCharge(atom.GetFormalCharge())
|
|
134
|
+
prefix = res.atoms[0].split("/", 1)[0] + "/" if "/" in res.atoms[0] else ""
|
|
135
|
+
for bond in template.GetBonds():
|
|
136
|
+
a, b = prefix + names[bond.GetBeginAtomIdx()], prefix + names[bond.GetEndAtomIdx()]
|
|
137
|
+
if a in index and b in index:
|
|
138
|
+
rw.AddBond(index[a], index[b], bond.GetBondType())
|
|
139
|
+
if prefix + "OXT" in index and prefix + "C" in index:
|
|
140
|
+
rw.AddBond(index[prefix + "OXT"], index[prefix + "C"], Chem.BondType.SINGLE)
|
|
141
|
+
for third in ("OP3", "O3P") if nucleotide else ():
|
|
142
|
+
if prefix + third in index and prefix + "P" in index:
|
|
143
|
+
rw.AddBond(index[prefix + third], index[prefix + "P"], Chem.BondType.SINGLE)
|
|
144
|
+
owner = {n: k for k, res in enumerate(residues) for n in res.atoms}
|
|
145
|
+
backbone = [{"N", "CA", "C"} <= {_local(n) for n in res.atoms} for res in residues]
|
|
146
|
+
names_in_order = list(index)
|
|
147
|
+
for i, a in enumerate(names_in_order):
|
|
148
|
+
for b in names_in_order[i + 1:]:
|
|
149
|
+
if owner[a] == owner[b]:
|
|
150
|
+
continue
|
|
151
|
+
p, q = by_name[a], by_name[b]
|
|
152
|
+
pair = {p.element.upper(), q.element.upper()}
|
|
153
|
+
d = ((p.x - q.x) ** 2 + (p.y - q.y) ** 2 + (p.z - q.z) ** 2) ** 0.5
|
|
154
|
+
if backbone[owner[a]] and backbone[owner[b]]:
|
|
155
|
+
# Two residues are joined by a peptide (or isopeptide) bond, a disulfide or a thioether,
|
|
156
|
+
# and by nothing else. Atoms of two residues that merely lie on top of each other, as a
|
|
157
|
+
# predicted model has them where it is unsure, are a clash and not a bond.
|
|
158
|
+
if not ((pair == {"C", "N"} and d < 1.7) or (pair == {"S"} and d < 2.3)
|
|
159
|
+
or (pair == {"C", "S"} and d < 1.95)):
|
|
160
|
+
continue
|
|
161
|
+
elif d >= (_COVALENT_BIG if pair & {"S", "P", "SE"} else _COVALENT):
|
|
162
|
+
continue # a cap or the like: whatever it is bonded to, by distance
|
|
163
|
+
rw.AddBond(index[a], index[b], Chem.BondType.SINGLE)
|
|
164
|
+
try:
|
|
165
|
+
mol = rw.GetMol()
|
|
166
|
+
Chem.SanitizeMol(mol)
|
|
167
|
+
except Exception as exc: # a valence that cannot be: the file's atoms do not add up
|
|
168
|
+
notes.append(f"the residues of the chain do not make a molecule ({str(exc).splitlines()[0][:80]}); "
|
|
169
|
+
f"bond orders of the ligand are not known")
|
|
170
|
+
return None, notes
|
|
171
|
+
return Chem.MolToSmiles(mol), notes
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def given_peptide(mol: Chem.Mol, names: Sequence[str], residues: Sequence[ChainResidue]) -> Peptide | None:
|
|
175
|
+
"""The decomposition of a chain ligand as the file gives it.
|
|
176
|
+
|
|
177
|
+
``mol`` and ``names`` are the ligand's molecule and atom names. A residue
|
|
178
|
+
of the file that has a backbone (``N``, ``CA``, ``C``) is a residue of the
|
|
179
|
+
chain, with every atom the file gives it; what has none hangs on the
|
|
180
|
+
residue it is bonded to. ``None`` if no residue has a backbone.
|
|
181
|
+
"""
|
|
182
|
+
idx = {n: i for i, n in enumerate(names)}
|
|
183
|
+
members = [[idx[n] for n in res.atoms if n in idx] for res in residues]
|
|
184
|
+
found: list[tuple[ChainResidue, list[int], dict[str, int]]] = []
|
|
185
|
+
loose: set[int] = set()
|
|
186
|
+
for res, atoms in zip(residues, members):
|
|
187
|
+
local = {_local(names[a]): a for a in atoms}
|
|
188
|
+
sugar = {n.replace("*", "'"): a for n, a in local.items()}
|
|
189
|
+
if {"N", "CA", "C"} <= set(local):
|
|
190
|
+
found.append((res, atoms, local))
|
|
191
|
+
elif {"C1'", "C3'", "C4'"} <= set(sugar):
|
|
192
|
+
# A nucleotide, by its sugar and not by its name: its atoms under the names of the templates.
|
|
193
|
+
found.append((res, atoms, {canonical_atom(canonical_resname(res.name), n) if canonical_resname(res.name)
|
|
194
|
+
in _NUCLEOTIDE_TEMPLATES else n: a for n, a in sugar.items()}))
|
|
195
|
+
else:
|
|
196
|
+
loose.update(atoms)
|
|
197
|
+
# An atom of the ligand that is of no residue of the file: an ion that was taken into the ligand.
|
|
198
|
+
loose.update(set(range(len(names))) - {a for atoms in members for a in atoms})
|
|
199
|
+
if not found:
|
|
200
|
+
return None
|
|
201
|
+
out: list[PeptideResidue] = []
|
|
202
|
+
for k, (res, atoms, local) in enumerate(found, start=1):
|
|
203
|
+
name = res.name.upper()
|
|
204
|
+
if "CA" not in local:
|
|
205
|
+
head = next(local[n] for n in ("P", "O5'", "C5'", "C4'") if n in local)
|
|
206
|
+
tail = local.get("O3'", local["C3'"])
|
|
207
|
+
out.append(PeptideResidue(
|
|
208
|
+
position=k, name=name, atoms=tuple(atoms), atom_names=tuple((a, n) for n, a in local.items()),
|
|
209
|
+
n=head, ca=local["C4'"], c=tail, o=tail, proteinogenic=False, number=res.number, kind="nucleotide"))
|
|
210
|
+
continue
|
|
211
|
+
out.append(PeptideResidue(
|
|
212
|
+
position=k, name=name, atoms=tuple(atoms), atom_names=tuple((a, n) for n, a in local.items()),
|
|
213
|
+
n=local["N"], ca=local["CA"], c=local["C"], o=local.get("O", local["C"]),
|
|
214
|
+
proteinogenic=name in _ONE, stereo=_stereo(mol, local["N"], local["CA"], local["C"]),
|
|
215
|
+
number=res.number))
|
|
216
|
+
owner = {a: r.position for r in out for a in r.atoms}
|
|
217
|
+
|
|
218
|
+
def bonded(a: int, b: int) -> bool:
|
|
219
|
+
return mol.GetBondBetweenAtoms(a, b) is not None
|
|
220
|
+
|
|
221
|
+
breaks = tuple(r.position for r, nxt in zip(out, out[1:]) if not bonded(r.c, nxt.n))
|
|
222
|
+
cyclic = len(out) > 2 and bonded(out[-1].c, out[0].n)
|
|
223
|
+
crosslinks = []
|
|
224
|
+
for bond in mol.GetBonds():
|
|
225
|
+
a, b = bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()
|
|
226
|
+
pa, pb = owner.get(a), owner.get(b)
|
|
227
|
+
if pa is None or pb is None or pa == pb:
|
|
228
|
+
continue
|
|
229
|
+
(pa, a), (pb, b) = sorted(((pa, a), (pb, b)))
|
|
230
|
+
peptide_bond = (pb == pa + 1 and a == out[pa - 1].c and b == out[pb - 1].n)
|
|
231
|
+
if not peptide_bond and {(pa, a), (pb, b)} & {(r.position, r.c) for r in out} \
|
|
232
|
+
and {(pa, a), (pb, b)} & {(r.position, r.n) for r in out} and not cyclic:
|
|
233
|
+
continue # a peptide bond between two residues that are not drawn next to each other
|
|
234
|
+
closing = cyclic and pa == 1 and pb == len(out) and a == out[0].n and b == out[-1].c
|
|
235
|
+
if not peptide_bond and not closing:
|
|
236
|
+
both_s = mol.GetAtomWithIdx(a).GetAtomicNum() == 16 and mol.GetAtomWithIdx(b).GetAtomicNum() == 16
|
|
237
|
+
crosslinks.append(Crosslink((pa, a), (pb, b), "disulfide" if both_s else "bond"))
|
|
238
|
+
# What is no residue: grouped by what is bonded together, each with the bonds that hold it to the chain.
|
|
239
|
+
appendages = []
|
|
240
|
+
left = set(loose)
|
|
241
|
+
while left:
|
|
242
|
+
start = min(left)
|
|
243
|
+
group, queue = {start}, [start]
|
|
244
|
+
while queue:
|
|
245
|
+
for nb in mol.GetAtomWithIdx(queue.pop()).GetNeighbors():
|
|
246
|
+
j = nb.GetIdx()
|
|
247
|
+
if j in left and j not in group:
|
|
248
|
+
group.add(j)
|
|
249
|
+
queue.append(j)
|
|
250
|
+
left -= group
|
|
251
|
+
holds = sorted((owner[nb.GetIdx()], nb.GetIdx(), a) for a in group
|
|
252
|
+
for nb in mol.GetAtomWithIdx(a).GetNeighbors() if nb.GetIdx() in owner)
|
|
253
|
+
appendages.append(Appendage(tuple(sorted(group)), tuple(holds)))
|
|
254
|
+
runs, length = [], 1
|
|
255
|
+
for r in out[:-1]:
|
|
256
|
+
if r.position in breaks:
|
|
257
|
+
runs.append(length)
|
|
258
|
+
length = 1
|
|
259
|
+
else:
|
|
260
|
+
length += 1
|
|
261
|
+
runs.append(length)
|
|
262
|
+
return Peptide(residues=tuple(out), appendages=tuple(appendages), crosslinks=tuple(crosslinks), cyclic=cyclic,
|
|
263
|
+
runs=tuple(sorted(runs, reverse=True)), n_atoms=mol.GetNumAtoms(), numbering="deposited",
|
|
264
|
+
breaks=breaks)
|
bindview/chem.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Small chemistry tables shared across the package."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from contextlib import contextmanager
|
|
6
|
+
from typing import Iterator
|
|
7
|
+
|
|
8
|
+
__all__ = ["METALS", "HALOGENS", "is_metal", "covalent_radius", "quiet_rdkit"]
|
|
9
|
+
|
|
10
|
+
# Elements treated as metals for bond pruning and coordination drawing.
|
|
11
|
+
METALS: frozenset[str] = frozenset({
|
|
12
|
+
"Li", "Na", "K", "Rb", "Cs", "Be", "Mg", "Ca", "Sr", "Ba",
|
|
13
|
+
"Al", "Ga", "In", "Tl", "Sn", "Pb", "Bi",
|
|
14
|
+
"Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn",
|
|
15
|
+
"Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd",
|
|
16
|
+
"Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg",
|
|
17
|
+
"La", "Ce", "Pr", "Nd", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu",
|
|
18
|
+
"U",
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
HALOGENS: frozenset[str] = frozenset({"F", "Cl", "Br", "I"})
|
|
22
|
+
|
|
23
|
+
# Covalent radii in angstrom (Cordero et al. 2008, rounded) for the elements a
|
|
24
|
+
# ligand realistically contains: two atoms that are not farther apart than their
|
|
25
|
+
# radii allow are bound (:mod:`bindview.covalent`).
|
|
26
|
+
_RCOV = {
|
|
27
|
+
"H": 0.31, "B": 0.84, "C": 0.76, "N": 0.71, "O": 0.66, "F": 0.57,
|
|
28
|
+
"Si": 1.11, "P": 1.07, "S": 1.05, "Cl": 1.02, "As": 1.19, "Se": 1.20,
|
|
29
|
+
"Br": 1.20, "I": 1.39,
|
|
30
|
+
"Li": 1.28, "Na": 1.66, "K": 2.03, "Mg": 1.41, "Ca": 1.76,
|
|
31
|
+
"Mn": 1.39, "Fe": 1.32, "Co": 1.26, "Ni": 1.24, "Cu": 1.32, "Zn": 1.22,
|
|
32
|
+
"Mo": 1.54, "Pt": 1.36, "Au": 1.36, "Hg": 1.32, "Cd": 1.44,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def is_metal(element: str) -> bool:
|
|
37
|
+
return element in METALS
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def covalent_radius(element: str) -> float:
|
|
41
|
+
return _RCOV.get(element, 1.20)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@contextmanager
|
|
45
|
+
def quiet_rdkit() -> Iterator[None]:
|
|
46
|
+
"""Silence RDKit's log output for the duration, then restore the caller's setting.
|
|
47
|
+
|
|
48
|
+
Template matching and kekulization probe molecules that RDKit complains
|
|
49
|
+
about on stderr; those complaints are handled here and are noise to a caller.
|
|
50
|
+
"""
|
|
51
|
+
from rdkit import rdBase
|
|
52
|
+
|
|
53
|
+
block = rdBase.BlockLogs()
|
|
54
|
+
try:
|
|
55
|
+
yield
|
|
56
|
+
finally:
|
|
57
|
+
del block
|
bindview/clearance.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""A contact's line, kept off words that are not its own.
|
|
2
|
+
|
|
3
|
+
The layout seats residues so that their lines miss other residues and, where it can, bonds. A line can
|
|
4
|
+
still run over a word: the letter of a ligand atom that lies on its way to a ring centre, the name of a
|
|
5
|
+
residue, the distance written on another line. Where it does, the line is broken around the word, the way
|
|
6
|
+
:func:`bindview.styles.base.link_items` breaks it around its own distance, so the word stays readable on any
|
|
7
|
+
backdrop and nothing needs a patch behind it.
|
|
8
|
+
|
|
9
|
+
A line that ends in a word is pointing at it and is left alone: the line to a residue drawn as a bubble
|
|
10
|
+
runs to the middle of the bubble, under its name. A figure in which no line runs over a word is left
|
|
11
|
+
exactly as it is.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from .scene import Group, Item, Line, Text, text_box
|
|
17
|
+
|
|
18
|
+
__all__ = ["keep_lines_off_words", "INSIDE", "ROOM", "KEPT"]
|
|
19
|
+
|
|
20
|
+
#: A line runs over a word when it passes this far inside the word's box; touching the edge is not it.
|
|
21
|
+
INSIDE = 0.75
|
|
22
|
+
#: What is left free beside and above a word: what a line leaves around its own distance.
|
|
23
|
+
ROOM = (1.5, 1.7)
|
|
24
|
+
#: A line is broken only while this share of it is left; a short line that would all but vanish stays whole.
|
|
25
|
+
#: The share is of the piece, or of all that is drawn of its contact: a line split by its own distance or
|
|
26
|
+
#: tag is two pieces, and the shorter of them can lose most of itself to a word while the line keeps most.
|
|
27
|
+
KEPT = 0.5
|
|
28
|
+
|
|
29
|
+
Box = tuple[float, float, float, float]
|
|
30
|
+
Point = tuple[float, float]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _span(a: Point, b: Point, box: Box) -> "tuple[float, float] | None":
|
|
34
|
+
"""The stretch of ``a``-``b`` inside ``box``, as two shares of its length."""
|
|
35
|
+
dx, dy = b[0] - a[0], b[1] - a[1]
|
|
36
|
+
t0, t1 = 0.0, 1.0
|
|
37
|
+
for p, q in ((-dx, a[0] - box[0]), (dx, box[2] - a[0]), (-dy, a[1] - box[1]), (dy, box[3] - a[1])):
|
|
38
|
+
if abs(p) < 1e-12:
|
|
39
|
+
if q < 0:
|
|
40
|
+
return None
|
|
41
|
+
continue
|
|
42
|
+
r = q / p
|
|
43
|
+
if p < 0:
|
|
44
|
+
t0 = max(t0, r)
|
|
45
|
+
else:
|
|
46
|
+
t1 = min(t1, r)
|
|
47
|
+
return (t0, t1) if t0 < t1 else None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _is_contact(group: Group) -> bool:
|
|
51
|
+
return group.cls.split(" ", 1)[0] == "interaction"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def keep_lines_off_words(content: Group) -> int:
|
|
55
|
+
"""Break the lines of the contacts in ``content`` around the words they run over; how many were broken."""
|
|
56
|
+
words: list[tuple[Box, int]] = [] # box in the coordinates of ``content``, whose it is
|
|
57
|
+
lines: list[tuple[Group, int, Line, float, float, int]] = []
|
|
58
|
+
|
|
59
|
+
def walk(group: Group, dx: float, dy: float, mine: int) -> None:
|
|
60
|
+
for k, it in enumerate(group.items):
|
|
61
|
+
if isinstance(it, Group):
|
|
62
|
+
tx, ty = it.translate or (0.0, 0.0)
|
|
63
|
+
walk(it, dx + tx, dy + ty, id(it) if _is_contact(it) else mine)
|
|
64
|
+
elif isinstance(it, Text):
|
|
65
|
+
if it.content.strip():
|
|
66
|
+
b = text_box(it)
|
|
67
|
+
words.append(((b[0] + dx, b[1] + dy, b[2] + dx, b[3] + dy), mine))
|
|
68
|
+
elif isinstance(it, Line) and mine and it.paint.stroke is not None:
|
|
69
|
+
lines.append((group, k, it, dx, dy, mine))
|
|
70
|
+
|
|
71
|
+
walk(content, 0.0, 0.0, 0)
|
|
72
|
+
broken = 0
|
|
73
|
+
replace: dict[int, dict[int, list[Line]]] = {}
|
|
74
|
+
groups: dict[int, Group] = {}
|
|
75
|
+
plans: list[tuple[Group, int, Line, list[tuple[float, float]], float, int]] = []
|
|
76
|
+
drawn: dict[int, float] = {} # length of every contact's line, all its pieces
|
|
77
|
+
lost: dict[int, float] = {} # ... and what breaking them would take away
|
|
78
|
+
for group, k, line, dx, dy, mine in lines:
|
|
79
|
+
a, b = (line.x1 + dx, line.y1 + dy), (line.x2 + dx, line.y2 + dy)
|
|
80
|
+
length = ((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2) ** 0.5
|
|
81
|
+
drawn[mine] = drawn.get(mine, 0.0) + length
|
|
82
|
+
if length < 1e-6:
|
|
83
|
+
continue
|
|
84
|
+
gaps: list[tuple[float, float]] = []
|
|
85
|
+
for box, whose in words:
|
|
86
|
+
if whose == mine:
|
|
87
|
+
continue
|
|
88
|
+
if _span(a, b, (box[0] + INSIDE, box[1] + INSIDE, box[2] - INSIDE, box[3] - INSIDE)) is None:
|
|
89
|
+
continue
|
|
90
|
+
gap = _span(a, b, (box[0] - ROOM[0], box[1] - ROOM[1], box[2] + ROOM[0], box[3] + ROOM[1]))
|
|
91
|
+
if gap is None or gap[0] <= 1e-9 or gap[1] >= 1.0 - 1e-9:
|
|
92
|
+
continue # it ends at the word: that is pointing at it
|
|
93
|
+
gaps.append(gap)
|
|
94
|
+
if not gaps:
|
|
95
|
+
continue
|
|
96
|
+
gaps.sort()
|
|
97
|
+
left: list[tuple[float, float]] = []
|
|
98
|
+
at = 0.0
|
|
99
|
+
for t0, t1 in gaps:
|
|
100
|
+
if t0 > at:
|
|
101
|
+
left.append((at, t0))
|
|
102
|
+
at = max(at, t1)
|
|
103
|
+
if at < 1.0:
|
|
104
|
+
left.append((at, 1.0))
|
|
105
|
+
left = [(t0, t1) for t0, t1 in left if (t1 - t0) * length > 0.4]
|
|
106
|
+
plans.append((group, k, line, left, length, mine))
|
|
107
|
+
lost[mine] = lost.get(mine, 0.0) + length * (1.0 - sum(t1 - t0 for t0, t1 in left))
|
|
108
|
+
for group, k, line, left, length, mine in plans:
|
|
109
|
+
if sum(t1 - t0 for t0, t1 in left) < KEPT and (not left or 1.0 - lost[mine] / drawn[mine] < KEPT):
|
|
110
|
+
continue # (a piece that would vanish is never broken away)
|
|
111
|
+
pieces = [Line(line.x1 + (line.x2 - line.x1) * t0, line.y1 + (line.y2 - line.y1) * t0,
|
|
112
|
+
line.x1 + (line.x2 - line.x1) * t1, line.y1 + (line.y2 - line.y1) * t1, line.paint)
|
|
113
|
+
for t0, t1 in left]
|
|
114
|
+
replace.setdefault(id(group), {})[k] = pieces
|
|
115
|
+
groups[id(group)] = group
|
|
116
|
+
broken += 1
|
|
117
|
+
for key, by_index in replace.items():
|
|
118
|
+
group = groups[key]
|
|
119
|
+
items: list[Item] = []
|
|
120
|
+
for k, it in enumerate(group.items):
|
|
121
|
+
items.extend(by_index.get(k, [it]))
|
|
122
|
+
group.items = items
|
|
123
|
+
return broken
|