From ba978f425cd527b3b02f0940cdf3c0aec3ea5f6c Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Tue, 5 Nov 2024 19:24:07 +0000 Subject: [PATCH] minor optimization to traceback --- pkg/utils.go | 4 ---- pkg/wfa.go | 18 ++++++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/utils.go b/pkg/utils.go index a806159..deb4e76 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -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 diff --git a/pkg/wfa.go b/pkg/wfa.go index 1bc52d5..1aad469 100644 --- a/pkg/wfa.go +++ b/pkg/wfa.go @@ -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() }