Compare commits
7 Commits
v2.0.4
..
f8f8636cc3
| Author | SHA1 | Date | |
|---|---|---|---|
| f8f8636cc3 | |||
| 13b522f6a0 | |||
| 05cd2d79df | |||
| bc52a250a4 | |||
| 830c0a7586 | |||
| c94a7902c0 | |||
| 8ccb45748d |
@@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
build: clean
|
build: clean
|
||||||
@echo "======================== Building Binary ======================="
|
@echo "======================== Building Binary ======================="
|
||||||
minify wfa.js > dist/wfa.js
|
mkdir -p dist
|
||||||
GOOS=js GOARCH=wasm CGO_ENABLED=0 tinygo build -panic=trap -no-debug -opt=2 -target=wasm -o dist/wfa.wasm .
|
cp wfa.js dist/wfa.js
|
||||||
|
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 ======================"
|
||||||
@@ -26,5 +27,4 @@ test:
|
|||||||
@rm -f test.test
|
@rm -f test.test
|
||||||
|
|
||||||
dev-init:
|
dev-init:
|
||||||
apt install minify
|
|
||||||
go get -t wfa/test
|
go get -t wfa/test
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
module wfa
|
module wfa
|
||||||
|
|
||||||
go 1.23.6
|
go 1.26.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/schollz/progressbar/v3 v3.17.1
|
github.com/schollz/progressbar/v3 v3.19.1
|
||||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
|
golang.org/x/exp v0.0.0-20260611194520-c48552f49976
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
golang.org/x/sys v0.27.0 // indirect
|
golang.org/x/sys v0.46.0 // indirect
|
||||||
golang.org/x/term v0.26.0 // indirect
|
golang.org/x/term v0.44.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="wfa.js" type="module"></script>
|
||||||
|
<script type="module">
|
||||||
|
import wfaInit from "./wfa.js";
|
||||||
|
wfaInit("dist/wfa.wasm");
|
||||||
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
|
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.wfa.wfAlign(a, b, penalties, true);
|
||||||
|
const alignment = global.wfa.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>
|
||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c := make(chan bool)
|
c := make(chan bool)
|
||||||
js.Global().Set("wfAlign", js.FuncOf(wfAlign))
|
js.Global().Get("wfa").Set("wfAlign", js.FuncOf(wfAlign))
|
||||||
js.Global().Set("DecodeCIGAR", js.FuncOf(DecodeCIGAR))
|
js.Global().Get("wfa").Set("DecodeCIGAR", js.FuncOf(DecodeCIGAR))
|
||||||
<-c
|
<-c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
+13
-19
@@ -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
|
||||||
|
|||||||
+4
-8
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
// wasm_exec.js from tinygo
|
|
||||||
|
|
||||||
// Copyright 2018 The Go Authors. All rights reserved.
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
@@ -134,6 +132,7 @@
|
|||||||
const decoder = new TextDecoder("utf-8");
|
const decoder = new TextDecoder("utf-8");
|
||||||
let reinterpretBuf = new DataView(new ArrayBuffer(8));
|
let reinterpretBuf = new DataView(new ArrayBuffer(8));
|
||||||
var logLine = [];
|
var logLine = [];
|
||||||
|
const wasmExit = {}; // thrown to exit via proc_exit (not an error)
|
||||||
|
|
||||||
global.Go = class {
|
global.Go = class {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -272,14 +271,11 @@
|
|||||||
fd_close: () => 0, // dummy
|
fd_close: () => 0, // dummy
|
||||||
fd_fdstat_get: () => 0, // dummy
|
fd_fdstat_get: () => 0, // dummy
|
||||||
fd_seek: () => 0, // dummy
|
fd_seek: () => 0, // dummy
|
||||||
"proc_exit": (code) => {
|
proc_exit: (code) => {
|
||||||
if (global.process) {
|
this.exited = true;
|
||||||
// Node.js
|
this.exitCode = code;
|
||||||
process.exit(code);
|
this._resolveExitPromise();
|
||||||
} else {
|
throw wasmExit;
|
||||||
// Can't exit in a browser.
|
|
||||||
throw 'trying to exit with code ' + code;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
random_get: (bufPtr, bufLen) => {
|
random_get: (bufPtr, bufLen) => {
|
||||||
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
|
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
|
||||||
@@ -287,31 +283,45 @@
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
gojs: {
|
gojs: {
|
||||||
// func ticks() float64
|
// func ticks() int64
|
||||||
"runtime.ticks": () => {
|
"runtime.ticks": () => {
|
||||||
return timeOrigin + performance.now();
|
return BigInt((timeOrigin + performance.now()) * 1e6);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func sleepTicks(timeout float64)
|
// func sleepTicks(timeout int64)
|
||||||
"runtime.sleepTicks": (timeout) => {
|
"runtime.sleepTicks": (timeout) => {
|
||||||
// Do not sleep, only reactivate scheduler after the given timeout.
|
// Do not sleep, only reactivate scheduler after the given timeout.
|
||||||
setTimeout(this._inst.exports.go_scheduler, timeout);
|
setTimeout(() => {
|
||||||
|
if (this.exited) return;
|
||||||
|
try {
|
||||||
|
this._inst.exports.go_scheduler();
|
||||||
|
} catch (e) {
|
||||||
|
if (e !== wasmExit) throw e;
|
||||||
|
}
|
||||||
|
}, Number(timeout)/1e6);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func finalizeRef(v ref)
|
// func finalizeRef(v ref)
|
||||||
"syscall/js.finalizeRef": (v_ref) => {
|
"syscall/js.finalizeRef": (v_ref) => {
|
||||||
const id = mem().getUint32(unboxValue(v_ref), true);
|
// Note: TinyGo does not support finalizers so this is only called
|
||||||
this._goRefCounts[id]--;
|
// for one specific case, by js.go:jsString. and can/might leak memory.
|
||||||
if (this._goRefCounts[id] === 0) {
|
const id = v_ref & 0xffffffffn;
|
||||||
const v = this._values[id];
|
if (this._goRefCounts?.[id] !== undefined) {
|
||||||
this._values[id] = null;
|
this._goRefCounts[id]--;
|
||||||
this._ids.delete(v);
|
if (this._goRefCounts[id] === 0) {
|
||||||
this._idPool.push(id);
|
const v = this._values[id];
|
||||||
|
this._values[id] = null;
|
||||||
|
this._ids.delete(v);
|
||||||
|
this._idPool.push(id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error("syscall/js.finalizeRef: unknown id", id);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// func stringVal(value string) ref
|
// func stringVal(value string) ref
|
||||||
"syscall/js.stringVal": (value_ptr, value_len) => {
|
"syscall/js.stringVal": (value_ptr, value_len) => {
|
||||||
|
value_ptr >>>= 0;
|
||||||
const s = loadString(value_ptr, value_len);
|
const s = loadString(value_ptr, value_len);
|
||||||
return boxValue(s);
|
return boxValue(s);
|
||||||
},
|
},
|
||||||
@@ -472,21 +482,25 @@
|
|||||||
this._ids = new Map(); // mapping from JS values to reference ids
|
this._ids = new Map(); // mapping from JS values to reference ids
|
||||||
this._idPool = []; // unused ids that have been garbage collected
|
this._idPool = []; // unused ids that have been garbage collected
|
||||||
this.exited = false; // whether the Go program has exited
|
this.exited = false; // whether the Go program has exited
|
||||||
|
this.exitCode = 0;
|
||||||
|
|
||||||
while (true) {
|
if (this._inst.exports._start) {
|
||||||
const callbackPromise = new Promise((resolve) => {
|
let exitPromise = new Promise((resolve, reject) => {
|
||||||
this._resolveCallbackPromise = () => {
|
this._resolveExitPromise = resolve;
|
||||||
if (this.exited) {
|
|
||||||
throw new Error("bad callback: Go program has already exited");
|
|
||||||
}
|
|
||||||
setTimeout(resolve, 0); // make sure it is asynchronous
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
this._inst.exports._start();
|
|
||||||
if (this.exited) {
|
// Run program, but catch the wasmExit exception that's thrown
|
||||||
break;
|
// to return back here.
|
||||||
|
try {
|
||||||
|
this._inst.exports._start();
|
||||||
|
} catch (e) {
|
||||||
|
if (e !== wasmExit) throw e;
|
||||||
}
|
}
|
||||||
await callbackPromise;
|
|
||||||
|
await exitPromise;
|
||||||
|
return this.exitCode;
|
||||||
|
} else {
|
||||||
|
this._inst.exports._initialize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,7 +508,11 @@
|
|||||||
if (this.exited) {
|
if (this.exited) {
|
||||||
throw new Error("Go program has already exited");
|
throw new Error("Go program has already exited");
|
||||||
}
|
}
|
||||||
this._inst.exports.resume();
|
try {
|
||||||
|
this._inst.exports.resume();
|
||||||
|
} catch (e) {
|
||||||
|
if (e !== wasmExit) throw e;
|
||||||
|
}
|
||||||
if (this.exited) {
|
if (this.exited) {
|
||||||
this._resolveExitPromise();
|
this._resolveExitPromise();
|
||||||
}
|
}
|
||||||
@@ -524,8 +542,9 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const go = new Go();
|
const go = new Go();
|
||||||
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
|
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then(async (result) => {
|
||||||
return go.run(result.instance);
|
let exitCode = await go.run(result.instance);
|
||||||
|
process.exit(exitCode);
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
@@ -537,22 +556,22 @@
|
|||||||
export default function init (path) {
|
export default function init (path) {
|
||||||
return new Promise ((res) => {
|
return new Promise ((res) => {
|
||||||
const go = new Go();
|
const go = new Go();
|
||||||
var wasm;
|
const run = (obj) => {
|
||||||
|
const wasm = obj.instance;
|
||||||
|
global.wfa = wasm
|
||||||
|
go.run(wasm);
|
||||||
|
res()
|
||||||
|
}
|
||||||
if ('instantiateStreaming' in WebAssembly) {
|
if ('instantiateStreaming' in WebAssembly) {
|
||||||
WebAssembly.instantiateStreaming(fetch(path), go.importObject).then(function (obj) {
|
WebAssembly.instantiateStreaming(fetch(path), go.importObject).then(function (obj) {
|
||||||
wasm = obj.instance;
|
run(obj)
|
||||||
go.run(wasm);
|
|
||||||
res()
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
fetch(path).then(resp =>
|
fetch(path).then(resp =>
|
||||||
resp.arrayBuffer()
|
resp.arrayBuffer()
|
||||||
).then(bytes =>
|
).then(bytes =>
|
||||||
WebAssembly.instantiate(bytes, go.importObject).then(function (obj) {
|
WebAssembly.instantiate(bytes, go.importObject).then(function (obj) {
|
||||||
wasm = obj.instance;
|
run(obj)
|
||||||
go.run(wasm);
|
|
||||||
res()
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user