WFA-JS/pkg/wfa.go

198 lines
4.0 KiB
Go
Raw Permalink Normal View History

2024-10-24 18:07:10 +00:00
package wfa
import (
"strings"
)
2024-10-24 18:07:10 +00:00
func WFAlign(s1 string, s2 string, penalties Penalty, doCIGAR bool) Result {
n := len(s1)
m := len(s2)
A_k := m - n
A_offset := uint32(m)
2024-10-24 18:07:10 +00:00
score := 0
M := NewWavefrontComponent()
M.SetLoHi(0, 0, 0)
M.SetVal(0, 0, 0, End)
I := NewWavefrontComponent()
D := NewWavefrontComponent()
2024-10-24 18:07:10 +00:00
for {
WFExtend(M, s1, n, s2, m, score)
ok, val, _ := M.GetVal(score, A_k)
2024-10-24 18:07:10 +00:00
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, A_offset, s1, s2)
2024-10-24 18:07:10 +00:00
}
return Result{
Score: score,
CIGAR: CIGAR,
}
}
func WFExtend(M *WavefrontComponent, s1 string, n int, s2 string, m int, score int) {
_, lo, hi := M.GetLoHi(score)
2024-10-24 18:07:10 +00:00
for k := lo; k <= hi; k++ {
// v = M[score][k] - k
// h = M[score][k]
ok, hu, _ := M.GetVal(score, k)
h := int(hu)
2024-10-24 18:07:10 +00:00
v := h - k
// exit early if v or h are invalid
if !ok {
continue
}
for v < n && h < m && s1[v] == s2[h] {
_, val, tb := M.GetVal(score, k)
M.SetVal(score, k, val+1, tb)
2024-10-24 18:07:10 +00:00
v++
h++
}
}
}
func WFNext(M *WavefrontComponent, I *WavefrontComponent, D *WavefrontComponent, score int, penalties Penalty) {
// get this score's lo, hi
lo, hi := NextLoHi(M, I, D, score, penalties)
2024-10-24 18:07:10 +00:00
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, A_offset uint32, s1 string, s2 string) string {
2024-10-24 18:07:10 +00:00
x := penalties.X
o := penalties.O
e := penalties.E
2024-10-24 18:07:10 +00:00
tb_s := score
tb_k := A_k
done := false
_, current_dist, current_traceback := M.GetVal(tb_s, tb_k)
Ops := []rune{'~'}
Counts := []uint{0}
idx := 0
2024-10-24 18:07:10 +00:00
for !done {
switch current_traceback {
case OpenIns:
if Ops[idx] == 'I' {
Counts[idx]++
} else {
Ops = append(Ops, 'I')
Counts = append(Counts, 1)
idx++
}
2024-10-24 18:07:10 +00:00
tb_s = tb_s - o - e
tb_k = tb_k - 1
_, current_dist, current_traceback = M.GetVal(tb_s, tb_k)
2024-10-24 18:07:10 +00:00
case ExtdIns:
if Ops[idx] == 'I' {
Counts[idx]++
} else {
Ops = append(Ops, 'I')
Counts = append(Counts, 1)
idx++
}
2024-10-24 18:07:10 +00:00
tb_s = tb_s - e
tb_k = tb_k - 1
_, current_dist, current_traceback = I.GetVal(tb_s, tb_k)
2024-10-24 18:07:10 +00:00
case OpenDel:
if Ops[idx] == 'D' {
Counts[idx]++
} else {
Ops = append(Ops, 'D')
Counts = append(Counts, 1)
idx++
}
2024-10-24 18:07:10 +00:00
tb_s = tb_s - o - e
tb_k = tb_k + 1
_, current_dist, current_traceback = M.GetVal(tb_s, tb_k)
2024-10-24 18:07:10 +00:00
case ExtdDel:
if Ops[idx] == 'D' {
Counts[idx]++
} else {
Ops = append(Ops, 'D')
Counts = append(Counts, 1)
idx++
}
2024-10-24 18:07:10 +00:00
tb_s = tb_s - e
tb_k = tb_k + 1
_, current_dist, current_traceback = D.GetVal(tb_s, tb_k)
2024-10-24 18:07:10 +00:00
case Sub:
tb_s = tb_s - x
// tb_k = tb_k;
_, next_dist, next_traceback := M.GetVal(tb_s, tb_k)
if int(current_dist-next_dist)-1 > 0 {
Ops = append(Ops, 'M')
Counts = append(Counts, uint(current_dist-next_dist)-1)
idx++
}
if Ops[idx] == 'X' {
Counts[idx]++
} else {
Ops = append(Ops, 'X')
Counts = append(Counts, 1)
idx++
}
current_dist = next_dist
current_traceback = next_traceback
2024-10-24 18:07:10 +00:00
case Ins:
// tb_s = tb_s;
// tb_k = tb_k;
_, next_dist, next_traceback := I.GetVal(tb_s, tb_k)
Ops = append(Ops, 'M')
Counts = append(Counts, uint(current_dist-next_dist))
idx++
current_dist = next_dist
current_traceback = next_traceback
2024-10-24 18:07:10 +00:00
case Del:
// tb_s = tb_s;
// tb_k = tb_k;
_, next_dist, next_traceback := D.GetVal(tb_s, tb_k)
Ops = append(Ops, 'M')
Counts = append(Counts, uint(current_dist-next_dist))
idx++
current_dist = next_dist
current_traceback = next_traceback
2024-10-24 18:07:10 +00:00
case End:
Ops = append(Ops, 'M')
Counts = append(Counts, uint(current_dist))
idx++
2024-10-24 18:07:10 +00:00
done = true
}
}
CIGAR := strings.Builder{}
for i := len(Ops) - 1; i > 0; i-- {
CIGAR.WriteString(UIntToString(Counts[i]))
CIGAR.WriteRune(Ops[i])
2024-10-24 18:07:10 +00:00
}
return CIGAR.String()
2024-10-24 18:07:10 +00:00
}