This is a test version of Biostars. For the public version, visit https://www.biostars.org.
UniProt REST Mapping Problem

I've got a list of 237 Ensembl protein IDs (e.g. ENSP00000493027), and I'm trying to convert them to UniProt accession numbers so that I can retrieve their REST text entry (e.g. https://rest.uniprot.org/uniprotkb/A0A286YF28.txt).

Officially, UniProt says to do it this way:

import urllib.parse
import urllib.request

url = 'https://www.uniprot.org/uploadlists/'

params = {
'from': 'ACC+ID',
'to': 'ENSEMBL_ID',
'format': 'tab',
'query': 'P40925 P40926 O43175 Q9UM73 P97793'
}

data = urllib.parse.urlencode(params)
data = data.encode('utf-8')
req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as f:
   response = f.read()
print(response.decode('utf-8'))

However, when I run their example code verbatin, I get:

urllib.error.HTTPError: HTTP Error 405: Not Allowed

Along with some tracebacks. What's going on? It happens when I pass my own data too. Never used UniProt programmatically before.

Thanks in advance for any help anyone can give.

uniprot mapping python rest ensembl

2 answers

Here is an example how to access UniProt's REST with Python 3 with the requests package (pip install requests).

import json
import requests
import time

URL = 'https://rest.uniprot.org/idmapping'
IDS = ['P40925', 'P40926', 'O43175', 'Q9UM73', 'P97793']


params = {
   'from': 'UniProtKB_AC-ID',
   'to': 'Ensembl_Protein',
   'ids': ' '.join(IDS)
}

response = requests.post(f'{URL}/run', params)
job_id = response.json()['jobId']
job_status = requests.get(f'{URL}/status/{job_id}')
d = job_status.json()

# Make three attemps to get the results
for i in range(3):
   if d.get("job_status") == 'FINISHED' or d.get('results'):
      job_results = requests.get(f'{URL}/results/{job_id}')
      results = job_results.json()
      for obj in results['results']:
         print(f'{obj["from"]}\t{obj["to"]}')
      break
   time.sleep(1)

Output:

P40925  ENSP00000233114.8
P40925  ENSP00000410073.2
P40925  ENSP00000446395.2
P40926  ENSP00000327070.5
P40926  ENSP00000408649.2
O43175  ENSP00000493175.1
O43175  ENSP00000493382.1
Q9UM73  ENSP00000373700.3
P97793  ENSMUSP00000083840

Awesome, thank you so much! This works; should be relatively straightforward to adapt to my application.

Use the new help document https://www.uniprot.org/help/id_mapping

Thanks, although those are just bash curl requests - forgive the newbie question, but how do I do the same thing from within Python 3?

The above help document should be updated with Python code snippet. Please keep an eye on that.

Log in to answer this question.