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.
- π Iterator-centric: Stream slices, slabs, or blocks 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
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!
[dependencies]
mrc = "0.2"
# For all features (defaults are usually sufficient)
mrc = { version = "0.2", features = ["mmap", "f16", "simd", "parallel"] }βββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ
β File System ββββββΆβ Header Parsing ββββββΆβ Iterator API β
β (.mrc file) β β (1024 bytes) β β (Zero-copy) β
βββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ
β β β
βββββββββββββββ ββββββββββ βββββββββββ
β Reader β β Header β β VoxelBlock
β MmapReader β β β β β
β Writer β ββββββββββ βββββββββββ
β MmapWriter β
βββββββββββββββ
| 1024 bytes | NSYMBT bytes | data_size bytes |
| header | ext header | voxel data |
use mrc::Reader;
fn main() -> Result<(), mrc::Error> {
// Open an MRC file - header is parsed automatically
let reader = Reader::open("protein.mrc")?;
// Get volume dimensions
let shape = reader.shape();
println!("Volume: {}Γ{}Γ{} voxels", shape.nx, shape.ny, shape.nz);
// Iterate over slices - zero-copy when file type matches
for slice in reader.slices::<f32>() {
let block = slice?; // VoxelBlock<f32>
println!("Slice {}: {} voxels", block.offset[2], block.len());
}
// Or read with automatic conversion to f32 (common cryo-EM workflow)
for slice in reader.slices_f32()? {
let block = slice?;
let sum: f32 = block.data.iter().sum();
println!("Slice sum: {}", sum);
}
Ok(())
}use mrc::{create, VoxelBlock};
fn main() -> Result<(), mrc::Error> {
// Create a new file with the builder pattern
let mut writer = create("output.mrc")
.shape([512, 512, 256])
.mode::<f32>()
.finish()?;
// Write voxel data slice by slice
for z in 0..256 {
let block = VoxelBlock::new(
[0, 0, z],
[512, 512, 1],
vec![0.0f32; 512 * 512],
);
writer.write_block(&block)?;
}
// Finalize rewrites the header to disk.
// Note: dmin/dmax/dmean/rms are NOT updated automatically.
writer.finalize()?;
Ok(())
}v0.2 is a complete architectural redesign. Key API changes:
| v0.1 | v0.2 |
|---|---|
MrcView::new(data) |
Reader::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 = std::fs::read("file.mrc")?;
let view = MrcView::new(data)?;
let floats = view.view::<f32>()?;
// v0.2: Stream with iterators
let reader = Reader::open("file.mrc")?;
for slice in reader.slices::<f32>() {
let block = slice?;
// process block.data
}New in v0.2: SIMD acceleration, parallel encoding, type conversion iterators, MmapReader.
| Type | Purpose | Example |
|---|---|---|
[Reader] |
Read 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 | MmapWriter::create("out.mrc", header)? |
[WriterBuilder] |
Configure new files | create(path).shape(dims).mode::<T>() |
[Header] |
1024-byte MRC header | Header::new() |
[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) |
The library provides an iterator-centric API for efficient processing:
// Iterate over individual slices (Z axis)
for slice in reader.slices::<f32>() {
let block = slice?;
// Process slice
}
// Iterate over slabs (multiple slices at once)
for slab in reader.slabs::<f32>(10) { // 10 slices per slab
let block = slab?;
// Process slab
}
// Iterate over arbitrary chunks
for chunk in reader.blocks::<f32>([64, 64, 64]) {
let block = chunk?;
// Process 64Β³ chunk
}The crate intentionally does not provide generic type conversion β that is the caller's responsibility. Only two overwhelmingly common cryo-EM workflows are supported as conveniences:
// Read an Int16/Uint16/Int8/Float32 file as f32
for slice in reader.slices_f32()? {
let block = slice?;
// block.data is Vec<f32>
}
// Write f32 data to a Float16 file
let mut writer = create("output.mrc")
.shape([256, 256, 128])
.mode::<f16>()
.finish()?;
let f32_data: VoxelBlock<f32> = /* ... */;
writer.write_f16_from_f32(&f32_data)?;Safety note: reader.slices::<f32>() on an Int16 file returns
Error::ModeMismatch instead of silently decoding 2-byte voxels as 4-byte
floats.
For large files that don't fit in RAM, memory-mapped I/O lets the OS handle paging:
use mrc::MmapReader;
let reader = MmapReader::open("large_volume.mrc")?;
// Same iterator API as Reader
for slice in reader.slices::<f32>() {
let block = slice?;
// OS automatically pages data in/out
}
// Direct byte access (zero-copy)
let bytes = reader.data_bytes()?; // &[u8] backed by mmap| Use | When |
|---|---|
Reader |
Small files, simple sequential access |
MmapReader |
Large files, memory-constrained environments, random access |
[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 | f161 |
2 | 16-bit float | Memory efficiency |
Packed4Bit |
101 | [Packed4Bit] |
0.5 | Packed 4-bit | Compression |
The simd feature (enabled by default) uses AVX2 (x86_64) or NEON (AArch64)
to accelerate the common i16βf32 and u16βf32 paths inside slices_f32() and
slabs_f32(). No explicit SIMD code is required in user code.
Reader loads the entire file into memory (as raw bytes) and decodes slices
on demand. For true zero-copy access, use MmapReader or MmapWriter:
use mrc::{MmapReader, MmapWriter, SliceAccess};
// Memory-mapped reading
let reader = MmapReader::open("data.mrc")?;
for slice in reader.slices::<f32>() {
// Decodes on demand; raw bytes are backed by the OS page cache
}
// Memory-mapped writing with direct slice mutation
let mut writer = mrc::create("out.mrc")
.shape([512, 512, 256])
.mode::<f32>()
.mmap()
.finish()?;
let slice = writer.slice_mut::<f32>(0)?;
slice[0] = 1.0;With the parallel feature, large writes use Rayon for parallel encoding:
let mut writer = create("large.mrc")
.shape([2048, 2048, 512])
.mode::<f32>()
.finish()?;
// This uses parallel encoding internally
writer.write_block_parallel(&large_block)?;
writer.finalize()?;| Feature | Description | Default |
|---|---|---|
mmap |
Memory-mapped I/O | β |
f16 |
Half-precision support | β |
simd |
SIMD acceleration | β |
parallel |
Parallel encoding | β |
The MRC header contains 56 fields (1024 bytes total):
use mrc::Header;
let mut header = Header::new();
// Basic dimensions
header.nx = 2048;
header.ny = 2048;
header.nz = 512;
// Data type
header.mode = 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(*b"FEI1");| 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 |
- Complete MRC-2014 format support
- Iterator-centric API (slices, slabs, blocks)
- 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)
- Extended header parsing (CCP4, FEI1, FEI2, etc.)
- Statistics functions (histogram, moments)
- Validation utilities
- Streaming API for very large datasets
- Python bindings via PyO3
- GPU acceleration
- Compression support (gzip, zstd)
- Cloud storage integration
# Run all tests
cargo test --all-features
# Run benchmarks
cargo bench --all-features
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
# Clone repository
git clone https://github.com/elemeng/mrc.git
cd mrc
# Build with all features
cargo build --all-features
# Run tests
cargo test --all-features
# Check formatting
cargo fmt --check
# Run clippy
cargo clippy --all-featuresMIT 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.
- 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
- π Issues: Report bugs
- π Documentation: Full docs
- π·οΈ Releases: Changelog
Made with β€οΈ by the cryo-EM community for the scientific computing world
[Zero-copy β’ SIMD-accelerated β’ 100% safe Rust]
Footnotes
-
Requires
f16feature and nightly Rust compiler. β©