Hi Lukas,

On Thu, Feb 22, 2018 at 1:14 PM, Lukas Pravda <lpra...@ebi.ac.uk> wrote:

> Dear rdkiters,
> I’m constructing molecules from scratch using python 3.5.4 and RDKit
> 2017.09.2 and due to the variety of reasons some of them are violating
> general principles of chemistry in a way implemented in rdkit, so I’m
> getting information like:
> Explicit valence for atom # 14 N, 4, is greater than permitted etc.
> I wonder if there is a way how to retrieve this piece of information in a
> programmatic way. In order to work with it. Presently, rdkit only prints
> this out into terminal and Chem.SanitizeMol() only returns first
> sanitization flag with the issue. Ideally, I’d like no information to be
> printed into console, while keeping the log info ‘Explicit valence for atom
> # 14 N, 4, is greater than permitted’ preferably in a structured way (in a
> property/method?), in order to further deal with those erroneous cases.
At last part of this is pretty straightforward.

There are two parts:
- making it so error messages don't go to the console
- capturing the failed operation.

The first is a bit fragile (i.e. doesn't always work), so you will
sometimes end up still seeing error messages (as here), but the second
should be reliable:

In [30]: rdBase.DisableLog('rdApp.*')

In [31]: m = Chem.MolFromSmiles('c1cccc1',sanitize=False)

In [32]: Chem.SanitizeMol(m,catchErrors=True)
[14:29:37] Can't kekulize mol.  Unkekulized atoms: 0 1 2 3 4

Out[32]: rdkit.Chem.rdmolops.SanitizeFlags.SANITIZE_KEKULIZE

In [35]:
[14:31:37] Explicit valence for atom # 1 O, 3, is greater than permitted
Out[35]: rdkit.Chem.rdmolops.SanitizeFlags.SANITIZE_PROPERTIES

You can see that the return value indicates what went wrong in the

I hope this helps,
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Rdkit-discuss mailing list

Reply via email to