{"id":256,"date":"2026-11-13T12:36:05","date_gmt":"2026-11-13T04:36:05","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-smith-waterman-aligns-two-sequences-locally\/"},"modified":"2026-11-13T21:53:50","modified_gmt":"2026-11-13T13:53:50","slug":"how-smith-waterman-aligns-two-sequences-locally","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-smith-waterman-aligns-two-sequences-locally\/","title":{"rendered":"How Smith-Waterman aligns two sequences locally"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Smith-Waterman<\/strong> finds the best local alignment: the highest-scoring pair of substrings under a substitution matrix and a gap penalty. The recurrence is Needleman-Wunsch with one extra choice. A cell may be <strong>0<\/strong>, which means &#8220;start a new alignment here.&#8221; The answer is the maximum cell anywhere in the table, not the corner. Traceback stops when it hits a 0. Time and space are <strong>O(nm)<\/strong> for the full table.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A global alignment is forced to explain every prefix, so a similar motif in the middle of two otherwise unrelated sequences is dragged down by the flanks. Local alignment lets the score restart. The 0 option is optimal exactly when every extension of an empty alignment would score below zero, which requires that the scoring scheme can go negative. A matrix that scores every event positive makes the local optimum the global one, and the clamp never fires.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The best local alignment might end anywhere. Searching only the corner misses it. Traceback from the maximum cell follows the same pointers as Needleman-Wunsch and stops at a cell that chose 0.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">With at least one negative possible contribution, the maximum cell is the optimal local alignment score, and the traceback is one pair of substrings that attains it. Time is \u0398(nm). Space is \u0398(nm) with pointers, or less if only the score and a rolling maximum are required. Several cells can share the maximum. A full list of non-overlapping local alignments is a different procedure that masks the one just found and repeats.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The algorithm is exact for additive local scores. It is the local counterpart of Needleman-Wunsch, not a heuristic seed-and-extend method. BLAST and similar tools use seeds to avoid filling the whole table and can miss the optimum this DP finds.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Use a substitution matrix and a gap penalty that can produce negative scores. Initialize the first row and column to 0, not to cumulative gap costs.<\/li>\n<li>Each cell is the maximum of 0, the diagonal plus the substitution score, the cell above plus the gap penalty, and the cell to the left plus the gap penalty.<\/li>\n<li>Track the cell with the largest value while the table is filled.<\/li>\n<li>Trace back from that cell until a 0 is reached. The path, reversed, is the local alignment.<\/li>\n<li>If only the score is needed, keep the running maximum and two rows. The substrings themselves need the path.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The 0 clamp is local alignment. Without it the algorithm is global. With an all-positive score the clamp never wins and the result stretches to the whole strings. Heuristic local aligners are not this DP. Affine local gaps add the same three states as affine global gaps, with the 0 option on the match state.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when every score is positive<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Use a match score of 1, a mismatch score of 0, and a gap score of 0<\/td>\n<td>Nothing is negative, so the maximum sits at the corner and the local formulation has collapsed to a long global path.<\/td>\n<\/tr>\n<tr>\n<td>Read the answer from the corner<\/td>\n<td>The best substrings may end in the middle of both sequences. The answer is the maximum cell.<\/td>\n<\/tr>\n<tr>\n<td>Stop the traceback at the edge of the table<\/td>\n<td>A local path ends where a cell chose 0, which may be interior. Walking to the edge pulls in a flank the score had discarded.<\/td>\n<\/tr>\n<tr>\n<td>Quote the Smith-Waterman optimum for a seed-and-extend heuristic<\/td>\n<td>The heuristic is faster and not exact. This table is the exact local optimum.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Global alignment is Needleman-Wunsch. Unit edit distance is Levenshtein. Smith-Waterman is the local scored alignment whose restart option is the zero clamp.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment aligns two sequences locally. A cell may fall back to 0, and the answer is the best cell rather than the corner. <code>AXC<\/code> against <code>X<\/code> scores <code>1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def sw(a, b):\n    best = 0\n    prev = [0] * (len(b) + 1)\n    for ca in a:\n        cur = [0]\n        for j, cb in enumerate(b, 1):\n            score = 1 if ca == cb else -1\n            cur.append(max(0, prev[j - 1] + score, prev[j] - 1, cur[j - 1] - 1))\n            best = max(best, cur[-1])\n        prev = cur\n    return best\n\nassert sw(&quot;AXC&quot;, &quot;X&quot;) == 1\nprint(sw(&quot;AXC&quot;, &quot;X&quot;))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>EMBOSS <code>water<\/code><\/strong> and other local-alignment tools use Smith-Waterman on two sequences. A match of a short motif inside a long protein is the local problem. Needleman-Wunsch forces the alignment to cover both strings end to end.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop Smith-Waterman when both strings must be consumed from end to end. Use Needleman-Wunsch. Drop the full table when a heuristic searcher is an acceptable filter and the optimum is allowed to be missed. Keep this DP when the best-scoring pair of substrings is required exactly.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Smith-Waterman returns the zero clamp and the rule that the answer is the maximum cell, not the corner. <strong>Smith-Waterman computes an optimal local alignment by adding a zero restart to the alignment recurrence and tracing back from the maximum cell.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Smith-Waterman finds the best local alignment: the highest-scoring pair of substrings under a substitution matrix and a gap penalty. The recurrence is Needleman-Wunsch with one extra choice. A cell may be 0, which means &#8220;start a new alignment here.&#8221; The answer is the maximum cell anywhere in&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,38],"tags":[],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-strings"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/256\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}