有没有办法调用之前与trim函数一起使用的函数来做出条件?

问题描述 投票:0回答:1

我正在将 DNA 翻译为蛋白质,或者基本上,引用列表中的值表,然后在满足某些条件时调用该列表中的值。然而,我现在预计将使用“trim”函数来编写问题 1 中名为 translate_trim 的翻译函数的扩展版本,其中包含一个布尔参数 trim,它指定该函数是否应修剪其核苷酸序列,使其成为以下值的倍数:在尝试翻译之前三遍。该函数应包括下面代码中指定的所有其他功能

geneticcode = {
    'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
    'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
    'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
    'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
    'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
    'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
    'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
    'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
    'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
    'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
    'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
    'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
    'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
    'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
    'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',
    'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W'
    }
def translate(seq):
    # Check if the length of the sequence is a multiple of three
    if len(seq) % 3 != 0:
        print("Please provide a sequence with a multiple of three")
        return None

    # Translate the DNA sequence to protein
    protein_sequence = ""
    for i in range(0, len(seq), 3):
        codon = seq[i:i+3]
        # Check if the codon is in the codon table
        if codon not in geneticcode:
            print("Invalid sequence, input sequence can only contain A, C, G, or T")
            return None
        amino_acid = geneticcode[codon]
        protein_sequence += amino_acid

    return protein_sequence

这是我尝试过的代码。他们说有一种方法可以不必回收代码而只使用修剪,但我不知道出了什么问题:

def translate_trim(seq, trim=True):
    bases = ('A','G','C','T')
# Check if the length of the sequence is a multiple of three
    if len(seq) % 3 != 0:
        print("Please provide a sequence with a multiple of three")
    if len(seq) not in bases:
        print ('Invalid sequence, input sequence can only contain A, C, G, or T')
        return none
    else:
        def translate(seq):
              return protein_sequence

谢谢!!

python loops bioinformatics
1个回答
-1
投票

好吧,实际上你离:

很近
  • none
    ->
    None
  • 该函数只需要被调用,因为您已经定义了它。

所以整个代码可能如下所示:

geneticcode = {
    'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
    'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
    'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
    'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
    'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
    'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
    'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
    'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
    'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
    'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
    'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
    'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
    'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
    'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
    'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',
    'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W'
    }

def translate(seq):
    # Check if the length of the sequence is a multiple of three
    if len(seq) % 3 != 0:
        print("Please provide a sequence with a multiple of three")
        return None

    # Translate the DNA sequence to protein
    protein_sequence = ""
    for i in range(0, len(seq), 3):
        codon = seq[i:i+3]
        # Check if the codon is in the codon table
        if codon not in geneticcode:
            print("Invalid sequence, input sequence can only contain A, C, G, or T")
            return None
        amino_acid = geneticcode[codon]
        protein_sequence += amino_acid

    return protein_sequence


def translate_trim(seq, trim=True):
    bases = ('A','G','C','T')
# Check if the length of the sequence is a multiple of three
    if len(seq) % 3 != 0:
        print("Please provide a sequence with a multiple of three")
    if len(seq) not in bases:
        print ('Invalid sequence, input sequence can only contain A, C, G, or T')
        return None
    else:
        x = translate(seq)
        return x



result = translate_trim(geneticcode)

print(result)

上面的游戏我这个答案:

Please provide a sequence with a multiple of three
Invalid sequence, input sequence can only contain A, C, G, or T
None

我不确定您在寻找什么,但基本错误已在此处修复。

© www.soinside.com 2019 - 2024. All rights reserved.