Reading at three different frames

2019-08-29 01:05发布

问题:

So I'm trying to create a class that reads a DNA string in three different frames - one that starts at position 0 (or the first base), another that starts in position 1 (the second base), and a third that starts reading at position 2 (the third base). So far, this is what I've been playing around with:

def codons(self, frame_one, frame_two, frame_three):
        start = frame_one
        while start + 3 <=len(self.seq):
            yield (self.seq[start:start+3], start)
            start += 3

        start+1 = frame_two
        while start + 3 <=len(self.seq):
            yield (self.seq[start+1:start+4], start)
            start += 3

        start+2 = frame_three
        while start + 3 <=len(self.seq):
            yield (self.seq[start+2:start+5], start)
            start += 3

I think it's pretty much nonsense at this point, but I tried my best. If anybody can give me an idea on where I can start to correct in this class, that would be great.

回答1:

First of all, you can not assign some values and name a variable like start+1, start+2 and so on. Next, as it is related to bioinformatics you can tag your question as bioinformatics. Also, you are repeating many of the stuffs three times that is too bad as a programmer. However, you can try with the following snippet:

class Codons(object):

        def __init__(self, seq):
                self.seq = seq

        def codons(self, frame_one, frame_two, frame_three):

                while frame_one <=len(self.seq):
                    yield (self.seq[frame_one:frame_one+3])
                    frame_one += 3

                while frame_two <=len(self.seq):
                    yield (self.seq[frame_two:frame_two+3])
                    frame_two += 3

                while frame_three <=len(self.seq):
                    yield (self.seq[frame_three:frame_three+3])
                    frame_three += 3


test_codons = Codons("ATCGTG-")

val = test_codons.codons(0,1,2)

print("Codons are: ")
for i in val:
        print(i)

print("")

And let us know if it worked for you. Cheers!!