remove imports,

switch to opt=s,
add debug index.html
This commit is contained in:
=
2025-09-30 17:41:14 +00:00
parent 8ccb45748d
commit c94a7902c0
5 changed files with 53 additions and 28 deletions

View File

@@ -3,7 +3,7 @@
build: clean build: clean
@echo "======================== Building Binary =======================" @echo "======================== Building Binary ======================="
minify wfa.js > dist/wfa.js minify wfa.js > dist/wfa.js
GOOS=js GOARCH=wasm CGO_ENABLED=0 tinygo build -panic=trap -no-debug -opt=2 -target=wasm -o dist/wfa.wasm . GOOS=js GOARCH=wasm CGO_ENABLED=0 tinygo build -panic=trap -no-debug -opt=s -target=wasm -o dist/wfa.wasm .
clean: clean:
@echo "======================== Cleaning Project ======================" @echo "======================== Cleaning Project ======================"

31
index.html Normal file
View File

@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<script src="dist/wfa.js" type="module"></script>
<script type="module">
import wfaInit from "./dist/wfa.js";
window.addEventListener("DOMContentLoaded", () => {
wfaInit("dist/wfa.wasm");
document.querySelector("#submit").addEventListener("click", () => {
a = document.querySelector("#a").value
b = document.querySelector("#b").value
const penalties = {
m: 0,
x: 1,
o: 0,
e: 1
};
const { score, CIGAR } = global.wfAlign(a, b, penalties, true);
const alignment = global.DecodeCIGAR(CIGAR);
document.querySelector("#result").innerText = `${score}, ${CIGAR}, ${alignment}`;
})
});
</script>
</head>
<body>
<label>A: </label><input id="a">
<label>B: </label><input id="b">
<button id="submit">Submit</button>
<p><span>Result: </span><span id="result"></span></p>
</body>
</html>

View File

@@ -1,5 +1,9 @@
package wfa package wfa
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type Result struct { type Result struct {
Score int Score int
CIGAR string CIGAR string

View File

@@ -1,24 +1,18 @@
package wfa package wfa
import ( const MaxInt = int(^uint(0) >> 1)
"math" const MinInt = -MaxInt - 1
"strings"
"golang.org/x/exp/constraints"
)
// convert an unsigned into to string // convert an unsigned into to string
func UIntToString(num uint) string { // num assumed to be positive func UIntToString(num uint) string { // num assumed to be positive
var builder strings.Builder str := []rune{}
for num > 0 { for num > 0 {
digit := num % 10 digit := num % 10
builder.WriteRune(rune('0' + digit)) str = append(str, rune('0'+digit))
num /= 10 num /= 10
} }
// Reverse the string as we built it in reverse order
str := []rune(builder.String())
for i, j := 0, len(str)-1; i < j; i, j = i+1, j-1 { for i, j := 0, len(str)-1; i < j; i, j = i+1, j-1 {
str[i], str[j] = str[j], str[i] str[i], str[j] = str[j], str[i]
} }
@@ -28,7 +22,7 @@ func UIntToString(num uint) string { // num assumed to be positive
// decode runlength encoded string such as CIGARs // decode runlength encoded string such as CIGARs
func RunLengthDecode(encoded string) string { func RunLengthDecode(encoded string) string {
decoded := strings.Builder{} decoded := []rune{}
length := len(encoded) length := len(encoded)
i := 0 i := 0
@@ -44,30 +38,30 @@ func RunLengthDecode(encoded string) string {
if i < length { if i < length {
char := encoded[i] char := encoded[i]
for j := 0; j < runLength; j++ { for j := 0; j < runLength; j++ {
decoded.WriteByte(char) decoded = append(decoded, rune(char))
} }
i++ // Move past the character i++ // Move past the character
} }
} }
return decoded.String() return string(decoded)
} }
// given the min index, return the item in values at that index // given the min index, return the item in values at that index
func SafeMin[T constraints.Integer](values []T, idx int) T { func SafeMin[T Integer](values []T, idx int) T {
return values[idx] return values[idx]
} }
// given the max index, return the item in values at that index // given the max index, return the item in values at that index
func SafeMax[T constraints.Integer](values []T, idx int) T { func SafeMax[T Integer](values []T, idx int) T {
return values[idx] return values[idx]
} }
// given array of values and corresponding array of valid flags, find the min of value which is valid or return false if there does not exist any // given array of values and corresponding array of valid flags, find the min of value which is valid or return false if there does not exist any
func SafeArgMin[T constraints.Integer](valids []bool, values []T) (bool, int) { func SafeArgMin[T Integer](valids []bool, values []T) (bool, int) {
hasValid := false hasValid := false
minIndex := 0 minIndex := 0
minValue := math.MaxInt minValue := MaxInt
for i := 0; i < len(valids); i++ { for i := 0; i < len(valids); i++ {
if valids[i] && int(values[i]) < minValue { if valids[i] && int(values[i]) < minValue {
hasValid = true hasValid = true
@@ -83,10 +77,10 @@ func SafeArgMin[T constraints.Integer](valids []bool, values []T) (bool, int) {
} }
// given array of values and corresponding array of valid flags, find the max of value which is valid or return false if there does not exist any // given array of values and corresponding array of valid flags, find the max of value which is valid or return false if there does not exist any
func SafeArgMax[T constraints.Integer](valids []bool, values []T) (bool, int) { func SafeArgMax[T Integer](valids []bool, values []T) (bool, int) {
hasValid := false hasValid := false
maxIndex := 0 maxIndex := 0
maxValue := math.MinInt maxValue := MinInt
for i := range valids { for i := range valids {
if valids[i] && int(values[i]) > maxValue { if valids[i] && int(values[i]) > maxValue {
hasValid = true hasValid = true

View File

@@ -1,9 +1,5 @@
package wfa package wfa
import (
"strings"
)
// WFAlign takes strings s1, s2, penalties, and returns the score and CIGAR if doCIGAR is true // WFAlign takes strings s1, s2, penalties, and returns the score and CIGAR if doCIGAR is true
func WFAlign(s1 string, s2 string, penalties Penalty, doCIGAR bool) Result { func WFAlign(s1 string, s2 string, penalties Penalty, doCIGAR bool) Result {
n := len(s1) n := len(s1)
@@ -189,11 +185,11 @@ func WFBacktrace(M *WavefrontComponent, I *WavefrontComponent, D *WavefrontCompo
} }
} }
CIGAR := strings.Builder{} CIGAR := ""
for i := len(Ops) - 1; i > 0; i-- { for i := len(Ops) - 1; i > 0; i-- {
CIGAR.WriteString(UIntToString(Counts[i])) CIGAR += UIntToString(Counts[i])
CIGAR.WriteRune(Ops[i]) CIGAR += string(Ops[i])
} }
return CIGAR.String() return CIGAR
} }