Global sequence alignment
Needleman-Wunsch algorithm
⋯
×
Overview
Compare DNA sequences using Needleman-Wunsch algorithm.
Configuration for this run
Python 3.11.2
Biopython 1.81
Input Files
DNA File A
sequence_A.fasta
DNA File B
sequence_B.fasta
Algorithm Parameters
1
-1
-2
Code Preview
Python 3.11.2
✦ Auto-Debug
import numpy as np
def needleman_wunsch(seq1, seq2, match_score=1, mismatch_score=-1, gap_score=-2):
# Initialize dynamic programming matrix
dp = np.zeros((len(seq1)+1, len(seq2)+1), dtype=int)
dp[0,:] = np.arange(len(seq2)+1) * gap_score
dp[:,0] = np.arange(len(seq1)+1) * gap_score
# Fill the matrix
for i in range(1, len(seq1)+1):
for j in range(1, len(seq2)+1):
match = dp[i-1][j-1] + (match_score if seq1[i-1] == seq2[j-1] else mismatch_score)
delete = dp[i-1][j] + gap_score
insert = dp[i][j-1] + gap_score
dp[i][j] = max(match, delete, insert)
return dp, dp[len(seq1)][len(seq2)]