dna补码中的关键错误

时间:2014-10-03 22:17:07

标签: python python-2.7 python-3.x bioinformatics dna-sequence

import string
import os,sys

file=open("C:\Python27\\New Text Document.txt",'r')\
seq =file.readlines() basecomplement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}

def translate(seq):
    aaseq = []
    for str in seq:
       aaseq.append(basecomplement[str])
    return aaseq

for frame in range(1):
    rseqn= (''.join(item.split('|')[0] for item in translate(seq[frame:])))

rseqn = rseqn[::-1]    
print(rseqn)
print(seq) 

错误在这里是文件

"C:\Users\ram\Desktop\pythonhw\dnacomp.py", line 13, in <module>
rseqn= (''.join(item.split('|')[0] for item in
translate(seq[frame:])))   File

"C:\Users\ram\Desktop\pythonhw\dnacomp.py", line 10, in translate
aaseq.append(basecomplement[str]) KeyError:
'AGTCTGGCATACCAGTACAGACTATCA'

如果我使用简单的字符串我也得到序列但我试图读取文件作为输入它显示以下错误

1 个答案:

答案 0 :(得分:2)

如果我理解你想要的东西

def translate(seq):
    basecomplement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
    return ''.join(basecomplement[letter] for letter in seq)

测试

s = 'ATCGGCATAATAC'
>>> translate(s)
'TAGCCGTATTATG'

要翻译文件,您可以执行类似

的操作
with open('file.txt', 'r') as f:
    for line in f:
        print(translate(line))