Skip to content

Commit

Permalink
wasm: work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
wojdyr committed Mar 22, 2024
1 parent 25aa3af commit 6764dfd
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 17 deletions.
12 changes: 6 additions & 6 deletions wasm/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# currently using Emscripten 3.1.53
# currently using Emscripten 3.1.56
CXX = em++

INCLUDE = -I../include
Expand All @@ -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 \
Expand All @@ -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 $@
Expand All @@ -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
4 changes: 4 additions & 0 deletions wasm/README.md
Original file line number Diff line number Diff line change
@@ -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 --
Expand Down
15 changes: 15 additions & 0 deletions wasm/cell.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Global Phasing Ltd.

#include "common.h"
#include <gemmi/unitcell.hpp>

void add_cell() {
em::class_<gemmi::UnitCell>("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)
;
}
5 changes: 3 additions & 2 deletions wasm/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
#include <emscripten/bind.h>
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
1 change: 1 addition & 0 deletions wasm/gemmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <emscripten/em_asm.h>

EMSCRIPTEN_BINDINGS(Gemmi) {
add_cell();
add_mol();
add_mtz_fft();
EM_ASM( finalize_gemmi(); );
Expand Down
9 changes: 0 additions & 9 deletions wasm/mol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ em::class_<T> wrap_children() {
}

void add_mol() {
em::class_<gemmi::UnitCell>("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<gemmi::Structure>()
.property("name", &gemmi::Structure::name)
.property("cell", &gemmi::Structure::cell)
Expand Down
28 changes: 28 additions & 0 deletions wasm/mtz.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions wasm/mtz_fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void add_mtz_fft() {

#ifdef STANDALONE_MTZ
EMSCRIPTEN_BINDINGS(GemmiMtz) {
add_cell();
add_mtz_fft();
}
#endif
16 changes: 16 additions & 0 deletions wasm/mtz_fft.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 6764dfd

Please sign in to comment.