Created
November 13, 2012 12:36
-
-
Save adamwespiser/4065548 to your computer and use it in GitHub Desktop.
Fasta module for python
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
class Dna: | |
''' Object representing a FASTA record. ''' | |
def __init__(self, header, sequence): | |
self.head = header | |
self.seq = sequence | |
def __repr__(self): | |
return '[HTML]' % (self.head) | |
def __str__(self, separator=''): | |
return '>%s\n%s' % (self.head, separator.join(self.seq)) | |
def __len__(self): | |
return len(''.join(self.seq)) | |
@property | |
def name(self, separator=''): | |
return self.head | |
def sequence(self, separator=''): | |
return separator.join(self.seq) | |
class Fasta: | |
''' A FASTA iterator/generates DNA objects. ''' | |
def __init__(self, handle): | |
self.handle = handle | |
def __repr__(self): | |
return '[HTML]' % (self.handle) | |
def __iter__(self): | |
header, sequence = '', [] | |
for line in self.handle: | |
#print line | |
if line[0] == '>': | |
#print "...line start" | |
if sequence: | |
#print "....","".join(sequence) | |
yield Dna(header, "".join(sequence)) | |
header = line[1:-1] | |
sequence = [] | |
else: | |
sequence.append(line.strip()) | |
yield Dna(header, "".join(sequence)) | |
~ | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment