File size: 1,582 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 | from __future__ import annotations
from pathlib import Path
from typing import Dict, Sequence
import numpy as np
from libs.utils.io_pdb import load_structure
def _residue_centroid(residue) -> np.ndarray | None:
coords = [atom.coord for atom in residue.get_atoms()]
if not coords:
return None
return np.mean(np.asarray(coords, dtype=float), axis=0)
def compute_structure_features(structure_path: str | Path, pocket_residues: Sequence[str] | None = None) -> Dict[str, float]:
"""Compute structure-derived summary features from PDB/mmCIF."""
structure = load_structure(structure_path)
residues = [r for r in structure.get_residues() if r.id[0] == " "]
atoms = list(structure.get_atoms())
centroids = []
for residue in residues:
c = _residue_centroid(residue)
if c is not None:
centroids.append(c)
if centroids:
xyz = np.vstack(centroids)
extent = xyz.max(axis=0) - xyz.min(axis=0)
mean_extent = float(np.mean(extent))
else:
mean_extent = 0.0
pocket_count = 0.0
pocket_residue_set = set(pocket_residues or [])
if pocket_residue_set:
for residue in residues:
chain = residue.get_parent().id
idx = residue.id[1]
key = f"{chain}:{idx}"
if key in pocket_residue_set:
pocket_count += 1.0
return {
"residue_count": float(len(residues)),
"atom_count": float(len(atoms)),
"mean_spatial_extent": mean_extent,
"pocket_residue_count": pocket_count,
}
|