ChemSpiPy - A Python wrapper for the ChemSpider API
I recently had the task of matching up a large amount of poorly organised molecular properties data with the corresponding structures, where the data was only identified by name. To make matters worse, the names were mostly an inconsistent mix of common names and trade names.
The best tool I’ve found for this kind of problem is ChemSpider - you can enter any type of chemical identifier into the simple search and it will attempt to resolve a structure for you. The success rate seems to be much higher than other services, and it has a web API, so the whole process can be automated and performed for thousands of structures at a time.
I was just about to write a Python interface to the API from scratch, when I came across ChemSpiPy by Cameron Neylon, a bare bones Python wrapper for the API. I made a few bug fixes and extended the functionality, so now you can easily search ChemSpider and retrieve properties and identifiers for chemical structures from your Python scripts.
Usage is pretty straightforward - simply download the
chemspipy.py file and put it in the same directory as your Python script or somewhere on your Python search path. Then simply import it at the top of your Python script:
To search ChemSpider with any kind of identifier, use the find function:
comp_list = chemspipy.find('Benzene')
This will return a list of Compound objects that each correspond to a ChemSpiderID. Alternatively, find_one will just return the best match.
c = chemspipy.find_one('Benzene')
If you already know the ChemSpiderID of a compound, you can simply use it to initialise a Compound object.
from chemspipy import Compound c = Compound(236)
Compound objects have the following properties:
c.csid c.imageurl c.m. c.smiles c.inchi c.inchikey c.averagemass c.molecularweight c.monoisotopicmass c.nominalmass c.alogp c.xlogp c.commonname c.image c.mol
These are all retrieved lazily from ChemSpider only when requested to avoid unnecessary calls to the API. More details about what the API returns are available in the ChemSpider API Documentation. Before using the service, you’ll need to create an account on ChemSpider to get a security token.