0

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)
2
  • Check out jeffknupp.com/blog/2014/06/18/… Commented Oct 20, 2014 at 21:40
  • thanks, this looks promising! Commented Oct 20, 2014 at 21:48

1 Answer 1

1

The Fasta instance takes a file object:

with open('testfile.txt') as infh:
    fasta = Fasta(infh)
    for dna in fasta:
        print(dna.head)
        print(dna.sequence)

It actually tells you this in the post:

You do fasta = Fasta(handle) and then for record in fasta to yield Dna objects

Sign up to request clarification or add additional context in comments.

1 Comment

I saw that but didn't know really know what to do with that information, now I can dissect it! Thanks so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.