A rust FFI wrapper library for minimap2 with support for SIMDe, htslib, zlib-ng, and curl.
minimap2 = "0.1.31+minimap2.2.30"Also see Features
Tested with rustc 1.86.0. Recommended to use the latest stable version of rust, but if you need support for an older version, please open an issue.
| minimap2-rs | minimap2 |
|---|---|
| 0.1.28 - 0.1.31 | 2.30 |
| 0.1.25 - 0.1.27 | 2.30 |
| 0.1.24 | 2.29 |
| 0.1.18 - 0.1.23 | 2.28 |
| 0.1.17 | 2.27 |
| 0.1.16 | 2.26 |
Create an Aligner
let mut aligner = Aligner::builder()
.map_ont()
.with_threads(8)
.with_cigar()
.with_index("ReferenceFile.fasta", None)
.expect("Unable to build index");Align a sequence:
let seq: Vec<u8> = b"ACTGACTCACATCGACTACGACTACTAGACACTAGACTATCGACTACTGACATCGA";
let alignment = aligner
.map(&seq, false, false, None, None, Some(b"My Sequence Name"))
.expect("Unable to align");All minimap2 presets should be available (see functions section):
let aligner = map_ont();
let aligner = asm20();Note Each preset overwrites different arguments. Using multiple at a time is not technically supported, but will work. Results unknown. So be careful! It's equivalent to running minimap2 -x map_ont -x short ...
MapOpts and IdxOpts can be customized with Rust's struct pattern, as well as applying mapping settings. Inspired by bevy.
let mut aligner: Aligner<PresetSet> = Aligner::builder().map_ont();
aligner.mapopt.seed = 42;
aligner.mapopt.best_n = 1;
aligner.idxopt.k = 21;
self.mapopt.flag |= MM_F_COPY_COMMENT as i64; // Setting a flag. If you do this frequently, open an [issue](https://github.com/jguhlin/minimap2-rs/issues/new) asking for an ergonomic function!
self.idxopt.flag |= MM_I_HPC as i32;See full list of options below.
There are two working examples directly in this repo. In both instances below, 64 is the number of threads to allocate.
Channel-based multi-threading
cargo run --example channels -- reference_genome.fasta reads.fasta 64
Rayon-based multi-threading
cargo run --example rayon -- reference_genome.fasta reads.fasta 64
Depending on your needs you can probably do just fine with Rayon. But for very large implementations, interactivity, or limited memory, using channels may be the way to go.
There is a binary called "fakeminimap2" which demonstrates basic usage and multithreading using channels or rayon. You can find it in this repo for an example. It it much more fully featured example, with an output interface, some mouse support, and interaction.
Alignment functions return a Mapping struct. The Alignment struct is only returned when the Aligner is created using .with_cigar().
A very simple example would be:
let mut file = std::fs::File::open(query_file);
let mut reader = BufReader::new(reader);
let mut fasta = Fasta::from_buffer(&mut reader)
for seq in reader {
let seq = seq.unwrap();
let alignment: Vec<Mapping> = aligner
.map(&seq.sequence.unwrap(), false, false, None, None, None)
.expect("Unable to align");
println!("{:?}", alignment);
}There is a map_file function that works on an entire file, but it is not-lazy and thus not suitable for large files. It may be removed in the future or moved to a separate lib.
let mappings: Result<Vec<Mapping>> = aligner.map_file("query.fa", false, false);Multithreading is supported, for implementation example see fakeminimap2. Minimap2 also supports threading itself, and will use a minimum of 3 cores for building the index. Multithreading for mapping is left to the end-user.
Adjust the number of threads used to build the index:
let mut aligner = Aligner::builder()
.map_ont()
.with_index_threads(8);This appears to work. See fakeminimap2 for full implementation.
use rayon::prelude::*;
let results = sequences.par_iter().map(|seq| {
aligner.map(seq.as_bytes(), false, false, None, None, None).unwrap()
}).collect::<Vec<_>>();Also works. Otherwise directly cloning the aligner will Arc clone the internal index.
The following crate features are available:
- map-file - Enables the ability to map a file directly to a reference. Enabled by deafult
- htslib - Provides an interface to minimap2 that returns rust_htslib::Records
- simde - Enables SIMD Everywhere library in minimap2
- zlib-ng - Enables the use of zlib-ng for faster compression
- curl - Enables curl for htslib
- static - Builds minimap2 as a static library
- sse2only - Builds minimap2 with only SSE2 support
Map-file is a default feature and enabled unless otherwise specified.
Create an issue if you need any of the following:
- setting mismatch penalty for base transitions minimap 2.27 release notes
- Generate ds tags to indicate uncertainty in indels
Potentially others. Please create an issue!
Follow these instructions.
In brief, using bash shell:
docker pull messense/rust-musl-cross:x86_64-musl
alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:x86_64-musl'
rust-musl-builder cargo build --releaseMinimap2 is tested on x86_64 and aarch64 (arm64). Other platforms may work, please open an issue if minimap2 compiles but minimap2-rs does not.
htslib- Successsimde- Success
- Chopper - Long read trimming and filtering
- mappy-rs - Drop-in multi-threaded replacement for python's mappy
- HiFiHLA - HLA star-calling tool for PacBio HiFi data
- STRdust - Tandem repeat genotyper for long reads
- oarfish - transcript quantification from long-read RNA-seq data
- lrge - Long Read-based Genome size Estimation from overlaps
- mmr - A minimap2-based aligner with BINSEQ file format support
Minimap2 sets cap_kalloc to ~500Mb, so for 32 threads this can be ~16Gb. You can set this manually with aligner.mapopt.cap_kalloc. This is a good idea if you are using a lot of threads, or have very long running jobs.
let per_thread_cap_kalloc = (1_000_000_000_f64 / (args.threads as f64)).ceil() as i64;
aligner.mapopt.cap_kalloc = per_thread_cap_kalloc;
- Minimap2 outputs and results are sensitive to the best_n parameter. Set it manually or be prepared for it to be changed upstream (potentially!)
- Iterator interface for map_file
- -sys Possible to decouple from pthread?