Skip to content

Instantly share code, notes, and snippets.

@adamwespiser
Created November 13, 2012 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamwespiser/4065548 to your computer and use it in GitHub Desktop.
Save adamwespiser/4065548 to your computer and use it in GitHub Desktop.
Fasta module for python
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