Skip to content

Commit

Permalink
Add support for using NAGL to generate AM1BCC charges.
Browse files Browse the repository at this point in the history
  • Loading branch information
lohedges committed Mar 22, 2024
1 parent c10d28b commit 40ecb82
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
42 changes: 41 additions & 1 deletion python/BioSimSpace/Parameters/_Protocol/_openforcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@
from openff.interchange import Interchange as _Interchange
from openff.toolkit.topology import Molecule as _OpenFFMolecule
from openff.toolkit.typing.engines.smirnoff import ForceField as _Forcefield

try:
from openff.toolkit.utils.nagl_wrapper import (
NAGLToolkitWrapper as _NAGLToolkitWrapper,
)

_has_nagl = _NAGLToolkitWrapper.is_available()
from openff.nagl_models import get_models_by_type as _get_models_by_type

_models = _get_models_by_type("am1bcc")
try:
# Find the most recent AM1-BCC release candidate.
_nagl = _NAGLToolkitWrapper()
_nagl_model = sorted(
[str(model) for model in _models if "rc" in str(model)], reverse=True
)[0]
except:
_has_nagl = False
del _models
except:
_has_nagl = False
else:
_Interchange = _openff
_OpenFFMolecule = _openff
Expand Down Expand Up @@ -289,6 +310,23 @@ def run(self, molecule, work_dir=None, queue=None):
else:
raise _ThirdPartyError(msg) from None

# Apply AM1-BCC charges using NAGL.
if _has_nagl:
try:
_nagl.assign_partial_charges(
off_molecule, partial_charge_method=_nagl_model
)
except Exception as e:
msg = "Failed to assign AM1-BCC charges using NAGL."
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise _ThirdPartyError(msg) from e
else:
raise _ThirdPartyError(msg) from None
charge_from_molecules = [off_molecule]
else:
charge_from_molecules = None

# Extract the molecular topology.
try:
off_topology = off_molecule.to_topology()
Expand All @@ -315,7 +353,9 @@ def run(self, molecule, work_dir=None, queue=None):
# Create an Interchange object.
try:
interchange = _Interchange.from_smirnoff(
force_field=forcefield, topology=off_topology
force_field=forcefield,
topology=off_topology,
charge_from_molecules=charge_from_molecules,
)
except Exception as e:
msg = "Unable to create OpenFF Interchange object!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@
from openff.interchange import Interchange as _Interchange
from openff.toolkit.topology import Molecule as _OpenFFMolecule
from openff.toolkit.typing.engines.smirnoff import ForceField as _Forcefield

try:
from openff.toolkit.utils.nagl_wrapper import (
NAGLToolkitWrapper as _NAGLToolkitWrapper,
)

_has_nagl = _NAGLToolkitWrapper.is_available()
from openff.nagl_models import get_models_by_type as _get_models_by_type

_models = _get_models_by_type("am1bcc")
try:
# Find the most recent AM1-BCC release candidate.
_nagl = _NAGLToolkitWrapper()
_nagl_model = sorted(
[str(model) for model in _models if "rc" in str(model)], reverse=True
)[0]
except:
_has_nagl = False
del _models
except:
_has_nagl = False
else:
_Interchange = _openff
_OpenFFMolecule = _openff
Expand Down Expand Up @@ -289,6 +310,23 @@ def run(self, molecule, work_dir=None, queue=None):
else:
raise _ThirdPartyError(msg) from None

# Apply AM1-BCC charges using NAGL.
if _has_nagl:
try:
_nagl.assign_partial_charges(
off_molecule, partial_charge_method=_nagl_model
)
except Exception as e:
msg = "Failed to assign AM1-BCC charges using NAGL."
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise _ThirdPartyError(msg) from e
else:
raise _ThirdPartyError(msg) from None
charge_from_molecules = [off_molecule]
else:
charge_from_molecules = None

# Extract the molecular topology.
try:
off_topology = off_molecule.to_topology()
Expand All @@ -315,7 +353,9 @@ def run(self, molecule, work_dir=None, queue=None):
# Create an Interchange object.
try:
interchange = _Interchange.from_smirnoff(
force_field=forcefield, topology=off_topology
force_field=forcefield,
topology=off_topology,
charge_from_molecules=charge_from_molecules,
)
except Exception as e:
msg = "Unable to create OpenFF Interchange object!"
Expand Down

0 comments on commit 40ecb82

Please sign in to comment.