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/peptide.py
ADDED
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
"""Seeing a peptide in a ligand that is stored as one lump of atoms.
|
|
2
|
+
|
|
3
|
+
A co-folded peptide ligand is not a chain of named residues in the structure
|
|
4
|
+
file: it was handed to the predictor as a ligand, so liraglutide is one hetero
|
|
5
|
+
component of 266 atoms with a single residue record. Nothing can be read; the
|
|
6
|
+
decomposition has to be *perceived* from the bonds.
|
|
7
|
+
|
|
8
|
+
1. **Backbone units.** Every ``N-CA-C(=O)`` with an sp3 alpha carbon.
|
|
9
|
+
2. **Runs.** Two units are consecutive when the carbonyl carbon of one is
|
|
10
|
+
bonded to the nitrogen of the next. The longest run is the chain; it is
|
|
11
|
+
walked from its N-terminus (a ring of units from a canonical start).
|
|
12
|
+
3. **Residues.** Each unit's side chain is compared with the 20 proteinogenic
|
|
13
|
+
side chains and a short list of common others. A side chain is recognised
|
|
14
|
+
when its atoms match exactly; more may hang off one of its heteroatoms (an
|
|
15
|
+
acyl group on a lysine nitrogen, a phosphate on a serine oxygen), never off
|
|
16
|
+
a carbon. A unit that matches nothing is ``Xaa`` and keeps its whole side
|
|
17
|
+
chain.
|
|
18
|
+
4. **The rest.** Atoms that belong to no residue are *appendages* (a fatty acid
|
|
19
|
+
on its linker, a terminal cap), each with the bonds that attach it; bonds
|
|
20
|
+
between two residues that are not peptide bonds are *crosslinks*.
|
|
21
|
+
|
|
22
|
+
Whether a molecule with such a chain is *drawn* as one is a separate question,
|
|
23
|
+
answered by :func:`decide`: the rule and the reason for its two numbers are in
|
|
24
|
+
``docs/reference.md``.
|
|
25
|
+
|
|
26
|
+
Positions count from the N-terminus of the chain **as perceived here**. They
|
|
27
|
+
are not sequence-database numbers, and nothing in this module pretends so.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
from dataclasses import dataclass, field
|
|
33
|
+
from typing import Iterable, Sequence
|
|
34
|
+
|
|
35
|
+
from rdkit import Chem
|
|
36
|
+
|
|
37
|
+
from .residues import _NUCLEOTIDES, _TEMPLATES, NUCLEOTIDE_BACKBONE, canonical_resname
|
|
38
|
+
|
|
39
|
+
__all__ = ["PeptideResidue", "Appendage", "Crosslink", "Peptide", "Decision", "perceive_peptide", "decide",
|
|
40
|
+
"decide_given", "MIN_RESIDUES", "MIN_RECOGNISED"]
|
|
41
|
+
|
|
42
|
+
#: A chain is drawn as a chain from this many residues on (see :func:`decide`).
|
|
43
|
+
MIN_RESIDUES = 8
|
|
44
|
+
#: A chain of nucleotides that a file gives is drawn as a chain from this many on: two nucleotides are
|
|
45
|
+
#: about 45 heavy atoms, which the drawing of atoms holds, and three are not.
|
|
46
|
+
MIN_NUCLEOTIDES = 3
|
|
47
|
+
#: ... and only if at least this share of them carries a proteinogenic side chain.
|
|
48
|
+
MIN_RECOGNISED = 0.5
|
|
49
|
+
|
|
50
|
+
_UNIT = Chem.MolFromSmarts("[#7;X3,X4+][#6;X4][#6;X3]=[#8;X1]")
|
|
51
|
+
|
|
52
|
+
_ONE = {"ALA": "A", "ARG": "R", "ASN": "N", "ASP": "D", "CYS": "C", "GLN": "Q", "GLU": "E", "GLY": "G", "HIS": "H",
|
|
53
|
+
"ILE": "I", "LEU": "L", "LYS": "K", "MET": "M", "PHE": "F", "PRO": "P", "SER": "S", "THR": "T", "TRP": "W",
|
|
54
|
+
"TYR": "Y", "VAL": "V"}
|
|
55
|
+
|
|
56
|
+
#: Common residues outside the twenty: named in the drawing, not counted as proteinogenic.
|
|
57
|
+
#: SMILES atom order is N, CA, C, O, then the side chain (as in :mod:`bindview.residues`).
|
|
58
|
+
_OTHERS: tuple[tuple[str, str, tuple[str, ...]], ...] = (
|
|
59
|
+
("AIB", "NC(C=O)(C)C", ("N", "CA", "C", "O", "CB1", "CB2")),
|
|
60
|
+
("ABU", "NC(C=O)CC", ("N", "CA", "C", "O", "CB", "CG")),
|
|
61
|
+
("NVA", "NC(C=O)CCC", ("N", "CA", "C", "O", "CB", "CG", "CD")),
|
|
62
|
+
("NLE", "NC(C=O)CCCC", ("N", "CA", "C", "O", "CB", "CG", "CD", "CE")),
|
|
63
|
+
("ORN", "NC(C=O)CCCN", ("N", "CA", "C", "O", "CB", "CG", "CD", "NE")),
|
|
64
|
+
("DAB", "NC(C=O)CCN", ("N", "CA", "C", "O", "CB", "CG", "ND")),
|
|
65
|
+
("CIT", "NC(C=O)CCCNC(N)=O", ("N", "CA", "C", "O", "CB", "CG", "CD", "NE", "CZ", "NH", "OH")),
|
|
66
|
+
("HYP", "N1C(C=O)CC(O)C1", ("N", "CA", "C", "O", "CB", "CG", "OD", "CD")),
|
|
67
|
+
("PCA", "N1C(C=O)CCC1=O", ("N", "CA", "C", "O", "CB", "CG", "CD", "OE")),
|
|
68
|
+
("HSE", "NC(C=O)CCO", ("N", "CA", "C", "O", "CB", "CG", "OD")),
|
|
69
|
+
("PEN", "NC(C=O)C(C)(C)S", ("N", "CA", "C", "O", "CB", "CG1", "CG2", "SG")),
|
|
70
|
+
("TLE", "NC(C=O)C(C)(C)C", ("N", "CA", "C", "O", "CB", "CG1", "CG2", "CG3")),
|
|
71
|
+
("CHA", "NC(C=O)CC1CCCCC1", ("N", "CA", "C", "O", "CB", "CG", "CD1", "CE1", "CZ", "CE2", "CD2")),
|
|
72
|
+
("PHG", "NC(C=O)c1ccccc1", ("N", "CA", "C", "O", "CB", "CG1", "CD1", "CE", "CD2", "CG2")),
|
|
73
|
+
("HPH", "NC(C=O)CCc1ccccc1", ("N", "CA", "C", "O", "CB", "CG", "CD", "CE1", "CZ1", "CH", "CZ2", "CE2")),
|
|
74
|
+
("NAL", "NC(C=O)Cc1ccc2ccccc2c1",
|
|
75
|
+
("N", "CA", "C", "O", "CB", "CG", "CD1", "CE1", "CZ1", "CH1", "CT1", "CT2", "CH2", "CZ2", "CE2", "CD2")),
|
|
76
|
+
("SEC", "NC(C=O)C[Se]", ("N", "CA", "C", "O", "CB", "SE")),
|
|
77
|
+
("MSE", "NC(C=O)CC[Se]C", ("N", "CA", "C", "O", "CB", "CG", "SE", "CE")),
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _title(name: str) -> str:
|
|
82
|
+
return name[0] + name[1:].lower()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass(frozen=True)
|
|
86
|
+
class _Template:
|
|
87
|
+
name: str
|
|
88
|
+
atoms: tuple[str, ...]
|
|
89
|
+
query: Chem.Mol
|
|
90
|
+
degrees: tuple[int, ...] # heavy-atom degree of every template atom
|
|
91
|
+
carbon: tuple[bool, ...]
|
|
92
|
+
proteinogenic: bool
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _query(smiles: str) -> tuple[Chem.Mol, tuple[int, ...], tuple[bool, ...]]:
|
|
96
|
+
"""A substructure query that fixes elements, aromaticity and the geometry of carbons, and no bond order.
|
|
97
|
+
|
|
98
|
+
Bond orders are left open because a guanidine, a carboxylate or an
|
|
99
|
+
imidazole is written with its double bond in different places by different
|
|
100
|
+
sources; that a carbon is sp3, sp2 or aromatic is what tells cyclohexyl-
|
|
101
|
+
alanine from phenylalanine.
|
|
102
|
+
"""
|
|
103
|
+
t = Chem.MolFromSmiles(smiles)
|
|
104
|
+
q = Chem.RWMol()
|
|
105
|
+
for a in t.GetAtoms():
|
|
106
|
+
if a.GetAtomicNum() == 6 and not a.GetIsAromatic():
|
|
107
|
+
smarts = f"[#6;A;X{a.GetTotalDegree()}]"
|
|
108
|
+
elif a.GetIsAromatic():
|
|
109
|
+
smarts = f"[#{a.GetAtomicNum()};a]"
|
|
110
|
+
else:
|
|
111
|
+
smarts = f"[#{a.GetAtomicNum()};A]"
|
|
112
|
+
q.AddAtom(Chem.AtomFromSmarts(smarts))
|
|
113
|
+
for b in t.GetBonds():
|
|
114
|
+
q.AddBond(b.GetBeginAtomIdx(), b.GetEndAtomIdx(), Chem.BondType.SINGLE)
|
|
115
|
+
q.ReplaceBond(q.GetNumBonds() - 1, Chem.BondFromSmarts("~"))
|
|
116
|
+
return q.GetMol(), tuple(a.GetDegree() for a in t.GetAtoms()), tuple(a.GetAtomicNum() == 6 for a in t.GetAtoms())
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _templates() -> tuple[_Template, ...]:
|
|
120
|
+
out = []
|
|
121
|
+
for t in _TEMPLATES.values():
|
|
122
|
+
q, deg, carbon = _query(t.smiles)
|
|
123
|
+
out.append(_Template(t.name, t.atoms, q, deg, carbon, True))
|
|
124
|
+
for name, smiles, atoms in _OTHERS:
|
|
125
|
+
q, deg, carbon = _query(smiles)
|
|
126
|
+
out.append(_Template(name, atoms, q, deg, carbon, False))
|
|
127
|
+
# Largest first: where two templates fit a unit exactly, the larger says more.
|
|
128
|
+
return tuple(sorted(out, key=lambda t: (-len(t.atoms), t.name)))
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
_ALL = _templates()
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@dataclass
|
|
135
|
+
class _Found:
|
|
136
|
+
"""A unit of the chain while its atoms are being collected."""
|
|
137
|
+
|
|
138
|
+
n: int
|
|
139
|
+
ca: int
|
|
140
|
+
c: int
|
|
141
|
+
o: int
|
|
142
|
+
template: _Template | None = None
|
|
143
|
+
atoms: list[int] = field(default_factory=list)
|
|
144
|
+
names: dict[int, str] = field(default_factory=dict)
|
|
145
|
+
n_methyl: bool = False
|
|
146
|
+
n_substituted: bool = False
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def name(self) -> str:
|
|
150
|
+
return self.template.name if self.template is not None else "XAA"
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@dataclass(frozen=True)
|
|
154
|
+
class PeptideResidue:
|
|
155
|
+
"""One residue of the chain, as perceived."""
|
|
156
|
+
|
|
157
|
+
position: int # 1-based, from the N-terminus of the chain as perceived
|
|
158
|
+
name: str # "LYS", "AIB", "XAA"
|
|
159
|
+
atoms: tuple[int, ...] # ligand atom indices that belong to it
|
|
160
|
+
atom_names: tuple[tuple[int, str], ...] # (atom index, name within the residue) where it is known
|
|
161
|
+
n: int
|
|
162
|
+
ca: int
|
|
163
|
+
c: int
|
|
164
|
+
o: int
|
|
165
|
+
proteinogenic: bool
|
|
166
|
+
stereo: str | None = None # "L" | "D" | None (glycine, or not determinable)
|
|
167
|
+
n_methyl: bool = False
|
|
168
|
+
#: The residue's number in the structure file, where the chain was given by the file ("63", "63A").
|
|
169
|
+
#: Empty for a perceived residue, which has only its position.
|
|
170
|
+
number: str = ""
|
|
171
|
+
#: ``"amino"``, or ``"nucleotide"`` for a residue of RNA or DNA. Of a nucleotide ``n`` is the atom its
|
|
172
|
+
#: chain comes in by (the phosphorus), ``c`` the one it goes on by (``O3'``), and ``ca`` its ``C4'``.
|
|
173
|
+
kind: str = "amino"
|
|
174
|
+
|
|
175
|
+
@property
|
|
176
|
+
def known(self) -> bool:
|
|
177
|
+
return self.name != "XAA"
|
|
178
|
+
|
|
179
|
+
@property
|
|
180
|
+
def shown(self) -> str:
|
|
181
|
+
"""The number printed with the residue: the deposited one, or else its position in the chain."""
|
|
182
|
+
return self.number or str(self.position)
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
def code3(self) -> str:
|
|
186
|
+
"""Three-letter code as it is printed: ``Lys``, ``Aib``, ``Xaa``; a D-residue is ``D-Phe``.
|
|
187
|
+
A nucleotide keeps its capitals and is named as the templates name it: ``G``, ``DA``."""
|
|
188
|
+
if self.kind == "nucleotide":
|
|
189
|
+
return canonical_resname(self.name)
|
|
190
|
+
base = _title(self.name)
|
|
191
|
+
if self.n_methyl:
|
|
192
|
+
base = "Me" + base
|
|
193
|
+
return ("D-" + base) if self.stereo == "D" else base
|
|
194
|
+
|
|
195
|
+
@property
|
|
196
|
+
def code1(self) -> str:
|
|
197
|
+
"""One-letter code: lower case for a D-residue, ``X`` outside the twenty. Of a nucleotide its
|
|
198
|
+
base, ``N`` for one that is not among the eight."""
|
|
199
|
+
if self.kind == "nucleotide":
|
|
200
|
+
return _NUCLEOTIDES.get(canonical_resname(self.name), "N")
|
|
201
|
+
letter = _ONE.get(self.name, "X")
|
|
202
|
+
return letter.lower() if self.stereo == "D" and letter != "X" else letter
|
|
203
|
+
|
|
204
|
+
def name_of(self, atom: int) -> str | None:
|
|
205
|
+
for idx, name in self.atom_names:
|
|
206
|
+
if idx == atom:
|
|
207
|
+
return name
|
|
208
|
+
return None
|
|
209
|
+
|
|
210
|
+
def is_backbone(self, atom: int) -> bool:
|
|
211
|
+
if self.kind == "nucleotide":
|
|
212
|
+
return self.name_of(atom) in NUCLEOTIDE_BACKBONE + ("OP3",)
|
|
213
|
+
return self.name_of(atom) in ("N", "CA", "C", "O", "OXT")
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
@dataclass(frozen=True)
|
|
217
|
+
class Appendage:
|
|
218
|
+
"""Atoms that are in no residue, and the bonds that attach them to the chain."""
|
|
219
|
+
|
|
220
|
+
atoms: tuple[int, ...]
|
|
221
|
+
#: ``(position of the residue, atom of the residue, atom of the appendage)`` per attaching bond
|
|
222
|
+
attachments: tuple[tuple[int, int, int], ...]
|
|
223
|
+
|
|
224
|
+
@property
|
|
225
|
+
def positions(self) -> tuple[int, ...]:
|
|
226
|
+
return tuple(sorted({a[0] for a in self.attachments}))
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
@dataclass(frozen=True)
|
|
230
|
+
class Crosslink:
|
|
231
|
+
"""A bond between two residues of the chain that is not a peptide bond."""
|
|
232
|
+
|
|
233
|
+
a: tuple[int, int] # (position, atom)
|
|
234
|
+
b: tuple[int, int]
|
|
235
|
+
kind: str # "disulfide" | "bond"
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@dataclass(frozen=True)
|
|
239
|
+
class Peptide:
|
|
240
|
+
residues: tuple[PeptideResidue, ...]
|
|
241
|
+
appendages: tuple[Appendage, ...] = ()
|
|
242
|
+
crosslinks: tuple[Crosslink, ...] = ()
|
|
243
|
+
cyclic: bool = False # the chain closes on itself head to tail
|
|
244
|
+
runs: tuple[int, ...] = () # the length of every run of units in the molecule, longest first
|
|
245
|
+
n_atoms: int = 0
|
|
246
|
+
#: Whose the numbers of the residues are: ``"derived"`` -- a count from the N-terminus of a chain
|
|
247
|
+
#: that was perceived -- or ``"deposited"``, read from the structure file with the chain itself.
|
|
248
|
+
numbering: str = "derived"
|
|
249
|
+
#: Positions after which a chain given by a file is not continuous (residues missing from the model).
|
|
250
|
+
breaks: tuple[int, ...] = ()
|
|
251
|
+
#: Does the chain go on before the first and after the last residue that is drawn? True for the
|
|
252
|
+
#: spine of an interface that starts in the middle of a chain: no "N" or "C" is printed there.
|
|
253
|
+
open_ends: tuple[bool, bool] = (False, False)
|
|
254
|
+
#: Draw one line per pair of residues and kind of contact (the spine of an interface).
|
|
255
|
+
pairwise: bool = False
|
|
256
|
+
#: What the chain is called where a tooltip names it: a ligand, or one side of an interface.
|
|
257
|
+
called: str = "the ligand"
|
|
258
|
+
#: Do the residues stand in the order of the sequence? False for the spine of an interface that is
|
|
259
|
+
#: ordered along the interface: then nothing joins two boxes unless they are neighbours in the chain.
|
|
260
|
+
sequential: bool = True
|
|
261
|
+
#: How much of a residue that makes a directional contact is drawn beside its box: ``None`` (nothing),
|
|
262
|
+
#: or one of :data:`bindview.fragments.DETAILS`.
|
|
263
|
+
detail: "str | None" = None
|
|
264
|
+
_owner: dict[int, int] = field(default_factory=dict, repr=False, compare=False)
|
|
265
|
+
|
|
266
|
+
def __post_init__(self) -> None:
|
|
267
|
+
owner = {a: r.position for r in self.residues for a in r.atoms}
|
|
268
|
+
object.__setattr__(self, "_owner", owner)
|
|
269
|
+
|
|
270
|
+
def __len__(self) -> int:
|
|
271
|
+
return len(self.residues)
|
|
272
|
+
|
|
273
|
+
def residue(self, position: int) -> PeptideResidue:
|
|
274
|
+
"""The residue at a position, counted from 1."""
|
|
275
|
+
return self.residues[position - 1]
|
|
276
|
+
|
|
277
|
+
def residue_of(self, atom: int) -> PeptideResidue | None:
|
|
278
|
+
"""The residue an atom belongs to, or ``None`` for an atom of an appendage."""
|
|
279
|
+
pos = self._owner.get(atom)
|
|
280
|
+
return None if pos is None else self.residues[pos - 1]
|
|
281
|
+
|
|
282
|
+
def appendage_of(self, atom: int) -> Appendage | None:
|
|
283
|
+
"""What is bonded to the chain and is no residue of it, at this atom."""
|
|
284
|
+
for app in self.appendages:
|
|
285
|
+
if atom in app.atoms:
|
|
286
|
+
return app
|
|
287
|
+
return None
|
|
288
|
+
|
|
289
|
+
@property
|
|
290
|
+
def sequence(self) -> str:
|
|
291
|
+
"""One-letter codes from the N-terminus; ``X`` for what is not one of the twenty."""
|
|
292
|
+
return "".join(r.code1 for r in self.residues)
|
|
293
|
+
|
|
294
|
+
@property
|
|
295
|
+
def recognised(self) -> int:
|
|
296
|
+
"""The residues whose side chain is known."""
|
|
297
|
+
return sum(1 for r in self.residues if r.proteinogenic)
|
|
298
|
+
|
|
299
|
+
@property
|
|
300
|
+
def in_chain(self) -> int:
|
|
301
|
+
"""Heavy atoms that belong to a residue of the chain."""
|
|
302
|
+
return len(self._owner)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
@dataclass(frozen=True)
|
|
306
|
+
class Decision:
|
|
307
|
+
"""Whether a ligand is drawn as a chain, and why -- the why is printed, so it is a sentence."""
|
|
308
|
+
|
|
309
|
+
polymer: bool
|
|
310
|
+
reason: str
|
|
311
|
+
longest_run: int = 0
|
|
312
|
+
recognised: int = 0
|
|
313
|
+
#: What the ligand is by the rules of :func:`decide` and :func:`decide_given`, when the caller
|
|
314
|
+
#: asked for it to be drawn otherwise (``ligand_as``). ``None``: the same as ``polymer``.
|
|
315
|
+
found: bool | None = None
|
|
316
|
+
|
|
317
|
+
@property
|
|
318
|
+
def is_chain(self) -> bool:
|
|
319
|
+
"""Is the ligand a chain of residues by what it is, however it is drawn?"""
|
|
320
|
+
return self.polymer if self.found is None else self.found
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
# ---------------------------------------------------------------------------
|
|
324
|
+
# Perception
|
|
325
|
+
# ---------------------------------------------------------------------------
|
|
326
|
+
|
|
327
|
+
def _units(mol: Chem.Mol) -> list[tuple[int, int, int, int]]:
|
|
328
|
+
"""Every backbone unit as ``(N, CA, C, O)``, one per alpha carbon."""
|
|
329
|
+
seen: dict[int, tuple[int, int, int, int]] = {}
|
|
330
|
+
for n, ca, c, o in sorted(mol.GetSubstructMatches(_UNIT, uniquify=True, maxMatches=1000000)):
|
|
331
|
+
seen.setdefault(ca, (n, ca, c, o))
|
|
332
|
+
return sorted(seen.values())
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def _runs(mol: Chem.Mol, units: Sequence[tuple[int, int, int, int]]) -> list[tuple[list[int], bool]]:
|
|
336
|
+
"""Runs of consecutive units, as lists of indices into ``units``, each with whether it is a ring."""
|
|
337
|
+
by_n: dict[int, int] = {}
|
|
338
|
+
for i, u in enumerate(units):
|
|
339
|
+
by_n.setdefault(u[0], i)
|
|
340
|
+
nxt: dict[int, int] = {}
|
|
341
|
+
prev: dict[int, int] = {}
|
|
342
|
+
for i, (_n, _ca, c, _o) in enumerate(units):
|
|
343
|
+
for nb in mol.GetAtomWithIdx(c).GetNeighbors():
|
|
344
|
+
j = by_n.get(nb.GetIdx())
|
|
345
|
+
if j is not None and j != i and j not in prev and i not in nxt:
|
|
346
|
+
nxt[i], prev[j] = j, i
|
|
347
|
+
runs: list[tuple[list[int], bool]] = []
|
|
348
|
+
used: set[int] = set()
|
|
349
|
+
for i in range(len(units)): # open runs, from their first unit
|
|
350
|
+
if i in prev or i in used:
|
|
351
|
+
continue
|
|
352
|
+
run = [i]
|
|
353
|
+
while run[-1] in nxt:
|
|
354
|
+
run.append(nxt[run[-1]])
|
|
355
|
+
used.update(run)
|
|
356
|
+
runs.append((run, False))
|
|
357
|
+
for i in range(len(units)): # what is left closes on itself
|
|
358
|
+
if i in used:
|
|
359
|
+
continue
|
|
360
|
+
run = [i]
|
|
361
|
+
while nxt[run[-1]] != i:
|
|
362
|
+
run.append(nxt[run[-1]])
|
|
363
|
+
used.update(run)
|
|
364
|
+
runs.append((run, True))
|
|
365
|
+
runs.sort(key=lambda r: (-len(r[0]), min(r[0])))
|
|
366
|
+
return runs
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def _matches(mol: Chem.Mol) -> dict[int, list[tuple[_Template, tuple[int, ...]]]]:
|
|
370
|
+
"""For every alpha carbon: the templates whose atoms are there exactly, with the atoms they map to."""
|
|
371
|
+
out: dict[int, list[tuple[_Template, tuple[int, ...]]]] = {}
|
|
372
|
+
for t in _ALL:
|
|
373
|
+
best: dict[int, tuple[int, ...]] = {}
|
|
374
|
+
for m in mol.GetSubstructMatches(t.query, uniquify=False, maxMatches=1000000):
|
|
375
|
+
ok = True
|
|
376
|
+
for k, a in enumerate(m):
|
|
377
|
+
name = t.atoms[k]
|
|
378
|
+
if name in ("N", "C", "O"):
|
|
379
|
+
continue
|
|
380
|
+
atom = mol.GetAtomWithIdx(a)
|
|
381
|
+
if t.carbon[k] and atom.GetDegree() != t.degrees[k]:
|
|
382
|
+
ok = False # something hangs off a carbon: not this residue
|
|
383
|
+
break
|
|
384
|
+
if not t.carbon[k] and atom.GetDegree() < t.degrees[k]:
|
|
385
|
+
ok = False
|
|
386
|
+
break
|
|
387
|
+
if ok and (m[1] not in best or m < best[m[1]]):
|
|
388
|
+
best[m[1]] = m
|
|
389
|
+
for ca, m in best.items():
|
|
390
|
+
out.setdefault(ca, []).append((t, m))
|
|
391
|
+
return out
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def _stereo(mol: Chem.Mol, n: int, ca: int, c: int) -> str | None:
|
|
395
|
+
"""L or D at an alpha carbon, from its CIP label; ``None`` where there is none."""
|
|
396
|
+
atom = mol.GetAtomWithIdx(ca)
|
|
397
|
+
if not atom.HasProp("_CIPCode"):
|
|
398
|
+
return None
|
|
399
|
+
label = atom.GetProp("_CIPCode")
|
|
400
|
+
side = [nb for nb in atom.GetNeighbors() if nb.GetIdx() not in (n, c)]
|
|
401
|
+
if len(side) != 1:
|
|
402
|
+
return None # glycine, or two side chains (Aib): no L or D
|
|
403
|
+
# L is S unless the side chain outranks the carboxyl carbon, which it does only through an
|
|
404
|
+
# atom heavier than oxygen on the beta carbon (cysteine and its like).
|
|
405
|
+
heavy = max((x.GetAtomicNum() for x in side[0].GetNeighbors() if x.GetIdx() != ca), default=0)
|
|
406
|
+
l_label = "R" if heavy > 8 else "S"
|
|
407
|
+
return "L" if label == l_label else "D"
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def perceive_peptide(mol: Chem.Mol, stereo_from: Chem.Mol | None = None) -> Peptide | None:
|
|
411
|
+
"""The peptide chain in a molecule, or ``None`` if it has no two consecutive backbone units.
|
|
412
|
+
|
|
413
|
+
``stereo_from`` is a molecule with trustworthy stereochemistry (the SMILES
|
|
414
|
+
the ligand was specified with): L and D are then read from it, residue by
|
|
415
|
+
residue, and not from a model that may have inverted a centre.
|
|
416
|
+
"""
|
|
417
|
+
units = _units(mol)
|
|
418
|
+
if not units:
|
|
419
|
+
return None
|
|
420
|
+
runs = _runs(mol, units)
|
|
421
|
+
lengths = tuple(len(r) for r, _ in runs)
|
|
422
|
+
order, cyclic = runs[0]
|
|
423
|
+
if len(order) < 2:
|
|
424
|
+
return None
|
|
425
|
+
exact = _matches(mol)
|
|
426
|
+
chain = [units[i] for i in order]
|
|
427
|
+
backbone = {a for u in chain for a in u}
|
|
428
|
+
|
|
429
|
+
def assign(start: int) -> tuple[list[_Found], set[int]]:
|
|
430
|
+
seq = chain[start:] + chain[:start]
|
|
431
|
+
taken: set[int] = set(backbone)
|
|
432
|
+
found: list[_Found] = []
|
|
433
|
+
for n, ca, c, o in seq:
|
|
434
|
+
fits = [(t, m) for t, m in exact.get(ca, []) if m[0] == n and m[2] == c]
|
|
435
|
+
entry = _Found(n, ca, c, o, atoms=[n, ca, c, o], names={n: "N", ca: "CA", c: "C", o: "O"})
|
|
436
|
+
if fits:
|
|
437
|
+
entry.template, match = fits[0]
|
|
438
|
+
for atom_name, a in zip(entry.template.atoms, match):
|
|
439
|
+
entry.names[a] = atom_name
|
|
440
|
+
if a not in entry.atoms:
|
|
441
|
+
entry.atoms.append(a)
|
|
442
|
+
found.append(entry)
|
|
443
|
+
taken.update(entry.atoms)
|
|
444
|
+
return found, taken
|
|
445
|
+
|
|
446
|
+
start = 0
|
|
447
|
+
if cyclic:
|
|
448
|
+
# No terminus to start from: take the rotation whose sequence reads smallest, so that the
|
|
449
|
+
# numbering does not depend on the order of the atoms in the file.
|
|
450
|
+
probe, _ = assign(0)
|
|
451
|
+
names = [e.name for e in probe]
|
|
452
|
+
start = min(range(len(names)), key=lambda s: (names[s:] + names[:s], s))
|
|
453
|
+
found, taken = assign(start)
|
|
454
|
+
n_res = len(found)
|
|
455
|
+
|
|
456
|
+
# Terminal atoms, N-methyls, and the side chains of units that match nothing.
|
|
457
|
+
for k, e in enumerate(found):
|
|
458
|
+
for nb in mol.GetAtomWithIdx(e.c).GetNeighbors():
|
|
459
|
+
i = nb.GetIdx()
|
|
460
|
+
if i in taken or nb.GetAtomicNum() != 8:
|
|
461
|
+
continue
|
|
462
|
+
e.atoms.append(i) # the second oxygen of a C-terminal acid (or ester)
|
|
463
|
+
e.names[i] = "OXT"
|
|
464
|
+
taken.add(i)
|
|
465
|
+
extra = [nb for nb in mol.GetAtomWithIdx(e.n).GetNeighbors() if nb.GetIdx() not in taken]
|
|
466
|
+
if len(extra) == 1 and extra[0].GetAtomicNum() == 6 and extra[0].GetDegree() == 1 and (k > 0 or cyclic):
|
|
467
|
+
e.atoms.append(extra[0].GetIdx())
|
|
468
|
+
e.names[extra[0].GetIdx()] = "CN"
|
|
469
|
+
e.n_methyl = True
|
|
470
|
+
taken.add(extra[0].GetIdx())
|
|
471
|
+
elif extra and (k > 0 or cyclic):
|
|
472
|
+
e.n_substituted = True # a side chain on the nitrogen: a peptoid unit
|
|
473
|
+
for e in found:
|
|
474
|
+
if e.template is not None:
|
|
475
|
+
continue
|
|
476
|
+
frontier = [nb.GetIdx() for nb in mol.GetAtomWithIdx(e.ca).GetNeighbors() if nb.GetIdx() not in taken]
|
|
477
|
+
while frontier:
|
|
478
|
+
i = frontier.pop()
|
|
479
|
+
if i in taken:
|
|
480
|
+
continue
|
|
481
|
+
taken.add(i)
|
|
482
|
+
e.atoms.append(i)
|
|
483
|
+
frontier.extend(nb.GetIdx() for nb in mol.GetAtomWithIdx(i).GetNeighbors() if nb.GetIdx() not in taken)
|
|
484
|
+
|
|
485
|
+
# Stereo, read where it can be trusted.
|
|
486
|
+
reference: list[str | None] | None = None
|
|
487
|
+
if stereo_from is not None:
|
|
488
|
+
ref = perceive_peptide(stereo_from)
|
|
489
|
+
if ref is not None and len(ref) == n_res and [r.name for r in ref.residues] == [
|
|
490
|
+
"XAA" if e.n_substituted else e.name for e in found]:
|
|
491
|
+
reference = [r.stereo for r in ref.residues]
|
|
492
|
+
if reference is None:
|
|
493
|
+
Chem.AssignStereochemistry(mol, cleanIt=False, force=True)
|
|
494
|
+
|
|
495
|
+
owner: dict[int, int] = {}
|
|
496
|
+
residues: list[PeptideResidue] = []
|
|
497
|
+
for k, e in enumerate(found):
|
|
498
|
+
recognised = e.template is not None and not e.n_substituted
|
|
499
|
+
stereo = reference[k] if reference is not None else _stereo(mol, e.n, e.ca, e.c)
|
|
500
|
+
residues.append(PeptideResidue(
|
|
501
|
+
position=k + 1, name=e.name if recognised else "XAA", atoms=tuple(sorted(e.atoms)),
|
|
502
|
+
atom_names=tuple(sorted(e.names.items())), n=e.n, ca=e.ca, c=e.c, o=e.o,
|
|
503
|
+
proteinogenic=bool(recognised and e.template is not None and e.template.proteinogenic),
|
|
504
|
+
stereo=stereo, n_methyl=e.n_methyl))
|
|
505
|
+
for a in e.atoms:
|
|
506
|
+
owner[a] = k + 1
|
|
507
|
+
|
|
508
|
+
# Appendages: what is left, in connected pieces, with the bonds that hold each.
|
|
509
|
+
rest = [a.GetIdx() for a in mol.GetAtoms() if a.GetIdx() not in owner]
|
|
510
|
+
appendages: list[Appendage] = []
|
|
511
|
+
seen: set[int] = set()
|
|
512
|
+
for a0 in rest:
|
|
513
|
+
if a0 in seen:
|
|
514
|
+
continue
|
|
515
|
+
piece, frontier = [], [a0]
|
|
516
|
+
attach: list[tuple[int, int, int]] = []
|
|
517
|
+
while frontier:
|
|
518
|
+
i = frontier.pop()
|
|
519
|
+
if i in seen:
|
|
520
|
+
continue
|
|
521
|
+
seen.add(i)
|
|
522
|
+
piece.append(i)
|
|
523
|
+
for nb in mol.GetAtomWithIdx(i).GetNeighbors():
|
|
524
|
+
j = nb.GetIdx()
|
|
525
|
+
if j in owner:
|
|
526
|
+
attach.append((owner[j], j, i))
|
|
527
|
+
elif j not in seen:
|
|
528
|
+
frontier.append(j)
|
|
529
|
+
appendages.append(Appendage(tuple(sorted(piece)), tuple(sorted(attach))))
|
|
530
|
+
|
|
531
|
+
# Crosslinks: bonds between two residues other than the peptide bonds along the chain.
|
|
532
|
+
crosslinks: list[Crosslink] = []
|
|
533
|
+
for b in mol.GetBonds():
|
|
534
|
+
i, j = b.GetBeginAtomIdx(), b.GetEndAtomIdx()
|
|
535
|
+
pi, pj = owner.get(i), owner.get(j)
|
|
536
|
+
if pi is None or pj is None or pi == pj:
|
|
537
|
+
continue
|
|
538
|
+
ri, rj = residues[pi - 1], residues[pj - 1]
|
|
539
|
+
peptide_bond = (i == ri.c and j == rj.n) or (j == rj.c and i == ri.n) or (i == ri.n and j == rj.c) or (
|
|
540
|
+
j == rj.n and i == ri.c)
|
|
541
|
+
if peptide_bond and (abs(pi - pj) == 1 or (cyclic and {pi, pj} == {1, n_res})):
|
|
542
|
+
continue
|
|
543
|
+
both_s = mol.GetAtomWithIdx(i).GetAtomicNum() == 16 and mol.GetAtomWithIdx(j).GetAtomicNum() == 16
|
|
544
|
+
lo, hi = sorted(((pi, i), (pj, j)))
|
|
545
|
+
crosslinks.append(Crosslink(lo, hi, "disulfide" if both_s else "bond"))
|
|
546
|
+
|
|
547
|
+
return Peptide(residues=tuple(residues), appendages=tuple(appendages),
|
|
548
|
+
crosslinks=tuple(sorted(crosslinks, key=lambda x: (x.a, x.b))), cyclic=cyclic, runs=lengths,
|
|
549
|
+
n_atoms=mol.GetNumAtoms())
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def decide(peptide: Peptide | None) -> Decision:
|
|
553
|
+
"""Is this ligand drawn as a chain of residues? By what it is, not by how big it is.
|
|
554
|
+
|
|
555
|
+
It is, on two conditions:
|
|
556
|
+
|
|
557
|
+
* a run of at least :data:`MIN_RESIDUES` backbone units. The atom-level
|
|
558
|
+
drawing is known to work up to about 55 heavy atoms, which is seven
|
|
559
|
+
residues; below eight a peptide loses nothing by being drawn as atoms,
|
|
560
|
+
and a peptidomimetic of four units drawn as beads would lose the atoms
|
|
561
|
+
that matter;
|
|
562
|
+
* at least :data:`MIN_RECOGNISED` of them with a proteinogenic side chain.
|
|
563
|
+
A row of ``Xaa`` says nothing.
|
|
564
|
+
"""
|
|
565
|
+
if peptide is None:
|
|
566
|
+
return Decision(False, "it has no chain of amino-acid residues")
|
|
567
|
+
n, known = len(peptide), peptide.recognised
|
|
568
|
+
if n < MIN_RESIDUES:
|
|
569
|
+
return Decision(False, f"its longest chain of amino-acid residues has {n}, fewer than {MIN_RESIDUES}", n, known)
|
|
570
|
+
if known < MIN_RECOGNISED * n:
|
|
571
|
+
return Decision(False, f"only {known} of the {n} residues in its chain carry a proteinogenic side chain", n,
|
|
572
|
+
known)
|
|
573
|
+
return Decision(True, f"it has a chain of {n} amino-acid residues, {known} of them proteinogenic", n, known)
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
def decide_given(peptide: Peptide | None) -> Decision:
|
|
577
|
+
"""The same question for a chain that the structure file gives as a chain of its own.
|
|
578
|
+
|
|
579
|
+
Only the first condition of :func:`decide` is asked. The second guards
|
|
580
|
+
against a row of ``Xaa``, and a residue given by a file has a name.
|
|
581
|
+
"""
|
|
582
|
+
if peptide is None:
|
|
583
|
+
return Decision(False, "its chain has no residue with a backbone")
|
|
584
|
+
n, known = len(peptide), peptide.recognised
|
|
585
|
+
if all(r.kind == "nucleotide" for r in peptide.residues):
|
|
586
|
+
if n < MIN_NUCLEOTIDES:
|
|
587
|
+
return Decision(False, f"its chain has {n} nucleotides in the structure file, fewer than "
|
|
588
|
+
f"{MIN_NUCLEOTIDES}", n, known)
|
|
589
|
+
return Decision(True, f"it is a chain of {n} nucleotides in the structure file", n, known)
|
|
590
|
+
if n < MIN_RESIDUES:
|
|
591
|
+
return Decision(False, f"its chain has {n} residues in the structure file, fewer than {MIN_RESIDUES}", n,
|
|
592
|
+
known)
|
|
593
|
+
return Decision(True, f"it is a chain of {n} residues in the structure file, {known} of them proteinogenic", n,
|
|
594
|
+
known)
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def describe(peptide: Peptide, names: Iterable[str] | None = None) -> str:
|
|
598
|
+
"""A few lines of plain text: sequence, appendages, crosslinks. For logs and for reading a result."""
|
|
599
|
+
label = list(names) if names is not None else None
|
|
600
|
+
lines = [f"{len(peptide)} residues{' (cyclic)' if peptide.cyclic else ''}: "
|
|
601
|
+
+ "-".join(r.code3 for r in peptide.residues)]
|
|
602
|
+
for app in peptide.appendages:
|
|
603
|
+
where = ", ".join(f"{peptide.residue(p).code3}{peptide.residue(p).shown} "
|
|
604
|
+
f"{peptide.residue(p).name_of(a) or (label[a] if label else a)}"
|
|
605
|
+
for p, a, _ in app.attachments)
|
|
606
|
+
lines.append(f"appendage of {len(app.atoms)} atoms on {where}")
|
|
607
|
+
for x in peptide.crosslinks:
|
|
608
|
+
lines.append(f"{x.kind} between {peptide.residue(x.a[0]).code3}{peptide.residue(x.a[0]).shown} and "
|
|
609
|
+
f"{peptide.residue(x.b[0]).code3}{peptide.residue(x.b[0]).shown}")
|
|
610
|
+
return "\n".join(lines)
|