我正在尝试实现Needleman-Wunsch 算法以获得全局比对函数中的最低分数,但是当两个序列相等时,我得到的不是最低分数 0,而是得到 8。
这段代码有什么问题?
alphabet = ["A", "C", "G", "T"]
score = [[0, 4, 2, 4, 8], \
[4, 0, 4, 2, 8], \
[2, 4, 0, 4, 8], \
[4, 2, 4, 0, 8], \
[8, 8, 8, 8, 8]]
def globalAlignment(x, y):
#Dynamic version very fast
D = []
for i in range(len(x)+1):
D.append([0]* (len(y)+1))
for i in range(1, len(x)+1):
D[i][0] = D[i-1][0] + score[alphabet.index(x[i-1])][-1]
for i in range(len(y)+1):
D[0][i] = D[0][i-1]+ score[-1][alphabet.index(y[i-1])]
for i in range(1, len(x)+1):
for j in range(1, len(y)+1):
distHor = D[i][j-1]+ score[-1][alphabet.index(y[j-1])]
distVer = D[i-1][j]+ score[-1][alphabet.index(x[i-1])]
if x[i-1] == y[j-1]:
distDiag = D[i-1][j-1]
else:
distDiag = D[i-1][j-1] + score[alphabet.index(x[i-1])][alphabet.index(y[j-1])]
D[i][j] = min(distHor, distVer, distDiag)
return D[-1][-1]
x = "ACGTGATGCTAGCAT"
y = "ACGTGATGCTAGCAT"
print(globalAlignment(x, y))