Skip to content

Instantly share code, notes, and snippets.

@skaae
Created October 7, 2015 14:04
Show Gist options
  • Save skaae/3851fe0eac0104eb50bc to your computer and use it in GitHub Desktop.
Save skaae/3851fe0eac0104eb50bc to your computer and use it in GitHub Desktop.
def read_fasta(fasta_filename):
'''Function to parse a fasta sequence file'''
# Initialize dictionary
fasta_dict = {}
# Open file and iterate over lines
for line in open(fasta_filename):
# Remove newline and other whitespace from end of line
line = line.rstrip()
# Test if line starts with '>'
if line[0] == '>':
# If so, save name in variable
name = line[1:]
# Register name in dictionary - with empty sequence
fasta_dict[name] = ""
else:
# Add partial sequence to the entry in the dictionary
fasta_dict[name] += line
# Return result
return fasta_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment