Created
June 7, 2019 22:54
-
-
Save JoaoRodrigues/afe11985e4cab4c0002eebae2213e0a8 to your computer and use it in GitHub Desktop.
Example of Uniprot REST API (Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.