Skip to content

Commit 7dd8796

Browse files
authored
Merge pull request #351 from OpenBioSim/backport_345_349
Backport fixes from PR #345 and #349
2 parents 71f86d7 + ed89cf7 commit 7dd8796

3 files changed

Lines changed: 37 additions & 152 deletions

File tree

python/BioSimSpace/Align/_merge.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,8 +1162,13 @@ def merge(
11621162
"perturbation will likely be unstable."
11631163
)
11641164

1165-
# Set the "connectivity" property.
1166-
edit_mol.setProperty("connectivity", conn)
1165+
# Set the "connectivity" property. If the end state connectivity is the same,
1166+
# then we can just set the "connectivity" property.
1167+
if conn0 == conn1:
1168+
edit_mol.setProperty("connectivity", conn0)
1169+
else:
1170+
edit_mol.setProperty("connectivity0", conn0)
1171+
edit_mol.setProperty("connectivity1", conn1)
11671172

11681173
# Create the CLJNBPairs matrices.
11691174
ff = molecule0.property(ff0)

python/BioSimSpace/Parameters/_Protocol/_openforcefield.py

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@
115115
from sire.legacy import System as _SireSystem
116116

117117
from ... import _isVerbose
118+
from ... import Convert as _Convert
118119
from ... import IO as _IO
120+
from ..._Exceptions import ConversionError as _ConversionError
119121
from ..._Exceptions import IncompatibleError as _IncompatibleError
120122
from ..._Exceptions import ThirdPartyError as _ThirdPartyError
121123
from ..._SireWrappers import Molecule as _Molecule
@@ -243,82 +245,20 @@ def run(self, molecule, work_dir=None, queue=None):
243245
else:
244246
raise IOError(msg) from None
245247
else:
246-
# If the molecule was originally loaded from an SDF format file,
247-
# then write back to the same format.
248-
fileformat_prop = self._property_map.get("fileformat", "fileformat")
249-
if (
250-
molecule._sire_object.hasProperty(fileformat_prop)
251-
and "SDF" in molecule._sire_object.property("fileformat").value()
252-
):
253-
# Write the molecule to SDF format.
254-
try:
255-
_IO.saveMolecules(
256-
_os.path.join(str(work_dir), "molecule"),
257-
molecule,
258-
"sdf",
259-
property_map=self._property_map,
260-
)
261-
except Exception as e:
262-
msg = "Failed to write the molecule to 'SDF' format."
263-
if _isVerbose():
264-
msg += ": " + getattr(e, "message", repr(e))
265-
raise IOError(msg) from e
266-
else:
267-
raise IOError(msg) from None
268-
269-
# Otherwise, go via an intermediate PDB file and use RDKit to try
270-
# to recover stereochemistry.
271-
else:
272-
# Write the molecule to a PDB file.
273-
try:
274-
_IO.saveMolecules(
275-
_os.path.join(str(work_dir), "molecule"),
276-
molecule,
277-
"pdb",
278-
property_map=self._property_map,
279-
)
280-
except Exception as e:
281-
msg = "Failed to write the molecule to 'PDB' format."
282-
if _isVerbose():
283-
msg += ": " + getattr(e, "message", repr(e))
284-
raise IOError(msg) from e
285-
else:
286-
raise IOError(msg) from None
287-
288-
# Create an RDKit molecule from the PDB file.
289-
try:
290-
rdmol = _Chem.MolFromPDBFile(
291-
_os.path.join(str(work_dir), "molecule.pdb"), removeHs=False
292-
)
293-
except Exception as e:
294-
msg = "RDKit was unable to read the molecular PDB file!"
295-
if _isVerbose():
296-
msg += ": " + getattr(e, "message", repr(e))
297-
raise _ThirdPartyError(msg) from e
298-
else:
299-
raise _ThirdPartyError(msg) from None
300-
301-
# Use RDKit to write back to SDF format.
302-
try:
303-
writer = _Chem.SDWriter(
304-
_os.path.join(str(work_dir), "molecule.sdf")
305-
)
306-
writer.write(rdmol)
307-
writer.close()
308-
except Exception as e:
309-
msg = "RDKit was unable to write the molecular SDF file!"
310-
if _isVerbose():
311-
msg += ": " + getattr(e, "message", repr(e))
312-
raise _ThirdPartyError(msg) from e
313-
else:
314-
raise _ThirdPartyError(msg) from None
315-
316-
# Create the Open Forcefield Molecule from the intermediate SDF file,
317-
# as recommended by @j-wags and @mattwthompson.
248+
# Try converting to RDKit format.
318249
try:
319-
off_molecule = _OpenFFMolecule.from_file(
320-
_os.path.join(str(work_dir), "molecule.sdf")
321-
)
250+
rdmol = _Convert.toRDKit(molecule, property_map=self._property_map)
251+
except Exception as e:
252+
msg = "Failed to convert molecule to RDKit format."
253+
if _isVerbose():
254+
msg += ": " + getattr(e, "message", repr(e))
255+
raise (msg) from e
256+
else:
257+
raise _ConversionError(msg) from None
258+
259+
# Create the Open Forcefield Molecule from the RDKit molecule.
260+
try:
261+
off_molecule = _OpenFFMolecule.from_rdkit(rdmol)
322262
except Exception as e:
323263
msg = "Unable to create OpenFF Molecule!"
324264
if _isVerbose():

python/BioSimSpace/Sandpit/Exscientia/Parameters/_Protocol/_openforcefield.py

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@
115115
from sire.legacy import System as _SireSystem
116116

117117
from ... import _isVerbose
118+
from ... import Convert as _Convert
118119
from ... import IO as _IO
120+
from ..._Exceptions import ConversionError as _ConversionError
119121
from ..._Exceptions import IncompatibleError as _IncompatibleError
120122
from ..._Exceptions import ThirdPartyError as _ThirdPartyError
121123
from ..._SireWrappers import Molecule as _Molecule
@@ -243,82 +245,20 @@ def run(self, molecule, work_dir=None, queue=None):
243245
else:
244246
raise IOError(msg) from None
245247
else:
246-
# If the molecule was originally loaded from an SDF format file,
247-
# then write back to the same format.
248-
fileformat_prop = self._property_map.get("fileformat", "fileformat")
249-
if (
250-
molecule._sire_object.hasProperty(fileformat_prop)
251-
and "SDF" in molecule._sire_object.property("fileformat").value()
252-
):
253-
# Write the molecule to SDF format.
254-
try:
255-
_IO.saveMolecules(
256-
_os.path.join(str(work_dir), "molecule"),
257-
molecule,
258-
"sdf",
259-
property_map=self._property_map,
260-
)
261-
except Exception as e:
262-
msg = "Failed to write the molecule to 'SDF' format."
263-
if _isVerbose():
264-
msg += ": " + getattr(e, "message", repr(e))
265-
raise IOError(msg) from e
266-
else:
267-
raise IOError(msg) from None
268-
269-
# Otherwise, go via an intermediate PDB file and use RDKit to try
270-
# to recover stereochemistry.
271-
else:
272-
# Write the molecule to a PDB file.
273-
try:
274-
_IO.saveMolecules(
275-
_os.path.join(str(work_dir), "molecule"),
276-
molecule,
277-
"pdb",
278-
property_map=self._property_map,
279-
)
280-
except Exception as e:
281-
msg = "Failed to write the molecule to 'PDB' format."
282-
if _isVerbose():
283-
msg += ": " + getattr(e, "message", repr(e))
284-
raise IOError(msg) from e
285-
else:
286-
raise IOError(msg) from None
287-
288-
# Create an RDKit molecule from the PDB file.
289-
try:
290-
rdmol = _Chem.MolFromPDBFile(
291-
_os.path.join(str(work_dir), "molecule.pdb"), removeHs=False
292-
)
293-
except Exception as e:
294-
msg = "RDKit was unable to read the molecular PDB file!"
295-
if _isVerbose():
296-
msg += ": " + getattr(e, "message", repr(e))
297-
raise _ThirdPartyError(msg) from e
298-
else:
299-
raise _ThirdPartyError(msg) from None
300-
301-
# Use RDKit to write back to SDF format.
302-
try:
303-
writer = _Chem.SDWriter(
304-
_os.path.join(str(work_dir), "molecule.sdf")
305-
)
306-
writer.write(rdmol)
307-
writer.close()
308-
except Exception as e:
309-
msg = "RDKit was unable to write the molecular SDF file!"
310-
if _isVerbose():
311-
msg += ": " + getattr(e, "message", repr(e))
312-
raise _ThirdPartyError(msg) from e
313-
else:
314-
raise _ThirdPartyError(msg) from None
315-
316-
# Create the Open Forcefield Molecule from the intermediate SDF file,
317-
# as recommended by @j-wags and @mattwthompson.
248+
# Try converting to RDKit format.
318249
try:
319-
off_molecule = _OpenFFMolecule.from_file(
320-
_os.path.join(str(work_dir), "molecule.sdf")
321-
)
250+
rdmol = _Convert.toRDKit(molecule, property_map=self._property_map)
251+
except Exception as e:
252+
msg = "Failed to convert molecule to RDKit format."
253+
if _isVerbose():
254+
msg += ": " + getattr(e, "message", repr(e))
255+
raise (msg) from e
256+
else:
257+
raise _ConversionError(msg) from None
258+
259+
# Create the Open Forcefield Molecule from the RDKit molecule.
260+
try:
261+
off_molecule = _OpenFFMolecule.from_rdkit(rdmol)
322262
except Exception as e:
323263
msg = "Unable to create OpenFF Molecule!"
324264
if _isVerbose():

0 commit comments

Comments
 (0)