Many ChemSpiPy search methods return Compound objects, which provide more functionality that a simple list of ChemSpider IDs. The primary benefit is allowing easy access to further compound properties after performing a search.

Creating a Compound

The easiest way to create a Compound for a given ChemSpider ID is to use the get_compound() method:

>>> compound = cs.get_compound(2157)

Alternatively, a Compound can be instantiated directly:

>>> compound = Compound(cs, 2157)

Either way, no requests are made to the ChemSpider servers until specific Compound properties are requested:

>>> print(compound.molecular_formula)
>>> print(compound.molecular_weight)
>>> print(compound.smiles)
>>> print(compound.common_name)

Properties are cached locally after the first time they are retrieved, speeding up subsequent access and reducing the number of unnecessary requests to the ChemSpider servers.

External References

Get a list of all external references for a given compound using the external_references property:

>>> refs = compound.external_references
>>> print(len(refs))
>>> print(refs[0])
{'source': 'ChemBank', 'sourceUrl': '', 'externalId': 'DivK1c_000555', 'externalUrl': ''}

Each reference is a dict with details for an external source. The list of references can be very large and slow to retrieve for popular compounds, so it is possible to filter it by datasource. To do this, use the get_external_references() method directly:

>>> refs = cs.get_external_references(2157, datasources=['PubChem'])
>>> print(refs)
[{'source': 'PubChem', 'sourceUrl': '', 'externalId': 2244, 'externalUrl': ''}]

See the Data Sources documentation for how to get a list of all available data sources.

Searching for Compounds

See the searching documentation for full details.

Implementation Details

Each Compound object is a simple wrapper around a ChemSpider ID. Behind the scenes, the property methods use the get_details(), convert(), get_image(), and get_external_references() API methods to retrieve the relevant information. It is possible to use these API methods directly if required:

>>> info = cs.get_details(2157)
>>> print(info.keys())
dict_keys(['id', 'smiles', 'formula', 'averageMass', 'molecularWeight', 'monoisotopicMass', 'nominalMass', 'commonName', 'referenceCount', 'dataSourceCount', 'pubMedCount', 'rscCount', 'mol2D', 'mol3D'])
>>> print(info['smiles'])

Results are returned as a python dictionary that is derived directly from the ChemSpider API JSON response.

Compound Properties

class chemspipy.objects.Compound

Compound record ID.

Return type:int

Return the URL of a PNG image of the 2D chemical structure.

Return type:string

Return the molecular formula for this Compound.

Return type:string

Return the InChI for this Compound.

Return type:string

Return the InChIKey for this Compound.

Return type:string

Return the average mass of this Compound.

Return type:float

Return the molecular weight of this Compound.

Return type:float

Return the monoisotopic mass of this Compound.

Return type:float

Return the nominal mass of this Compound.

Return type:float

Return the common name for this Compound.

Return type:string

Return the MOL file for this Compound with 2D coordinates.

Return type:string

Return the MOL file for this Compound with 3D coordinates.

Return type:string

Return a 2D depiction of this Compound.

Return type:bytes

Return external references for this Compound.

Return type:list[dict]