This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Use Python Retrieve Results From Uniprot Automatically

I wanna use a Gene Ontology term to get related sequences in Uniprot. It is simple to do it manually, however, I wanna use python to achieve it. Anybody has ideas with it? For example, I have GO:0070337, then I wanna download all the search results in a fasta file. Thanks

Thank you guys. Finally I chose URLLIB 'http://www.uniprot.org/uniprot/?query=go%3a'+GOTERM+'&force=yes&format=list'

python biopython uniprot

3 answers

Or more specifically this FAQ: http://www.uniprot.org/faq/28

You can use this:
wget --quiet -O- ftp://ftp.uniprot.org/pub/databases/uniprot/currentrelease/knowledgebase/complete/uniprotsprot.dat.gz | zcat | python uniprot2go.py GO:0070337 GO:0016021 > out.fasta

#!/usr/bin/env python
"""Fetch uniprot entries for given go terms"""
import sys
from Bio import SwissProt
#load go terms
gos = set(sys.argv[1:])
sys.stderr.write("Looking for %s GO term(s): %s\n" % (len(gos)," ".join(gos)))
#parse swisprot dump
k = 0
sys.stderr.write("Parsing...\n")
for i,r in enumerate(SwissProt.parse(sys.stdin)):  
    sys.stderr.write(" %9i\r"%(i+1,))
    #parse cross_references
    for ex_db_data in r.cross_references:
        #print ex_db_data
        extdb,extid = ex_db_data[:2]
        if extdb=="GO" and extid in gos:
          k += 1
          sys.stdout.write( ">%s %s\n%s\n" % (r.accessions[0], extid, r.sequence) )
sys.stderr.write("Reported %s entries\n" % k)  

For me it's less than 6 minutes to parse the latest swissprot dump (it depends on your internet connection).
Of course, if you will run it multiple times, better download the dump and run it from local copy.

cool, was not aware of this. anyway, I run cross-reference with UniProt regularly and someone may benefit from the code:)

Read the faq and manual from Uniprot. Do your homework.

Faq & Help

very helpful, especially since the documentation sucks.

Log in to answer this question.