Home > Software engineering >  How to resolve 400 Client Error when mapping uniprot IDs programatically?
How to resolve 400 Client Error when mapping uniprot IDs programatically?

Time:07-17

Here's the code:

API_URL = "https://rest.uniprot.org"

def submit_id_mapping(from_db, to_db, ids):
    request = requests.get(
        f"{API_URL}/idmapping/run",
        data={"from": from_db, "to": to_db, "ids": ",".join(ids)},
    )
    #request.raise_for_status()
    return request.json()

submit_id_mapping(from_db="UniProtKB_AC-ID", to_db="ChEMBL", ids=["P05067", "P12345"])

Taken directly from the official example

And returning the following 404 client error. I tried accessing the url myself and it doesn't seem to work. Given that this is the official documentation I don't really know what to do. Any suggestions are welcomed.

{'url': 'http://rest.uniprot.org/idmapping/run',
 'messages': ['Internal server error']}

I also have another script for this but it no longer works! And I don't know why :(

in_f = open("filename")
url = 'https://www.uniprot.org/uploadlists/'

ids=in_f.read().splitlines()
ids_s=" ".join(ids)

params = {
'from': 'PDB_ID',
'to': 'ACC',
'format': 'tab',
'query': ids_s
}

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'))

Error:

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

CodePudding user response:

Try to change .get to .post:

import requests

API_URL = "https://rest.uniprot.org"


def submit_id_mapping(from_db, to_db, ids):
    data = {"from": from_db, "to": to_db, "ids": ids}
    request = requests.post(f"{API_URL}/idmapping/run", data=data)
    return request.json()


result = submit_id_mapping(
    from_db="UniProtKB_AC-ID", to_db="ChEMBL", ids=["P05067", "P12345"]
)
print(result)

Prints:

{'jobId': 'da05fa39cf0fc3fb3ea1c4718ba094b8ddb64461'}
  • Related