kylelutz / chemkit

A C++ library for molecular modelling, cheminformatics and molecular visualization.
http://www.chemkit.org
BSD 3-Clause "New" or "Revised" License
54 stars 26 forks source link

chemkit.MoleculeFile should accept Python file objects #26

Closed itdaniher closed 12 years ago

itdaniher commented 12 years ago

Right now, chemkit.MoleculeFile takes a filename passed as string.

This keeps you from doing useful things like wrapping a string as a file object with io.BytesIO. When you have a database / web query that returns Molfile information as a string, it's a pain to have to write it to a temporary file before parsing it with chemkit.

A possible alternative to overhauling the .read() method might be to add another method to moleculefile.pxi that lets you pass a string and bypass the GenericFile object all together.

kylelutz commented 12 years ago

I've pushed a few topics which implement a readString() and a writeString() method for the MoleculeFile class (http://review.source.kitware.com/#/t/803/ and http://review.source.kitware.com/#/t/804/). After going through code review they should be merged into master.

itdaniher commented 12 years ago

So just tested the code up for review - something's not right:

import chemkit s = open("ethane.sdf").read() f = chemkit.MoleculeFile() f.readString(s) False f.read("ethane.sdf") True

ethane.sdf is at http://itdaniher.com/ethane.sdf.

kylelutz commented 12 years ago

The readString() method requires a file format to be set (i.e. call f.setFormat("sdf") first). The read("ethane.sdf") call works because it will automatically set the file format using the suffix of the file name. With just the contents of the file in the string we can't do that (or at least not yet).

Just FYI, after read() or readString() return False you can find out what went wrong by using the errorString() method as follows:

f.readString(s) False print f.errorString() No file format set for reading.

kylelutz commented 12 years ago

Fixed: 2c993cf7034d55c290298acbdda15e6ec62ce3d7