I am having difficulties following how to implement code in python with functions embedded within a class. Most examples online end after the code is written, but do not show you (perhaps because it is too easy) how to run it on an input example file "testfile.txt". So assuming the code below is correct (it does not matter to me if it is efficient etc), how would I run this and retrieve both header and sequence variables on "testfile.txt"?
This is code was borrowed from: https://www.biostars.org/p/710/
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 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]' % handle
def __iter__(self):
header, sequence = '', []
for line in self.handle:
if line[0] == '>':
if sequence: yield Dna(header, sequence)
header = line[1:-1]
sequence = []
else:
sequence.append(line.strip())
yield Dna(header, sequence)