PubChemPy examples

Table of Contents

1. Introduction

PubChemPy provides a way to interact with PubChem in Python. It allows chemical searches by name, substructure and similarity, chemical standardization, conversion between chemical file formats, depiction and retrieval of chemical properties.

Here’s a quick example showing how to search for a compound by name:


In [4]:
from pubchempy import get_compounds

for compound in get_compounds('glucose', 'name'):
    print(compound.cid)
    print(compound.isomeric_smiles)


5793
C([C@@H]1[C@H]([C@@H]([C@H](C(O1)O)O)O)O)O
79025
C([C@@H]1[C@H]([C@@H]([C@H]([C@H](O1)O)O)O)O)O
64689
C([C@@H]1[C@H]([C@@H]([C@H]([C@@H](O1)O)O)O)O)O
206
C(C1C(C(C(C(O1)O)O)O)O)O

So how does this work behind the scenes?

  1. We call the PubChemPy function get_compounds with the parameters 'glucose' and 'name'
  2. This is translated into a request for the PubChem PUG REST API.
  3. PubChemPy parses the JSON response into a list of Compound objects.
  4. Each Compound has properties like cid and isomeric_smiles, which we print.

Here’s how you get calculated properties for a specific compound:


In [5]:
from pubchempy import Compound

vioxx = Compound.from_cid(5090)
print vioxx.molecular_formula
print vioxx.molecular_weight
print vioxx.xlogp


C17H14O4S
314.35566
2.3

When using PubChemPy, it is important to remember that every request you make is transmitted to the PubChem servers, evaluated, and then a response is sent back. There are some downsides to this: It is less suitable for confidential work, it requires a constant internet connection, and some tasks will be slower than if they were performed locally on your own computer. On the other hand, this means we have the vast resources of the PubChem database and chemical toolkits at our disposal. As a result, it is possible to do complex similarity and substructure searching against a database containing tens of millions of compounds in seconds, without needing any of the storage space or computational power on your own local computer.

You don’t need to worry too much about how the PubChem web service works, because PubChemPy handles all of the details for you. But if you want to go beyond the capabilities of PubChemPy, there is some helpful documentation on the PubChem website.