File size: 3,861 Bytes
c289d87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | from __future__ import annotations
from pathlib import Path
from typing import Dict, List, Sequence
import numpy as np
from Bio.PDB.Polypeptide import protein_letters_3to1
from libs.utils.io_pdb import load_structure
from .schemas import LigandGraph, ProteinGraph
def build_ligand_graph(mol) -> LigandGraph:
"""Build atom-level graph from an RDKit Mol object."""
atom_features: List[List[float]] = []
for atom in mol.GetAtoms():
atom_features.append(
[
float(atom.GetAtomicNum()),
float(atom.GetTotalDegree()),
float(atom.GetFormalCharge()),
float(atom.GetIsAromatic()),
float(atom.GetMass()),
]
)
edges: List[List[int]] = []
edge_features: List[List[float]] = []
for bond in mol.GetBonds():
i = bond.GetBeginAtomIdx()
j = bond.GetEndAtomIdx()
btype = float(bond.GetBondTypeAsDouble())
ring = float(bond.IsInRing())
edges.extend([[i, j], [j, i]])
edge_features.extend([[btype, ring], [btype, ring]])
if not edges:
edge_index = np.zeros((2, 0), dtype=int)
edge_attr = np.zeros((0, 2), dtype=float)
else:
edge_index = np.asarray(edges, dtype=int).T
edge_attr = np.asarray(edge_features, dtype=float)
node = np.asarray(atom_features, dtype=float) if atom_features else np.zeros((0, 5), dtype=float)
return LigandGraph(node_features=node, edge_index=edge_index, edge_features=edge_attr)
def _residue_name_to_one_letter(resname: str) -> str:
return protein_letters_3to1.get(resname.upper(), "X")
def build_protein_graph(structure_path: str | Path, distance_threshold: float = 8.0, pocket_residues: Sequence[str] | None = None) -> ProteinGraph:
"""Build residue-level graph with distance-threshold edges."""
structure = load_structure(structure_path)
residues = [r for r in structure.get_residues() if r.id[0] == " "]
coords = []
node_features = []
node_labels: List[str] = []
pocket_set = set(pocket_residues or [])
for residue in residues:
chain = residue.get_parent().id
idx = residue.id[1]
label = f"{chain}:{idx}"
ca = residue["CA"].coord if "CA" in residue else None
if ca is None:
atoms = [atom.coord for atom in residue.get_atoms()]
ca = np.mean(np.asarray(atoms, dtype=float), axis=0) if atoms else np.zeros(3, dtype=float)
aa = _residue_name_to_one_letter(residue.resname)
aa_index = float(ord(aa) - ord("A")) if aa.isalpha() else -1.0
node_features.append(
[
float(idx),
aa_index,
float(label in pocket_set),
]
)
coords.append(np.asarray(ca, dtype=float))
node_labels.append(label)
if not coords:
return ProteinGraph(node_features=np.zeros((0, 3)), edge_index=np.zeros((2, 0), dtype=int), node_labels=[])
xyz = np.vstack(coords)
n = xyz.shape[0]
edges: List[List[int]] = []
for i in range(n):
for j in range(i + 1, n):
dist = float(np.linalg.norm(xyz[i] - xyz[j]))
if dist <= distance_threshold:
edges.extend([[i, j], [j, i]])
edge_index = np.asarray(edges, dtype=int).T if edges else np.zeros((2, 0), dtype=int)
return ProteinGraph(node_features=np.asarray(node_features, dtype=float), edge_index=edge_index, node_labels=node_labels)
def protein_sequence_from_structure(structure_path: str | Path) -> str:
"""Derive a rough sequence by concatenating residue symbols from the first model."""
structure = load_structure(structure_path)
residues = [r for r in structure.get_residues() if r.id[0] == " "]
return "".join(_residue_name_to_one_letter(r.resname) for r in residues)
|