Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save avrilcoghlan/e44ce43224ac601f53f1d58944ce93cf to your computer and use it in GitHub Desktop.
Save avrilcoghlan/e44ce43224ac601f53f1d58944ce93cf to your computer and use it in GitHub Desktop.
script to retrieve the UniProt id for a particular PDB id
#!/usr/bin/env python
# example from https://github.com/PDBeurope/PDBe_Programming/blob/master/REST_API/snippets/basic_get_post.py
# edited to use the python 'requests' module, and to get the UniProt id. for a particular PDBe entry id.
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"
UNIPROT = "/mappings/uniprot"
#====================================================================#
def get_request(url, arg, pretty=False):
full_url = "%s/%s/%s?pretty=%s" % (SERVER_URL, url, arg, str(pretty).lower())
# e.g. for PDB id. 1ivv we get:
# full_url = https://www.ebi.ac.uk/pdbe/api//mappings/uniprot/1ivv?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 UniProt id. for this PDB id:
uniprot_id = json_results[arg] # 'arg' is the input PDB ID e.g. 1ivv
uniprot_id2 = uniprot_id["UniProt"]
uniprot_id3 = list(uniprot_id2.keys()) # a list of the UniProt ids. for this input PDB id.
assert(len(uniprot_id3) == 1)
uniprot_id4 = uniprot_id3[0]
print("UniProt id=",uniprot_id4)
return uniprot_id
#====================================================================#
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-e', type=str, default=None, action='store', help='the uniprotid')
args = parser.parse_args()
# If you type:
# % python3 pdb_rest_example_get_uniprot_for_pdbid.py
# You will see:
# usage: pdb_rest_example_get_uniprot_for_pdbid.py [-h] [-e E]
#
# optional arguments:
# -h, --help show this help message and exit
# -e E the uniprotid
# Note we defined at the top of the script that:
# INPDB = "/mappings/uniprot"
if args.e:
response = get_request(UNIPROT, 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