Skip to content

Instantly share code, notes, and snippets.

@jacob-ogre
Created April 5, 2013 12:36
Show Gist options
  • Save jacob-ogre/5318981 to your computer and use it in GitHub Desktop.
Save jacob-ogre/5318981 to your computer and use it in GitHub Desktop.
Python: convert fasta to dict
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