minor optimization to traceback

This commit is contained in:
Arthur Lu 2024-11-05 19:24:07 +00:00
parent 3da3ddf10c
commit ba978f425c
2 changed files with 12 additions and 10 deletions

View File

@ -62,10 +62,6 @@ func Reverse(s string) string {
return string(buf)
}
func Splice(s string, c rune, idx int) string {
return s[:idx] + string(c) + s[idx:]
}
func NextLoHi(M WavefrontComponent, I WavefrontComponent, D WavefrontComponent, score int, penalties Penalty) (int, int) {
x := penalties.X
o := penalties.O

View File

@ -1,5 +1,9 @@
package wfa
import (
"strings"
)
func WFAlign(s1 string, s2 string, penalties Penalty, doCIGAR bool) Result {
n := len(s1)
m := len(s2)
@ -115,28 +119,30 @@ func WFBacktrace(M WavefrontComponent, I WavefrontComponent, D WavefrontComponen
}
CIGAR_part := Reverse(CIGAR_rev)
CIGAR_full := strings.Builder{}
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++
for (i < len(s1) && j < len(s2)) || (c < len(CIGAR_part)) {
if i < len(s1) && j < len(s2) && s1[i] == s2[j] {
CIGAR_full.WriteByte('M')
i++
j++
} else if CIGAR_part[c] == 'X' {
CIGAR_full.WriteByte('X')
c++
i++
j++
} else if CIGAR_part[c] == 'I' {
CIGAR_full.WriteByte('I')
c++
j++
} else if CIGAR_part[c] == 'D' {
CIGAR_full.WriteByte('D')
c++
i++
}
}
return CIGAR_part
return CIGAR_full.String()
}