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