Last active
August 29, 2015 14:23
-
-
Save chiragmatkar/9449bcfe2561d797769f to your computer and use it in GitHub Desktop.
get list of Uniprot FASTA Sequences by "EC no" and "organism name" from unirprotkb
This file contains hidden or 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
| from bs4 import BeautifulSoup | |
| import requests | |
| import urllib | |
| import sys | |
| # python uniprot_ec.py 2.3.1.29 | |
| # | |
| ec=sys.argv[1] | |
| id=[] | |
| url='http://www.uniprot.org' | |
| r = requests.get(url+'/uniprot/?query='+ec+'+and+ftu&sort=score') | |
| if r.status_code == 404: | |
| print "no url present" | |
| sys.exit() | |
| soup = BeautifulSoup(r.text) | |
| table = soup.find('table', attrs={'class':'entryID'}) | |
| for link in soup.find_all(table): | |
| a=link.get('href') | |
| if a is not None: | |
| if 'uniprot/' in a and len(a)==15: | |
| print 'ID****>',a | |
| id.append(a) | |
| def get_file(url,file,ec): | |
| print 'Downloading..... '+ url | |
| handle = urllib.urlopen(url) | |
| with open(file, 'a') as out: | |
| while True: | |
| data = handle.read(1024) | |
| if len(data) == 0: break | |
| out.write(ec) | |
| out.write("\n") | |
| out.write(url) | |
| out.write("\n") | |
| out.write(data) | |
| out.write("\n\n") | |
| for i in id: | |
| fastaurl=url+''+ i+'.fasta' | |
| print get_file(fastaurl,'fasta.txt',ec) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment