rdkit-rs / rdkit

RDKit Made Idiomatic for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Catching RDKit exceptions: make `mol_to_smiles` fallible

JHucker opened this issue · comments

It is possible for RDKit to throw an exception when a mol is converted to SMILES. This occasionally occurs to me in python when hoovering up large amounts of data and converting to canonical SMILES. In these cases I simply wrap that section in a try-except and move on.

I use these rust bindings to verify whether generated SMILES are valid, then convert to canonical SMILES. When an RDKit exception is thrown in these cases my application will panic. This is the motivation behind the requested changes in this PR.

Please let me know if these requested changes are acceptable / if any alterations are required.

Python excerpt showing RDKit unable to complete a roundtrip for some generated invalid SMILES:

Python 3.11.5 | packaged by conda-forge | (main, Aug 27 2023, 03:34:09) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rdkit
>>> rdkit.__version__
'2022.09.1'
>>> from rdkit import Chem
>>> smi = r"CCOC(=O)/C=S(/c1ccc(C(F)(F)F)cc1)=C1/C=C(\C)CCC1=O"
>>> mol = Chem.MolFromSmiles(smi)
>>> print(Chem.MolToSmiles(mol))
[15:08:59] 

****
Pre-condition Violation
bad bond type
Violation occurred on line 121 in file /project/build/temp.linux-x86_64-cpython-311/rdkit/Code/GraphMol/Canon.cpp
Failed Expression: bond->getBondType() == Bond::SINGLE || bond->getIsAromatic()
****

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Pre-condition Violation
        bad bond type
        Violation occurred on line 121 in file Code/GraphMol/Canon.cpp
        Failed Expression: bond->getBondType() == Bond::SINGLE || bond->getIsAromatic()
        RDKIT: 2022.09.1
        BOOST: 1_78