python - 在字符串中映射
问题描述
给定这样的 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
解决方案
您可以使用以下命令将您的 RNA 代码指定为密码子列表textwrap
:
import textwrap
textwrap.wrap("AUGUAA", 3) # returns ["AUG", "UAA"]
现在您可以轻松地遍历它们。
推荐阅读
- java - Spring过滤请求
- python - Googletrans API 错误 - 每日限制或阻止 IP?
- audiokit - AudioKit 可以在麦克风分析频率时播放声音吗?
- java - 如何将 Java Swing 应用程序与同一网络上的其他应用程序实例连接?
- python - 我有办法重写代码吗?,我似乎无法正确,我似乎无法正确获取输入
- authentication - 将文件中的自定义 JS 函数添加到 Svelte 应用程序的最佳方法是什么?
- git - 如果文件未更改,提交历史记录会丢失吗?
- r - kableExtra::collapse_rows 不适用于多行的长表头
- python - 如何将 Django asgi 项目部署到 Heroku?
- python - 有什么方法可以减少“嵌套”吗?