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-12 18:44:12 +00:00
|
|
|
// bitpacked wavefront lo/hi values with 32 bits each
|
|
|
|
type WavefrontLoHi uint64
|
|
|
|
|
|
|
|
func PackWavefrontLoHi(lo int, hi int) WavefrontLoHi {
|
|
|
|
loBM := int64(int32(lo)) & 0x0000_0000_FFFF_FFFF
|
|
|
|
hiBM := int64(int64(hi) << 32)
|
|
|
|
return WavefrontLoHi(hiBM | loBM)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnpackWavefrontLoHi(lohi WavefrontLoHi) (int, int) {
|
|
|
|
loBM := int(int32(lohi & 0x0000_0000_FFFF_FFFF))
|
|
|
|
hiBM := int(int32(lohi & 0xFFFF_FFFF_0000_0000 >> 32))
|
|
|
|
return loBM, hiBM
|
|
|
|
}
|
|
|
|
|
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
|
2024-11-12 18:44:12 +00:00
|
|
|
// technically this restricts to alignments with less than 268 million characters but that should be sufficient for most cases
|
2024-11-05 18:28:28 +00:00
|
|
|
type WavefrontValue uint32
|
|
|
|
|
2024-11-12 18:47:25 +00:00
|
|
|
// TODO: add 64 bit packed value in case more than 268 million characters are needed
|
2024-11-05 18:28:28 +00:00
|
|
|
|
|
|
|
// PackWavefrontValue: packs a diag value and traceback into a WavefrontValue
|
|
|
|
func PackWavefrontValue(value uint32, traceback Traceback) WavefrontValue {
|
2024-11-12 18:44:12 +00:00
|
|
|
validBM := uint32(0x8000_0000)
|
2024-11-05 18:28:28 +00:00
|
|
|
tracebackBM := uint32(traceback&0x0000_0007) << 28
|
2024-11-12 18:44:12 +00:00
|
|
|
valueBM := value & 0x0FFF_FFFF
|
|
|
|
return WavefrontValue(validBM | tracebackBM | valueBM)
|
2024-11-05 18:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
validBM := wfv&0x8000_0000 != 0
|
2024-11-12 18:44:12 +00:00
|
|
|
tracebackBM := uint8(wfv & 0x7000_0000 >> 28)
|
|
|
|
valueBM := uint32(wfv & 0x0FFF_FFFF)
|
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
|
2024-11-12 18:44:12 +00:00
|
|
|
lohi WavefrontLoHi
|
2024-11-05 18:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWavefront: returns a new wavefront with size accomodating lo and hi (inclusive)
|
|
|
|
func NewWavefront(lo int, hi int) *Wavefront {
|
|
|
|
a := &Wavefront{}
|
|
|
|
|
2024-11-12 18:44:12 +00:00
|
|
|
a.lohi = PackWavefrontLoHi(lo, hi)
|
|
|
|
size := hi - lo
|
2024-11-05 18:28:28 +00:00
|
|
|
|
|
|
|
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 {
|
2024-11-12 18:44:12 +00:00
|
|
|
lo := int(int32(a.lohi & 0x0000_0000_FFFF_FFFF))
|
|
|
|
return diagonal - lo
|
2024-11-05 18:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12 18:44:12 +00:00
|
|
|
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-12 18:53:03 +00:00
|
|
|
func NewWavefrontComponent() *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-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
|
|
|
|
2024-11-12 18:53:03 +00:00
|
|
|
//w.W.Preallocate(preallocateSize)
|
2024-10-29 17:03:19 +00:00
|
|
|
|
|
|
|
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) {
|
2024-11-12 18:44:12 +00:00
|
|
|
lo, hi := UnpackWavefrontLoHi(w.W.Get(score).lohi)
|
|
|
|
return w.W.Valid(score), lo, hi
|
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) {
|
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
|
|
|
}
|