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()
@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