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, }