Created
April 5, 2013 12:36
-
-
Save jacob-ogre/5318981 to your computer and use it in GitHub Desktop.
Python: convert fasta to dict
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
def fasta2dict(fil): | |
""" | |
Read fasta-format file fil, return dict of form scaffold:sequence. | |
Note: Uses only the unique identifier of each sequence, rather than the | |
entire header, for dict keys. | |
""" | |
dic = {} | |
cur_scaf = '' | |
cur_seq = [] | |
for line in open(fil): | |
if line.startswith(">") and cur_scaf == '': | |
cur_scaf = line.split(' ')[0] | |
elif line.startswith(">") and cur_scaf != '': | |
dic[cur_scaf] = ''.join(cur_seq) | |
cur_scaf = line.split(' ')[0] | |
cur_seq = [] | |
else: | |
cur_seq.append(line.rstrip()) | |
dic[cur_scaf] = ''.join(cur_seq) | |
return dic |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment