diff --git a/wasm/Makefile b/wasm/Makefile index 83991317..42f85496 100644 --- a/wasm/Makefile +++ b/wasm/Makefile @@ -1,4 +1,4 @@ -# currently using Emscripten 3.1.53 +# currently using Emscripten 3.1.56 CXX = em++ INCLUDE = -I../include @@ -14,7 +14,7 @@ LINK_FLAGS = \ --post-js post.js \ --no-entry \ -lembind \ - -s EXPORTED_RUNTIME_METHODS=ccall,writeArrayToMemory,getValue,UTF8ToString \ + -s EXPORTED_RUNTIME_METHODS=writeArrayToMemory,HEAPU8 \ -s EXPORTED_FUNCTIONS=_malloc \ -s INCOMING_MODULE_JS_API=print,printErr,setStatus,onRuntimeInitialized \ -s ALLOW_MEMORY_GROWTH=1 \ @@ -25,19 +25,19 @@ LINK_FLAGS = \ GEMMI_OBJS = mmcif.o polyheur.o resinfo.o sprintf.o json.o -BINDING_OBJS = gemmi.o mol.o mtz_fft.o +BINDING_OBJS = gemmi.o cell.o mol.o mtz_fft.o gemmi.js: $(GEMMI_OBJS) $(BINDING_OBJS) $(CXX) $(FLAGS) $(LINK_FLAGS) $(GEMMI_OBJS) $(BINDING_OBJS) -o $@ # old mtz module (not built by default) -mtz.js: mtz_fft.cpp +mtz.js: mtz_fft.cpp cell.o $(CXX) -DSTANDALONE_MTZ=1 $(INCLUDE) $(FLAGS) \ -lembind --no-entry --pre-js=pre.js \ -s MODULARIZE=1 -s ALLOW_MEMORY_GROWTH=1 \ -s EXPORTED_FUNCTIONS=_malloc \ -s EXPORTED_RUNTIME_METHODS=writeArrayToMemory \ - -s EXPORT_NAME=GemmiMtz $< -o $@ + -s EXPORT_NAME=GemmiMtz $^ -o $@ $(GEMMI_OBJS): %.o: ../src/%.cpp $(CXX) $(INCLUDE) $(FLAGS) -c $< -o $@ @@ -49,6 +49,6 @@ $(BINDING_OBJS): %.o: %.cpp $(CXX) $(INCLUDE) $(FLAGS) -c $< clean: - rm -f *.o gemmi.js *.wasm + rm -f *.o gemmi.js mtz.js *.wasm .PHONY: clean diff --git a/wasm/README.md b/wasm/README.md index caa36c43..abd7eb28 100644 --- a/wasm/README.md +++ b/wasm/README.md @@ -1,3 +1,7 @@ +Work in progress... + +here is old documentation for reading MTZ only + # MTZ reader in WebAssembly The MTZ file format is used for the storage of reflection data -- diff --git a/wasm/cell.cpp b/wasm/cell.cpp new file mode 100644 index 00000000..c955ced7 --- /dev/null +++ b/wasm/cell.cpp @@ -0,0 +1,15 @@ +// Copyright Global Phasing Ltd. + +#include "common.h" +#include + +void add_cell() { + em::class_("UnitCell") + .property("a", &gemmi::UnitCell::a) + .property("b", &gemmi::UnitCell::b) + .property("c", &gemmi::UnitCell::c) + .property("alpha", &gemmi::UnitCell::alpha) + .property("beta", &gemmi::UnitCell::beta) + .property("gamma", &gemmi::UnitCell::gamma) + ; +} diff --git a/wasm/common.h b/wasm/common.h index db5eb057..3d66a2d6 100644 --- a/wasm/common.h +++ b/wasm/common.h @@ -4,5 +4,6 @@ #include namespace em = emscripten; -void add_mol(); -void add_mtz_fft(); +void add_cell(); // cell.cpp +void add_mol(); // mol.cpp +void add_mtz_fft(); // mtz_fft.cpp diff --git a/wasm/gemmi.cpp b/wasm/gemmi.cpp index f46ad0ee..bf57dd85 100644 --- a/wasm/gemmi.cpp +++ b/wasm/gemmi.cpp @@ -4,6 +4,7 @@ #include EMSCRIPTEN_BINDINGS(Gemmi) { + add_cell(); add_mol(); add_mtz_fft(); EM_ASM( finalize_gemmi(); ); diff --git a/wasm/mol.cpp b/wasm/mol.cpp index a7223f8b..4fd5a374 100644 --- a/wasm/mol.cpp +++ b/wasm/mol.cpp @@ -48,15 +48,6 @@ em::class_ wrap_children() { } void add_mol() { - em::class_("UnitCell") - .property("a", &gemmi::UnitCell::a) - .property("b", &gemmi::UnitCell::b) - .property("c", &gemmi::UnitCell::c) - .property("alpha", &gemmi::UnitCell::alpha) - .property("beta", &gemmi::UnitCell::beta) - .property("gamma", &gemmi::UnitCell::gamma) - ; - wrap_children() .property("name", &gemmi::Structure::name) .property("cell", &gemmi::Structure::cell) diff --git a/wasm/mtz.d.ts b/wasm/mtz.d.ts new file mode 100644 index 00000000..948207bd --- /dev/null +++ b/wasm/mtz.d.ts @@ -0,0 +1,28 @@ +// interface for mtz.js that can be built with "make mtz.js" + +export interface UnitCell { + a: number; + b: number; + c: number; + alpha: number; + beta: number; + gamma: number; + delete(): void; +} + +export interface Mtz { + readonly cell: UnitCell; + readonly nx: number; + readonly ny: number; + readonly nz: number; + readonly rmsd: number; + readonly last_error: string; + read(_0: number, _1: number): boolean; + calculate_map(_0: boolean): any; + calculate_map_from_labels(_0: string, _1: string): any; + delete(): void; +} + +export interface Module { + readMtz(mtz_buf: string|ArrayBuffer): Mtz; +} diff --git a/wasm/mtz_fft.cpp b/wasm/mtz_fft.cpp index c6b5a632..6b2e30a3 100644 --- a/wasm/mtz_fft.cpp +++ b/wasm/mtz_fft.cpp @@ -118,6 +118,7 @@ void add_mtz_fft() { #ifdef STANDALONE_MTZ EMSCRIPTEN_BINDINGS(GemmiMtz) { + add_cell(); add_mtz_fft(); } #endif diff --git a/wasm/mtz_fft.test.js b/wasm/mtz_fft.test.js new file mode 100644 index 00000000..1f4c59c0 --- /dev/null +++ b/wasm/mtz_fft.test.js @@ -0,0 +1,16 @@ +const fs = require('node:fs'); +const zlib = require('node:zlib'); +const Gemmi = require('./gemmi.js') + + +test('counts atom occupancies', () => { + Gemmi().then((gemmi) => { + const path = '../tests/5wkd_phases.mtz.gz'; + const buffer_gz = fs.readFileSync(path); + const buffer = zlib.gunzipSync(new Buffer.from(buffer_gz)); + const mtz = gemmi.readMtz(buffer); + expect(mtz.cell.a).toBe(50.347); + expect(mtz.cell.gamma).toBe(90.); + mtz.delete(); + }); +});