Compare commits
6 Commits
v2.0.4
...
13b522f6a0
| Author | SHA1 | Date | |
|---|---|---|---|
| 13b522f6a0 | |||
| 05cd2d79df | |||
| bc52a250a4 | |||
| 830c0a7586 | |||
| c94a7902c0 | |||
| 8ccb45748d |
@@ -2,8 +2,9 @@
|
||||
|
||||
build: clean
|
||||
@echo "======================== Building Binary ======================="
|
||||
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 .
|
||||
mkdir -p dist
|
||||
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:
|
||||
@echo "======================== Cleaning Project ======================"
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
module wfa
|
||||
|
||||
go 1.23.6
|
||||
go 1.26.4
|
||||
|
||||
require (
|
||||
github.com/schollz/progressbar/v3 v3.17.1
|
||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
|
||||
github.com/schollz/progressbar/v3 v3.19.1
|
||||
golang.org/x/exp v0.0.0-20260611194520-c48552f49976
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
golang.org/x/sys v0.27.0 // indirect
|
||||
golang.org/x/term v0.26.0 // indirect
|
||||
golang.org/x/sys v0.46.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() {
|
||||
c := make(chan bool)
|
||||
js.Global().Set("wfAlign", js.FuncOf(wfAlign))
|
||||
js.Global().Set("DecodeCIGAR", js.FuncOf(DecodeCIGAR))
|
||||
js.Global().Get("wfa").Set("wfAlign", js.FuncOf(wfAlign))
|
||||
js.Global().Get("wfa").Set("DecodeCIGAR", js.FuncOf(DecodeCIGAR))
|
||||
<-c
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package wfa
|
||||
|
||||
type Integer interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Score int
|
||||
CIGAR string
|
||||
|
||||
+13
-19
@@ -1,24 +1,18 @@
|
||||
package wfa
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
const MaxInt = int(^uint(0) >> 1)
|
||||
const MinInt = -MaxInt - 1
|
||||
|
||||
// convert an unsigned into to string
|
||||
func UIntToString(num uint) string { // num assumed to be positive
|
||||
var builder strings.Builder
|
||||
str := []rune{}
|
||||
|
||||
for num > 0 {
|
||||
digit := num % 10
|
||||
builder.WriteRune(rune('0' + digit))
|
||||
str = append(str, rune('0'+digit))
|
||||
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 {
|
||||
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
|
||||
func RunLengthDecode(encoded string) string {
|
||||
decoded := strings.Builder{}
|
||||
decoded := []rune{}
|
||||
length := len(encoded)
|
||||
i := 0
|
||||
|
||||
@@ -44,30 +38,30 @@ func RunLengthDecode(encoded string) string {
|
||||
if i < length {
|
||||
char := encoded[i]
|
||||
for j := 0; j < runLength; j++ {
|
||||
decoded.WriteByte(char)
|
||||
decoded = append(decoded, rune(char))
|
||||
}
|
||||
i++ // Move past the character
|
||||
}
|
||||
}
|
||||
|
||||
return decoded.String()
|
||||
return string(decoded)
|
||||
}
|
||||
|
||||
// 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]
|
||||
}
|
||||
|
||||
// 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]
|
||||
}
|
||||
|
||||
// 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
|
||||
minIndex := 0
|
||||
minValue := math.MaxInt
|
||||
minValue := MaxInt
|
||||
for i := 0; i < len(valids); i++ {
|
||||
if valids[i] && int(values[i]) < minValue {
|
||||
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
|
||||
func SafeArgMax[T constraints.Integer](valids []bool, values []T) (bool, int) {
|
||||
func SafeArgMax[T Integer](valids []bool, values []T) (bool, int) {
|
||||
hasValid := false
|
||||
maxIndex := 0
|
||||
maxValue := math.MinInt
|
||||
maxValue := MinInt
|
||||
for i := range valids {
|
||||
if valids[i] && int(values[i]) > maxValue {
|
||||
hasValid = true
|
||||
|
||||
+4
-8
@@ -1,9 +1,5 @@
|
||||
package wfa
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
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-- {
|
||||
CIGAR.WriteString(UIntToString(Counts[i]))
|
||||
CIGAR.WriteRune(Ops[i])
|
||||
CIGAR += UIntToString(Counts[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.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
@@ -134,6 +132,7 @@
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
let reinterpretBuf = new DataView(new ArrayBuffer(8));
|
||||
var logLine = [];
|
||||
const wasmExit = {}; // thrown to exit via proc_exit (not an error)
|
||||
|
||||
global.Go = class {
|
||||
constructor() {
|
||||
@@ -272,14 +271,11 @@
|
||||
fd_close: () => 0, // dummy
|
||||
fd_fdstat_get: () => 0, // dummy
|
||||
fd_seek: () => 0, // dummy
|
||||
"proc_exit": (code) => {
|
||||
if (global.process) {
|
||||
// Node.js
|
||||
process.exit(code);
|
||||
} else {
|
||||
// Can't exit in a browser.
|
||||
throw 'trying to exit with code ' + code;
|
||||
}
|
||||
proc_exit: (code) => {
|
||||
this.exited = true;
|
||||
this.exitCode = code;
|
||||
this._resolveExitPromise();
|
||||
throw wasmExit;
|
||||
},
|
||||
random_get: (bufPtr, bufLen) => {
|
||||
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
|
||||
@@ -287,31 +283,45 @@
|
||||
},
|
||||
},
|
||||
gojs: {
|
||||
// func ticks() float64
|
||||
// func ticks() int64
|
||||
"runtime.ticks": () => {
|
||||
return timeOrigin + performance.now();
|
||||
return BigInt((timeOrigin + performance.now()) * 1e6);
|
||||
},
|
||||
|
||||
// func sleepTicks(timeout float64)
|
||||
// func sleepTicks(timeout int64)
|
||||
"runtime.sleepTicks": (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)
|
||||
"syscall/js.finalizeRef": (v_ref) => {
|
||||
const id = mem().getUint32(unboxValue(v_ref), true);
|
||||
this._goRefCounts[id]--;
|
||||
if (this._goRefCounts[id] === 0) {
|
||||
const v = this._values[id];
|
||||
this._values[id] = null;
|
||||
this._ids.delete(v);
|
||||
this._idPool.push(id);
|
||||
// Note: TinyGo does not support finalizers so this is only called
|
||||
// for one specific case, by js.go:jsString. and can/might leak memory.
|
||||
const id = v_ref & 0xffffffffn;
|
||||
if (this._goRefCounts?.[id] !== undefined) {
|
||||
this._goRefCounts[id]--;
|
||||
if (this._goRefCounts[id] === 0) {
|
||||
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
|
||||
"syscall/js.stringVal": (value_ptr, value_len) => {
|
||||
value_ptr >>>= 0;
|
||||
const s = loadString(value_ptr, value_len);
|
||||
return boxValue(s);
|
||||
},
|
||||
@@ -472,21 +482,25 @@
|
||||
this._ids = new Map(); // mapping from JS values to reference ids
|
||||
this._idPool = []; // unused ids that have been garbage collected
|
||||
this.exited = false; // whether the Go program has exited
|
||||
this.exitCode = 0;
|
||||
|
||||
while (true) {
|
||||
const callbackPromise = new Promise((resolve) => {
|
||||
this._resolveCallbackPromise = () => {
|
||||
if (this.exited) {
|
||||
throw new Error("bad callback: Go program has already exited");
|
||||
}
|
||||
setTimeout(resolve, 0); // make sure it is asynchronous
|
||||
};
|
||||
if (this._inst.exports._start) {
|
||||
let exitPromise = new Promise((resolve, reject) => {
|
||||
this._resolveExitPromise = resolve;
|
||||
});
|
||||
this._inst.exports._start();
|
||||
if (this.exited) {
|
||||
break;
|
||||
|
||||
// Run program, but catch the wasmExit exception that's thrown
|
||||
// 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) {
|
||||
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) {
|
||||
this._resolveExitPromise();
|
||||
}
|
||||
@@ -524,8 +542,9 @@
|
||||
}
|
||||
|
||||
const go = new Go();
|
||||
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
|
||||
return go.run(result.instance);
|
||||
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then(async (result) => {
|
||||
let exitCode = await go.run(result.instance);
|
||||
process.exit(exitCode);
|
||||
}).catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
@@ -537,22 +556,22 @@
|
||||
export default function init (path) {
|
||||
return new Promise ((res) => {
|
||||
const go = new Go();
|
||||
var wasm;
|
||||
|
||||
const run = (obj) => {
|
||||
const wasm = obj.instance;
|
||||
global.wfa = wasm
|
||||
go.run(wasm);
|
||||
res()
|
||||
}
|
||||
if ('instantiateStreaming' in WebAssembly) {
|
||||
WebAssembly.instantiateStreaming(fetch(path), go.importObject).then(function (obj) {
|
||||
wasm = obj.instance;
|
||||
go.run(wasm);
|
||||
res()
|
||||
run(obj)
|
||||
})
|
||||
} else {
|
||||
fetch(path).then(resp =>
|
||||
resp.arrayBuffer()
|
||||
).then(bytes =>
|
||||
WebAssembly.instantiate(bytes, go.importObject).then(function (obj) {
|
||||
wasm = obj.instance;
|
||||
go.run(wasm);
|
||||
res()
|
||||
run(obj)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user