Python为输入/输出列表

时间:2018-02-21 00:44:39

标签: python for-loop recursion

我试图通过在Python中更新每个时间步的基因概率来模拟生物基因网络。然后,将绘制每个时间步的结果。我可以通过一次又一次地处理和粘贴来做到这一点,但是一旦时间步长变得大于10,它就不理想。这是我到目前为止所完成的事情。

Pgt1, Pkr1, Pkni1, Phb1 = test.simUpdateFun(PgtI, PkrI, PkniI, PhbI)
Pgt2, Pkr2, Pkni2, Phb2 = test.simUpdateFun(Pgt1, Pkr1, Pkni1, Phb1)
Pgt3, Pkr3, Pkni3, Phb3 = test.simUpdateFun(Pgt2, Pkr2, Pkni2, Phb2)
Pgt4, Pkr4, Pkni4, Phb4 = test.simUpdateFun(Pgt3, Pkr3, Pkni3, Phb3)
Pgt5, Pkr5, Pkni5, Phb5 = test.simUpdateFun(Pgt4, Pkr4, Pkni4, Phb4)

data.dataPlot(PgtI, PkrI, PkniI, PhbI)
data.dataPlot(Pgt1, Pkr1, Pkni1, Phb1)
data.dataPlot(Pgt2, Pkr2, Pkni2, Phb2)
data.dataPlot(Pgt3, Pkr3, Pkni3, Phb3)
data.dataPlot(Pgt4, Pkr4, Pkni4, Phb4)
data.dataPlot(Pgt5, Pkr5, Pkni5, Phb5)

simUpdateFun是我在一个类中编写的函数,用于实现基因的交互并更新概率。此外,每个变量的数据结构是一个包含大约20个数据点的列表。

首先,我正在考虑为更新执行递归功能。不幸的是,我在Python方面的知识相当有限(夏天自学),而我所知道的Python中的递归函数都是简单的例子,例如阶乘函数和斐波那契数列。对我来说,最大的问题是无法编写循环甚至是递归函数,因为simUpdateFun的输入和输出都是列表。

simUpdateFun如下:

def simUpdateFun(self, PgtPre, PkrPre, PkniPre, PhbPre):
    """A function to update the gap gene probabilities based on the mutual repression relationship from the previous ones"""
    PgtS = [] 
    PkrS = []
    PkniS = []
    PhbS = []
    #PgtS = list(i for i in PgtPre)
    # implement the mutual strong repression interaction
    for i in range(self.xlen):
        PgtS.append(1-PkrPre[i])
        PkrS.append(1-PgtPre[i])
        PkniS.append(1-PhbPre[i])
        PhbS.append(1-PkniPre[i])

    # implementation of the weak overlapping repression interaction
    PgtR = []
    PkrR = []
    PkniR = []
    PhbR = []

    x = len(PgtPre)
    P1 = 0
    P2 = int(x/4 )
    P3 = int(x * 2/4 )
    P4 = int(x * 3/4 )
    P5 = int(x )

    # first calculate the repressor probability function for the gap genes
    for i in PgtPre:
        PgtR.append(self.repressor(i, 1))
    for i in PkrPre:
        PkrR.append(self.repressor(i, 1))
    for i in PkniPre:
        PkniR.append(self.repressor(i,1))
    for i in PhbPre:
        PhbR.append(self.repressor(i, 1))

    # implement the interactions of weak repression of overlapping genes
    # Try to avoid alternating the initial condition values by copy a new set of list objects.

    PgtW = list(i for i in PgtPre) # using generator expression simplified the codes and also minimize # of lines
    PkrW = list(i for i in PkrPre)
    PkniW = list(i for i in PkniPre)
    PhbW= list(i for i in PhbPre)

    for i in range(P4, P5): # qudrant 4 regulation
        PgtW[i] = PgtPre[i] * PhbR[i]
    for i in range(P3, P4): # qudrant 3 regulation
        PkniW[i] = PkniPre[i] * PgtR[i]

    for i in range(P2, P3): # qudrant 2 regulation
        PkrW[i] = PkrPre[i] * PkniR[i]

    for i in range(P1, P2): # qudrant 1 regulation
        PhbW[i] = PhbPre[i] * PkrR[i]
        PkrW[i] = PkrPre[i] * PhbR[i]

    # determinate the final probabilites of the two effects by multiplying since they take place simultatesily.

    Pgtf = []
    Pkrf = []
    Pknif = []
    Phbf = []
    for i in range(len(PgtPre)):
        Pgtf.append(PgtS[i] * PgtW[i])
        Pkrf.append(PkrS[i] * PkrW[i])
        Pknif.append(PkniS[i] * PkniW[i])
        Phbf.append(PhbS[i] * PhbW[i])

    return(Pgtf, Pkrf, Pknif, Phbf)

该函数基本上接受一组列表数据,这些列表数据是概率值,并输出列表的更新版本。

0 个答案:

没有答案