仿射间隙序列比对器的输出不正确

2024-04-27 13:11:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用仿射间隙代价来实现全局对齐算法。我首先用Java实现了它,然后用Python实现了它。但是我在Java中的输出与在python中得到的不同。我在Python中实现的代码与在Java中完全相同。在

我用Python编写的代码如下:

import numpy as np

def deletionMatrix(D,S,i,j):
    res = 0
    if i==0 and j>=0:
        res = S[i-1][j] - (gapOpen + gapExt)
        D[i][j] = res

    if i > 1 and j >= 0:
        res_S = S[i-1][j] - (gapOpen + gapExt)
        res_D = D[i-1][j] - gapOpen

        res = max(res_S, res_D)
        D[i][j] = res

    return(res)

def insertionMatrix(I, S, i, j):
    res = 0

    if i >= 0 and j == 1:
        res = S[i][j-1] - (gapOpen + gapExt)
        I[i][j] = res

    if i >= 0 and j > 1:
        res_S = S[i][j-1] - (gapOpen + gapExt)
        res_I = I[i][j-1] - gapOpen

        res = max(res_S, res_I)
        I[i][j] = res

    return(res)

def matrix(S,D,I,m,n,match,mismatch):
    for i in range(0,len(m)):
        for j in range(0,len(n)):
             if i == 0 and j == 0:
                 S[i][j] = 0

             if i == 0 and j > 0:
                 S[i][j] = insertionMatrix(I,S,i,j)

             if i > 0 and j == 0:
                 S[i][j] = deletionMatrix(D,S,i,j)

             if i > 0 and j > 0:
                 if m[i-1] == n[j-1]:
                    res_S = S[i-1][j-1] + match
                    res_D = deletionMatrix(D,S,i,j)
                    res_I = insertionMatrix(I,S,i,j)

                    S[i][j] = max(res_S,res_D,res_I)

                 elif m[i-1] != n[j-1]:
                    res_S = S[i-1][j-1] + mismatch
                    res_D = deletionMatrix(D,S,i,j)
                    res_I = insertionMatrix(I,S,i,j)

                    S[i][j] = max(res_S,res_D,res_I)

     return(S)


gapOpen = 5
gapExt = 2

m = "GAATTCAGTTA"
n = "GGATCGA"

mLen = len(m) + 1
nLen = len(n) + 1

S = np.zeros([mLen,nLen])
D = np.zeros([mLen,nLen])
I = np.zeros([mLen,nLen])

match = 1
mismatch = -3

S = matrix(S,D,I,m,n,match,mismatch)

for i in range(0,len(m)):
   for j in range(0,len(n)):
       print(S[i][j],)

   print()

我附上了我要实现的算法的图像。谁能告诉我哪里出了问题。 This is the algorithm that I'm trying to implement. The alpha is the gapOpen variable in my code and beta is the gapExt variable. And A[i],B[j] simply is match or mismatch variable. That is the two characters being read at a given time (from String m and n), if they are the same then its a match otherwise it is a mismatch

下面是我的java代码。在

^{pr2}$

如果有人能告诉我哪里出了问题,我会非常感激的。在过去的两天里,我一直在努力寻找问题,但似乎无法真正找出我做错了什么。在


Tags: andtheinlenifismatchnp
1条回答
网友
1楼 · 发布于 2024-04-27 13:11:49

以下是一些潜在的问题:

deletionMatrix中,Java说:

if(i == 1 && j>=0)

但是Python说:

^{pr2}$

matrix中,Java说:

for(int i=0;i<=m.length();i++) {
    for(int j=0;j<=n.length();j++) {

但是Python说:

for i in range(0,len(m)):
    for j in range(0,len(n)):

每圈缩短一个。range(m,n)m变为{},因此您可能希望:

for i in range(len(m) + 1):
    for j in range(len(n) + 1):

main代码的嵌套打印循环中,这种情况再次发生。在

下面是Python代码的重做,它产生与Java代码相同的输出(如果在Java代码中注释掉对未定义方法traceBack()的调用):

import numpy as np

gapOpen = 5
gapExt = 2

def deletionMatrix(D, S, i, j):
    res = 0
    if i == 1 and j >= 0:
        res = S[i - 1][j] - (gapOpen + gapExt)
        D[i][j] = res

    if i > 1 and j >= 0:
        res_S = S[i - 1][j] - (gapOpen + gapExt)
        res_D = D[i - 1][j] - gapOpen

        res = max(res_S, res_D)
        D[i][j] = res

    return res

def insertionMatrix(I, S, i, j):
    res = 0

    if i >= 0 and j == 1:
        res = S[i][j-1] - (gapOpen + gapExt)
        I[i][j] = res

    if i >= 0 and j > 1:
        res_S = S[i][j-1] - (gapOpen + gapExt)
        res_I = I[i][j-1] - gapOpen

        res = max(res_S, res_I)
        I[i][j] = res

    return res

def matrix(S, D, I, m, n, match, mismatch):

    for i in range(len(m) + 1):

        for j in range(len(n) + 1):

            if i == 0 and j == 0:
                S[i][j] = 0

            if i == 0 and j > 0:
                S[i][j] = insertionMatrix(I, S, i, j)

            if i > 0 and j == 0:
                S[i][j] = deletionMatrix(D, S, i, j)

            if i > 0 and j > 0:
                res_S = S[i-1][j-1] + (match if m[i-1] == n[j-1] else mismatch)
                res_D = deletionMatrix(D, S, i, j)
                res_I = insertionMatrix(I, S, i, j)

                S[i][j] = max(res_S, res_D, res_I)

    return S

if __name__ == '__main__':

    # Initializing two strings
    m = "GAATTCAGTTA"
    n = "GGATCGA"

    mLen = len(m) + 1
    nLen = len(n) + 1

    S = np.zeros([mLen, nLen])
    D = np.zeros([mLen, nLen])
    I = np.zeros([mLen, nLen])

    match = 1
    mismatch = -3

    S = matrix(S, D, I, m, n, match, mismatch)

    for i in range(mLen):
        for j in range(nLen):
            print(S[i][j], end='\t')
        print()

相关问题 更多 >