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) 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) { func NextLoHi(M WavefrontComponent, I WavefrontComponent, D WavefrontComponent, score int, penalties Penalty) (int, int) {
x := penalties.X x := penalties.X
o := penalties.O o := penalties.O

View File

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