|
| 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