@molcrafts/molrs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,175 @@
1
+ # molrs-wasm: WASM bindings for molrs
2
+
3
+ Object-oriented WASM bindings for molrs using FFI handle-based architecture.
4
+
5
+ ## Quick Start
6
+
7
+ ```javascript
8
+ import init, { Frame, XyzReader } from './pkg/molrs.js';
9
+
10
+ await init();
11
+
12
+ // Create a new frame
13
+ const frame = new Frame();
14
+ const atoms = frame.createBlock("atoms");
15
+
16
+ // Set column data
17
+ atoms.setColumnF32("x", new Float32Array([1.0, 2.0, 3.0]));
18
+ const data = atoms.getColumnF32("x");
19
+
20
+ // Read from file
21
+ const reader = new XyzReader(fileContent);
22
+ const loadedFrame = reader.read(0);
23
+ ```
24
+
25
+ ## API Reference
26
+
27
+ ### Frame
28
+
29
+ The main container for molecular data.
30
+
31
+ ```javascript
32
+ const frame = new Frame();
33
+
34
+ // Create a block
35
+ const atoms = frame.createBlock("atoms");
36
+
37
+ // Get an existing block
38
+ const atomsAgain = frame.getBlock("atoms");
39
+
40
+ // Remove a block
41
+ frame.removeBlock("atoms");
42
+
43
+ // Clear all blocks
44
+ frame.clear();
45
+
46
+ // Drop the frame (invalidates all blocks)
47
+ frame.drop();
48
+ ```
49
+
50
+ ### Block
51
+
52
+ A block contains columnar data with consistent row counts.
53
+
54
+ ```javascript
55
+ const block = frame.createBlock("atoms");
56
+
57
+ // Set column data
58
+ const positions = new Float32Array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
59
+ const shape = new Uint32Array([2, 3]); // 2 atoms, 3 coordinates each
60
+ block.setColumnF32("positions", positions, shape);
61
+
62
+ // Set integer columns (e.g., atom ids or bond indices)
63
+ const ids = new BigInt64Array([1n, 2n, 3n]);
64
+ block.setColumnI64("id", ids);
65
+
66
+ // Get column data (returns copy)
67
+ const data = block.getColumnF32("positions"); // Float32Array
68
+ const idData = block.getColumnI64("id"); // BigInt64Array
69
+
70
+ // Get column with shape info
71
+ const { data, shape } = block.getColumnF32WithShape("positions");
72
+
73
+ // Query block info
74
+ const keys = block.keys(); // ["positions"]
75
+ const nrows = block.nrows(); // 2
76
+ ```
77
+
78
+ ## File I/O
79
+
80
+ ### Reading Files
81
+
82
+ ```javascript
83
+ // XYZ files
84
+ const xyzReader = new XyzReader(fileContent);
85
+ const frameCount = xyzReader.len();
86
+ const frame = xyzReader.read(0); // Read frame 0
87
+
88
+ // PDB files
89
+ const pdbReader = new PdbReader(pdbContent);
90
+ const frame2 = pdbReader.read(0);
91
+
92
+ // LAMMPS data files
93
+ const lammpsReader = new LammpsReader(lammpsContent);
94
+ const frame3 = lammpsReader.read(0);
95
+ ```
96
+
97
+ ### Writing Files
98
+
99
+ ```javascript
100
+ // TODO: Writer API needs implementation
101
+ // const output = writeFrame(frame, "xyz");
102
+ ```
103
+
104
+ ## TypeScript Example
105
+
106
+ ```typescript
107
+ import init, { Frame, Block, XyzReader } from './pkg/molrs.js';
108
+
109
+ await init();
110
+
111
+ // Create frame and block
112
+ const frame: Frame = new Frame();
113
+ const atoms: Block = frame.createBlock("atoms");
114
+
115
+ // Set positions
116
+ const positions = new Float32Array([
117
+ 0.0, 0.0, 0.0, // atom 1
118
+ 1.0, 0.0, 0.0, // atom 2
119
+ 0.0, 1.0, 0.0, // atom 3
120
+ ]);
121
+ atoms.setColumnF32("x", positions);
122
+
123
+ // Query data
124
+ const nAtoms = atoms.nrows(); // 3
125
+ const x = atoms.getColumnF32("x");
126
+ console.log(x[0]); // 0.0
127
+
128
+ // Read from file
129
+ const reader = new XyzReader(fileContent);
130
+ const loadedFrame = reader.read(0);
131
+ if (loadedFrame) {
132
+ const loadedAtoms = loadedFrame.getBlock("atoms");
133
+ if (loadedAtoms) {
134
+ console.log(`Loaded ${loadedAtoms.nrows()} atoms`);
135
+ }
136
+ }
137
+ ```
138
+
139
+ ## Handle Invalidation
140
+
141
+ Handles follow strict invalidation rules:
142
+
143
+ - **Block handles** become invalid when:
144
+ - The parent frame is dropped
145
+ - The block is removed from the frame
146
+ - The frame is cleared
147
+
148
+ - **Frame handles** become invalid when:
149
+ - `frame.drop()` is called
150
+
151
+ Attempting to use an invalid handle will throw an error.
152
+
153
+ ## Architecture
154
+
155
+ This WASM layer is a thin wrapper around `molrs-ffi`, which provides:
156
+ - Stable handle-based references (no raw pointers)
157
+ - Explicit invalidation semantics
158
+ - Consistent behavior across Python and WASM
159
+
160
+ The API is designed to be:
161
+ - **Object-oriented**: Natural class hierarchy (Frame → Block)
162
+ - **Type-safe**: Clear ownership and lifetime semantics
163
+ - **Explicit**: No hidden copies or implicit conversions
164
+ - **Simple**: Store is hidden - Frame is the main entry point
165
+
166
+ ## Testing
167
+
168
+ ```bash
169
+ cargo test -p molrs-wasm --lib
170
+ ```
171
+
172
+ For WASM-specific tests:
173
+ ```bash
174
+ wasm-pack test --node wasm
175
+ ```