WFA-JS/pkg/types.go

119 lines
2.7 KiB
Go
Raw Normal View History

2024-10-24 18:07:10 +00:00
package wfa
type Result struct {
Score int
CIGAR string
}
2024-10-24 18:07:10 +00:00
type Penalty struct {
M int
X int
O int
E int
}
type traceback byte
const (
OpenIns traceback = iota
ExtdIns
OpenDel
ExtdDel
Sub
Ins
Del
End
)
type WavefrontComponent struct {
lo *PositiveSlice[int] // lo for each wavefront
hi *PositiveSlice[int] // hi for each wavefront
W *PositiveSlice[*Wavefront[int]] // wavefront diag distance for each wavefront
A *PositiveSlice[*Wavefront[traceback]] // compact CIGAR for backtrace for each wavefront
2024-10-24 18:07:10 +00:00
}
func NewWavefrontComponent(preallocateSize int) WavefrontComponent {
2024-10-24 18:07:10 +00:00
// new wavefront component = {
// lo = [0]
// hi = [0]
// W = []
// A = []
// }
w := WavefrontComponent{
2024-10-24 18:07:10 +00:00
lo: &PositiveSlice[int]{
data: []int{0},
valid: []bool{true},
},
hi: &PositiveSlice[int]{
data: []int{0},
valid: []bool{true},
},
W: &PositiveSlice[*Wavefront[int]]{
defaultValue: &Wavefront[int]{
data: []int{0},
valid: []bool{false},
},
},
A: &PositiveSlice[*Wavefront[traceback]]{
defaultValue: &Wavefront[traceback]{
data: []traceback{0},
valid: []bool{false},
},
},
2024-10-24 18:07:10 +00:00
}
w.lo.Preallocate(preallocateSize)
w.hi.Preallocate(preallocateSize)
w.W.Preallocate(preallocateSize)
w.A.Preallocate(preallocateSize)
return w
2024-10-24 18:07:10 +00:00
}
// get value for wavefront=score, diag=k => returns ok, value
func (w *WavefrontComponent) GetVal(score int, k int) (bool, int) {
return w.W.Valid(score) && w.W.Get(score).Valid(k), w.W.Get(score).Get(k)
2024-10-24 18:07:10 +00:00
}
// set value for wavefront=score, diag=k
func (w *WavefrontComponent) SetVal(score int, k int, val int) {
w.W.Get(score).Set(k, val)
2024-10-24 18:07:10 +00:00
}
// get alignment traceback for wavefront=score, diag=k => returns ok, value
func (w *WavefrontComponent) GetTraceback(score int, k int) (bool, traceback) {
return w.A.Valid(score) && w.A.Get(score).Valid(k), w.A.Get(score).Get(k)
2024-10-24 18:07:10 +00:00
}
// set alignment traceback for wavefront=score, diag=k
func (w *WavefrontComponent) SetTraceback(score int, k int, val traceback) {
w.A.Get(score).Set(k, val)
2024-10-24 18:07:10 +00:00
}
// get hi for wavefront=score
func (w *WavefrontComponent) GetLoHi(score int) (bool, int, int) {
// if lo[score] and hi[score] are valid
if w.lo.Valid(score) && w.hi.Valid(score) {
// return lo[score] hi[score]
return true, w.lo.Get(score), w.hi.Get(score)
2024-10-24 18:07:10 +00:00
} else {
return false, 0, 0
2024-10-24 18:07:10 +00:00
}
}
// set hi for wavefront=score
func (w *WavefrontComponent) SetLoHi(score int, lo int, hi int) {
// lo[score] = lo
w.lo.Set(score, lo)
2024-10-24 18:07:10 +00:00
// hi[score] = hi
w.hi.Set(score, hi)
// preemptively setup w.A
a := NewWavefront[traceback](lo, hi)
w.A.Set(score, a)
2024-10-24 18:07:10 +00:00
// preemptively setup w.W
b := NewWavefront[int](lo, hi)
w.W.Set(score, b)
2024-10-24 18:07:10 +00:00
}