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> 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
> 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