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

Backport fixes from PR #345 and #349 #351

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions python/BioSimSpace/Align/_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,13 @@ def merge(
"perturbation will likely be unstable."
)

# Set the "connectivity" property.
edit_mol.setProperty("connectivity", conn)
# Set the "connectivity" property. If the end state connectivity is the same,
# then we can just set the "connectivity" property.
if conn0 == conn1:
edit_mol.setProperty("connectivity", conn0)
else:
edit_mol.setProperty("connectivity0", conn0)
edit_mol.setProperty("connectivity1", conn1)

# Create the CLJNBPairs matrices.
ff = molecule0.property(ff0)
Expand Down
90 changes: 15 additions & 75 deletions python/BioSimSpace/Parameters/_Protocol/_openforcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@
from sire.legacy import System as _SireSystem

from ... import _isVerbose
from ... import Convert as _Convert
from ... import IO as _IO
from ..._Exceptions import ConversionError as _ConversionError
from ..._Exceptions import IncompatibleError as _IncompatibleError
from ..._Exceptions import ThirdPartyError as _ThirdPartyError
from ..._SireWrappers import Molecule as _Molecule
Expand Down Expand Up @@ -243,82 +245,20 @@ def run(self, molecule, work_dir=None, queue=None):
else:
raise IOError(msg) from None
else:
# If the molecule was originally loaded from an SDF format file,
# then write back to the same format.
fileformat_prop = self._property_map.get("fileformat", "fileformat")
if (
molecule._sire_object.hasProperty(fileformat_prop)
and "SDF" in molecule._sire_object.property("fileformat").value()
):
# Write the molecule to SDF format.
try:
_IO.saveMolecules(
_os.path.join(str(work_dir), "molecule"),
molecule,
"sdf",
property_map=self._property_map,
)
except Exception as e:
msg = "Failed to write the molecule to 'SDF' format."
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise IOError(msg) from e
else:
raise IOError(msg) from None

# Otherwise, go via an intermediate PDB file and use RDKit to try
# to recover stereochemistry.
else:
# Write the molecule to a PDB file.
try:
_IO.saveMolecules(
_os.path.join(str(work_dir), "molecule"),
molecule,
"pdb",
property_map=self._property_map,
)
except Exception as e:
msg = "Failed to write the molecule to 'PDB' format."
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise IOError(msg) from e
else:
raise IOError(msg) from None

# Create an RDKit molecule from the PDB file.
try:
rdmol = _Chem.MolFromPDBFile(
_os.path.join(str(work_dir), "molecule.pdb"), removeHs=False
)
except Exception as e:
msg = "RDKit was unable to read the molecular PDB file!"
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise _ThirdPartyError(msg) from e
else:
raise _ThirdPartyError(msg) from None

# Use RDKit to write back to SDF format.
try:
writer = _Chem.SDWriter(
_os.path.join(str(work_dir), "molecule.sdf")
)
writer.write(rdmol)
writer.close()
except Exception as e:
msg = "RDKit was unable to write the molecular SDF file!"
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise _ThirdPartyError(msg) from e
else:
raise _ThirdPartyError(msg) from None

# Create the Open Forcefield Molecule from the intermediate SDF file,
# as recommended by @j-wags and @mattwthompson.
# Try converting to RDKit format.
try:
off_molecule = _OpenFFMolecule.from_file(
_os.path.join(str(work_dir), "molecule.sdf")
)
rdmol = _Convert.toRDKit(molecule, property_map=self._property_map)
except Exception as e:
msg = "Failed to convert molecule to RDKit format."
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise (msg) from e
else:
raise _ConversionError(msg) from None

# Create the Open Forcefield Molecule from the RDKit molecule.
try:
off_molecule = _OpenFFMolecule.from_rdkit(rdmol)
except Exception as e:
msg = "Unable to create OpenFF Molecule!"
if _isVerbose():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@
from sire.legacy import System as _SireSystem

from ... import _isVerbose
from ... import Convert as _Convert
from ... import IO as _IO
from ..._Exceptions import ConversionError as _ConversionError
from ..._Exceptions import IncompatibleError as _IncompatibleError
from ..._Exceptions import ThirdPartyError as _ThirdPartyError
from ..._SireWrappers import Molecule as _Molecule
Expand Down Expand Up @@ -243,82 +245,20 @@ def run(self, molecule, work_dir=None, queue=None):
else:
raise IOError(msg) from None
else:
# If the molecule was originally loaded from an SDF format file,
# then write back to the same format.
fileformat_prop = self._property_map.get("fileformat", "fileformat")
if (
molecule._sire_object.hasProperty(fileformat_prop)
and "SDF" in molecule._sire_object.property("fileformat").value()
):
# Write the molecule to SDF format.
try:
_IO.saveMolecules(
_os.path.join(str(work_dir), "molecule"),
molecule,
"sdf",
property_map=self._property_map,
)
except Exception as e:
msg = "Failed to write the molecule to 'SDF' format."
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise IOError(msg) from e
else:
raise IOError(msg) from None

# Otherwise, go via an intermediate PDB file and use RDKit to try
# to recover stereochemistry.
else:
# Write the molecule to a PDB file.
try:
_IO.saveMolecules(
_os.path.join(str(work_dir), "molecule"),
molecule,
"pdb",
property_map=self._property_map,
)
except Exception as e:
msg = "Failed to write the molecule to 'PDB' format."
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise IOError(msg) from e
else:
raise IOError(msg) from None

# Create an RDKit molecule from the PDB file.
try:
rdmol = _Chem.MolFromPDBFile(
_os.path.join(str(work_dir), "molecule.pdb"), removeHs=False
)
except Exception as e:
msg = "RDKit was unable to read the molecular PDB file!"
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise _ThirdPartyError(msg) from e
else:
raise _ThirdPartyError(msg) from None

# Use RDKit to write back to SDF format.
try:
writer = _Chem.SDWriter(
_os.path.join(str(work_dir), "molecule.sdf")
)
writer.write(rdmol)
writer.close()
except Exception as e:
msg = "RDKit was unable to write the molecular SDF file!"
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise _ThirdPartyError(msg) from e
else:
raise _ThirdPartyError(msg) from None

# Create the Open Forcefield Molecule from the intermediate SDF file,
# as recommended by @j-wags and @mattwthompson.
# Try converting to RDKit format.
try:
off_molecule = _OpenFFMolecule.from_file(
_os.path.join(str(work_dir), "molecule.sdf")
)
rdmol = _Convert.toRDKit(molecule, property_map=self._property_map)
except Exception as e:
msg = "Failed to convert molecule to RDKit format."
if _isVerbose():
msg += ": " + getattr(e, "message", repr(e))
raise (msg) from e
else:
raise _ConversionError(msg) from None

# Create the Open Forcefield Molecule from the RDKit molecule.
try:
off_molecule = _OpenFFMolecule.from_rdkit(rdmol)
except Exception as e:
msg = "Unable to create OpenFF Molecule!"
if _isVerbose():
Expand Down