𧬠mrc
Type-safe MRC-2014 file format reader/writer for Rust
A high-performance, memory-efficient library for reading and writing MRC (Medical Research Council) format files used in cryo-electron microscopy and structural biology. Designed for scientific computing with safety and performance as top priorities.
β¨ Why mrc?
- π Iterator-centric: Stream slices, slabs, or tiles on demand
- β‘ SIMD-accelerated: AVX2/NEON for the common i16βf32 path
- π Type-safe I/O: Compile-time mode matching prevents silent data corruption
- πΊοΈ Memory-mapped I/O:
MmapReader/MmapWriterfor files larger than RAM - π¦ Compression: Auto-detect and read gzip / bzip2 MRC files
- π·οΈ FEI metadata: Structured parsing of FEI1/FEI2 extended headers
Note: This crate is currently under active development. While most features are functional, occasional bugs and API changes are possible. Contributions are welcomeβplease report issues and share your ideas!
π¦ Installation
[]
= "0.2"
# For all features (defaults are usually sufficient)
= { = "0.2", = ["mmap", "f16", "simd", "parallel", "gzip"] }
π Quick Start
Architecture
βββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ
β File System ββββββΆβ Header Parsing ββββββΆβ Iterator API β
β (.mrc/.mrc.gz) β β (1024 bytes) β β (Zero-copy) β
βββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ
β β β
βββββββββββββββ ββββββββββ βββββββββββ
β Reader β β Header β β VoxelBlock
β MmapReader β β β β β
β Writer β ββββββββββ βββββββββββ
β MmapWriter β
β GzipWriter β
β Bzip2Writer β
βββββββββββββββ
MRC File Structure
| 1024 bytes | NSYMBT bytes | data_size bytes |
| header | ext header | voxel data |
π Reading MRC Files
use open;
βοΈ Creating New Files
use ;
β οΈ Migrating from v0.1
v0.2 is a complete architectural redesign. Key API changes:
| v0.1 | v0.2 |
|---|---|
MrcView::new(data) |
Reader::open(path) / open(path) |
MrcFile::create(path, header) |
create(path).shape(dims).mode::<T>().finish() |
MrcView::view::<f32>() |
reader.slices::<f32>() |
MrcViewMut |
Writer + VoxelBlock<T> |
MrcMmap |
MmapReader / MmapWriter |
Migration example:
// v0.1: Load entire file into memory
let data = read?;
let view = new?;
let floats = view.?;
// v0.2: Stream with iterators
let reader = open?;
for slice in reader.
New in v0.2: SIMD acceleration, parallel encoding, type conversion iterators, MmapReader, MmapWriter, compression support, unified reader API, FEI extended header parsing.
πΊοΈ API Overview
Core Types
| Type | Purpose | Example |
|---|---|---|
[Reader] |
Auto-detect compression | [Reader::open] / [open()] |
[Reader] |
Read plain MRC files | Reader::open("file.mrc")? |
[MmapReader] |
Memory-mapped reading | MmapReader::open("large.mrc")? |
[Writer] |
Write MRC files | create("out.mrc").shape([64,64,64]).mode::<f32>().finish()? |
[MmapWriter] |
Memory-mapped writing | create("out.mrc").shape(...).finish_mmap()? |
[WriterBuilder] |
Configure new files | create(path).shape(dims).mode::<T>() |
[Header] |
1024-byte MRC header | Header::new() |
[HeaderBuilder] |
Fluent header construction | HeaderBuilder::new().shape([64,64,64]).mode::<f32>().build()? |
[Mode] |
Data type enumeration | Mode::Float32 |
[VoxelBlock<T>] |
Chunk of voxel data | VoxelBlock::new(offset, shape, data) |
[VolumeShape] |
Volume dimensions | VolumeShape::new(nx, ny, nz) |
[GzipWriter] |
Gzip-compressed writer | create("out.mrc.gz").shape(dims).mode::<T>().finish_gzip()? |
[Bzip2Writer] |
Bzip2-compressed writer | create("out.mrc.bz2").shape(dims).mode::<T>().finish_bzip2()? |
[Fei1Metadata] |
FEI1 extended metadata | Fei1Metadata::from_bytes(bytes) |
[Fei2Metadata] |
FEI2 extended metadata | Fei2Metadata::from_bytes(bytes) |
Iterator API
All reader types provide a unified iterator API directly:
// Iterate over individual slices (Z axis)
for slice in reader.
// Iterate over slabs (multiple slices at once)
for slab in reader.
// Iterate over arbitrary 3D tiles
for tile in reader.
// Semantic aliases
for image in reader.
for plane in reader.
for stack in reader.
for stack in reader.
for vol in reader.?
Direct Access
// Read a specific subregion directly
let block = reader.?;
Type Conversion
The crate intentionally does not provide generic type conversion β that is the caller's responsibility. Only the overwhelmingly common cryo-EM workflows are supported as conveniences:
// Read an Int16/Uint16/Int8/Float32/Float16 file as f32
for slice in reader.slices_f32?
// Iterate over slabs with f32 conversion
for slab in reader.slabs_f32?
// Convert Mode 6 (Uint16) voxels to u8
for slice in reader.slices_u8
// Mode 0 (8-bit) with signed/unsigned interpretation
use M0Interpretation;
for slice in reader.slices_mode0
for slab in reader.slabs_mode0
// Write f32 data to a Float16 file
let mut writer = create
.shape
.
.finish?;
let f32_data: = /* ... */;
writer.write_f16_from_f32?;
// Write u8 data to a Uint16 (Mode 6) file (auto-widened)
let mut writer = create
.shape
.
.finish?;
writer.write_u8_block?;
Safety note: reader.slices::<f32>() on an Int16 file returns
Error::ModeMismatch instead of silently decoding 2-byte voxels as 4-byte
floats. Use slices_f32() for automatic conversion.
Compression
[Reader::open] (and the convenience [open()]) automatically detects gzip and bzip2
compression from the file magic bytes:
use open;
// Works for plain .mrc, .mrc.gz, and .mrc.bz2
let reader = open?;
You can also open compressed files directly:
use Reader;
let reader = open_gzip?;
let reader = open_bzip2?;
And write compressed files:
use ;
let mut writer = create
.shape
.
.finish_gzip?;
writer.write_block?;
writer.finalize?;
Memory-Mapped I/O
For large files that don't fit in RAM, memory-mapped I/O lets the OS handle paging:
use MmapReader;
let reader = open?;
// Same iterator API as Reader
for slice in reader.
// Direct byte access (zero-copy)
let bytes = reader.data_bytes; // &[u8] backed by mmap
Memory-mapped writes are also supported:
use create;
let mut writer = create
.shape
.
.finish_mmap?;
writer.write_block?;
writer.finalize?;
| Use | When |
|---|---|
Reader |
Small files, simple sequential access |
MmapReader |
Large files, memory-constrained environments, random access |
Permissive Mode
Readers support a permissive open mode that collects non-fatal issues as warnings instead of hard errors:
use Reader;
let = open_permissive?;
for w in &warnings
This is useful for reading files from less strict sources (e.g., legacy instruments) where the data is valid but the header has minor issues.
Convenience Functions
use ;
// Reading
let reader = open?; // auto-detect compression (plain/gzip/bzip2)
// Writing
let writer = create // standard file I/O
.shape
.
.finish?;
let mmap_writer = create // memory-mapped (requires mmap)
.shape
.
.finish_mmap?;
π§ Header Construction
Direct Header Manipulation
use Header;
let mut header = new;
// Basic dimensions
header.nx = 2048;
header.ny = 2048;
header.nz = 512;
// Data type
header.mode = Float32 as i32;
// Physical dimensions in Γ
ngstrΓΆms
header.xlen = 204.8;
header.ylen = 204.8;
header.zlen = 102.4;
// Cell angles for crystallography
header.alpha = 90.0;
header.beta = 90.0;
header.gamma = 90.0;
// Extended header type (optional)
header.set_exttyp;
Fluent Builder
use HeaderBuilder;
let header = new
.shape
.
.cell_lengths
.cell_angles
.ispg
.exttyp
.build?;
Key Header Fields
| Field | Type | Description |
|---|---|---|
nx, ny, nz |
i32 |
Image dimensions |
mode |
i32 |
Data type (see Mode enum) |
xlen, ylen, zlen |
f32 |
Cell dimensions (Γ ) |
alpha, beta, gamma |
f32 |
Cell angles (Β°) |
mapc, mapr, maps |
i32 |
Axis mapping (1,2,3 permutation) |
dmin, dmax, dmean |
f32 |
Data statistics |
ispg |
i32 |
Space group number |
nsymbt |
i32 |
Extended header size |
origin |
[f32; 3] |
Origin coordinates |
exttyp |
[u8; 4] |
Extended header type |
rms |
f32 |
RMS deviation from mean |
nlabl |
i32 |
Number of labels (0β10) |
Volume Type Introspection
The Header provides convenience methods following Python mrcfile conventions:
let h = header;
// Volume type checks
h.is_single_image; // nz == 1
h.is_image_stack; // ispg == 0
h.is_volume; // 3D volume (ispg != 0 and not a stack)
h.is_volume_stack; // ispg in 401β630
// Computed properties
h.voxel_size; // [xlen/mx, ylen/my, zlen/mz] in Γ
/pixel
h.logical_shape; // 4D shape following mrcfile conventions
h.get_labels; // Vec<String> of non-empty labels
π Data Type Support
[Mode] |
Value | Rust Type | Bytes | Description | Use Case |
|---|---|---|---|---|---|
Int8 |
0 | i8 |
1 | Signed 8-bit integer | Binary masks |
Int16 |
1 | i16 |
2 | Signed 16-bit integer | Cryo-EM density |
Float32 |
2 | f32 |
4 | 32-bit float | Standard density |
Int16Complex |
3 | [Int16Complex] |
4 | Complex 16-bit | Phase data |
Float32Complex |
4 | [Float32Complex] |
8 | Complex 32-bit | Fourier transforms |
Uint16 |
6 | u16 |
2 | Unsigned 16-bit | Segmentation |
Float16 |
12 | f16[^1] |
2 | 16-bit float | Memory efficiency |
Packed4Bit |
101 | [Packed4Bit] |
0.5 | Packed 4-bit[^2] | Compression |
[^1]: Requires f16 feature. Uses the half crate; no nightly Rust required.
[^2]: Packed4Bit is provided for manual nibble unpacking via first()/second(). Full read/write support for Mode 101 is not yet implemented.
Complex numbers can be converted to real values via [ComplexToRealStrategy]:
| Strategy | Description |
|---|---|
RealPart |
Extract the real component |
ImaginaryPart |
Extract the imaginary component |
Magnitude |
Compute sqrt(realΒ² + imagΒ²) |
Phase |
Compute atan2(imag, real) |
π·οΈ FEI Extended Headers
This crate provides structured parsing of FEI1 and FEI2 extended headers commonly found in cryo-EM data collected on Thermo Fisher/FEI microscopes.
use ;
// After opening a file with FEI extended headers
let reader = open?;
let ext_bytes = reader.ext_header_bytes;
// Parse FEI1 records (768 bytes each)
if let Some = parse_fei1_records
// Or parse a single record directly
if let Some = from_bytes
// FEI2 extends FEI1 with additional v2 fields (888 bytes each)
if let Some = parse_fei2_records
β‘ Performance Features
SIMD Acceleration
The simd feature (enabled by default) uses AVX2 (x86_64) or NEON (AArch64)
to accelerate the common i16βf32, u16βf32, and i8βf32 paths inside
slices_f32() and slabs_f32(). No explicit SIMD code is required in user
code.
Zero-Copy Reading
Reader loads the entire file into memory (as raw bytes) and decodes slices
on demand. For memory-mapped access, use MmapReader:
use MmapReader;
// Memory-mapped reading β zero-copy raw byte access
let reader = open?;
// True zero-copy typed access (native-endian files only):
let slice: & = reader.?;
// Generic typed iteration (always allocates per block):
for slice in reader.
Parallel Writing
With the parallel feature, large writes use Rayon for parallel encoding:
let mut writer = create
.shape
.
.finish?;
// This uses parallel encoding internally
writer.write_block_parallel?;
writer.finalize?;
Header Statistics
The writer can compute and update header statistics after writing data:
let mut writer = create
.shape
.
.finish?;
// Write all data ...
writer.update_header_stats?; // updates dmin, dmax, dmean, rms
writer.finalize?;
The reader can cross-check header statistics against actual data:
let reader = open?;
reader.validate_header_stats?; // Returns Ok or StatsMismatch error
π― Feature Flags
| Feature | Description | Default |
|---|---|---|
mmap |
Memory-mapped I/O | β |
f16 |
Half-precision support (via half crate) |
β |
simd |
SIMD acceleration | β |
parallel |
Parallel encoding | β |
gzip |
Gzip-compressed MRC files | β |
bzip2 |
Bzip2-compressed MRC files | β |
π οΈ CLI Tools
The crate ships two standalone binaries:
mrc-validate β validation
Runs comprehensive checks: header structure, file size, endianness, data statistics cross-check (1 % tolerance), NaN/Inf scan in float modes, and volume type classification.
mrc-header β header inspection
Prints every header field with semantic interpretation: volume type (single image / stack / volume / volume stack), axis names (X/Y/Z), space group description, extended header type, sentinel-aware statistics display, and validation summary.
mrc-invert β contrast inversion
Negates every voxel value (v β βv) to flip black-on-white to white-on-black and vice versa. Reads any mode (auto-detects compression), writes Float32 output with updated header statistics.
π£οΈ Development Roadmap
β Current Release (v0.2.x): Core + SIMD + FEI
- Complete MRC-2014 format support
- Iterator-centric API (slices, slabs, tiles)
- Type-safe I/O with compile-time mode checking
- SIMD acceleration (AVX2, NEON)
- Zero-copy fast paths
- Parallel encoding
- Memory-mapped I/O (
MmapReader,MmapWriter) - All data types (modes 0β4, 6, 12, 101)
- Compression support (gzip, bzip2)
- Unified reader API (inherent methods on Reader / MmapReader)
- FEI1/FEI2 extended header parsing
- Type conversion conveniences (
slices_f32,slices_u8,slices_mode0) - Header statistics computation and validation
-
mrc-validateCLI tool - Permissive mode for reading non-standard files
- Volume stack support
π§ Next Release (v0.3.x): Extended Features
- Extended header parsing for CCP4, MRCO, SERI, AGAR formats
- Streaming decompression (avoid loading entire compressed files into RAM)
- Dedicated benchmark suite (
criterionin dev-deps but nobenches/dir)
π Future Releases (v1.x)
- Python bindings via PyO3
- GPU acceleration
- Cloud storage integration
π§ͺ Testing
# Run all tests
# Run benchmarks
π€ Contributing
We welcome contributions! Here's how to get started:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
Development Setup
# Clone repository
# Build with all features
# Run tests
# Check formatting
# Run clippy
π MIT License
MIT License
Copyright (c) 2024-2025 mrc contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
π Acknowledgments
- CCP-EM for the MRC-2014 specification
- EMDB for providing real-world test data
- Cryo-EM community for invaluable feedback
- Rust community for the amazing ecosystem
π Support & Community
- π Issues: Report bugs
- π Documentation: Full docs
- π·οΈ Releases: Changelog
Made with β€οΈ by the cryo-EM community for the scientific computing world
[SIMD-accelerated β’ Memory-mapped]