This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"tool": "godbolt",
|
||||
"compiler": "mojo_1_2_0",
|
||||
"mojoVersion": "1.2.0",
|
||||
"source_sha256": "cd659ffc727fffe9b7493b2f77e9e37e758ad6ff36e9afb122a10d29cb5aae57",
|
||||
"stdin": "4 GATTACA GCATGCU 1 -1 -1 ACGT ACGT 1 -1 -2 ACG 1 -1 -2 AAAA TTTT 2 -3 -1 end\n",
|
||||
"args": "--target-cpu x86-64-v3 --fp-mode contract=off",
|
||||
"asked": "2026-11-12",
|
||||
"result": {
|
||||
"compiled": true,
|
||||
"buildStderr": "<source>:7:13: warning: ref 'j' was never used, remove it?\n for j in range(m + 1):\n ^\n<source>:5:9: warning: ref 'i' was never used, remove it?\n for i in range(n + 1):\n ^",
|
||||
"executed": true,
|
||||
"exitCode": 0,
|
||||
"stdout": "0\n4\n-6\n-8",
|
||||
"stderr": "",
|
||||
"timedOut": false,
|
||||
"asm": null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
def needleman_wunsch_score(s: String, t: String, match_: Int, mismatch: Int, gap: Int) -> Int:
|
||||
var n = s.count_codepoints()
|
||||
var m = t.count_codepoints()
|
||||
var d = List[List[Int]]()
|
||||
for i in range(n + 1):
|
||||
var row = List[Int]()
|
||||
for j in range(m + 1):
|
||||
row.append(0)
|
||||
d.append(row^)
|
||||
for i in range(n + 1):
|
||||
d[i][0] = i * gap
|
||||
for j in range(1, m + 1):
|
||||
d[0][j] = j * gap
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 1):
|
||||
var score = mismatch
|
||||
if String(s[codepoint=i - 1]) == String(t[codepoint=j - 1]):
|
||||
score = match_
|
||||
var best = d[i - 1][j - 1] + score
|
||||
if d[i - 1][j] + gap > best:
|
||||
best = d[i - 1][j] + gap
|
||||
if d[i][j - 1] + gap > best:
|
||||
best = d[i][j - 1] + gap
|
||||
d[i][j] = best
|
||||
return d[n][m]
|
||||
@@ -0,0 +1,12 @@
|
||||
def needleman_wunsch_score(s, t, match, mismatch, gap):
|
||||
n, m = len(s), len(t)
|
||||
d = [[0] * (m + 1) for _ in range(n + 1)]
|
||||
for i in range(n + 1):
|
||||
d[i][0] = i * gap
|
||||
for j in range(1, m + 1):
|
||||
d[0][j] = j * gap
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 1):
|
||||
diagonal = d[i - 1][j - 1] + (match if s[i - 1] == t[j - 1] else mismatch)
|
||||
d[i][j] = max(diagonal, d[i - 1][j] + gap, d[i][j - 1] + gap)
|
||||
return d[n][m]
|
||||
@@ -0,0 +1,10 @@
|
||||
def needleman_wunsch_score(s, t, match, mismatch, gap):
|
||||
m = len(t)
|
||||
prev = [j * gap for j in range(m + 1)]
|
||||
for i in range(1, len(s) + 1):
|
||||
cur = [i * gap] + [0] * m
|
||||
for j in range(1, m + 1):
|
||||
diagonal = prev[j - 1] + (match if s[i - 1] == t[j - 1] else mismatch)
|
||||
cur[j] = max(diagonal, prev[j] + gap, cur[j - 1] + gap)
|
||||
prev = cur
|
||||
return prev[m]
|
||||
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "NeedlemanWunschScore",
|
||||
"summary": "The best score of a global alignment of two sequences with a linear gap model, by dynamic programming.",
|
||||
"tags": [
|
||||
"alignment",
|
||||
"dynamic programming"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Needleman and Wunsch, A general method applicable to the search for similarities in the amino acid sequence of two proteins, J. Mol. Biol. 48 (1970)",
|
||||
"url": "https://doi.org/10.1016/0022-2836(70)90057-4"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "NeedlemanWunschScore(\"GATTACA\", \"GCATGCU\", 1, -1, -1)"
|
||||
},
|
||||
{
|
||||
"call": "NeedlemanWunschScore(\"ACGT\", \"ACGT\", 1, -1, -2)"
|
||||
},
|
||||
{
|
||||
"call": "NeedlemanWunschScore(\"\", \"ACG\", 1, -1, -2)"
|
||||
},
|
||||
{
|
||||
"call": "NeedlemanWunschScore(\"AAAA\", \"TTTT\", 2, -3, -1)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same table, filled row by row."
|
||||
},
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "two-rows",
|
||||
"file": "ref/python/two-rows.py",
|
||||
"note": "Only the previous row is kept: memory grows with the length of T alone, the first step toward Hirschberg's linear-space alignment."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same table in Mojo, as a list of rows."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// NeedlemanWunschScore: the best score of a global alignment of S and T
|
||||
// (Needleman and Wunsch, 1970) with a linear gap model, by dynamic
|
||||
// programming. D[I, J] is the best score of aligning the first I letters of
|
||||
// S with the first J letters of T; each cell takes the best of three moves:
|
||||
// align S[I - 1] with T[J - 1], or put a gap in one sequence or the other.
|
||||
USE align.MatchScore
|
||||
USE align.LinearGapPenalty
|
||||
FUNCTION NeedlemanWunschScore(S : STRING, T : STRING, Match : INTEGER, Mismatch : INTEGER, Gap : INTEGER) RETURNS INTEGER
|
||||
DECLARE N : INTEGER <- LENGTH(S)
|
||||
DECLARE M : INTEGER <- LENGTH(T)
|
||||
DECLARE D : ARRAY[N + 1, M + 1] OF INTEGER
|
||||
FOR I <- 0 TO N + 1
|
||||
D[I, 0] <- LinearGapPenalty(I, Gap)
|
||||
NEXT I
|
||||
FOR J <- 1 TO M + 1
|
||||
D[0, J] <- LinearGapPenalty(J, Gap)
|
||||
NEXT J
|
||||
FOR I <- 1 TO N + 1
|
||||
FOR J <- 1 TO M + 1
|
||||
DECLARE Diagonal : INTEGER <- D[I - 1, J - 1] + MatchScore(S[I - 1], T[J - 1], Match, Mismatch)
|
||||
DECLARE Down : INTEGER <- D[I - 1, J] + Gap
|
||||
DECLARE Across : INTEGER <- D[I, J - 1] + Gap
|
||||
D[I, J] <- MAX(Diagonal, Down, Across)
|
||||
NEXT J
|
||||
NEXT I
|
||||
RETURN D[N, M]
|
||||
ENDFUNCTION
|
||||
Reference in new issue
Block a user