Skip to content

Instantly share code, notes, and snippets.

@jonchang
Created September 7, 2013 00:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonchang/6471846 to your computer and use it in GitHub Desktop.
Save jonchang/6471846 to your computer and use it in GitHub Desktop.
Python utility function: Given a path tries to parse a FASTA file. Returns an iterator which yields a (name, sequence) tuple
def parse_fasta(path):
"""Given a path tries to parse a fasta file. Returns an iterator which
yields a (name, sequence) tuple"""
with open(path) as handle:
name = sequence = ""
for line in handle:
line = line.strip()
if line.startswith(">"):
if name:
yield name, sequence
name = line[1:]
sequence = ""
continue
sequence += line
# yield the last sequence
if name and sequence:
yield name, sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment