-
Notifications
You must be signed in to change notification settings - Fork 53
Non-deterministic charge validation #2132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
d7dcd14
84174f3
3c76241
2874599
36784f9
251474c
ba3f47d
045954d
6c0d7d4
960b874
96b5d24
e2099ed
b8f7ecb
98152db
df24ca1
7dfdb3f
0c8e7dd
540aefa
bacb1c3
9cb4322
7b7e4ee
c43dbf0
6c32c99
4cf822d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,21 @@ | |
| import gzip | ||
| import logging | ||
| import os | ||
| import re | ||
| import sys | ||
| from importlib import resources | ||
| from pathlib import Path | ||
| from unittest import mock | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from gufe import ChemicalSystem | ||
| from gufe.components.errors import ComponentValidationError | ||
| from gufe.protocols.errors import ProtocolValidationError | ||
| from gufe.settings import OpenMMSystemGeneratorFFSettings, ThermoSettings | ||
| from numpy.testing import assert_allclose, assert_equal | ||
| from openff.toolkit import Molecule as OFFMol | ||
| from openff.toolkit import ForceField | ||
| from openff.toolkit.utils.toolkit_registry import ToolkitRegistry | ||
| from openff.toolkit.utils.toolkits import RDKitToolkitWrapper | ||
| from openff.units import unit | ||
|
|
@@ -1283,3 +1287,76 @@ def test_set_metadata_none_clears(): | |
| _set_offmol_metadata(mol, "residue_name", "LIG") | ||
| _set_offmol_metadata(mol, "residue_name", None) | ||
| assert all("residue_name" not in a.metadata for a in mol.atoms) | ||
|
|
||
|
|
||
| class TestChargeValidation: | ||
| """Test validation of nondeterministic partial charge assignment.""" | ||
|
|
||
| @pytest.fixture | ||
| def benzene_charged_system(self, benzene_modifications): | ||
| return ChemicalSystem({"ligand": benzene_modifications["benzene"]}, name="charged") | ||
|
|
||
| @pytest.fixture | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to also add a test where the first smc is charged and the second does not have charges? How would we want to handle such a case?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test with a system with a mix of charged and uncharged SMCs, this will raise an error on the first one that fails validation. Do you think it would be better however to list all of the SMCs which fail to avoid playing wack-a-mole?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to flag all components in the ChemicalSystem which fail validation in one error message to hopefully make the experience better! |
||
| def benzene_no_charge_system(self, benzene_modifications_uncharged): | ||
| return ChemicalSystem({"ligand": benzene_modifications_uncharged["benzene"]}, name="no charges") | ||
|
|
||
| def test_gaff_with_molecule_charges(self, benzene_charged_system): | ||
| system_validation.validate_nondeterministic_charges( | ||
| benzene_charged_system, | ||
| small_molecule_forcefield="gaff-2.11" | ||
| ) | ||
|
|
||
| def test_gaff_no_charges(self, benzene_no_charge_system): | ||
| with pytest.raises(ProtocolValidationError, match=re.escape("SmallMoleculeComponent(name=benzene) from system no charges would have am1bcc charges")): | ||
| system_validation.validate_nondeterministic_charges( | ||
| benzene_no_charge_system, | ||
| small_molecule_forcefield="gaff-2.11" | ||
| ) | ||
|
|
||
| def test_espaloma_with_molecule_charges(self, benzene_charged_system): | ||
| system_validation.validate_nondeterministic_charges( | ||
| benzene_charged_system, | ||
| small_molecule_forcefield="espaloma-0.3.2" | ||
| ) | ||
|
|
||
| def test_espaloma_no_charges(self, benzene_no_charge_system): | ||
| with pytest.raises(ProtocolValidationError, match=re.escape("SmallMoleculeComponent(name=benzene) from system no charges would have am1bcc charges")): | ||
| system_validation.validate_nondeterministic_charges( | ||
| benzene_no_charge_system, | ||
| small_molecule_forcefield="espaloma-0.3.2" | ||
| ) | ||
|
|
||
| def test_openff_with_molecule_charges(self, benzene_charged_system): | ||
| system_validation.validate_nondeterministic_charges( | ||
| benzene_charged_system, | ||
| small_molecule_forcefield="openff-2.2.0.offxml" | ||
| ) | ||
|
|
||
| def test_openff_no_charges(self, benzene_no_charge_system): | ||
| with pytest.raises(ProtocolValidationError, match=re.escape("SmallMoleculeComponent(name=benzene) from system no charges would have am1bcc charges")): | ||
| system_validation.validate_nondeterministic_charges( | ||
| benzene_no_charge_system, | ||
| small_molecule_forcefield="openff-2.2.0.offxml" | ||
| ) | ||
|
|
||
| def test_openff_nagl_no_charges(self, benzene_no_charge_system): | ||
| system_validation.validate_nondeterministic_charges( | ||
| benzene_no_charge_system, | ||
| small_molecule_forcefield="openff-2.3.0.offxml" | ||
| ) | ||
|
|
||
| def test_openff_lib_charges_no_charges(self, benzene_no_charge_system, benzene_modifications): | ||
| # add some library charges to the force field and test using a string | ||
| benzene = benzene_modifications["benzene"].to_openff() | ||
| # use a force field with an am1bcc handler | ||
| ff = ForceField("openff-2.2.0.offxml") | ||
| lib_charge_handler = ff.get_parameter_handler("LibraryCharges") | ||
| # make a new parameter | ||
| lib_charge = lib_charge_handler._INFOTYPE.from_molecule(benzene) | ||
| # add it to the handler | ||
| lib_charge_handler.add_parameter(parameter=lib_charge) | ||
| # run the validation | ||
| system_validation.validate_nondeterministic_charges( | ||
| benzene_no_charge_system, | ||
| small_molecule_forcefield=ff.to_string() | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we want to support a case where the user supplies partial charges will all zeros?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 - given we have our own benchmark case where we want to do this, we probably should support it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes good idea. Currently, this only works as a Library charge would we want to keep that as a source of protection to make sure users know what they are doing or allow it as charges on the molecule as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point - I think if it's easily allowed by LibraryCharge then it might be better to keep the check in. There's too many ways someone could accidentally add partial charges.