Hi Noel,

Thanks very much, this works great! Also, If I add the DoNotAddH InChI option 
without adding explicit hydrogens with OBMol.AddHydrogens(), to the molecule 
before writing InChI, I get InChIs with no H’s.

So, C (methane), becomes:
InChI=1S/C

Instead of
InChI=1S/CH4/h1H4

But you are right, it seems that I was specifying the default options anyway, 
and I do not need to add explicit H nor add the DoNotAddH option. Thanks for 
the explanation. In general I found the InChIs are the same between the two 
methods (i.e., no options vs. DoNotAddH). I did find a few examples where these 
two methods produce different InChIs with Open Babel. However, I think this is 
an unrelated bug, maybe related to OBMol.AddHydrogens(), and not InChI? I’m not 
sure though. I’m happy to file a bug report if I uncovered something real.

Here is a Gist for an example of a molecule that has a different InChI based on 
the two methods. The first InChI is calculated with no InChI options, and the 
second with specifying DoNotAddH, and adding explicit hydrogens before 
calculation.

https://nbviewer.jupyter.org/gist/vfscalfani/724c47df3880edf4c0588001ba72d69e

Thanks again.

Vin


From: Noel O'Boyle <baoille...@gmail.com>
Sent: Wednesday, September 11, 2019 6:05 AM
To: Scalfani, Vincent <vfscalf...@ua.edu>
Cc: openbabel-discuss@lists.sourceforge.net
Subject: Re: [Open Babel] Error handling with smi file in pybel?

Hi Vin,

By default the command-line application stops on error, a behavior that can be 
overridden by "-e". I have to say that this behavior drives me bananas. But 
anyway...

I've just been trying to do the same through Python, but have been failing. 
Adding obconversion.AddOption("e", obconversion.GENOPTIONS) should work but 
doesn't. So I'll file a bug.

But for your purposes you don't need this. Just do what I always do :-)

# loop through molecules and print InChI strings to a file
with open("mySMILES.smi") as inp:
    with open("InChI_ouput.inchi", 'w') as f:
        for line in inp:
            try:
                mol = pybel.readstring("smi", line)
            except IOError:
                f.write("Open_Babel_Error\n")
                continue

Pro-tip: name your file handles "inp" and "out" instead of "f", etc. I've been 
there. :-)

Regarding what you're trying to do, AddHydrogens doesn't actually add hydrogens 
(I know, I know), it converts from implicit hydrogens to explicit. The 
hydrogens are already there on every atom. But it's no harm making them 
explicit, and you should get the same InChI either way. Similarly, I'd have to 
check to be sure, but I think the code that uses the InChI has "/DoNoAddH" 
implicitly (it's done through the API). Again, no harm specifying it.

On Mon, 9 Sep 2019 at 22:05, Scalfani, Vincent 
<vfscalf...@ua.edu<mailto:vfscalf...@ua.edu>> wrote:
Dear All,

I am trying to process a SMILES file (.smi) and calculate InChIs, where there 
are some SMILES that Open Babel will not be able to read, due to syntax or 
other errors. In those cases, I would like to print a string such as 
“Open_Babel_Error” on the line (or something even more descriptive).

However, everything I have tried results in Open Babel stopping once it reads a 
SMILES string it cannot parse. Here is where I am at (code below), I did read 
the Errors and Warnings documentation with OBMessageHandler class, but I could 
not figure out how to incorporate that. Any direction/examples would be much 
appreciated.

Thanks

Vin
--
import pybel

# Set up conversion to InChI
conv = pybel.ob.OBConversion()
conv.SetOutFormat("inchi")
conv.AddOption("X", conv.OUTOPTIONS, "DoNotAddH")

# Read a SMILES file
mols = pybel.readfile("smi","mySMILES.smi")

# loop through molecules and print InChI strings to a file
with open("InChI_ouput.inchi", 'w') as f:
    for mol in mols:
        if not mol:
            f.write("Open_Babel_Error\n".format(inchi))
            continue
        # N.B. Adding H's to OB Molecular Object, and not through InChI
        mol.OBMol.AddHydrogens()
        inchi = conv.WriteString(mol.OBMol)
        f.write("{}".format(inchi))
f.close()

—-
Vincent F. Scalfani, Ph.D.
University Libraries
The University of Alabama

_______________________________________________
OpenBabel-discuss mailing list
OpenBabel-discuss@lists.sourceforge.net<mailto:OpenBabel-discuss@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
_______________________________________________
OpenBabel-discuss mailing list
OpenBabel-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss

Reply via email to