Compound¶
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)
C_{9}H_{8}O_{4}
>>> print(compound.molecular_weight)
180.1574
>>> print(compound.smiles)
CC(=O)Oc1ccccc1C(=O)O
>>> print(compound.common_name)
Aspirin
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))
28181
>>> print(refs[0])
{'source': 'ChemBank', 'sourceUrl': 'http://chembank.broadinstitute.org/', 'externalId': 'DivK1c_000555', 'externalUrl': 'http://chembank.broad.harvard.edu/chemistry/viewMolecule.htm?cbid=1171'}
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': 'http://pubchem.ncbi.nlm.nih.gov/', 'externalId': 2244, 'externalUrl': 'http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=2244'}]
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'])
CC(=O)Oc1ccccc1C(=O)O
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
Compound record ID.
Return type: int
-
Compound.
image_url
Return the URL of a PNG image of the 2D chemical structure.
Return type: string
-
Compound.
molecular_formula
Return the molecular formula for this Compound.
Return type: string
-
Compound.
inchi
Return the InChI for this Compound.
Return type: string
-
Compound.
inchikey
Return the InChIKey for this Compound.
Return type: string
-
Compound.
average_mass
Return the average mass of this Compound.
Return type: float
-
Compound.
molecular_weight
Return the molecular weight of this Compound.
Return type: float
-
Compound.
monoisotopic_mass
Return the monoisotopic mass of this Compound.
Return type: float
-
Compound.
nominal_mass
Return the nominal mass of this Compound.
Return type: float
-
Compound.
common_name
Return the common name for this Compound.
Return type: string
-
Compound.
mol_2d
Return the MOL file for this Compound with 2D coordinates.
Return type: string
-
Compound.
mol_3d
Return the MOL file for this Compound with 3D coordinates.
Return type: string
-
Compound.
image
Return a 2D depiction of this Compound.
Return type: bytes
-