I am trying to implement Needleman-Wunsch algorithm in VB. unfortunately I get lot of runtime errors. could someone help me to solve this. The code is given below.
Public Class SequenceAlignment
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
'declare Score variables
Dim MatchScore As Integer = 5
Dim MisMatchScore As Integer = 0.5
Dim GapScore As Integer = -2
'+======================================================================================+
'initialization; set initial Score and get initial Pointer ‘left’ or ‘up’
'on top columns and Rows on the left respectively
'+======================================================================================+
Dim Matrix(,) As Integer = New Integer(,) {} 'Created a matrix to fill the score
Dim RowColpointer(,) As String = New String(,) {} 'Created another matrix
'for the pointer to the left and up
Dim J As Integer = 1 'matrix index j
Dim I As Integer = 1 'matrix index i
'(0, 0) of the matrix is score is set to 0 and pointer to ‘none’.
'This position is the end during trace back.
'Matrix(1, 0) = 0 'run time error
'RowColpointer(1, 0) = "none" ' I am getting run time error!!!
'Index was outside the bounds of the array!!!.
'creates values for each column on top of the matrix except the 0,0
For J = 1 To J <= Seq1.Text.Length
Matrix(0, J) = GapScore * J 'creates score on each column
RowColpointer(0, J) = "left" 'creates pointer on each column
Next J
'creates pointer for each row on the left except 0,0
For I = 1 To J <= Seq2.Text.Length
Matrix(I, 0) = GapScore * I 'creates score on each row
RowColpointer(I, 0) = "up" 'creates pointer on each column
Next I
'==============================================================================+
'fill matrix with scores and pointers
'==============================================================================+
Dim diagonal_score As Integer
Dim left_score As Integer
Dim up_score As Integer
Dim letter1 As Integer
Dim letter2 As Integer
For I = 1 To I <= Seq2.Text.Length 'looping through the entire matrix
For J = 1 To Seq1.Text.Length
'Calculate matchScore
letter1 = Mid(Seq1.Text, J - 1, 1)
letter2 = Mid(Seq2.Text, I - 1, 1)
'condition set to generate diagonal score
If letter1 = letter2 Then
diagonal_score = Matrix(I - 1, J - 1) + MatchScore
Else : diagonal_score = Matrix(I - 1, J - 1) + MisMatchScore
End If
'Calculate gap scores
up_score = Matrix(I - 1, J) + GapScore
left_score = Matrix(I, J - 1) + GapScore
'conditions to generate best score and pointers the entire matrix
If diagonal_score >= up_score Then
If diagonal_score >= left_score Then
Matrix(I, J) = diagonal_score
RowColpointer(I, J) = "diagonal"
Else : Matrix(I, J) = left_score
RowColpointer(I, J) = "left"
End If
If up_score >= left_score Then
Matrix(I, J) = up_score
RowColpointer(I, J) = "up"
Else : Matrix(I, J) = left_score
RowColpointer(I, J) = "left"
End If
End If
Next J
Next I
'=======================================================================================+
'trace back
'=======================================================================================+
Dim align1 As String = " "
Dim align2 As String = " "
'starts at the last cell of the matrix
J = Seq1.Text.Length '|
I = Seq2.Text.Length '| to select the last cell in the matrix
'when the trace back reaches 'none' it stops
While (1)
If RowColpointer(I, J) = "none" Then '//run time error
'//Index was outside the bounds of the array.
End If
End While
If RowColpointer(I, J) = "Diagonal" Then '//run time error
'//Index was outside the bounds of the array.
align1 = Mid(Seq1.Text, J - 1, 1)
align2 = Mid(Seq2.Text, I - 2, 1)
I -= 1
J -= 1
ElseIf RowColpointer(I, J) = "left" Then '//run time error
'//Index was outside the bounds of the array.
align1 = Mid(Seq1.Text, J - 1, 1)
align2 = "-"
J -= 1
ElseIf Matrix(I, J) = "up" Then '//run time error
'//Index was outside the bounds of the array.
align1 = "-"
align2 = Mid(Seq1.Text, I - 1, 1)
I -= 1
End If
'since the trace back is performed backwards thorugh the sequence, the string is
'generated in a reverse order. so the sequence should be arranged in a correct order
StrReverse(align1)
StrReverse(align2)
'Disply in message box
MessageBox.Show(align1 + "n", align2 + "n")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
To dynamically increase the bounds of your arrays, use the ReDim statement.
ReDim Matrix(Seq2.text.length, Seq1.text.length)
ReDim RowColPointer(Seq2.text.length, Seq1.text.length)
Note - if you dynamically ReDim an array after any values have been set in the array, you must use "ReDim Preserve <name>(<newbounds>)".
Free Guide: Managing storage for virtual environments
Complete a brief survey to get a complimentary 70-page whitepaper featuring the best methods and solutions for your virtual environment, as well as hypervisor-specific management advice from TechTarget experts. Don’t miss out on this exclusive content!
Discuss This Question: 2 Replies