我想为Needleman-Wunsch算法找到不同的比对 我在Traceback中封锁了代码,并计算了所有最佳对齐方式(不同的对齐方式)
那是我迄今为止能够进行的工作:
import numpy as np
from string import *
gap_penalty = 0
match_award = 1
mismatch_penalty = 0
def W_score (n1,n2):
if(n1 == n2):
return 1
else:
return 0
def Max_source(di,ho,ve):
pointer = max(di,ho,ve)
if(di == pointer):
return 'D'
elif(ho == pointer):
return 'H'
else:
return 'V'
def needleman_wunsch(seq1,seq2):
n = len(seq1) + 1
p = len(seq2) + 1
# initialisation :
M = np.zeros((n,p),dtype = int)
Dir = np.zeros((n,p),dtype = str)
for i in range(n):
M[i][0] =M[i-1][0] + 0
Dir[i][0] = 'V'
for j in range (p):
M[0][j] = 0
Dir [0][j] = 'H'
Dir [0][0] = 0 #Return the first element of the pointer matrix back to 0.
for i in range(1,n):
for j in range(1,p):
di = M[i-1][j-1] + W_score(seq1[i-1],seq2[j-1])
ve = M[i - 1][j] + 0 # because gap_penalty = 0
ho = M[i][j-1] + 0 # //
M[i][j] = max(di,ho,ve)
Dir[i][j] = Max_source(di,ho,ve)
# print("Global Alignments ' Needlman et Wunsch ' ")
print("Matrix of optimal scores M is :")
print (np.matrix(M))
print("\n")
print("Matrix of directions Dir is :")
print (np.matrix(Dir))
print(" Score is :")
print(M[i][j])
seq1= ["A","C","G","G","G","T"]
seq2= ["A","C","G"]
needleman_wunsch(seq1,seq2)