-
Notifications
You must be signed in to change notification settings - Fork 52
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
Open
jthorton
wants to merge
3
commits into
main
Choose a base branch
from
charge_validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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? |
||
| 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() | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?