尝试在密码子之间获得空间并在达到特定密码子时停止生成以进行 RNA 到蛋白质模拟

Trying to get spaces between codons and stop the generation when reaching a certain codon for RNA to protein simulation

这里有一些我需要帮助的事情。
但首先,请让我先拉出代码。

from base_printer import *

def dna_complement(dna):
  coup = ""
  for letter in dna:
    if letter == "C":
      coup += "G"
    if letter == "G":
      coup += "C"
    if letter == "A":
      coup += "T"
    if letter == "T":
      coup += "A"
  return coup
  

def convert_to_rna(dna):
  coup2 = ""
  for letter in dna:
    if letter == "C":
      coup2 += "G"
    if letter == "G":
      coup2 += "C"
    if letter == "A":
      coup2 += "U"
    if letter == "T":
      coup2 += "A"
  return coup2

def translate(rna):
  amino_acid = ""
  for i in range(len(rna)-2):
    three_letter = rna[i:i+3]
    if three_letter in CODON_TABLE:
      amino_acid += CODON_TABLE[three_letter]
      i += 2 
  return amino_acid

CODON_TABLE = {'UUU':'Phe','UUC':'Phe','UUA':'Leu','UUG':'Leu','CUU':'Leu','CUC':'Leu','CUA':'Leu','CUG':'Leu','AUU':'Ile','AUC':'Ile','AUA':'Ile','AUG':'Met','GUU':'Val','GUC':'Val','GUA':'Val','GUG':'Val','UCU':'Ser','UCC':'Ser','UCA':'Ser','UCG':'Ser','CCU':'Pro','CCC':'Pro','CCA':'Pro','CCG':'Pro','ACU':'Thr','ACC':'Thr','ACA':'Thr','ACG':'Thr','GCU':'Ala','GCC':'Ala','GCA':'Ala','GCG':'Ala','UAU':'Tyr','UAC':'Tyr','UAA':'STOP','UAG':'STOP','CAU':'His','CAC':'His','CAA':'Gln','CAG':'Gln','AAU':'Asn','AAC':'Asn','AAA':'Lys','AAG':'Lys','GAU':'Asp','GAC':'Asp','GAA':'Glu','GAG':'Glu','UGU':'Cys','UGC':'Cys','UGA':'STOP','UGG':'Trp','CGU':'Arg','CGC':'Arg','CGA':'Arg','CGG':'Arg','AGU':'Ser','AGC':'Ser','AGA':'Arg','AGG':'Arg','GGU':'Gly','GGC':'Gly','GGA':'Gly','GGG':'Gly'}

dna="AAGAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTCTTAGCGGGGCCACATCGGCCACCGCTGCCCTGCCCCTGGAGGGTGGCCCCACCGGCCGTTACAGCGAGCATAC" 


def main():
  print("\nWelcome to the DNA program: The Code of Life.")
  print("\nSample DNA strand:\n")
  print("Regular DNA:")
  print_bases(dna)
  print("DNA after complement: ")
  dna2 = dna_complement(dna)
  print_bases(dna2)
  print("DNA after RNA convertion: ")
  rna = convert_to_rna(dna)
  print_bases(rna)
  print("The result of translation: ")
  amino_acid = translate(rna)
  print_bases(amino_acid)


main()

我正试图让大家看到的密码子打印出来,翻译的输出已经成功打印出来了

PHESERLEUSERLEUTYRTHRARGGLYVALSTOPASNTHRGLNARGGLYGLYGLYGLYALAPROARGGLYGLUARGGLYASPTHRARGASPTHRARGASPTHRARGGLUARGGLUASNILESERARGALAPROPROPROARGGLYVALCYSVALSTOPSERALAPROARGGLYVALTRPGLYALAARGASPTHRARGGLYGLYASPTHRARGGLYGLYGLYASPTHRPROLEUSERPROPROHISTHRPROARGGLYGLYGLYVALTRPGLYALAPROARGGLYALAGLNASNMETCYSVALSERARGALALEUSERARGVALTYRMET

现在我想做的是用空格分隔程序输出中列出的每个密码子,并在达到 UAA/UGA/UAG 时停止生成,这正是我想要弄清楚的。但现在的问题是,我什至不知道该从哪里开始,这很尴尬。

我想要的输出示例:Phe Ser Leu Ser Leu Tyr(到达 UAA/UGA/UAG 后停止)

有人愿意提供一些提示吗?

编辑:这是 base_print 的信息,它基本上以不同的颜色打印输出

import colorama as cr
cr.init(autoreset=True)

def print_bases(string):
    """prints dna/rna bases with color coding for each base"""
    for char in string.upper():
        if char == "A":
            print(cr.Back.BLACK + cr.Fore.GREEN + cr.Style.BRIGHT + char, end="")
        elif char == "C":
            print(cr.Back.BLACK + cr.Fore.YELLOW + cr.Style.BRIGHT + char, end="")
        elif char == "T":
            print(cr.Back.BLACK + cr.Fore.BLUE + cr.Style.BRIGHT + char, end="")
        elif char == "G":
            print(cr.Back.BLACK + cr.Fore.MAGENTA + cr.Style.BRIGHT + char, end="")
        elif char == "U":
            print(cr.Back.BLACK + cr.Fore.WHITE + char, end="")
        else:
            print(char, end="") 
    print()

假设您要打印 'STOP' 之前的所有内容,每个都切成 3 个字符,这里是 main 函数的扩展:

    res = amino_acid[:amino_acid.index('STOP')]  if 'STOP' in amino_acid else amino_acid
    res = ' '.join(res[i:i+3] for i in range(0, len(res), 3))
    print(res)

或者,如果您只想翻译直到到达终止密码子 并且 已经有了带空格的氨基酸,这里是对 translate 函数的修改:

def translate(rna):
    amino_acid = ""
    for i in range(len(rna) - 2):
        three_letter = rna[i:i + 3]
        if three_letter in CODON_TABLE:
            next_aa = CODON_TABLE[three_letter]
            if next_aa == 'STOP':
                return amino_acid.strip()
            amino_acid += ' ' + next_aa
            i += 2
    return amino_acid.strip()

输出:

Phe Ser Leu Ser Leu Tyr Thr Arg Gly Val

这是我的做法

def translate(rna):
    amino_acid = []
    for i in range(len(rna) - 2):
        three_letter = rna[i:i + 3]
        if three_letter in ['UAA', 'UGA', 'UAG']:
            break
        if three_letter in CODON_TABLE:
            amino_acid.append(CODON_TABLE[three_letter])
            i += 2
    return ' '.join(amino_acid)