2015-11-12 58 views
1

我正在研究一個從序列中識別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)來,但不知道我怎麼會去修復那。

+1

一個'StopIteration'錯誤只是意味着你已經打在你的迭代器的最後一個項目。只需將該行放在try塊中,然後通過中斷循環來處理異常/無論你需要做什麼 –

回答

3

而不是

codon_frame = self.codon(frame) 
while True: 
    i, index = next(codon_frame)` 

你或許應該這樣做:

for i, index in self.codon(frame): 

如果由於某種原因,你想保持做手工,那麼你需要用的next()通話用try/except

try: 
    i, index = next(codon_frame) 
except StopIteration: 
    break 
+0

@lamazibiji:'self.stopcodons'是一個列表,'self.start'是一個字符串,'i'是一個'Seq' - 通常當使用'in'時,左邊的項目需要與您正在檢查的項目相同;所以'i'應該是一個字符串,或者'self.stopcodons'應該是'Seq'的列表。 –

+0

感謝您的回覆Ethan!我重寫了我和self.stopcodons作爲str()和代碼現在運行。這不應該影響我的結果,但對嗎? – lamazibiji

+0

我也試圖使用「」{:+ d} {:> 5d} ...... {:> 5d} {:> 5d}「打印結果」但我的結果存儲爲str。我怎麼能使它工作格式代碼d? – lamazibiji

2

替換:

codon_frame = self.codon(frame) 
while True: 
    i, index = next(codon_frame) 

有了:

for i, index in self.codon(frame): 
+0

這給我帶來了新的錯誤: 第3行4,在ORF 如果我在self.start或不self.start和我不self.stopcodons: TypeError:'在'需要字符串作爲左操作數,而不是Seq – lamazibiji

相關問題