Python Forum
needleman wunsch algorithm for two sequences of different length
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
needleman wunsch algorithm for two sequences of different length
#1
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 !!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  detect equal sequences in list flash77 17 2,683 Oct-28-2022, 06:38 AM
Last Post: flash77
  help for escape sequences NewPi 1 1,997 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  copying parts of mutable sequences Skaperen 1 2,188 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  Convert weekly sequences to date and time. SinPy 0 1,421 Nov-23-2019, 05:20 PM
Last Post: SinPy
  Escape sequences display in python Uchikago 1 2,377 Jun-27-2019, 03:25 PM
Last Post: Gribouillis
  type for sequences Skaperen 3 67,796 Oct-19-2018, 03:36 AM
Last Post: Skaperen
  comparing strings (or sequences) Skaperen 5 4,668 Jan-25-2017, 10:30 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020