## Needleman-Wunsch Algorithm

15 pts.
Tags:
Bioinformatics
Visual Basic
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

Software/Hardware used:
Visual Studio

## Answer Wiki

Thanks. We'll let you know when a new response is added.

When you declare a matrix this way:

<pre>Dim Matrix(,) As Integer = <b>New Integer(,) {}</b></pre>

The last part (in bold) defines the size of the matrix (based on the initial elements provided). In this case you are creating a matrix of 0x0 because the size of the matrix was not specified and no initial elements were provided, so you will get an index-out-of-bounds exception every time you assign a value to any matrix position.

I would recommend to add some debug messages to your code where you are getting ‘index out of bounds’ errors.

You can display the dimensions of your matrix with some code like this:

<pre>MsgBox(matrix.GetLength(0) & “x” & matrix.GetLength(1))</pre>

You should also display the value of the variables you are using as indexes for the matrix.

-CarlosDL

## Discuss This Question: 2 Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
• 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>)".
3,830 pointsBadges:
report
• Thanks guys
15 pointsBadges:
report