File size: 1,028 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
from __future__ import annotations

from pathlib import Path
from typing import Any, Dict

from Bio.PDB import MMCIFParser, PDBParser


def load_structure(path: str | Path, structure_id: str = "target") -> Any:
    """Load PDB or mmCIF structure object using Biopython."""
    source = Path(path)
    suffix = source.suffix.lower()
    if suffix in {".pdb", ".ent"}:
        parser = PDBParser(QUIET=True)
        return parser.get_structure(structure_id, str(source))
    if suffix in {".cif", ".mmcif"}:
        parser = MMCIFParser(QUIET=True)
        return parser.get_structure(structure_id, str(source))
    raise ValueError(f"Unsupported structure extension: {source}")


def summarize_structure(path: str | Path) -> Dict[str, int]:
    structure = load_structure(path)
    residues = [r for r in structure.get_residues() if r.id[0] == " "]
    atoms = list(structure.get_atoms())
    chains = list(structure.get_chains())
    return {"residue_count": len(residues), "atom_count": len(atoms), "chain_count": len(chains)}