From: Noel O'B. <no...@ca...> - 2006-03-16 11:24:21
|
I'm not having great luck with this method. Attempt 1 Using Read after ReadFile ========= from openbabel import * myfile = "3d.head.sdf" obConversion = OBConversion() obConversion.SetInFormat("sdf") mymol = OBMol() obConversion.ReadFile(mymol,myfile) print mymol.GetMolWt() print obConversion.Read(mymol) print "The program segfaults before this line" Attempt 2 Using Read after ReadString ========= from openbabel import * myfilestring = open("3d.head.sdf","r").read() obConversion = OBConversion() obConversion.SetInFormat("sdf") mymol = OBMol() obConversion.ReadString(mymol,myfilestring) print mymol.GetMolWt() print obConversion.Read(mymol) print "The program aborts before this line" >From the python point of view, what is needed is a function that when repeatedly called, will return additional molecules. This doesn't need to be in the main openbabel library, but can be in the SWIG wrapper somewhere. Regards, Noel On Wed, 2006-03-15 at 14:18 -0500, Geoffrey Hutchison wrote: > On Mar 15, 2006, at 5:18 AM, Noel O'Boyle wrote: > > > It seems that OBConversion.ReadFile(OBMolecule) just reads in the > > first > > molecule, and I cannot see a way of iterating over the molecules. > > The OBConversion.Read(OBMol) function should continue to read from > the same file. However, since Python and Perl have no concept of > "streams", it's not possible to use OBConversion.Read() to set up the > input file or string. > > So something like this (sorry, I haven't written much Python, so the > syntax might be off): > > import openbabel > > mol = openbabel.OBMol() > conv = openbabel.OBConversion() > conv.SetInAndOutFormats("sdf", "sdf") > obConversion.ReadString(mol, "test.sdf") > > while (obConversion.Read(mol)) > ... > > > Cheers, > -Geoff |