I'm having trouble removing bonds from molecules with the Python API. (In particular, I want to remove "Unknown"-type bonds, but the problem seems to apply to all bonds. For example, after loading the JEXNAB crystal with the 2018 Python API, executing the following snippet gives the following error:

 

>>> crystal.molecule.remove_bonds([b for b in crystal.molecule.bonds])

  File <long-fs-path>/miniconda/lib/python2.7/site-packages/ccdc/molecule.py", line 1815, in remove_bonds
    raise RuntimeError('A bond is not in the molecule')
RuntimeError: A bond is not in the molecule

 

The same thing happens if I try to remove only 1 bond. Any idea what's going on?

 

Thanks!

The problem is that crystal.molecule returns a fresh copy of a molecule object and so the bonds in crystal.molecule.bonds are in a different molecule from that on which you are calling remove_bonds.

The solution is to extract the molecule once, then re-assign it to the crystal, i.e.

mol = crystal.molecule

mol.remove_bonds(b for b in mol.bonds if b.bond_type == 'Unknown')

crystal.molecule = mol

You may also consider using molecule.assign_bond_types('Unknown') which will deduce bond types where possible.

I think the API might be a little nicer if the crystal object had methods to remove atoms and bonds.  I shall certainly consider it for a  future release.

Best wishes

Richard

 

Oh, great. That makes a lot of sense!

You must be signed in to post in this forum.