Python Forum
needleman wunsch algorithm for two sequences of different length - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: needleman wunsch algorithm for two sequences of different length (/thread-28640.html)



needleman wunsch algorithm for two sequences of different length - johnny_sav1992 - Jul-27-2020

i wrote a code to align two sequences with the needleman-wunsch algorithm. it is going well for two sequences same length. When i use two sequences of different length i get an error : string index out of range. Could anyone help me ?

Thats is my code:

M=len(s1)
N=len(s2)
match=+2
gap=-1
mismatch=-2
matrix=np.zeros((N+1,M+1))

#builduing the score matrix 

# 1. putting horizontal and vertical gaps in columns and rows 
for i in range(1,N+1):
    matrix[i,0]=matrix[i-1,0]+gap
for j  in range(1,M+1):
    matrix[0,j]=matrix[j-1,0]+gap
    
# 2. putting in the score/values in the score matrix 
    
for i in range(1,N+1):
       for j in range(1,M+1):
           if s1[i-1] == s2[j-1]:
               score1 = matrix[i-1,j-1] + match
           else:
               score1 = matrix[i-1,j-1] + mismatch
           score2 = matrix[i,j-1] + gap
           score3 = matrix[i-1,j] + gap
           matrix[i,j] = max(score1,score2,score3)
#create a traceback matrix with D=diagonal, V=vertikal, H=horizontal 
trace_mat=np.zeros((N+1,M+1),dtype=str)
for i in range(1,N+1):
    trace_mat[i,0]= 'V'
for j  in range(1,M+1):
    trace_mat[0,j]='H'
I would appreciate your help !!!