This is a test version of Biostars. For the public version, visit https://www.biostars.org.
IndexError: list assignment index out of range | Needleman-Wunsch Algorithm - Python

I am attempting to implement the Dynamic Programming Table Needleman-Wunsch Algorithm in Python:

My code is inspired by this.

The far left column and top row need to start from 0, decrementing downwards

Note: this works perfectly fine if I limit the 2 sequences to just 100 characters long each. So maybe it's because the data is too big?

m, n = len(seqA), len(seqB)  # let m, n denote the lengths of two sequences

score_matrix = tasks.empty_matrix(m+1, n+1)

for i in range(0, m+1):
    score_matrix[i][0] = i * -1
for j in range(0, n+1):
    score_matrix[0][j] = j * -1

Error:

for j in range(0, n+1):
    score_matrix[0][j] = j * -1
IndexError: list assignment index out of range

Note: my DNA sequences are all ~10,000 characters long. So, 10K * 10K DP table.

python needleman-wunsch dynamic-programming

the error simply means your matrix has the wrong dimensions, you accessing an index that does not exist

that being said I don't think you can make a 10K x 10K nested list in python,

use a numpy array instead

1 answer

score_matrix had it's x and y value muddled up.

please do not delete posts that have comments or answers, we can all learn from various errors, people would not contribute to a post if they knew that their contributions will be deleted

Log in to answer this question.