我正在编写代码将 DNA 菌株翻译成相应的蛋白质,我已经设法将 DNA 分成三联体,将它们变成列表的元素。
然而,问题是,由于我使用 s.split() 命令拆分字符串,因此元素被写为例如 'AUC' 等。我试图让我的代码读取数据表并根据它翻译三元组。我也完成了这部分,我只需要找到一种方法从三元组中删除字符,我的意思是我想写例如 ['AUC' , 'GCA'] 为 [AUC , GCA ].
提前谢谢你。
dna = 'ATCGCGATG'
def dna_to_mrna(dna):
mrna = ''
for base in dna:
if base == 'A' or base == 'G' or base == 'C':
mrna = mrna + base
else:
mrna = mrna + 'U'
return mrna
mrna = dna_to_mrna(dna)
# We split the mRNA into triplets as elements of arrays.
def mrna_to_triplets(mrna):
triplets = ""
tripletsnew = ""
for base in mrna:
len(triplets)
if len(triplets) == 0:
triplets += base
tripletsnew += base
elif 0 < len(triplets) % 3 < 3:
triplets += base
tripletsnew += base
elif len(triplets) % 3 == 0 :
triplets += base
tripletsnew += ('-' + base)
return tripletsnew
tripletl = mrna_to_triplets(mrna)
tripletstr = tripletl.split('-')
print(tripletstr)
这是我用来从 DNA 中创建 mRNA 串然后将其分离的代码。我还是新手,所以我试图在以某种方式组合它们之前弄清楚各个部分,并且我知道将字符串拆分为列表元素的更有效方法。但是,这种方法和那种方法都会导致元素被写入 ' '。
再次感谢大家。
我试图将输出插入的代码是:
def triplets_to_prot(tripletstr):
with open( '../data/problem_1_codons.txt','r') as f:
for line in f:
rows = line.strip()
columns = line.split()
lmrna = columns[0]
prot = columns[1]
corres = {lmrna : prot}
translation = [corres[base] for base in tripletstr]
print(translation)