# # title: uniprot_fetch.py # author: Nick Fitzkee (nfitzkee@chemistry.msstate.edu) # date: June 20, 2022 # summary: Fetch a list of sequences given UniProtKB IDs # # To use $> python3 ./uniprot_fetch.py.txt uniprot_ids.txt | tee uniprot.fasta # the above command will make the file "uniprot.fasta" import urllib.request def open_list(fname): result = [] with open(fname) as uplist: l = uplist.readline() while l: l = l.strip() if not l or l[0] == '#': l = uplist.readline() continue result.append(l) l = uplist.readline() return result def main(fname): uplist = open_list(fname) for up in uplist: url = 'https://www.uniprot.org/uniprot/%s.fasta' % up with urllib.request.urlopen(url) as page: print(page.read().decode('utf-8')) if __name__ == '__main__': import os, sys try: fn = sys.argv[1] except: print('usage: %s ' % os.path.split(sys.argv[0])[1]) sys.exit(1) main(fn)