Created
September 7, 2013 00:53
-
-
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
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 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