𧬠mrc
Zero-copy, zero-allocation, no_std-friendly 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?
- π Zero-copy: Direct memory mapping with no intermediate buffers
- π¦ no_std: Works in embedded environments and WebAssembly
- β‘ SIMD-accelerated: AVX2/NEON accelerated type conversions
- π Type conversion: Automatic conversion between voxel types
- π 100% safe: No unsafe blocks in public API
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
= { = "0.2", = ["std", "mmap", "f16", "simd", "parallel"] }
π Quick Start
Architecture
βββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ
β File System ββββββΆβ Header Parsing ββββββΆβ Iterator API β
β (.mrc file) β β (1024 bytes) β β (Zero-copy) β
βββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ
β β β
βββββββββββββββ ββββββββββ βββββββββββ
β Reader β β Header β β VoxelBlock
β MmapReader β β β β β
β Writer β ββββββββββ βββββββββββ
β MmapWriter β
βββββββββββββββ
MRC File Structure
| 1024 bytes | NSYMBT bytes | data_size bytes |
| header | ext header | voxel data |
π Reading MRC Files
use Reader;
βοΈ 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) |
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.
πΊοΈ API Overview
Core Types
| 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) |
Iterator API
The library provides an iterator-centric API for efficient processing:
// Iterate over individual slices (Z axis)
for slice in reader.
// Iterate over slabs (multiple slices at once)
for slab in reader.
// Iterate over arbitrary chunks
for chunk in reader.
Type Conversion
Automatic type conversion is supported via the Convert trait:
// Read file as one type, convert to another
for slice in reader.
// Write with type conversion
let mut writer = create
.shape
. // File stores i16
.finish?;
let f32_data: = ...;
writer.?; // Converts f32 -> i16
Memory-Mapped I/O
For large files (>1GB), 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 for zero-copy scenarios
if reader.
| Use | When |
|---|---|
Reader |
Small files, no_std compatibility, simple sequential access |
MmapReader |
Large files, memory-constrained environments, random access |
π 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 | Compression |
[^1]: Requires f16 feature and nightly Rust compiler.
β‘ Performance Features
SIMD Acceleration
The library automatically uses SIMD instructions (AVX2 on x86_64, NEON on AArch64) for type conversions:
// These conversions use SIMD when available:
// i8 β f32, i16 β f32, u16 β f32, u8 β f32
// Access SIMD functions directly
Zero-Copy Reading
When the file's native type matches your target type and endianness:
let reader = open?;
// Check if zero-copy is possible
if reader.
// The iterator will use zero-copy automatically when possible
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?;
π― Feature Flags
| Feature | Description | Default | no_std Compatible |
|---|---|---|---|
std |
Standard library support | β | β |
mmap |
Memory-mapped I/O | β | β |
f16 |
Half-precision support | β | β |
simd |
SIMD acceleration | β | β |
parallel |
Parallel encoding | β | β |
no_std Usage
For embedded systems, WebAssembly, and other constrained environments:
[]
= { = "0.2", = false }
use ;
// Create a header (works in no_std)
let mut header = new;
header.nx = 256;
header.ny = 256;
header.nz = 100;
header.mode = Float32 as i32;
no_std Compatible APIs
| API | Available in no_std | Description |
|---|---|---|
Header |
β | 1024-byte MRC header structure |
Mode |
β | Data type enumeration |
VoxelBlock |
β | Voxel data container |
VolumeShape |
β | Volume geometry |
Convert trait |
β | Type conversion |
Reader |
β | Requires file system |
Writer |
β | Requires file system |
| SIMD functions | β | Requires std |
π§ Header Structure
The MRC header contains 56 fields (1024 bytes total):
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_str?;
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 |
π£οΈ Development Roadmap
β Current Release (v0.2.x): Core + SIMD
- Complete MRC-2014 format support
- Iterator-centric API (slices, slabs, blocks)
- Type conversion pipeline
- 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)
- no_std support
π§ Next Release (v0.3.x): Extended Features
- Extended header parsing (CCP4, FEI1, FEI2, etc.)
- Statistics functions (histogram, moments)
- Validation utilities
- Streaming API for very large datasets
π Future Releases (v1.x)
- Python bindings via PyO3
- GPU acceleration
- Compression support (gzip, zstd)
- Cloud storage integration
π§ͺ Testing
# Run all tests
# Run benchmarks
# Check no_std build
π€ 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
[Zero-copy β’ SIMD-accelerated β’ 100% safe Rust]