2024-10-24 18:07:10 +00:00
|
|
|
package wfa
|
|
|
|
|
2024-10-29 17:03:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-11-05 18:28:28 +00:00
|
|
|
type Traceback byte
|
2024-10-24 18:07:10 +00:00
|
|
|
|
|
|
|
const (
|
2024-11-05 18:28:28 +00:00
|
|
|
OpenIns Traceback = iota
|
2024-10-24 18:07:10 +00:00
|
|
|
ExtdIns
|
|
|
|
OpenDel
|
|
|
|
ExtdDel
|
|
|
|
Sub
|
|
|
|
Ins
|
|
|
|
Del
|
|
|
|
End
|
|
|
|
)
|
|
|
|
|
2024-11-05 18:28:28 +00:00
|
|
|
// bitpacked wavefront values with 1 valid bit, 3 traceback bits, and 28 bits for the diag distance
|
|
|
|
// technically this restricts to solutions within 268 million score but that should be sufficient for most cases
|
|
|
|
type WavefrontValue uint32
|
|
|
|
|
|
|
|
// TODO: add 64 bit packed value in case more than 268 million score is needed
|
|
|
|
|
|
|
|
// PackWavefrontValue: packs a diag value and traceback into a WavefrontValue
|
|
|
|
func PackWavefrontValue(value uint32, traceback Traceback) WavefrontValue {
|
|
|
|
valueBM := value & 0x0FFF_FFFF
|
|
|
|
tracebackBM := uint32(traceback&0x0000_0007) << 28
|
|
|
|
return WavefrontValue(0x8000_0000 | valueBM | tracebackBM)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpackWavefrontValue: opens a WavefrontValue into a valid bool, diag value and traceback
|
2024-11-07 19:01:01 +00:00
|
|
|
func UnpackWavefrontValue(wfv WavefrontValue) (bool, uint32, Traceback) {
|
|
|
|
valueBM := uint32(wfv & 0x0FFF_FFFF)
|
|
|
|
tracebackBM := uint8(wfv & 0x7000_0000 >> 28)
|
|
|
|
validBM := wfv&0x8000_0000 != 0
|
2024-11-05 18:28:28 +00:00
|
|
|
return validBM, valueBM, Traceback(tracebackBM)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wavefront: stores a single wavefront, stores wavefront's lo value and hi is naturally lo + len(data)
|
|
|
|
type Wavefront struct { // since wavefronts store diag distance, they should never be negative, and traceback data can be stored as uint8
|
|
|
|
data []WavefrontValue
|
|
|
|
lo int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWavefront: returns a new wavefront with size accomodating lo and hi (inclusive)
|
|
|
|
func NewWavefront(lo int, hi int) *Wavefront {
|
|
|
|
a := &Wavefront{}
|
|
|
|
|
|
|
|
a.lo = lo
|
|
|
|
size := a.TranslateIndex(hi)
|
|
|
|
|
|
|
|
newData := make([]WavefrontValue, size+1)
|
|
|
|
a.data = newData
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// TranslateIndex: utility function for getting the data index given a diagonal
|
|
|
|
func (a *Wavefront) TranslateIndex(diagonal int) int {
|
|
|
|
return diagonal - a.lo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get: returns WavefrontValue for given diagonal
|
|
|
|
func (a *Wavefront) Get(diagonal int) WavefrontValue {
|
|
|
|
actualIdx := a.TranslateIndex(diagonal)
|
|
|
|
if 0 <= actualIdx && actualIdx < len(a.data) { // idx is in the slice
|
|
|
|
return a.data[actualIdx]
|
|
|
|
} else { // idx is out of the slice
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set: the diagonal to a WavefrontValue
|
|
|
|
func (a *Wavefront) Set(diagonal int, value WavefrontValue) {
|
|
|
|
actualIdx := a.TranslateIndex(diagonal)
|
|
|
|
|
|
|
|
/* in theory idx is always in bounds because the wavefront is preallocated
|
|
|
|
if actualIdx < 0 || actualIdx >= len(a.data) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
a.data[actualIdx] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// WavefrontComponent: each M/I/D wavefront matrix including the wavefront data, lo and hi
|
2024-10-24 18:07:10 +00:00
|
|
|
type WavefrontComponent struct {
|
2024-11-05 18:28:28 +00:00
|
|
|
lo *PositiveSlice[int] // lo for each wavefront
|
|
|
|
hi *PositiveSlice[int] // hi for each wavefront
|
|
|
|
W *PositiveSlice[*Wavefront] // wavefront diag distance and traceback for each wavefront
|
2024-10-24 18:07:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 18:28:28 +00:00
|
|
|
// NewWavefrontComponent: returns initialized WavefrontComponent
|
2024-11-08 21:51:17 +00:00
|
|
|
func NewWavefrontComponent(preallocateSize int) *WavefrontComponent {
|
2024-10-24 18:07:10 +00:00
|
|
|
// new wavefront component = {
|
|
|
|
// lo = [0]
|
|
|
|
// hi = [0]
|
|
|
|
// W = []
|
|
|
|
// }
|
2024-11-08 21:51:17 +00:00
|
|
|
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},
|
|
|
|
},
|
2024-11-05 18:28:28 +00:00
|
|
|
W: &PositiveSlice[*Wavefront]{
|
|
|
|
defaultValue: &Wavefront{
|
|
|
|
data: []WavefrontValue{0},
|
2024-10-29 17:03:19 +00:00
|
|
|
},
|
|
|
|
},
|
2024-10-24 18:07:10 +00:00
|
|
|
}
|
2024-10-29 17:03:19 +00:00
|
|
|
|
|
|
|
w.lo.Preallocate(preallocateSize)
|
|
|
|
w.hi.Preallocate(preallocateSize)
|
|
|
|
w.W.Preallocate(preallocateSize)
|
|
|
|
|
|
|
|
return w
|
2024-10-24 18:07:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 18:28:28 +00:00
|
|
|
// GetVal: get value for wavefront=score, diag=k => returns ok, value, traceback
|
|
|
|
func (w *WavefrontComponent) GetVal(score int, k int) (bool, uint32, Traceback) {
|
|
|
|
return UnpackWavefrontValue(w.W.Get(score).Get(k))
|
2024-10-24 18:07:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 18:28:28 +00:00
|
|
|
// SetVal: set value, traceback for wavefront=score, diag=k
|
|
|
|
func (w *WavefrontComponent) SetVal(score int, k int, val uint32, tb Traceback) {
|
|
|
|
w.W.Get(score).Set(k, PackWavefrontValue(val, tb))
|
2024-10-24 18:07:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 18:28:28 +00:00
|
|
|
// GetLoHi: get lo and hi for wavefront=score
|
2024-10-29 17:03:19 +00:00
|
|
|
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 {
|
2024-10-29 17:03:19 +00:00
|
|
|
return false, 0, 0
|
2024-10-24 18:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 18:28:28 +00:00
|
|
|
// SetLoHi: set lo and hi for wavefront=score
|
2024-10-29 17:03:19 +00:00
|
|
|
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)
|
|
|
|
|
2024-10-29 17:03:19 +00:00
|
|
|
// preemptively setup w.W
|
2024-11-05 18:28:28 +00:00
|
|
|
b := NewWavefront(lo, hi)
|
2024-11-05 05:35:46 +00:00
|
|
|
w.W.Set(score, b)
|
2024-10-24 18:07:10 +00:00
|
|
|
}
|