BioPython

Global sequence alignment

Needleman-Wunsch algorithm

×
Executing
⏸ Pause
Last run: 2:34 PM View Historical Run

Compare DNA sequences using Needleman-Wunsch algorithm.

Python 3.11.2
Biopython 1.81
DNA File A

sequence_A.fasta

DNA File B

sequence_B.fasta

1
-1
-2
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)]