首页 > 解决方案 > 在字符串中映射

问题描述

给定这样的 kay 值对的字典,我需要使用字符串将 RNA 转换为蛋白质。制作蛋白质的开始需要在字符串中检测到“AUG”,最后需要检测到“UAA”、“UAG”和“UGA”以停止该过程。

例如,在访问名为 codon2amino 的字典中的键值对时, translate("AUGUAA") 会给出 'M_' 并且 translate("AGAGAUGCCCUGAGGG") 会给出 'MP_'。

    def get_mapping(csvfilename):
        mapping = read_csv(csvfilename)
        return dict(map(lambda x : (x[0],x[3]),mapping))

这是我尝试过的,但我不知道从哪里开始:

    def translate(rna_strand):
         codon2amino = get_mapping("codon_mapping.csv")
         start_codon = "AUG"
         start_index = rna_strand.find(start_codon)
         stop_codons = ["UAA", "UAG", "UGA"]
         acid_list=[]
         if start_codon and "UAA" not in rna_strand:
             return None
         elif start_codon and "UAG" not in rna_strand:
             return None
         elif start_codon and "UGA" not in rna_strand:
             return None
         else:
               codons = [rna_strand[i:i+3] for i in range(start_index+3, len(rna_strand), 3)]
         for codon in codons:
             if codon in stop_codons:
                  break
 
         acid_list.append(codon2amino[codon])
      amino_acid = ''.join(amino_list) + "_"
      return amino_acid

标签: python

解决方案


您可以使用以下命令将您的 RNA 代码指定为密码子列表textwrap

import textwrap
textwrap.wrap("AUGUAA", 3) # returns ["AUG", "UAA"]

现在您可以轻松地遍历它们。


推荐阅读