# Copyright 2026 Emile de Bruyn # SPDX-License-Identifier: Apache-2.0 from pathlib import Path from Bio.PDB import PDBParser from motifbench_prosite.normalisation import normalise_motif_pdb def _write_tiny_pdb(path: Path, chain_id: str = "L", n_res: int = 5, resnum_start: int = 24, append: bool = False): """Write a minimal PDB with n_res GLY residues for the given chain.""" lines = [] atom_serial = 1 for i in range(n_res): resnum = resnum_start + i lines.append( f"ATOM {atom_serial:5d} CA GLY {chain_id}{resnum:4d} " f"{1.0 + i:8.3f}{0.0:8.3f}{0.0:8.3f} 1.00 20.00 C" ) atom_serial += 1 new_block = "\n".join(lines) if append and path.exists(): existing = path.read_text() existing = existing.rstrip() if existing.endswith("END"): existing = existing[: -len("END")].rstrip() combined = existing + ("\n" if existing else "") + new_block + "\nEND\n" path.write_text(combined) else: path.write_text(new_block + "\nEND\n") def test_normalise_motif_pdb_simple(tmp_pdb_dir: Path): pdb_path = tmp_pdb_dir / "test.pdb" _write_tiny_pdb(pdb_path, chain_id="L", n_res=3) segment_map, residue_map, chain_map_str = normalise_motif_pdb(pdb_path, "L24-26") assert segment_map == {"L24-26": "A1-3"} assert residue_map[("L", 24)] == ("A", 1) assert residue_map[("L", 26)] == ("A", 3) parser = PDBParser(QUIET=True) structure = parser.get_structure("test", pdb_path.as_posix()) model = next(structure.get_models()) chain = model["A"] res_ids = [res.id[1] for res in chain.get_residues()] assert res_ids == [1, 2, 3] text = pdb_path.read_text() assert "NORMALISED MOTIFBENCH PDB" in text assert "L24-26 -> A1-3" in text assert "REMARK" in text def test_normalise_motif_pdb_two_segments_two_chains(tmp_pdb_dir: Path): pdb_path = tmp_pdb_dir / "test2.pdb" _write_tiny_pdb(pdb_path, chain_id="L", n_res=3) _write_tiny_pdb(pdb_path, chain_id="H", n_res=2, resnum_start=10, append=True) seg_map, residue_map, chain_map_str = normalise_motif_pdb(pdb_path, "L24-26;H10-11") assert seg_map["L24-26"].startswith("A") assert seg_map["H10-11"].startswith("B") assert set(ch for (ch, _) in residue_map.values()) == {"A", "B"}