yo Folks,

You cannot use exception handling for this - as the molecule-input methods
do not throw exceptions.  You can see this with:

#!/usr/bin/env python

import rdkit
from rdkit import Chem
try:
    print 1
    Chem.MolFromSmiles('XXX')
    print 2
except Exception as msg:
    print 3
    print "That's broken mate: ", msg

# Outputs:
1
[19:27:21] SMILES Parse Error: syntax error for input: XXX
2

I suspect this is the behaviour you want - I frequently read multi-million
molecule files, and I don't want the whole process to fail because of a few
invalid molecules.  So returning None on those few molecules is kind of
intuitive.  I can see people arguing the other way round (they'd rather get
an exception and handle invalid cases - but then you'd need this
boilerplate everytime) - so it is hard to take a side in this argument.  It
is, of course, always good to know why a molecule failed (and which one),
but as Greg said it's something on the todo list.  Meanwhile I came up with
this code which I think does what I want without opening another process (I
am leaving it here as I know I will be looking this up in a few months
time).

#!/usr/bin/env python

import os
import sys

import rdkit
from rdkit import Chem

# keep the current stderr
stderr_fileno = sys.stderr.fileno()
stderr_save = os.dup(stderr_fileno)
# file descriptor of log file
stderr_fd = open('error.log', 'w')
os.dup2(stderr_fd.fileno(), stderr_fileno)

Chem.MolFromSmiles('X') # logging from this goes into error.log

# close the log file
stderr_fd.close()
# restore old sys err
os.dup2(stderr_save, stderr_fileno)

Chem.MolFromSmiles('Y') # logging from this goes to std err (terminal?)



The idea for this is lifted from
http://stackoverflow.com/questions/24277488/in-python-how-to-capture-the-stdout-from-a-c-shared-library-to-a-variable
- but this isn't very pythonesque and/or understandable.  So all in all an
unproductive afternoon fighting with 6 to 8 lines of code, but I can say
the python has been finally tamed.  For today.

Have a good weekend folks and thanks for all your answers.

Beertime,
JP

p.s. I am somewhat relieved this is not as straightforward as I first
thought.

-
Jean-Paul Ebejer
Early Stage Researcher

On 23 January 2015 at 16:13, Abhik Seal <[email protected]> wrote:

> Hi JP,
>
> I think you can use try and catch ,
>
>     try:
>         throws()
>         return 0
>     except Exception, err:
>         sys.stderr.write('ERROR: %s\n' % str(err))
>         return 1
>
>  Hope this helps.
>
> Abhik Seal
> Indiana University Bloomington
> School of Informatics and Computing
> Cheminformatics and Chemgenomics group <http://registratio54.wix.com/ccrg>
> [email protected]
> http://mypage.iu.edu/~abseal/index.htm
>
> On Fri, Jan 23, 2015 at 8:58 AM, JP <[email protected]> wrote:
>
>>
>> Yo RDKitters,
>>
>> I am stuck on something so basic, its embarrassing.  But for the life of
>> me I cannot figure it out on my own.  This is probably more of a python
>> question than an RDKit one.
>>
>> I want to capture the RDKit warning/error message from python.  e.g.
>>
>> >>> import rdkit
>> >>> from rdkit import Chem
>> >>> Chem.MolFromSmiles('XXX')
>> [14:51:32] SMILES Parse Error: syntax error for input: XXX
>>
>> I want to capture that error message (which in this case isn't very
>> informative, but if you read in a mol2 you can get something like
>> [14:25:46] 3ZGZ.A: warning - O.co2 with non C.2 or S.o2 neighbor. Which I
>> am also interested in).  The big picture is that I built a web up where you
>> upload an sdf, mol, mol2 or smi file - and I want to show RDKit's error
>> message if there something funny in the query file.
>>
>> I have tried the obvious (I think):
>>
>> #!/usr/bin/env python
>> import sys
>> import rdkit
>> from rdkit import Chem
>>
>> f = open("err.log", "w")
>> original_stderr = sys.stderr
>> sys.stderr = f
>> Chem.MolFromSmiles('XXX')
>> sys.stderr = original_stderr
>> f.close()
>>
>> This still shows the error message in the terminal and not in the file.
>> I tried the same for stdout, still no cigar.
>>
>> Any ideas ?
>>
>> THANKS!
>> JP
>>
>>
>> ------------------------------------------------------------------------------
>> New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
>> GigeNET is offering a free month of service with a new server in Ashburn.
>> Choose from 2 high performing configs, both with 100TB of bandwidth.
>> Higher redundancy.Lower latency.Increased capacity.Completely compliant.
>> http://p.sf.net/sfu/gigenet
>> _______________________________________________
>> Rdkit-discuss mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>>
>>
>
------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
_______________________________________________
Rdkit-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to