|
| 1 | +import MDAnalysis as mda |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | +from rdkit import Chem |
| 5 | + |
| 6 | +from openfe_analysis.rmsd import make_Universe |
| 7 | +from openfe_analysis.utils.universe_utils import ( |
| 8 | + correct_elements, |
| 9 | + guess_ligand_bonds, |
| 10 | + select_state_atoms, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def universe(hybrid_system_skipped_pdb, simulation_skipped_nc): |
| 16 | + u = make_Universe(hybrid_system_skipped_pdb, simulation_skipped_nc, state=0) |
| 17 | + yield u |
| 18 | + u.trajectory.close() |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def ligand_ag(universe): |
| 23 | + return select_state_atoms(universe, end_state="A").select_atoms("resname UNK") |
| 24 | + |
| 25 | + |
| 26 | +def test_guess_ligand_bonds_adds_bonds(ligand_ag): |
| 27 | + """Bonds should be present on the atomgroup after guess_ligand_bonds.""" |
| 28 | + original_count = len(ligand_ag.bonds) |
| 29 | + # This also has stateB bond |
| 30 | + assert original_count == 49 |
| 31 | + guess_ligand_bonds(ligand_ag, delete_existing=True) |
| 32 | + # Now only 48 stateA bonds |
| 33 | + assert len(ligand_ag.bonds) == 48 |
| 34 | + |
| 35 | + |
| 36 | +def test_guess_ligand_bonds_modifies_universe_inplace(ligand_ag): |
| 37 | + """Bond topology should be reflected on the parent universe after guessing.""" |
| 38 | + guess_ligand_bonds(ligand_ag) |
| 39 | + universe_bonds = ligand_ag.universe.select_atoms("resname UNK").bonds |
| 40 | + assert len(universe_bonds) > 0 |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize( |
| 44 | + "end_state, expected_bfactors", |
| 45 | + [ |
| 46 | + ("A", (0.25, 0.5)), |
| 47 | + ("B", (0.75, 0.5)), |
| 48 | + ], |
| 49 | +) |
| 50 | +def test_select_state_atoms(universe, end_state, expected_bfactors): |
| 51 | + """State selection should include state-unique and shared atoms.""" |
| 52 | + state = select_state_atoms(universe, end_state=end_state) |
| 53 | + assert len(state) > 0 |
| 54 | + assert all(atom.bfactor in expected_bfactors for atom in state) |
| 55 | + |
| 56 | + |
| 57 | +def test_select_state_atoms_invalid_state(universe): |
| 58 | + """Invalid end_state should raise a ValueError.""" |
| 59 | + with pytest.raises(ValueError, match="end_state must be 'A' or 'B'"): |
| 60 | + select_state_atoms(universe, end_state="C") |
| 61 | + |
| 62 | + |
| 63 | +def test_select_state_atoms_shared_atoms(universe): |
| 64 | + """Shared atoms (bfactor 0.5) should appear in both state A and B selections.""" |
| 65 | + state_a = select_state_atoms(universe, end_state="A") |
| 66 | + state_b = select_state_atoms(universe, end_state="B") |
| 67 | + shared_a = set(atom.ix for atom in state_a if atom.bfactor == 0.5) |
| 68 | + shared_b = set(atom.ix for atom in state_b if atom.bfactor == 0.5) |
| 69 | + assert shared_a == shared_b |
| 70 | + |
| 71 | + |
| 72 | +def test_correct_elements_fixes_element(): |
| 73 | + """correct_elements should update element where rdmol differs.""" |
| 74 | + |
| 75 | + # Build a minimal universe with a C atom |
| 76 | + u = mda.Universe.empty(2, n_residues=1, trajectory=True) |
| 77 | + u.add_TopologyAttr("elements", ["C", "C"]) # second atom is wrong |
| 78 | + u.add_TopologyAttr("names", ["C1", "C2"]) |
| 79 | + u.add_TopologyAttr("resnames", ["UNK"]) |
| 80 | + u.add_TopologyAttr("resids", [1]) |
| 81 | + u.load_new( |
| 82 | + np.array([[[0.0, 0.0, 0.0], [1.5, 0.0, 0.0]]]), |
| 83 | + order="fac", |
| 84 | + ) |
| 85 | + ag = u.select_atoms("all") |
| 86 | + |
| 87 | + mol = Chem.RWMol() |
| 88 | + mol.AddAtom(Chem.Atom(6)) # C |
| 89 | + mol.AddAtom(Chem.Atom(7)) # N |
| 90 | + rdmol = mol.GetMol() |
| 91 | + |
| 92 | + with pytest.warns(UserWarning, match="No atom_mapping provided"): |
| 93 | + correct_elements(ag, rdmol) |
| 94 | + |
| 95 | + assert ag[0].element == "C" |
| 96 | + assert ag[1].element == "N" |
| 97 | + assert ag[1].name == "N" |
| 98 | + |
| 99 | + |
| 100 | +def test_correct_elements_no_change_when_correct(): |
| 101 | + """correct_elements should not modify atoms that already have correct elements.""" |
| 102 | + |
| 103 | + u = mda.Universe.empty(2, n_residues=1, trajectory=True) |
| 104 | + u.add_TopologyAttr("elements", ["C", "N"]) |
| 105 | + u.add_TopologyAttr("names", ["C1", "N1"]) |
| 106 | + u.add_TopologyAttr("resnames", ["UNK"]) |
| 107 | + u.add_TopologyAttr("resids", [1]) |
| 108 | + u.load_new( |
| 109 | + np.array([[[0.0, 0.0, 0.0], [1.5, 0.0, 0.0]]]), |
| 110 | + order="fac", |
| 111 | + ) |
| 112 | + ag = u.select_atoms("all") |
| 113 | + |
| 114 | + mol = Chem.RWMol() |
| 115 | + mol.AddAtom(Chem.Atom(6)) # C |
| 116 | + mol.AddAtom(Chem.Atom(7)) # N |
| 117 | + rdmol = mol.GetMol() |
| 118 | + |
| 119 | + with pytest.warns(UserWarning, match="No atom_mapping provided"): |
| 120 | + correct_elements(ag, rdmol) |
| 121 | + |
| 122 | + assert ag[0].element == "C" |
| 123 | + assert ag[0].name == "C1" # name unchanged |
| 124 | + assert ag[1].element == "N" |
| 125 | + assert ag[1].name == "N1" # name unchanged |
| 126 | + |
| 127 | + |
| 128 | +def test_correct_elements_with_atom_mapping(): |
| 129 | + """correct_elements with atom_mapping should use mapping without warning.""" |
| 130 | + |
| 131 | + u = mda.Universe.empty(2, n_residues=1, trajectory=True) |
| 132 | + u.add_TopologyAttr("elements", ["C", "C"]) # second atom is wrong |
| 133 | + u.add_TopologyAttr("names", ["C1", "C2"]) |
| 134 | + u.add_TopologyAttr("resnames", ["UNK"]) |
| 135 | + u.add_TopologyAttr("resids", [1]) |
| 136 | + u.load_new( |
| 137 | + np.array([[[0.0, 0.0, 0.0], [1.5, 0.0, 0.0]]]), |
| 138 | + order="fac", |
| 139 | + ) |
| 140 | + ag = u.select_atoms("all") |
| 141 | + |
| 142 | + # rdmol has atoms in reverse order: N, C |
| 143 | + mol = Chem.RWMol() |
| 144 | + mol.AddAtom(Chem.Atom(7)) # N at rdmol index 0 |
| 145 | + mol.AddAtom(Chem.Atom(6)) # C at rdmol index 1 |
| 146 | + rdmol = mol.GetMol() |
| 147 | + |
| 148 | + # explicitly map ag index 0 -> rdmol index 1 (C), ag index 1 -> rdmol index 0 (N) |
| 149 | + correct_elements(ag, rdmol, atom_mapping={0: 1, 1: 0}) |
| 150 | + |
| 151 | + assert ag[0].element == "C" # mapped to rdmol index 1 (C) |
| 152 | + assert ag[1].element == "N" # mapped to rdmol index 0 (N) |
| 153 | + assert ag[1].name == "N" |
| 154 | + |
| 155 | + |
| 156 | +def test_correct_elements_raises_size_error(): |
| 157 | + """correct_elements should raise ValueError if atom counts don't match.""" |
| 158 | + |
| 159 | + u = mda.Universe.empty(2, n_residues=1, trajectory=True) |
| 160 | + u.add_TopologyAttr("elements", ["C", "N"]) |
| 161 | + u.add_TopologyAttr("names", ["C1", "N1"]) |
| 162 | + u.add_TopologyAttr("resnames", ["UNK"]) |
| 163 | + u.add_TopologyAttr("resids", [1]) |
| 164 | + u.load_new(np.array([[[0.0, 0.0, 0.0], [1.5, 0.0, 0.0]]]), order="fac") |
| 165 | + ag = u.select_atoms("all") |
| 166 | + |
| 167 | + mol = Chem.RWMol() |
| 168 | + mol.AddAtom(Chem.Atom(6)) # only 1 atom |
| 169 | + rdmol = mol.GetMol() |
| 170 | + |
| 171 | + with pytest.raises(ValueError, match="atomgroup has 2 atoms but rdmol has 1"): |
| 172 | + correct_elements(ag, rdmol) |
0 commit comments