python中实现RNA序列的翻译
001、 利用循环结构实现
[root@PC1 test01]# ls a.fa test.py [root@PC1 test01]# cat a.fa ## 测试RNA序列 AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA [root@PC1 test01]# cat test.py ## 翻译程序 #!/usr/bin/env python # -*- coding: utf-8 -*- in_file = open("a.fa", "r") file = in_file.read().strip() in_file.close() table = { 'UUU':'F','CUU':'L','AUU':'I','GUU':'V', 'UUC':'F','CUC':'L','AUC':'I','GUC':'V', 'UUA':'L','CUA':'L','AUA':'I','GUA':'V', 'UUG':'L','CUG':'L','AUG':'M','GUG':'V', 'UCU':'S','CCU':'P','ACU':'T','GCU':'A', 'UCC':'S','CCC':'P','ACC':'T','GCC':'A', 'UCA':'S','CCA':'P','ACA':'T','GCA':'A', 'UCG':'S','CCG':'P','ACG':'T','GCG':'A', 'UAU':'Y','CAU':'H','AAU':'N','GAU':'D', 'UAC':'Y','CAC':'H','AAC':'N','GAC':'D', 'UAA':'Stop','CAA':'Q','AAA':'K','GAA':'E', 'UAG':'Stop','CAG':'Q','AAG':'K','GAG':'E', 'UGU':'C','CGU':'R','AGU':'S','GGU':'G', 'UGC':'C','CGC':'R','AGC':'S','GGC':'G', 'UGA':'Stop','CGA':'R','AGA':'R','GGA':'G', 'UGG':'W','CGG':'R','AGG':'R','GGG':'G' } str1 = "" if len(file) % 3 != 0: print("no integrate sequence!") exit else: for i in range(0, len(file), 3): idx = file[i:i+3] if table[idx] == "Stop": exit else: str1 += table[idx] print(str1) [root@PC1 test01]# python test.py ## 翻译结果 MAMAPRTEINSTRING
002、解法2 循环结构
[root@PC1 test01]# ls a.fa test.py [root@PC1 test01]# cat a.fa ## 测试RNA序列 AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA [root@PC1 test01]# cat test.py ## 翻译程序 #!/usr/bin/env python # -*- coding: utf-8 -*- in_file = open("a.fa", "r") file = in_file.read().strip() table = { 'UUU':'F','CUU':'L','AUU':'I','GUU':'V', 'UUC':'F','CUC':'L','AUC':'I','GUC':'V', 'UUA':'L','CUA':'L','AUA':'I','GUA':'V', 'UUG':'L','CUG':'L','AUG':'M','GUG':'V', 'UCU':'S','CCU':'P','ACU':'T','GCU':'A', 'UCC':'S','CCC':'P','ACC':'T','GCC':'A', 'UCA':'S','CCA':'P','ACA':'T','GCA':'A', 'UCG':'S','CCG':'P','ACG':'T','GCG':'A', 'UAU':'Y','CAU':'H','AAU':'N','GAU':'D', 'UAC':'Y','CAC':'H','AAC':'N','GAC':'D', 'UAA':'Stop','CAA':'Q','AAA':'K','GAA':'E', 'UAG':'Stop','CAG':'Q','AAG':'K','GAG':'E', 'UGU':'C','CGU':'R','AGU':'S','GGU':'G', 'UGC':'C','CGC':'R','AGC':'S','GGC':'G', 'UGA':'Stop','CGA':'R','AGA':'R','GGA':'G', 'UGG':'W','CGG':'R','AGG':'R','GGG':'G' } str1 = "" while(len(file) >= 3): idx = file[0:3] if table[idx] != "Stop": str1 += table[idx] else: break file = file[3:] print(str1) [root@PC1 test01]# python3 test.py ## 翻译结果 MAMAPRTEINSTRING
003、利用函数结构实现
[root@PC1 test01]# ls a.fa test.py [root@PC1 test01]# cat a.fa ## 测试RNA序列 AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA [root@PC1 test01]# cat test.py ## 翻译程序 #!/usr/bin/env python # -*- coding: utf-8 -*- in_file = open("a.fa", "r") file = in_file.read().strip() table = { 'UUU':'F','CUU':'L','AUU':'I','GUU':'V', 'UUC':'F','CUC':'L','AUC':'I','GUC':'V', 'UUA':'L','CUA':'L','AUA':'I','GUA':'V', 'UUG':'L','CUG':'L','AUG':'M','GUG':'V', 'UCU':'S','CCU':'P','ACU':'T','GCU':'A', 'UCC':'S','CCC':'P','ACC':'T','GCC':'A', 'UCA':'S','CCA':'P','ACA':'T','GCA':'A', 'UCG':'S','CCG':'P','ACG':'T','GCG':'A', 'UAU':'Y','CAU':'H','AAU':'N','GAU':'D', 'UAC':'Y','CAC':'H','AAC':'N','GAC':'D', 'UAA':'Stop','CAA':'Q','AAA':'K','GAA':'E', 'UAG':'Stop','CAG':'Q','AAG':'K','GAG':'E', 'UGU':'C','CGU':'R','AGU':'S','GGU':'G', 'UGC':'C','CGC':'R','AGC':'S','GGC':'G', 'UGA':'Stop','CGA':'R','AGA':'R','GGA':'G', 'UGG':'W','CGG':'R','AGG':'R','GGG':'G' } #amino_acid = "" def translation(rna, table): ## 翻译函数,参数为RNA序列和密码子字典 amino_acid = "" for i in range(0, len(rna), 3): codon = rna[i:i+3] if codon not in table or table[codon] == "Stop": exit else: amino_acid += table[codon] return amino_acid print(translation(file, table)) [root@PC1 test01]# python test.py ## 翻译结果 MAMAPRTEINSTRING
。
参考:
https://blog.csdn.net/weshengxin/article/details/110790253