It's impossible to access the values of data fields in
SD files using Python. The problem is that the
GetData() method should return OBPairData(), but
instead returns OBGenericData(). The latter does not
have a GetValue() method.
Here is some example code:
Python
from openbabel import *
obconversion = OBConversion()
obconversion.SetInFormat("sdf")
obmol = OBMol()
notatend = obconversion.ReadFile(obmol,"test.sdf")
assert notatend, "No molecules in file!"
assert obmol.HasData('Molecular weight'), "Doesn't have
a field called molecular weight!"
data = obmol.GetData('Molecular weight')
print "The data has the following class: %s" %
data.class
print "The available methods include:"
print dir(data)
Output
The data has the following class: <class 'openbabel.obgenericdata'="">
The available methods include:
['Clone', 'GetAttribute', 'GetDataType', SetAttribute',
'class', 'del', 'delattr', 'dict',
'doc', 'getattribute', 'hash', 'init',
'module', 'new', 'reduce','reduce_ex',
'repr', 'setattr', 'str',
'swig_destroy', 'weakref', 'this', 'thisown']</class>Notes
Please find attached test.sdf
One molecule sdf file with fields
Logged In: YES
user_id=1189615
A possible solution to this is to give OBGenericData a
virtual GetValue function returning a std::string. Calling
GetValue on a OBGenericData pointer to a OBPairData object
would use the OBPairData function. The base class version
actually returns the Attribute, which was easier than making
it return an empty string, which would look neater, but it
should never be called.
This solution is not very elegant, making OBGenericData just
a little bit less generic. But it isn't too bad - lots of
classes might find returning a string value useful - and I
hope gets the job done. (OBCommentData has a GetData
function - I'll make its virtual GetValue do the same.)
I've added the required code to generic.h in SVN. I can't
test with Python since we still lack a Windows version.
Logged In: YES
user_id=850620
Thanks! It's working now...
data.GetValue() returns 122.03677944
Logged In: YES
user_id=21420
This bug was fixed, correct? I'm marking this "pending" so it will close unless
someone comments and re-opens it.
I do believe this is now fixed in the development repository. (Almost typed CVS
there -- now it's SVN...)
-Geoff
Logged In: YES
user_id=1312539
This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).