Skip to content
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

Allow parsing of offxml strings #288

Merged
merged 5 commits into from
Jul 27, 2023
Merged

Allow parsing of offxml strings #288

merged 5 commits into from
Jul 27, 2023

Conversation

jthorton
Copy link
Contributor

This PR allows an offxml string to be used as a small molecule force field for the SystemGenerator and SMIRNOFFTemplateGenerator see the example which now works with this PR.

"Try and load a OFFXML string into a system generator object"
from openmmforcefields.generators import SystemGenerator
from openff.toolkit.typing.engines.smirnoff import ForceField
from openff.toolkit.topology import Molecule
import openmm

# load the ff
ff = ForceField("openff-2.0.0.offxml")
# create a system generator
system_gen = SystemGenerator(
    forcefields=['amber/ff14SB.xml', 'amber/tip3p_standard.xml'],
    small_molecule_forcefield=ff.to_string()
)
mol = Molecule.from_smiles("CC")
system = system_gen.create_system(topology=mol.to_topology().to_openmm(), molecules=mol)
with open("system.xml", "w") as output:
    output.write(openmm.XmlSerializer.serialize(system))

@jthorton jthorton marked this pull request as draft July 11, 2023 17:33
@jthorton jthorton marked this pull request as ready for review July 14, 2023 15:14
@jthorton
Copy link
Contributor Author

Confirmed this works when running the solvation leg of a benchmark free energy calculation via openfe.

@mikemhenry
Copy link
Collaborator

@jthorton I did a merge commit to bring in a fix that was needed, let me know if it looks okay!

@jthorton
Copy link
Contributor Author

Thanks @mikemhenry looks like it changed the exception message slightly which is fine for me. Just want to check if my use of the walrus := was okay? I think this will only work with python 3.8+

@mikemhenry
Copy link
Collaborator

I am happy with using python 3.8+ language features. We can even use python 3.9+ language features since we follow NEP29 (or try to)

@mattwthompson
Copy link
Collaborator

OpenFF is pretty good about using it, so congrats - you've inherited it!

@mikemhenry
Copy link
Collaborator

Ah classic, can't run this from a fork. @jthorton I will push this to a branch in the repo so we can make sure it passes in CI

@mikemhenry
Copy link
Collaborator

@jthorton I don't know if this trick will keep working if you make a commit, so ping me if you do and CI stops running

@mikemhenry mikemhenry mentioned this pull request Jul 21, 2023
@mikemhenry
Copy link
Collaborator

Looks like it passes 🚀 @ijpulidos would you mind reviewing this one? Should we wait for #293 first?

@mikemhenry
Copy link
Collaborator

I don't think we need to wait on any other PRs, just need another review from @ijpulidos

Copy link
Collaborator

@ijpulidos ijpulidos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! I just added some comment about the error message when you get a poorly formatted string.

self._smirnoff_forcefield = openff.toolkit.typing.engines.smirnoff.ForceField(forcefield)
except Exception as e:
_logger.error(e)
raise ValueError(f"Can't find specified SMIRNOFF force field ({forcefield}) in install paths") from e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to improve the error message here, since as far as I can see it's not now that we don't find the ff on the install paths but it's rather that the provided string is not correctly formatted as a ffxml string.

Is it too crazy to add some more logic here such that we can have specific error messages as to what is causing this? It doesn't seem too hard to accomplish, as far as I can tell.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the error to reflect that a missing file or poorly formatted string could cause this error. As for specific messages on the formatting of the forcefield I would prefer to leave this to the openff.toolkit.ForceField and just raise the error from there as it's quite descriptive itself.

Copy link
Collaborator

@ijpulidos ijpulidos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mikemhenry mikemhenry enabled auto-merge (squash) July 27, 2023 21:14
@mikemhenry mikemhenry merged commit 3970498 into openmm:main Jul 27, 2023
@jthorton jthorton deleted the ff_string branch July 28, 2023 07:58
@ijpulidos ijpulidos added this to the 0.11.3 milestone Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants