|
| 1 | +"""Tests for ``mdpp.prep.protein`` protonation handling (PROPKA + PDBFixer).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from pathlib import Path |
| 7 | +from types import SimpleNamespace |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from mdpp.prep.protein import ( |
| 12 | + PropkaResidue, |
| 13 | + PropkaResult, |
| 14 | + _propka_variants, |
| 15 | + fix_pdb, |
| 16 | +) |
| 17 | + |
| 18 | +# Minimal single-residue glutamate with crude but valid geometry. PDBFixer |
| 19 | +# assigns bonds from residue templates, and Modeller adds the GLH proton (HE2) |
| 20 | +# only when the GLU variant is forced -- a clean protonation discriminator. |
| 21 | +_GLU_PDB = """ATOM 1 N GLU A 1 0.000 0.000 0.000 1.00 0.00 N |
| 22 | +ATOM 2 CA GLU A 1 1.458 0.000 0.000 1.00 0.00 C |
| 23 | +ATOM 3 C GLU A 1 2.009 1.420 0.000 1.00 0.00 C |
| 24 | +ATOM 4 O GLU A 1 1.251 2.390 0.000 1.00 0.00 O |
| 25 | +ATOM 5 CB GLU A 1 1.988 -0.773 -1.209 1.00 0.00 C |
| 26 | +ATOM 6 CG GLU A 1 3.508 -0.889 -1.250 1.00 0.00 C |
| 27 | +ATOM 7 CD GLU A 1 4.000 -1.700 -2.430 1.00 0.00 C |
| 28 | +ATOM 8 OE1 GLU A 1 3.250 -2.560 -2.930 1.00 0.00 O |
| 29 | +ATOM 9 OE2 GLU A 1 5.150 -1.480 -2.850 1.00 0.00 O |
| 30 | +ATOM 10 OXT GLU A 1 3.330 1.560 0.000 1.00 0.00 O |
| 31 | +TER |
| 32 | +END |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +def _fake_topology(residues: list[tuple[str, str, str]]) -> object: |
| 37 | + """Fake OpenMM topology whose ``residues()`` yields (chain.id, id, name) stubs.""" |
| 38 | + objs = [ |
| 39 | + SimpleNamespace(chain=SimpleNamespace(id=chain), id=rid, name=name) |
| 40 | + for chain, rid, name in residues |
| 41 | + ] |
| 42 | + return SimpleNamespace(residues=lambda: iter(objs)) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture() |
| 46 | +def glu_pdb(tmp_path: Path) -> Path: |
| 47 | + """Write the minimal glutamate fixture and return its path.""" |
| 48 | + path = tmp_path / "glu.pdb" |
| 49 | + path.write_text(_GLU_PDB, encoding="utf-8") |
| 50 | + return path |
| 51 | + |
| 52 | + |
| 53 | +# --------------------------------------------------------------------------- # |
| 54 | +# _propka_variants (pure mapping logic) |
| 55 | +# --------------------------------------------------------------------------- # |
| 56 | +def test_propka_variants_overrides_disagreeing_residues() -> None: |
| 57 | + nonstandard = ( |
| 58 | + PropkaResidue("GLU", 54, "A", 7.64, 4.50), # protonated -> GLH |
| 59 | + PropkaResidue("LYS", 100, "A", 6.00, 10.50), # deprotonated -> LYN |
| 60 | + PropkaResidue("HIS", 267, "A", 7.50, 6.50), # protonated -> HIP |
| 61 | + ) |
| 62 | + topology = _fake_topology([ |
| 63 | + ("A", "53", "ALA"), |
| 64 | + ("A", "54", "GLU"), |
| 65 | + ("A", "100", "LYS"), |
| 66 | + ("A", "267", "HIS"), |
| 67 | + ]) |
| 68 | + assert _propka_variants(topology, nonstandard, pH=7.0) == [None, "GLH", "LYN", "HIP"] |
| 69 | + |
| 70 | + |
| 71 | +def test_propka_variants_cys_deprotonated_to_cyx() -> None: |
| 72 | + nonstandard = (PropkaResidue("CYS", 10, "A", 5.0, 9.0),) # deprotonated -> CYX |
| 73 | + topology = _fake_topology([("A", "10", "CYS")]) |
| 74 | + assert _propka_variants(topology, nonstandard, pH=7.0) == ["CYX"] |
| 75 | + |
| 76 | + |
| 77 | +def test_propka_variants_skips_unsupported_type(caplog: pytest.LogCaptureFixture) -> None: |
| 78 | + nonstandard = (PropkaResidue("N+", 1, "A", 9.0, 8.0),) # terminus: no variant |
| 79 | + topology = _fake_topology([("A", "1", "MET")]) |
| 80 | + with caplog.at_level(logging.WARNING): |
| 81 | + assert _propka_variants(topology, nonstandard, pH=7.0) == [None] |
| 82 | + assert "no OpenMM variant" in caplog.text |
| 83 | + |
| 84 | + |
| 85 | +def test_propka_variants_empty_is_all_none() -> None: |
| 86 | + topology = _fake_topology([("A", "1", "ALA"), ("A", "2", "GLY")]) |
| 87 | + assert _propka_variants(topology, (), pH=7.0) == [None, None] |
| 88 | + |
| 89 | + |
| 90 | +# --------------------------------------------------------------------------- # |
| 91 | +# fix_pdb dispatch (real PDBFixer + Modeller; PROPKA result is controlled) |
| 92 | +# --------------------------------------------------------------------------- # |
| 93 | +def _atom_names(pdb_path: Path) -> set[str]: |
| 94 | + return { |
| 95 | + line[12:16].strip() |
| 96 | + for line in pdb_path.read_text().splitlines() |
| 97 | + if line.startswith(("ATOM", "HETATM")) |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | +def test_fix_pdb_model_leaves_glu_deprotonated( |
| 102 | + glu_pdb: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 103 | +) -> None: |
| 104 | + monkeypatch.setattr("mdpp.prep.protein.run_propka", lambda _p: PropkaResult(())) |
| 105 | + out = tmp_path / "model.pdb" |
| 106 | + fix_pdb(glu_pdb, out) |
| 107 | + names = _atom_names(out) |
| 108 | + assert "H" in names # hydrogens were added |
| 109 | + assert "HE2" not in names # GLU stays charged under the model default |
| 110 | + |
| 111 | + |
| 112 | +def test_fix_pdb_propka_protonates_glu( |
| 113 | + glu_pdb: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 114 | +) -> None: |
| 115 | + result = PropkaResult((PropkaResidue("GLU", 1, "A", 7.64, 4.50),)) |
| 116 | + monkeypatch.setattr("mdpp.prep.protein.run_propka", lambda _p: result) |
| 117 | + out = tmp_path / "propka.pdb" |
| 118 | + fix_pdb(glu_pdb, out, protonation="propka") |
| 119 | + assert "HE2" in _atom_names(out) # PROPKA-driven GLH proton applied |
| 120 | + |
| 121 | + |
| 122 | +def test_fix_pdb_propka_without_nonstandard_matches_model( |
| 123 | + glu_pdb: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 124 | +) -> None: |
| 125 | + monkeypatch.setattr("mdpp.prep.protein.run_propka", lambda _p: PropkaResult(())) |
| 126 | + out = tmp_path / "propka_empty.pdb" |
| 127 | + fix_pdb(glu_pdb, out, protonation="propka") |
| 128 | + assert "HE2" not in _atom_names(out) # nothing to override -> model default |
0 commit comments