将数字转换为模式作为 DNA 序列:试图用 python 解决这个生物信息学问题

converting number to pattern as DNA sequence : trying to solve this bioinformatics problem with python

也就是将整数0、1、2、3分别转化为符号A、C、G、T

def NumberToSymbol(index):
    SeqDict = {0:'A', 1:'C', 2:'G', 3:'T'}
    str_index = str(index)
    result = ""
    for i in str_index:
        result = result + SeqDict[int(i)]
    return result

整数n除以整数m的商为Quotient(n, m)

例如,商(11, 4) = 2

def Quotient(index,4):
    index = int(index/4)
    return index

整数n除以整数m的余数为Remainder(n, m)

例如,余数(11, 4) = 3。

def Remainder(index, 4)
    index = index%4
    return index

计算Pattern = NumberToPattern(9904, 7)时,我们将9904除以4得到商2476,余数0。这个余数代表Pattern的最后一个核苷酸,即NumberToSymbol(0) = A。然后我们重复这个过程,将每个后续商除以 4,直到商为 0。核苷酸列中的符号,从底部向上读取,产生 Pattern = GCGGTAA。

def NumberToPatten(index, k):
    if K==1:
        return NumberToSymbol(index)
    else:
        prefixIndex = Quotient(index, 4)
        r = Remainder(index, 4)
        symbol = NumberToSymbol(r)
        PrefixPattern = NumberToPatten(prefixIndex, k -1)
        return str(PrefixPattern) + symbol

我有这 4 个功能一起工作: 输入:整数索引和 k。 输出:字符串 NumberToPattern(index, k)。 例如: 输入 : 45 , 4 输出:AGTC

让我先帮助您了解问题,然后告诉您一些解决此问题的方法

在python(或任何其他编程语言)中函数可以有

def fun_name(arg1, arg2): #Normal arguments

def fun_name(arg, arg2=4): #默认值参数,如果用户没有为 arg2 提供值,那么将分配默认值,即 4 .

def Quotient(index,4):

在您的代码中,您定义了一个名为 Quotient 的函数,其参数索引和您定义为“4”的其他参数,这是文字,变量名称不能是文字

解决方案/修复

解决方案 1. #Preferred #Default value 参数,因此如果用户传递该值,它将采用该值,否则它将被分配默认值 4

def Quotient(index,divisor = 4):
    index = int(index/divisor)
    return index

解决方案 2:# 而不是使用变量

def Quotient(index,divisor): 
    index = int(index/divisor)
    return index

解决方案 3. # 只从调用方传递 1 个变量

def Quotient(index): 
    index = int(index/4)
    return index

针对评论中讨论的其他问题进行编辑

**使用 NumberToSymbol 函数将 0112 转换为 ACCG **

def NumberToSymbol(index): #Consider value of index is "0112" string and return will be ACCG
    SeqDict = {0:'A', 1:'C', 2:'G', 3:'T'}
    str_index = str(index)
    result = ""
    for i in str_index:
        result = result + SeqDict[int(i)]
    return result

注意:如果您在调用此函数时遇到问题,请尝试将值作为字符串传递,默认情况下 python 将 0112 视为八进制数,因此请尝试将其作为“0112”

发送