Accessing PubChem through PUG-REST: Part II#

About this interactive icons recipe
import requests
import time
from IPython.display import Image, display

1. Accessing PubChem data using a chemical name#

You can access PubChem data using a chemical name.

print("CID             :", requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/aspirin/cids/TXT").text.strip())
print("MolecularFormula:", requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/aspirin/property/MolecularFormula/TXT").text.strip())
print("MolecularWeight :", requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/aspirin/property/MolecularWeight/TXT").text.strip())
print("IsomericSMILES  :", requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/aspirin/property/IsomericSMILES/TXT").text.strip())
CID             : 2244
MolecularFormula: C9H8O4
MolecularWeight : 180.16
IsomericSMILES  : CC(=O)OC1=CC=CC=C1C(=O)O

It is very important to understand that, in the real world, people use a chemical name to refer to different, often closely related, chemicals. An example is a drug name, as shown in this example.

cids1 = requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/aleve/cids/TXT").text.strip().split()
print(cids1)
['156391', '23681059', '6925666']
for mycid in cids1:
    display(Image(requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + mycid + "/record/PNG?image_size=200x200").content))
    print("CID " + mycid, ":", requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + mycid + "/property/MolecularFormula/TXT").text)
    time.sleep(0.2)
../_images/ceb2eb736a4baf98ac60c0cf2962d49e3e620b6111c9c22fe46031e1ead16832.png
CID 156391 : C14H14O3
../_images/198f5296d0a55e99a74954b6a256e42b85418779a609e6ba2ae5047432467cb1.png
CID 23681059 : C14H13NaO3
../_images/735af5550dd31c8b76f75b46b4dc5b56b1f26935fe6d255c24ec82cc0aae73e7.png
CID 6925666 : C14H13O3-

Aleve” is the name of a drug product, whose active ingredient is naproxen sodium (CID 23681059). When dissolved in water, naproxen sodium dissociates into a naproxen anion (CID 6925666), which inhibits with the drug target, and a sodium atom. Note that the naproxen anion is the conjugate base of naproxen (CID 156391). While the three CIDs are structurally different, they are all freqently called “Aleve”.

It is also possible to get any PubChem compounds records whose name contains the string “aleve” (i.e., partial matching with the input name).

cids2 = requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/aleve/cids/TXT?name_type=word").text.strip().split()
print(cids2)
['56841568', '23681059', '156391', '6925666', '9581', '77098502']
cids3 = [ x for x in cids2 if x not in cids1]   # remove the hits from the exact match.

for mycid in cids3:
    display(Image(requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + mycid + "/record/PNG?image_size=200x200").content))
    print("CID " + mycid, ":", requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + mycid + "/property/MolecularFormula/TXT").text)
    time.sleep(0.2)
../_images/a5bf5d2aa0b0260638df0b17b97d84cbc3c573f47d0e4ee88ed7b15af5b7ec2c.png
CID 56841568 : C24H30ClNO4
../_images/72793c29696013f027987259a64b8046adb68e4869b32f23e3082adc2b770aef.png
CID 9581 : C10H16ClNO
../_images/36e4c75367ecbb2ec6a21b2b78bcc67496596fe776c3c0c46c57eb7975839f4b.png
CID 77098502 : C31H35ClNNaO4

2. Getting chemical names for a given chemical structure#

In the above example, the two CIDs from partial matching (CIDs 56841568 and CIDs 77098502) are the mixture of naproxen with other compounds. Because they are from partial matching with the name “aleve”, we know their names contain “aleve”, but what are they called exactly? We can figure this out by getting all synonyms for these compounds using PUG-REST.

for mycid in cids3:
    print("#-- CID", mycid)
    print(requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + mycid + "/synonyms/TXT").text)
    time.sleep(0.2)
#-- CID 56841568
Aleve-D Sinus & Cold
Aleve-D Sinus and Cold
Naproxen / pseudoephedrine
Aleve-D Sinus and headache
141623-29-2
DTXSID80931146
2-(6-Methoxynaphthalen-2-yl)propanoic acid--2-(methylamino)-1-phenylpropan-1-ol--hydrogen chloride (1/1/1)
#-- CID 9581
PSEUDOEPHEDRINE HYDROCHLORIDE
Pseudoephedrine Hcl
345-78-8
Rhinalair
Otrinol
Novafed
Sudomyl
Tussaphed
First sign
Sinufed
Sudafed
Sudafed hydrochloride
d-Pseudoephedrine hydrochloride
CoAdvil
(+)-Pseudoephedrine hydrochloride
Symptom 2
Sudafed 12 Hour
Besan
Nexafed
Pseudophedrine hydrochloride
Suphedrine
Galsud
Sun mark sinus
Sudafed 24 Hour
CPDD 0050
1S,2S-(+)-Pseudoephedrine hydrochloride
L(+)-Pseudoephedrine hydrochloride
UNII-6V9V2RYJ8N
EINECS 206-462-1
6V9V2RYJ8N
NSC 33634
NSC-33634
Topcare 12 hour decongestant
NSC 106567
NSC-106567
Efidac 24
GALPSEUD
Pseudoephedrine, hydrochloride, L-(+)-
CHEBI:8604
(1S,2S)-2-(methylamino)-1-phenylpropan-1-ol hydrochloride
D-(alpha-(1-Methylamino)ethyl)benzyl alcohol hydrochloride
EC 206-462-1
NSC33634
Benzenemethanol, alpha-(1-(methylamino)ethyl)-, hydrochloride, (S-(R*,R*))-
NSC106567
Pseudoephedrine hydrochloride [USAN:USP]
(+)-Pseudoephedrine.HCl
Benzenemethanol, alpha-((1S)-1-(methylamino)ethyl)-, hydrochloride, (alphaS)-
Benzenemethanol, alpha-(1-(methylamino)ethyl)-, hydrochloride, (S-(theta,theta))-
MYFED COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
ACTAHIST COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
ACTIFED COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
ALEVE-D COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
ALLERFED COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
CORPHED COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
HISTAFED COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
ISOCLOR COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF MYFED
Pseudoephedrine, (+)-
TRIPHED COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
ZYRTEC-D COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
ALLEGRA-D COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
BROMFED-DM COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
MUCINEX D COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF ACTIFED
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF ALEVE-D
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF CORPHED
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF ISOCLOR
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF TRIPHED
SEMPREX-D COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
TRIACIN-C COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
TRILITRON COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
BROMANATE DM COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
DIMETANE-DX COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
HYCOFENIX COMPONENT OF PSEUDOEPHEDRINE HYDROCHLORIDE
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF ACTAHIST
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF ALLEGRA-D
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF ALLERFED
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF HISTAFED
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF HYCOFENIX
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF MUCINEX D
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF SEMPREX-D
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF TRIACIN-C
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF TRILITRON
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF ZYRTEC-D
SINE-AID IB COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
Pseudoephedrine, L-(+)-
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF BROMANATE DM
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF BROMFED-DM
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF DIMETANE-DX
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF SINE-AID IB
Dorcol
ADVIL ALLERGY SINUS COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
CODIMAL-L.A. 12 COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF CODIMAL-L.A. 12
L-(+)-Pseudoephedrine hydrochloride
CHILDREN'S ADVIL COLD COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
CHILDREN'S MOTRIN COLD COMPONENT PSEUDOEPHEDRINE HYDROCHLORIDE
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF ADVIL ALLERGY SINUS
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF CHILDREN'S ADVIL COLD
Deconsal II
PSEUDOEPHEDRINE HYDROCHLORIDE COMPONENT OF CHILDREN'S MOTRIN COLD
Sudafed Liquid, Children's
PediaCare Decongestant Drops
Pseudoephedrine hydrochloride (USAN:USP)
Benzenemethanol, hydrochloride, S-(R,R)-
WLN: QYR & Y1 & M1 & GH-L
Dimetapp Decongestant
PSEUDOEPHEDRINE HYDROCHLORIDE (MART.)
PSEUDOEPHEDRINE HYDROCHLORIDE [MART.]
Actifed Sinus Daytime
PSEUDOEPHEDRINE HYDROCHLORIDE (USP-RS)
PSEUDOEPHEDRINE HYDROCHLORIDE [USP-RS]
Benzenemethanol, hydrochloride, [S-(R*,R*)]-
PSEUDOEPHEDRINE HYDROCHLORIDE (EP MONOGRAPH)
PSEUDOEPHEDRINE HYDROCHLORIDE (USP MONOGRAPH)
PSEUDOEPHEDRINE HYDROCHLORIDE [EP MONOGRAPH]
PSEUDOEPHEDRINE HYDROCHLORIDE [USP MONOGRAPH]
Pseudoephedrine hydrochloride [USAN]
D-[.alpha.-(1-Methylamino)ethyl]benzyl alcohol hydrochloride
walphed d
Pseudo-time
Zephrex D
Zephrex-D
Childrens Sudafed
Ornex and Maximum Strength Ornex
Sinus Decongestion
pseudoephedrine.hcl
Nasal DecongestantD
Harris Teeter Sinus
Sudafed (TN)
SudoGest 12 Hour
Sinus and Congestion
Nasal Decongestant D
Good Sense Suphedrine
equate sinus 12 hour
RugbyNasal Decongestant
Tylenol Sinus Medication, Maximum Strength
Isoephedrine Hydrochloride
Leader Nasal Decongestant
Picnic Nasal Decongestant
equaline nasal decongestant
SUDAFED Sinus Congestion
Topcare Nasal Decongestant
Contac Day & Night Allergy Sinus Day Caplets
psi-Ephedrine Hydrochloride
Sudogest Nasal Decongestant
12 Hour Nasal Decongestant
Pseudoephedrine HClUnit Dose
SCHEMBL33285
Good Sense Nasal Decongestant
MLS001304069
Good Sense Suphedrine12 Hour
SPECTRUM1500516
Sinus and CongestionNon-Drowsy
Equaline 12 Hour Decongestant
CHEMBL1200724
Sine-Off Maximum Strength No Drowsiness Formula Caplets
DTXSID10889343
BALXUFOVQVENIU-KXNXZCPBSA-N
Sudafed Sinus Congestion 12 Hour
Sudafed Sinus Congestion 24 Hour
Childrens SudafedNasal Decongestant
Childrens UniphedNasal Decongestant
HMS1920N04
Pharmakon1600-01500516
Leader 12 Hour Nasal Decongestant
Pseudoephedrine hydrochloride (USP)
CareOne Nasal and Sinus Decongestant
CCG-39241
NSC759616
AKOS027383893
Benzenemethanol, alpha-(1-(methylamino)ethyl)-, (S-(R*,R*))-, hydrochloride
NSC-759616
Benzenemethanol, .alpha.-[(1S)-1-(methylamino)ethyl]-, hydrochloride, (.alpha.S)-
Cvs Health Non-drowsy Nasal Decongestant
Good Neighbor Pharmacy Nasal Decongestant
PSEUDOEPHEDRINE HYDROCHLORIDE [MI]
Rugby Nasal DecongestantNasal Decongestant
SMR000718787
walphed dmaximum strength non drowsy sinus
(+)-Pseudoephedrine hydrochloride, >=98%
(1S,2S)-(+)-Pseudoephedrine hydrochloride
nasal decongestantmaximum strength non drowsy
NS00079374
P1654
PSEUDOEPHEDRINE HYDROCHLORIDE [VANDF]
PSEUDOEPHEDRINE HYDROCHLORIDE [WHO-DD]
D00485
Nasal DecongestantMaximum Strength, Non-Drowsy
PSEUDOEPHEDRINE HYDROCHLORIDE [ORANGE BOOK]
Sinus Pressure and Congestion ReliefMaximum Strength
W-106727
equaline nasal decongestantnon drowsy maximum strength
Q27108108
Topcare Nasal DecongestantMaximum Strength Non Drowsy
(+)-Pseudoephedrine hydrochloride, Sigma Reference Standard
(1S,2S)-2-(Methylamino)-1-phenyl-1-propanol Hydrochloride
(+)-Pseudoephedrine Hydrochloride 1.0 mg/ml in Methanol (as free base)
Pseudoephedrine hydrochloride, British Pharmacopoeia (BP) Reference Standard
Benzenemethanol, .alpha.-[(1S)-1-(methylamino)ethyl]-, hydrochloride (1:1), (.alpha.S)-
Benzenemethanol, alpha-((1S)-1-(methylamino)ethyl)-, hydrochloride (1:1), (alphaS)-
Pseudoephedrine hydrochloride, European Pharmacopoeia (EP) Reference Standard
Pseudoephedrine hydrochloride, United States Pharmacopeia (USP) Reference Standard
Pseudoephedrine hydrochloride, Pharmaceutical Secondary Standard; Certified Reference Material

#-- CID 77098502
Aleve PM
Naproxen sodium/DPH combination
Diphenhydramine HCl / naproxen sodium
BAY98-7111
BAY 98-7111
Naproxen Sodium and diphenhydramine combination
Diphenhydramine hydrochloride and naproxen sodium
S900006580

CID 56841568 is “Aleve-D Sinus & Cold” and CID 77098502 is “Aleve PM”