Case Study · Computational Biology

Needleman-Wunsch DNA Alignment

A Python implementation of the global sequence-alignment algorithm that optimizes DNA / protein alignments with dynamic programming. Supports custom scoring matrices (e.g. BLOSUM 62) or simple match/mismatch schemes, calculates detailed statistics, and exports publication-ready alignments.

Python 3Dynamic ProgrammingStandard Library Only
01

overview

The algorithm fills an O(mn) scoring matrix cell by cell. Each cell depends only on its top, left, and top-left neighbors, and the algorithm then traces back from the bottom-right corner to reconstruct the optimal alignment path.

02

live_preview

The matrix fills in anti-diagonal wavefronts (the only order that respects each cell's dependencies), then the traceback path lights up from bottom-right to top-left.

G
A
T
T
A
C
G
0
1
2
3
4
5
A
1
2
3
4
5
6
C
2
3
4
5
6
7
T
3
4
5
6
7
8
G
4
5
6
7
8
9
03

features

Dynamic Programming Core

O(mn) time/space Needleman-Wunsch matrix with traceback.

Custom Scoring Matrices

Imports any text-matrix (BLOSUM, PAM, DNA identity) or simple match/mismatch values.

Rich Output

Alignment blocks wrapped at 80 chars, plus score, matches, mismatches, gaps & similarity %.

04

stack

Python 3Standard LibraryFASTA ParserAlgorithm AnalysisUnit Testing
05

output

GATTACA-CTG ||| | | | GA-T-CAACTG Score: 23 Matches: 7 Mismatches: 2 Gaps: 2 Similarity: 77.8%