我正在研究一個從序列中識別ORF的程序。我認爲程序應該是從序列中分析密碼子,而我似乎正在絆倒一個錯誤。 我的代碼是:從循環中的序列獲取下一組字母
def __init__(self,sequence):
self.sequence = Seq(sequence)
self.reverse = self.sequence.reverse_complement()
self.sequence = str(self.sequence)
self.stopcodons = ["TAG", "TAA", "TGA"]
self.start = 'ATG'
self.length = 0
self.result = ("+",0,0,0,0)
def codon(self,frame):
start = frame
while start +3 <=len(self.sequence):
yield (self.sequence[start:start+3],start)
start += 3
#print (start)
def direction(self):
direction = "+"
for frame in range(3):
self.ORF(frame, direction)
direction = "-"
for frame in range(3):
self.sequence = self.reverse
self.ORF(frame, direction)
SeqAnalyzer.results()
def ORF(self, frame, direction):
orf_beg = 0
codon_frame = self.codon(frame)
while True:
i, index = next(codon_frame)
if i in self.start or not self.start and i not in self.stopcodons:
orf_beg = index + 1
elif i in self.stopcodons:
orf_ending = index + 3
length = (orf_ending - orf_beg) + 1
if length > self.length:
self.length = length
self.result = (direction, frame, orf_beg, orf_ending, length)
print(self.result)
break
def results(self):
print("'{}{:+d} {:>5d}..{:>5d} {:>5d}'" .format(self.result))
當運行它,我得到一個錯誤:
File "C:/Users/Alex/OneDrive/Python/Lab 5/findORFs.py", line 33, in direction
self.ORF(frame, direction)
File "C:/Users/Alex/OneDrive/Python/Lab 5/findORFs.py", line 44, in ORF
i, index = next(codon_frame)
StopIteration
這個問題似乎從我的下一個(codon_frame)來,但不知道我怎麼會去修復那。
一個'StopIteration'錯誤只是意味着你已經打在你的迭代器的最後一個項目。只需將該行放在try塊中,然後通過中斷循環來處理異常/無論你需要做什麼 –