Skip to content

Commit

Permalink
Merge pull request #349 from OpenBioSim/fix_348
Browse files Browse the repository at this point in the history
Fix issue #348
  • Loading branch information
lohedges authored Oct 2, 2024
2 parents bdd69c6 + 2927c15 commit 0cf9f5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 150 deletions.
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

0 comments on commit 0cf9f5f

Please sign in to comment.