Skip to content

Commit 1ef41f3

Browse files
committed
Init
1 parent 02e2188 commit 1ef41f3

10 files changed

Lines changed: 3171 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version: 20
17+
cache: npm
18+
- run: npm ci
19+
- run: npm run build
20+
- run: npm test

.github/workflows/publish.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version: 20
17+
cache: npm
18+
registry-url: https://registry.npmjs.org
19+
- run: npm ci
20+
- run: npm run build
21+
- run: npm test
22+
- run: npm publish --access public
23+
env:
24+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# @warpem/mrc-parser
2+
3+
Parser for **MRC** (Medical Research Council) format files, commonly used in cryo-EM (cryogenic electron microscopy) for storing 2D images, 3D volumes, and image stacks.
4+
5+
## Install
6+
7+
```bash
8+
npm install @warpem/mrc-parser
9+
```
10+
11+
## Usage
12+
13+
### Parse an entire MRC file
14+
15+
```ts
16+
import { parseMRC } from "@warpem/mrc-parser";
17+
18+
const buffer = await fs.readFile("map.mrc");
19+
const { header, data } = parseMRC(buffer.buffer);
20+
21+
console.log(header.dimensions); // { x: 256, y: 256, z: 256 }
22+
console.log(data.length); // 16777216
23+
```
24+
25+
### Parse header only, then read individual slices
26+
27+
```ts
28+
import { parseHeader, readSlice } from "@warpem/mrc-parser";
29+
30+
const buffer = await fs.readFile("stack.mrc");
31+
const header = parseHeader(buffer.buffer);
32+
33+
console.log(`${header.dimensions.z} slices, ${header.dimensions.x}x${header.dimensions.y} each`);
34+
35+
const slice0 = readSlice(buffer.buffer, header, 0);
36+
```
37+
38+
### Fetch from a URL with progress
39+
40+
```ts
41+
import { fetchMRC } from "@warpem/mrc-parser";
42+
43+
const { header, data } = await fetchMRC("/api/file/abc123", {
44+
onProgress: (received, total) => {
45+
console.log(`${((received / total) * 100).toFixed(0)}%`);
46+
},
47+
});
48+
```
49+
50+
### Fetch only the header (Range request)
51+
52+
```ts
53+
import { fetchMRCHeader, fetchSlice } from "@warpem/mrc-parser";
54+
55+
const header = await fetchMRCHeader("/api/file/abc123");
56+
const middleSlice = await fetchSlice("/api/file/abc123", header, Math.floor(header.dimensions.z / 2));
57+
```
58+
59+
## API
60+
61+
### Types
62+
63+
| Type | Description |
64+
|---|---|
65+
| `Int3` | `{ x: number; y: number; z: number }` — integer triplet |
66+
| `Float3` | `{ x: number; y: number; z: number }` — float triplet |
67+
| `MRCDataType` | Data mode enum: `Byte`, `Short`, `Float`, `Half`, `UnsignedShort`, `ShortComplex`, `FloatComplex`, `RGB` |
68+
| `MRCHeader` | Parsed header with dimensions, pixel size, statistics, labels, and IMOD metadata |
69+
| `ReadOptions` | `{ sliceStart?: number; sliceEnd?: number }` |
70+
| `FetchOptions` | `{ signal?: AbortSignal; onProgress?: (received, total) => void }` |
71+
72+
### Functions
73+
74+
| Function | Description |
75+
|---|---|
76+
| `parseHeader(buffer)` | Parse an MRC header from an `ArrayBuffer` |
77+
| `readData(buffer, header, options?)` | Read voxel data (all or a slice range) as `Float32Array` |
78+
| `readSlice(buffer, header, sliceIndex)` | Read a single slice as `Float32Array` |
79+
| `parseMRC(buffer)` | Parse header + all data in one call |
80+
| `fetchMRC(url, options?)` | Fetch and parse a complete MRC file |
81+
| `fetchMRCHeader(url, options?)` | Fetch only the header via Range request |
82+
| `fetchSlice(url, header, sliceIndex, options?)` | Fetch a single slice via Range request |
83+
| `fetchSlices(url, header, sliceStart, sliceEnd, options?)` | Fetch a range of slices via Range request |
84+
85+
All data is decoded into `Float32Array` regardless of the source data type (Byte, Short, Half, etc.).
86+
87+
## Supported MRC modes
88+
89+
| Mode | Description | Bytes/element |
90+
|---|---|---|
91+
| 0 | Unsigned 8-bit integer | 1 |
92+
| 1 | Signed 16-bit integer | 2 |
93+
| 2 | 32-bit float | 4 |
94+
| 3 | Complex (2 × int16) | 4 |
95+
| 4 | Complex (2 × float32) | 8 |
96+
| 6 | Unsigned 16-bit integer | 2 |
97+
| 12 | 16-bit float (half) | 2 |
98+
| 16 | RGB (3 × uint8) | 3 |
99+
100+
## License
101+
102+
BSD-3-Clause

0 commit comments

Comments
 (0)