rewrite in go and compile to wasm

This commit is contained in:
2024-10-24 18:07:10 +00:00
parent bc50e3ee4d
commit 547dffd8ee
18 changed files with 810 additions and 468 deletions

97
pkg/custom_slice.go Normal file
View File

@@ -0,0 +1,97 @@
package wfa
type IntegerSlice[T any] struct {
data []T
valid []bool
defaultValue T
}
func (a *IntegerSlice[T]) TranslateIndex(idx int) int {
if idx >= 0 { // 0 -> 0, 1 -> 2, 2 -> 4, 3 -> 6, ...
return 2 * idx
} else { // -1 -> 1, -2 -> 3, -3 -> 5, ...
return (-2 * idx) - 1
}
}
func (a *IntegerSlice[T]) Valid(idx int) bool {
actualIdx := a.TranslateIndex(idx)
if actualIdx < len(a.valid) { // idx is in the slice
return a.valid[actualIdx]
} else { // idx is out of the slice
return false
}
}
func (a *IntegerSlice[T]) Get(idx int) T {
actualIdx := a.TranslateIndex(idx)
if actualIdx < len(a.valid) { // idx is in the slice
return a.data[actualIdx]
} else { // idx is out of the slice
return a.defaultValue
}
}
func (a *IntegerSlice[T]) Set(idx int, value T) {
actualIdx := a.TranslateIndex(idx)
if actualIdx >= len(a.valid) { // idx is outside the slice
// expand data array to actualIdx
newData := make([]T, actualIdx+1)
copy(newData, a.data)
a.data = newData
// expand valid array to actualIdx
newValid := make([]bool, actualIdx+1)
copy(newValid, a.valid)
a.valid = newValid
}
a.data[actualIdx] = value
a.valid[actualIdx] = true
}
type PositiveSlice[T any] struct {
data []T
valid []bool
defaultValue T
}
func (a *PositiveSlice[T]) TranslateIndex(idx int) int {
return idx
}
func (a *PositiveSlice[T]) Valid(idx int) bool {
actualIdx := a.TranslateIndex(idx)
if actualIdx >= 0 && actualIdx < len(a.valid) { // idx is in the slice
return a.valid[actualIdx]
} else { // idx is out of the slice
return false
}
}
func (a *PositiveSlice[T]) Get(idx int) T {
actualIdx := a.TranslateIndex(idx)
if actualIdx >= 0 && actualIdx < len(a.valid) { // idx is in the slice
return a.data[actualIdx]
} else { // idx is out of the slice
return a.defaultValue
}
}
func (a *PositiveSlice[T]) Set(idx int, value T) {
actualIdx := a.TranslateIndex(idx)
if actualIdx < 0 || actualIdx >= len(a.valid) { // idx is outside the slice
// expand data array to actualIdx
newData := make([]T, actualIdx+1)
copy(newData, a.data)
a.data = newData
// expand valid array to actualIdx
newValid := make([]bool, actualIdx+1)
copy(newValid, a.valid)
a.valid = newValid
}
a.data[actualIdx] = value
a.valid[actualIdx] = true
}

214
pkg/types.go Normal file
View File

@@ -0,0 +1,214 @@
package wfa
import (
"fmt"
"math"
)
type Penalty struct {
M int
X int
O int
E int
}
type traceback byte
const (
OpenIns traceback = iota
ExtdIns
OpenDel
ExtdDel
Sub
Ins
Del
End
)
type WavefrontComponent struct {
lo *PositiveSlice[int] // lo for each wavefront
hi *PositiveSlice[int] // hi for each wavefront
W *PositiveSlice[*IntegerSlice[int]] // wavefront diag distance for each wavefront
A *PositiveSlice[*IntegerSlice[traceback]] // compact CIGAR for backtrace for each wavefront
}
func NewWavefrontComponent() WavefrontComponent {
// new wavefront component = {
// lo = [0]
// hi = [0]
// W = []
// A = []
// }
return WavefrontComponent{
lo: &PositiveSlice[int]{
data: []int{0},
valid: []bool{true},
},
hi: &PositiveSlice[int]{
data: []int{0},
valid: []bool{true},
},
W: &PositiveSlice[*IntegerSlice[int]]{},
A: &PositiveSlice[*IntegerSlice[traceback]]{},
}
}
// get value for wavefront=score, diag=k => returns ok, value
func (w *WavefrontComponent) GetVal(score int, k int) (bool, int) {
// if W[score][k] is valid
if w.W.Valid(score) && w.W.Get(score).Valid(k) {
// return W[score][k]
return true, w.W.Get(score).Get(k)
} else {
return false, 0
}
}
// set value for wavefront=score, diag=k
func (w *WavefrontComponent) SetVal(score int, k int, val int) {
// if W[score] is valid
if w.W.Valid(score) {
// W[score][k] = val
w.W.Get(score).Set(k, val)
} else {
// W[score] = []
w.W.Set(score, &IntegerSlice[int]{})
// W[score][k] = val
w.W.Get(score).Set(k, val)
}
}
// get alignment traceback for wavefront=score, diag=k => returns ok, value
func (w *WavefrontComponent) GetTraceback(score int, k int) (bool, traceback) {
// if W[score][k] is valid
if w.A.Valid(score) && w.A.Get(score).Valid(k) {
// return W[score][k]
return true, w.A.Get(score).Get(k)
} else {
return false, 0
}
}
// set alignment traceback for wavefront=score, diag=k
func (w *WavefrontComponent) SetTraceback(score int, k int, val traceback) {
// if A[score] is valid
if w.A.Valid(score) {
// A[score][k] = val
w.A.Get(score).Set(k, val)
} else {
// W[score] = []
w.A.Set(score, &IntegerSlice[traceback]{})
// W[score][k] = val
w.A.Get(score).Set(k, val)
}
}
// get hi for wavefront=score
func (w *WavefrontComponent) GetHi(score int) (bool, int) {
// if hi[score] is valid
if w.hi.Valid(score) {
// return hi[score]
return true, w.hi.Get(score)
} else {
return false, 0
}
}
// set hi for wavefront=score
func (w *WavefrontComponent) SetHi(score int, hi int) {
// hi[score] = hi
w.hi.Set(score, hi)
}
// get lo for wavefront=score
func (w *WavefrontComponent) GetLo(score int) (bool, int) {
// if lo[score] is valid
if w.lo.Valid(score) {
// return lo[score]
return true, w.lo.Get(score)
} else {
return false, 0
}
}
// set hi for wavefront=score
func (w *WavefrontComponent) SetLo(score int, lo int) {
// lo[score] = lo
w.lo.Set(score, lo)
}
type Result struct {
Score int
CIGAR string
}
func (w *WavefrontComponent) String(score int) string {
traceback_str := []string{"OI", "EI", "OD", "ED", "SB", "IN", "DL", "EN"}
s := "<"
min_lo := math.MaxInt
max_hi := math.MinInt
for i := 0; i <= score; i++ {
if w.lo.Valid(i) && w.lo.Get(i) < min_lo {
min_lo = w.lo.Get(i)
}
if w.hi.Valid(i) && w.hi.Get(i) > max_hi {
max_hi = w.hi.Get(i)
}
}
for k := min_lo; k <= max_hi; k++ {
s = s + fmt.Sprintf("%02d", k)
if k < max_hi {
s = s + "|"
}
}
s = s + ">\t<"
for k := min_lo; k <= max_hi; k++ {
s = s + fmt.Sprintf("%02d", k)
if k < max_hi {
s = s + "|"
}
}
s = s + ">\n"
for i := 0; i <= score; i++ {
s = s + "["
lo := w.lo.Get(i)
hi := w.hi.Get(i)
// print out wavefront matrix
for k := min_lo; k <= max_hi; k++ {
if w.W.Valid(i) && w.W.Get(i).Valid(k) {
s = s + fmt.Sprintf("%02d", w.W.Get(i).Get(k))
} else if k < lo || k > hi {
s = s + "--"
} else {
s = s + " "
}
if k < max_hi {
s = s + "|"
}
}
s = s + "]\t["
// print out traceback matrix
for k := min_lo; k <= max_hi; k++ {
if w.A.Valid(i) && w.A.Get(i).Valid(k) {
s = s + traceback_str[w.A.Get(i).Get(k)]
} else if k < lo || k > hi {
s = s + "--"
} else {
s = s + " "
}
if k < max_hi {
s = s + "|"
}
}
s = s + "]\n"
}
return s
}

168
pkg/utils.go Normal file
View File

@@ -0,0 +1,168 @@
package wfa
import (
"math"
"unicode/utf8"
)
func SafeMin(valids []bool, values []int) (bool, int) {
ok, idx := SafeArgMin(valids, values)
return ok, values[idx]
}
func SafeMax(valids []bool, values []int) (bool, int) {
ok, idx := SafeArgMax(valids, values)
return ok, values[idx]
}
func SafeArgMax(valids []bool, values []int) (bool, int) {
hasValid := false
maxIndex := 0
maxValue := math.MinInt
for i := 0; i < len(valids); i++ {
if valids[i] && values[i] > maxValue {
hasValid = true
maxIndex = i
maxValue = values[i]
}
}
if hasValid {
return true, maxIndex
} else {
return false, 0
}
}
func SafeArgMin(valids []bool, values []int) (bool, int) {
hasValid := false
minIndex := 0
minValue := math.MaxInt
for i := 0; i < len(valids); i++ {
if valids[i] && values[i] < minValue {
hasValid = true
minIndex = i
minValue = values[i]
}
}
if hasValid {
return true, minIndex
} else {
return false, 0
}
}
func Reverse(s string) string {
size := len(s)
buf := make([]byte, size)
for start := 0; start < size; {
r, n := utf8.DecodeRuneInString(s[start:])
start += n
utf8.EncodeRune(buf[size-start:], r)
}
return string(buf)
}
func Splice(s string, c rune, idx int) string {
return s[:idx] + string(c) + s[idx:]
}
func NextLo(M WavefrontComponent, I WavefrontComponent, D WavefrontComponent, score int, penalties Penalty) int {
x := penalties.X
o := penalties.O
e := penalties.E
a_ok, a := M.GetLo(score - x)
b_ok, b := M.GetLo(score - o - e)
c_ok, c := I.GetLo(score - e)
d_ok, d := D.GetLo(score - e)
ok, lo := SafeMin(
[]bool{a_ok, b_ok, c_ok, d_ok},
[]int{a, b, c, d},
)
lo--
if ok {
M.SetLo(score, lo)
I.SetLo(score, lo)
D.SetLo(score, lo)
}
return lo
}
func NextHi(M WavefrontComponent, I WavefrontComponent, D WavefrontComponent, score int, penalties Penalty) int {
x := penalties.X
o := penalties.O
e := penalties.E
a_ok, a := M.GetHi(score - x)
b_ok, b := M.GetHi(score - o - e)
c_ok, c := I.GetHi(score - e)
d_ok, d := D.GetHi(score - e)
ok, hi := SafeMax(
[]bool{a_ok, b_ok, c_ok, d_ok},
[]int{a, b, c, d},
)
hi++
if ok {
M.SetHi(score, hi)
I.SetHi(score, hi)
D.SetHi(score, hi)
}
return hi
}
func NextI(M WavefrontComponent, I WavefrontComponent, score int, k int, penalties Penalty) {
o := penalties.O
e := penalties.E
a_ok, a := M.GetVal(score-o-e, k-1)
b_ok, b := I.GetVal(score-e, k-1)
ok, nextIVal := SafeMax([]bool{a_ok, b_ok}, []int{a, b})
if ok {
I.SetVal(score, k, nextIVal+1) // important that the +1 is here
}
ok, nextITraceback := SafeArgMax([]bool{a_ok, b_ok}, []int{a, b})
if ok {
I.SetTraceback(score, k, []traceback{OpenIns, ExtdIns}[nextITraceback])
}
}
func NextD(M WavefrontComponent, D WavefrontComponent, score int, k int, penalties Penalty) {
o := penalties.O
e := penalties.E
a_ok, a := M.GetVal(score-o-e, k+1)
b_ok, b := D.GetVal(score-e, k+1)
ok, nextDVal := SafeMax([]bool{a_ok, b_ok}, []int{a, b})
if ok {
D.SetVal(score, k, nextDVal) // nothing special
}
ok, nextDTraceback := SafeArgMax([]bool{a_ok, b_ok}, []int{a, b})
if ok {
D.SetTraceback(score, k, []traceback{OpenDel, ExtdDel}[nextDTraceback])
}
}
func NextM(M WavefrontComponent, I WavefrontComponent, D WavefrontComponent, score int, k int, penalties Penalty) {
x := penalties.X
a_ok, a := M.GetVal(score-x, k)
a++ // important to have +1 here
b_ok, b := I.GetVal(score, k)
c_ok, c := D.GetVal(score, k)
ok, nextMVal := SafeMax([]bool{a_ok, b_ok, c_ok}, []int{a, b, c})
if ok {
M.SetVal(score, k, nextMVal)
}
ok, nextMTraceback := SafeArgMax([]bool{a_ok, b_ok, c_ok}, []int{a, b, c})
if ok {
M.SetTraceback(score, k, []traceback{Sub, Ins, Del}[nextMTraceback])
}
}

146
pkg/wfa.go Normal file
View File

@@ -0,0 +1,146 @@
package wfa
func WFAlign(s1 string, s2 string, penalties Penalty, doCIGAR bool) Result {
n := len(s1)
m := len(s2)
A_k := m - n
A_offset := m
score := 0
M := NewWavefrontComponent()
M.SetVal(0, 0, 0)
M.SetHi(0, 0)
M.SetLo(0, 0)
M.SetTraceback(0, 0, End)
I := NewWavefrontComponent()
D := NewWavefrontComponent()
for {
WFExtend(M, s1, n, s2, m, score)
ok, val := M.GetVal(score, A_k)
if ok && val >= A_offset {
break
}
score = score + 1
WFNext(M, I, D, score, penalties)
}
CIGAR := ""
if doCIGAR {
CIGAR = WFBacktrace(M, I, D, score, penalties, A_k, s1, s2)
}
return Result{
Score: score,
CIGAR: CIGAR,
}
}
func WFExtend(M WavefrontComponent, s1 string, n int, s2 string, m int, score int) {
_, lo := M.GetLo(score)
_, hi := M.GetHi(score)
for k := lo; k <= hi; k++ {
// v = M[score][k] - k
// h = M[score][k]
ok, h := M.GetVal(score, k)
v := h - k
// exit early if v or h are invalid
if !ok {
continue
}
for v < n && h < m && s1[v] == s2[h] {
_, val := M.GetVal(score, k)
M.SetVal(score, k, val+1)
v++
h++
}
}
}
func WFNext(M WavefrontComponent, I WavefrontComponent, D WavefrontComponent, score int, penalties Penalty) {
// get this score's lo
lo := NextLo(M, I, D, score, penalties)
// get this score's hi
hi := NextHi(M, I, D, score, penalties)
for k := lo; k <= hi; k++ {
NextI(M, I, score, k, penalties)
NextD(M, D, score, k, penalties)
NextM(M, I, D, score, k, penalties)
}
}
func WFBacktrace(M WavefrontComponent, I WavefrontComponent, D WavefrontComponent, score int, penalties Penalty, A_k int, s1 string, s2 string) string {
traceback_CIGAR := []string{"I", "I", "D", "D", "X", "", "", ""}
x := penalties.X
o := penalties.O
e := penalties.E
CIGAR_rev := ""
tb_s := score
tb_k := A_k
_, current_traceback := M.GetTraceback(tb_s, tb_k)
done := false
for !done {
CIGAR_rev = CIGAR_rev + traceback_CIGAR[current_traceback]
switch current_traceback {
case OpenIns:
tb_s = tb_s - o - e
tb_k = tb_k - 1
_, current_traceback = M.GetTraceback(tb_s, tb_k)
case ExtdIns:
tb_s = tb_s - e
tb_k = tb_k - 1
_, current_traceback = I.GetTraceback(tb_s, tb_k)
case OpenDel:
tb_s = tb_s - o - e
tb_k = tb_k + 1
_, current_traceback = M.GetTraceback(tb_s, tb_k)
case ExtdDel:
tb_s = tb_s - e
tb_k = tb_k + 1
_, current_traceback = D.GetTraceback(tb_s, tb_k)
case Sub:
tb_s = tb_s - x
// tb_k = tb_k;
_, current_traceback = M.GetTraceback(tb_s, tb_k)
case Ins:
// tb_s = tb_s;
// tb_k = tb_k;
_, current_traceback = I.GetTraceback(tb_s, tb_k)
case Del:
// tb_s = tb_s;
// tb_k = tb_k;
_, current_traceback = D.GetTraceback(tb_s, tb_k)
case End:
done = true
}
}
CIGAR_part := Reverse(CIGAR_rev)
c := 0
i := 0
j := 0
for i < len(s1) && j < len(s2) {
if s1[i] == s2[j] {
//CIGAR_part.splice(c, 0, "M")
CIGAR_part = Splice(CIGAR_part, 'M', c)
c++
i++
j++
} else if CIGAR_part[c] == 'X' {
c++
i++
j++
} else if CIGAR_part[c] == 'I' {
c++
j++
} else if CIGAR_part[c] == 'D' {
c++
i++
}
}
return CIGAR_part
}