Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
script to retrieve the UniProt id for a particular PDB id
#!/usr/bin/env python
# example from
# 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
import urllib2
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 =
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
# You will see:
# usage: [-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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.