Re: [Rdkit-devel] Python vs C++ exceptions
Open-Source Cheminformatics and Machine Learning
Brought to you by:
glandrum
|
From: Greg L. <gre...@gm...> - 2010-12-13 16:14:29
|
HI Gianluca,
On Mon, Dec 13, 2010 at 3:50 PM, gi...@gm... <gi...@gm...> wrote:
> While reading some SD molecules in python, I've got on the console
> messages like:
>
> [15:18:47] Explicit valence for atom # 0 N, 4, is greater than permitted
> [15:18:47] ERROR: Could not sanitize molecule ending on line 48
> [15:18:47] ERROR: Explicit valence for atom # 0 N, 4, is greater than permitted
>
> Then my script breaks (rightfully) when accessing the structure which
> is not loaded
>
> Traceback (most recent call last):
> File "importdata.py", line 23, in <module>
> molecule = Chem.MolToSmiles(sup[0])
> Boost.Python.ArgumentError: Python argument types in
> rdkit.Chem.rdmolfiles.MolToSmiles(NoneType)
> did not match C++ signature:
> MolToSmiles(RDKit::ROMol {lvalue} mol, bool isomericSmiles=False,
> bool kekuleSmiles=False, int rootedAtAtom=-1)
>
>
> I've checked the C++ code and it seems the error comes from the
> calcExplicitValence() method in Code/GraphMol/Atom.cpp
>
> Now my question is: since the method throws MolSanitizeException(msg);
> maybe there should be a way from python to catch it?
The errors are actually already caught in the python wrapper. You're
seeing the error message so that you know what happend, but the
exception doesn't make it back to python. What makes it back is a None
instead of a molecule. If you look at the error message above you will
see that you have called MolToSmiles with a NoneType.
This is very easy to test for:
#--------
supplier = Chem.SDMolSupplier('foo.sdf')
for mol in supplier:
if mol is None: continue
#otherwise do whatever you need to do with the molecule.
#--------
Best regards,
-greg
|