Files
obitools4/pkg/obialign/backtracking.go

95 lines
1.3 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obialign
import "slices"
2022-01-14 15:30:01 +01:00
func _Backtracking(pathMatrix []int, lseqA, lseqB int, path *[]int) []int {
2022-01-13 23:27:39 +01:00
needed := (lseqA + lseqB) * 2
(*path) = (*path)[:0]
(*path) = slices.Grow((*path), needed)
2022-01-13 23:27:39 +01:00
p := cap(*path)
*path = (*path)[:p]
2022-01-13 23:27:39 +01:00
i := lseqA - 1
j := lseqB - 1
ldiag := 0
lup := 0
lleft := 0
for i > -1 || j > -1 {
2022-01-14 15:46:36 +01:00
step := _GetMatrix(&pathMatrix, lseqA, i, j)
2022-01-13 23:27:39 +01:00
// log.Printf("I: %d J:%d -> %d\n", i, j, step)
switch {
case step == 0:
if lleft != 0 {
p--
(*path)[p] = ldiag
p--
(*path)[p] = lleft
lleft = 0
ldiag = 0
}
if lup != 0 {
p--
(*path)[p] = ldiag
p--
(*path)[p] = lup
lup = 0
ldiag = 0
}
ldiag++
i--
j--
case step > 0:
if lup != 0 {
p--
(*path)[p] = ldiag
p--
(*path)[p] = lup
lup = 0
ldiag = 0
}
lleft += step
j -= step
case step < 0:
if lleft != 0 {
p--
(*path)[p] = ldiag
p--
(*path)[p] = lleft
lleft = 0
ldiag = 0
}
lup += step
i += step
}
}
if lleft != 0 {
p--
(*path)[p] = ldiag
p--
(*path)[p] = lleft
ldiag = 0
}
if lup != 0 {
p--
(*path)[p] = ldiag
p--
(*path)[p] = lup
ldiag = 0
}
if ldiag != 0 {
p--
(*path)[p] = ldiag
p--
(*path)[p] = 0
}
*path = (*path)[p:cap((*path))]
return *path
}