|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | +from ase.units import eV, kcal, mol |
| 4 | +from neural_optimiser.calculators import MMFF94Calculator |
| 5 | +from neural_optimiser.conformers import Conformer |
| 6 | +from rdkit import Chem |
| 7 | +from rdkit.Chem import AllChem, rdDetermineBonds |
| 8 | + |
| 9 | +KCAL_PER_MOL_TO_EV = kcal / mol / eV |
| 10 | + |
| 11 | + |
| 12 | +def test_MMFF94Calculator_calculate(mol): |
| 13 | + """Compare energy and forces to RDKit's MMFF94 implementation.""" |
| 14 | + mp = AllChem.MMFFGetMoleculeProperties(mol) |
| 15 | + ff = AllChem.MMFFGetMoleculeForceField(mol, mp) |
| 16 | + _energy = ff.CalcEnergy() |
| 17 | + _forces = torch.tensor(ff.CalcGrad()) * -KCAL_PER_MOL_TO_EV |
| 18 | + |
| 19 | + data = Conformer.from_rdkit(mol) |
| 20 | + calc = MMFF94Calculator() |
| 21 | + energy, forces = calc._calculate(data) |
| 22 | + |
| 23 | + assert torch.isclose(energy, torch.tensor(_energy)) |
| 24 | + assert torch.allclose(forces.flatten(), torch.Tensor(_forces)) |
| 25 | + |
| 26 | + |
| 27 | +def test_repr_no_args(): |
| 28 | + """Test the __repr__ method with no arguments.""" |
| 29 | + calc = MMFF94Calculator() |
| 30 | + assert repr(calc) == "MMFFCalculator()" |
| 31 | + |
| 32 | + |
| 33 | +def test_repr_with_args_only_affects_repr_not_behavior(): |
| 34 | + """Test the __repr__ method with arguments.""" |
| 35 | + calc = MMFF94Calculator( |
| 36 | + MMFFGetMoleculeProperties={"mmffVariant": "MMFF94s"}, |
| 37 | + MMFFGetMoleculeForceField={"nonBondedThresh": 10.0}, |
| 38 | + ) |
| 39 | + s = repr(calc) |
| 40 | + assert "mmffVariant=MMFF94s" in s |
| 41 | + assert "nonBondedThresh=10.0" in s |
| 42 | + |
| 43 | + |
| 44 | +def test_single_conformer_energy_and_forces_shape(batch): |
| 45 | + """Test that energy is scalar and forces have correct shape.""" |
| 46 | + calc = MMFF94Calculator() |
| 47 | + energy, forces = calc._calculate(batch) |
| 48 | + |
| 49 | + # energy is a scalar tensor |
| 50 | + assert isinstance(energy, torch.Tensor) |
| 51 | + assert energy.ndim == 1 |
| 52 | + |
| 53 | + # forces match [n_atoms, 3] |
| 54 | + assert isinstance(forces, torch.Tensor) |
| 55 | + assert forces.ndim == 2 and forces.shape[1] == 3 |
| 56 | + assert forces.shape[0] == batch.pos.shape[0] |
| 57 | + |
| 58 | + |
| 59 | +def test_get_energies_matches_calculate(batch): |
| 60 | + """Test that get_energies matches the energy from _calculate.""" |
| 61 | + calc = MMFF94Calculator() |
| 62 | + e_only = calc.get_energies(batch) |
| 63 | + e_calc, _ = calc._calculate(batch) |
| 64 | + |
| 65 | + assert torch.allclose(e_only, e_calc) |
| 66 | + |
| 67 | + |
| 68 | +def test_multi_conformer_raises(minimised_batch): |
| 69 | + """Test that multi-conformer batches raise ValueError.""" |
| 70 | + calc = MMFF94Calculator() |
| 71 | + with pytest.raises(ValueError): |
| 72 | + calc._calculate(minimised_batch) |
| 73 | + with pytest.raises(ValueError): |
| 74 | + calc.get_energies(minimised_batch) |
| 75 | + |
| 76 | + |
| 77 | +def test_bond_determination_cached(monkeypatch, batch): |
| 78 | + """Test that bond determination is cached based on molecule key.""" |
| 79 | + calls = {"n": 0} |
| 80 | + orig = rdDetermineBonds.DetermineBonds |
| 81 | + |
| 82 | + def wrapper(mol, *args, **kwargs): |
| 83 | + calls["n"] += 1 |
| 84 | + return orig(mol, *args, **kwargs) |
| 85 | + |
| 86 | + monkeypatch.setattr(rdDetermineBonds, "DetermineBonds", wrapper) |
| 87 | + |
| 88 | + calc = MMFF94Calculator() |
| 89 | + |
| 90 | + # First call should determine bonds once |
| 91 | + _ = calc._calculate(batch) |
| 92 | + assert calls["n"] == 1 |
| 93 | + |
| 94 | + # Second prepare on a new mol with same key should reuse cached bonds |
| 95 | + mol2 = Chem.RWMol(batch.to_rdkit()[0]) |
| 96 | + _ = calc._prepare_mol(mol2) |
| 97 | + assert calls["n"] == 1 # unchanged |
| 98 | + |
| 99 | + |
| 100 | +def test_smiles_property_controls_cache_key(monkeypatch, batch): |
| 101 | + """Test that the 'smiles' property controls the caching of bond determination.""" |
| 102 | + calls = {"n": 0} |
| 103 | + orig = rdDetermineBonds.DetermineBonds |
| 104 | + |
| 105 | + def wrapper(mol, *args, **kwargs): |
| 106 | + calls["n"] += 1 |
| 107 | + return orig(mol, *args, **kwargs) |
| 108 | + |
| 109 | + monkeypatch.setattr(rdDetermineBonds, "DetermineBonds", wrapper) |
| 110 | + |
| 111 | + calc = MMFF94Calculator() |
| 112 | + |
| 113 | + # First molecule with a specific 'smiles' property |
| 114 | + mol1 = Chem.RWMol(batch.to_rdkit()[0]) |
| 115 | + mol1.SetProp("smiles", "KEY") |
| 116 | + _ = calc._prepare_mol(mol1) |
| 117 | + assert calls["n"] == 1 |
| 118 | + |
| 119 | + # Same key => should not re-run DetermineBonds |
| 120 | + mol2 = Chem.RWMol(batch.to_rdkit()[0]) |
| 121 | + mol2.SetProp("smiles", "KEY") |
| 122 | + _ = calc._prepare_mol(mol2) |
| 123 | + assert calls["n"] == 1 |
| 124 | + |
| 125 | + # Different key => should run DetermineBonds again |
| 126 | + mol3 = Chem.RWMol(batch.to_rdkit()[0]) |
| 127 | + mol3.SetProp("smiles", "KEY2") |
| 128 | + _ = calc._prepare_mol(mol3) |
| 129 | + assert calls["n"] == 2 |
0 commit comments