Skip to content

Instantly share code, notes, and snippets.

@JoaoRodrigues
Created June 7, 2019 22:54
Show Gist options
  • Save JoaoRodrigues/afe11985e4cab4c0002eebae2213e0a8 to your computer and use it in GitHub Desktop.
Save JoaoRodrigues/afe11985e4cab4c0002eebae2213e0a8 to your computer and use it in GitHub Desktop.
Example of Uniprot REST API (Python)
#!/usr/bin/env python
"""
Queries Uniprot database and retrieves (canonical) sequences.
"""
import argparse
import sys
import urllib.request
ap = argparse.ArgumentParser()
ap.add_argument('seqfile', help='Input file with protein names')
ap.add_argument('-o', dest='output', help='Output file for protein sequences')
args = ap.parse_args()
if args.output:
ostream = open(args.output, 'w')
else:
ostream = sys.stdout
with open(args.seqfile) as handle:
for line in handle:
line = line.strip()
if not line:
continue
url = f'https://www.uniprot.org/uniprot/?query={line}&format=fasta'
with urllib.request.urlopen(url) as r:
fasta = r.read().decode('utf-8').strip()
print(fasta, file=ostream)
if args.output:
ostream.close()
@jacobastern
Copy link

Thanks for this snippet! Based on your snippet, I figured out how to batch download an entire list of queries, and came up with this function. This function also avoids some false positives by only querying by Uniprot ENTRY name:

import urllib.request

def get_protein_sequences(uniprot_list):
    """Retrieves the sequences from the UniProt database based on the list of
    UniProt ids.
    In general, 
        1. Compose your query here with the advanced search tool:
    https://www.uniprot.org/uniprot/?query=id%3Ap40925+OR+id%3Ap40926+OR+id%3Ao43175&sort=score
        2. Replace `&sort=score` with `&format=fasta`
        3. Edit this function as necessary
    Returns:
        protein_dict (dict): the updated dictionary
    """
    # This makes it so we match only the ENTRY field
    uniprot_list = ['id%3A'+id for id in uniprot_list]
    line = '+OR+'.join(uniprot_list)
    url = f'https://www.uniprot.org/uniprot/?query={line}&format=fasta'
    with urllib.request.urlopen(url) as f:
        fasta = f.read().decode('utf-8').strip()
    return fasta
    
uniprot_list = ['P40925', 'P40926']
print(get_protein_sequences(uniprot_list))
>sp|P40926|MDHM_HUMAN Malate dehydrogenase, mitochondrial OS=Homo sapiens OX=9606 GN=MDH2 PE=1 SV=3
MLSALARPASAALRRSFSTSAQNNAKVAVLGASGGIGQPLSLLLKNSPLVSRLTLYDIAH
TPGVAADLSHIETKAAVKGYLGPEQLPDCLKGCDVVVIPAGVPRKPGMTRDDLFNTNATI
VATLTAACAQHCPEAMICVIANPVNSTIPITAEVFKKHGVYNPNKIFGVTTLDIVRANTF
VAELKGLDPARVNVPVIGGHAGKTIIPLISQCTPKVDFPQDQLTALTGRIQEAGTEVVKA
KAGAGSATLSMAYAGARFVFSLVDAMNGKEGVVECSFVKSQETECTYFSTPLLLGKKGIE
KNLGIGKVSSFEEKMISDAIPELKASIKKGEDFVKTLK
>sp|P40925|MDHC_HUMAN Malate dehydrogenase, cytoplasmic OS=Homo sapiens OX=9606 GN=MDH1 PE=1 SV=4
MSEPIRVLVTGAAGQIAYSLLYSIGNGSVFGKDQPIILVLLDITPMMGVLDGVLMELQDC
ALPLLKDVIATDKEDVAFKDLDVAILVGSMPRREGMERKDLLKANVKIFKSQGAALDKYA
KKSVKVIVVGNPANTNCLTASKSAPSIPKENFSCLTRLDHNRAKAQIALKLGVTANDVKN
VIIWGNHSSTQYPDVNHAKVKLQGKEVGVYEALKDDSWLKGEFVTTVQQRGAAVIKARKL
SSAMSAAKAICDHVRDIWFGTPEGEFVSMGVISDGNSYGVPDDLLYSFPVVIKNKTWKFV
EGLPINDFSREKMDLTAKELTEEKESAFEFLSSA

@georopon
Copy link

how make search bases into organism name and perform download the complete proteome?

@yazhinia
Copy link

Could you suggest how to use your snippet to get other information associated with Uniprot entry such as PDB ID, GO, PFAM ID etc?

Thanks very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment