Friday, 19 June 2009

Using PyActiveResource to access ChemCaster

ChemCaster, from Rich's Metamolecular, is a platform for developing web-based cheminformatics applications. The advantage of such a system is that the user does not need to install any special software, nor does the application developer need to maintain a server.

Rich invited me to take it for a spin, so I signed up for a trial account and moved quickly on to my first problem, how do I access the API through Python?

It turns out that RESTful APIs tend to have common patterns, a fact which is taken advantage of by Active Resource, a Ruby library for defining classes which directly map onto the objects implied by a RESTful API. Or something like that - I neglected to read any documentation. Instead I just took Rich's example and tried to code it up in Python using PyActiveResource (this is a documentation-free project so using it is quite exciting).

Et voilá
# Requires PyActiveResource (http://code.google.com/p/pyactiveresource/)
from pyactiveresource.activeresource import ActiveResource
registry_id = "16" # Replace this with your registry_id
password = "password"
user = "baoilleach@gmail.com"
site = "https://chemcaster.com/registries/$registry_id/"
class Structure(ActiveResource):
_site = site
_user = user
_password = password
def demo():
name = "benzene"
molfile = "[NO NAME]\n CHEMWRIT 2D\nCreated with ChemWriter - http://metamolecular.com/chemwriter\n 6 6 0 0 0 0 0 0 0 0 0 V2000\n 5.0800 -5.0400 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 5.9460 -5.5400 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 6.8121 -5.0400 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 6.8121 -4.0400 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 5.9460 -3.5400 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 5.0800 -4.0400 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n 1 2 2 0 0 0 0\n 2 3 1 0 0 0 0\n 3 4 2 0 0 0 0\n 4 5 1 0 0 0 0\n 5 6 2 0 0 0 0\n 6 1 1 0 0 0 0\nM END"
s = Structure({'name':name, 'molfile':molfile},
{'registry_id': registry_id})
success = s.save()
if not success:
raise Exception, "Didn't work!"
if __name__ == "__main__":
demo()
view raw chemcaster.py hosted with ❤ by GitHub

1 comment:

Rich Apodaca said...

@Noel, thanks for the writeup and the library.

Creating Chemcaster libraries should now be a lot easier with the availability of the Chemcaster Web API documentation.