Created
June 17, 2019 10:58
-
-
Save avrilcoghlan/f3c7ab4e3a1395543542c555f62d7b17 to your computer and use it in GitHub Desktop.
Use the PDB REST API to get a list of PDB entries that contain a particular ligand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# adapted example from https://github.com/PDBeurope/PDBe_Programming/blob/master/REST_API/snippets/basic_get_post.py | |
# edited to use the python 'requests' module | |
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/pdbe/api" | |
INPDB = "/pdb/compound/in_pdb" | |
#====================================================================# | |
def get_request(url, arg, pretty=False): | |
full_url = "%s/%s/%s?pretty=%s" % (SERVER_URL, url, arg, str(pretty).lower()) | |
# e.g. for ligand ID. 'ATP' we get: | |
# full_url = https://www.ebi.ac.uk/pdbe/api//pdb/compound/in_pdb/ATP?pretty=true | |
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 | |
# pull out the list of PDB ids. that have this ligand ID: | |
list_of_pdb_ids = json_results[arg] # 'arg' is the input ligand ID e.g. ATP | |
# print out the list of PDB ids.: | |
for pdb_id in range(len(list_of_pdb_ids)): | |
print(list_of_pdb_ids[pdb_id]) | |
return(list_of_pdb_ids) | |
#====================================================================# | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) | |
parser.add_argument('-e', type=str, default=None, action='store', help='the ligandID') | |
args = parser.parse_args() | |
# If you type: | |
# % python3 pdb_rest_example_get_pbids_with_ligand.py | |
# You will see: | |
# usage: pdb_rest_example_get_pbids_with_ligand.py [-h] [-e E] | |
# | |
# optional arguments: | |
# -h, --help show this help message and exit | |
# -e E the ligandID | |
# Note we defined at the top of the script that: | |
# INPDB = "/pdb/compound/in_pdb" | |
if args.e: | |
response = get_request(INPDB, 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