Commit 871934e2 authored by Mathieu Giraud's avatar Mathieu Giraud
Browse files parses Fasta file

Old commit, go with e4df4530, I forgot to push it some weeks ago.
parent 64c46d8a
def parse(fasta, endline=''):
'''Iterates over sequences in a fasta files, yielding (header, sequence) pairs'''
header = ''
sequence = ''
for l in fasta:
l = l.strip()
if not l:
if l[0] == '>':
if header or sequence:
yield (header, sequence)
header = l[1:]
sequence = ''
sequence += l + endline
if header or sequence:
yield (header, sequence)
def parse_as_Fasta(fasta):
for (header, sequence) in parse(fasta):
yield Fasta(header, sequence)
class Fasta():
def __init__(self, header, sequence):
self.header = header
self.seq = sequence
def name(self):
return self.header.split('|')[1]
def species(self):
return self.header.split('|')[2]
def __len__(self):
return len(self.seq)
def __str__(self):
return '>%s\n%s\n' % (self.header, self.seq)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment