Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save avrilcoghlan/70c98573c7a77d7df560fc95f551bcba to your computer and use it in GitHub Desktop.
Save avrilcoghlan/70c98573c7a77d7df560fc95f551bcba to your computer and use it in GitHub Desktop.
Script to use the UniChem REST API to get the PDB ligand identifier for a particular ChEMBL identifier
#!/usr/bin/env python
# script to find out the PDB three-letter ligand id. for a ChEMBL id., using UniChem
import argparse
import sys
import requests # this is used to access json files
PY3 = sys.version > '3'
if PY3:
import urllib.request as urllib2
else:
import urllib2
SERVER_URL = "https://www.ebi.ac.uk/unichem/rest"
UNICHEM = "/src_compound_id"
#====================================================================#
def get_request(url, arg, pretty=False):
full_url = "%s/%s/%s/1/3" % (SERVER_URL, url, arg)
# e.g. for ChEMBL id. CHEMBL14249 we get:
# full_url = https://www.ebi.ac.uk/unichem/rest//src_compound_id/CHEMBL14249/1/3
print("This is the url string:\n{}".format(full_url))
json_results = requests.get( full_url ).json() #This calls the information back from the API using the 'requests' module, and converts it to json format
# e.g. [{'src_compound_id': 'ATP'}]
# This seems to have been changed from json format to Python format by the 'requests' module.
# Pull out the dictionary:
json_results2 = json_results[0] # e.g. a dictionary like {'src_compound_id': 'ATP'}
# pull out the PDB ligand id. for this ChEMBL id.:
ligand_id = json_results2['src_compound_id']
print("Ligand id. in PDB:",ligand_id)
return(json_results)
#====================================================================#
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-e', type=str, default=None, action='store', help='the chemblID')
args = parser.parse_args()
# If you type:
# % python3 unichem_rest_example_get_pdbligandids_for_chemblid.py
# usage: unichem_rest_example_get_pdbligandids_for_chemblid.py [-h] [-e E]
# optional arguments:
# -h, --help show this help message and exit
# -e E the chemblID
# Note we defined at the top of the script that:
# UNICHEM = "/rest/src_compound_id"
if args.e:
response = get_request(UNICHEM, args.e, True)
else:
parser.print_help()
sys.exit(1)
print("FINISHED\n")
#====================================================================#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment