|
115 | 115 | from sire.legacy import System as _SireSystem |
116 | 116 |
|
117 | 117 | from ... import _isVerbose |
| 118 | +from ... import Convert as _Convert |
118 | 119 | from ... import IO as _IO |
| 120 | +from ..._Exceptions import ConversionError as _ConversionError |
119 | 121 | from ..._Exceptions import IncompatibleError as _IncompatibleError |
120 | 122 | from ..._Exceptions import ThirdPartyError as _ThirdPartyError |
121 | 123 | from ..._SireWrappers import Molecule as _Molecule |
@@ -243,82 +245,20 @@ def run(self, molecule, work_dir=None, queue=None): |
243 | 245 | else: |
244 | 246 | raise IOError(msg) from None |
245 | 247 | 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. |
318 | 249 | 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) |
322 | 262 | except Exception as e: |
323 | 263 | msg = "Unable to create OpenFF Molecule!" |
324 | 264 | if _isVerbose(): |
|
0 commit comments