minimap2 Documentation

repository·master·Indexed 24 days ago

https://github.com/lh3/minimap2

A versatile and fast sequence alignment program for mapping long reads (ONT, PacBio), short reads, and RNA-seq data against reference genomes, or performing assembly-to-assembly alignments. It includes various preset modes for different sequencing technologies, support for PAF and SAM output formats, and the paftools.js script for processing alignments, evaluating mapping accuracy, and calling variants.

Tokens
8.3K
Snippets
28
Records
47
Agent score
81%

What's inside minimap2

  1. Evaluate mapping accuracy with mapeval

    master

    The mapeval command evaluates mapped SAM/PAF files. It is typically used with simulated reads converted via pbsim2fq (for MAF) or mason2fq (for SAM).

    Output includes:

    • Q lines: Quality threshold, number of reads with mapping quality $\ge$ threshold, number of wrong mappings, accumulative mapping error rate, and accumulative number of mapped reads.
    • U line: Number of unmapped reads (if present in SAM).
  2. Understand Minimap2 hardware and sequence limitations

    master

    Before using Minimap2, be aware of the following technical constraints:

    Hardware Requirements

    • x86 CPUs: Requires SSE2 instructions.
    • ARM CPUs: Requires NEON instructions.
    • While non-SIMD support is possible, it significantly reduces performance.

    Sequence Constraints

    • Maximum Sequence Length: A single query or database sequence cannot be approximately 2 billion bases or longer (specifically, the limit is 2,147,483,647 bases). However, the total length of all sequences combined can exceed this limit.

    Alignment Limitations

    • Low-complexity regions: Minimap2 may produce suboptimal alignments in long, low-complexity regions because seed positions may be suboptimal.
    • Exons: Minimap2 may occasionally miss small exons.
  3. Call variants from haploid assemblies with paftools.js call

    master

    The call command identifies variants from coordinate-sorted assembly-to-reference alignments.

    Requirements:

    1. Use the --cs tag in minimap2.
    2. The alignment must be sorted by reference start coordinate.

    Workflow:

    1. Align assembly to reference: minimap2 -cx <preset> --cs ref.fa asm.fa > asm.paf
    2. Sort: sort -k6,6 -k8,8n asm.paf > asm.srt.paf
    3. Call: k8 paftools.js call asm.srt.paf > asm.var.txt

    Output Format:

    • R lines: Regions covered by exactly one query contig.
    • V lines: Variants. Format: chr, start, end, query depth, mapping quality, REF allele, ALT allele, query name, query start, end, orientation.

    Note: For long-read assemblies, the command ignores alignments $<50$kb and uses $10$kb thresholds for callable regions. For short reads, these thresholds should be reduced.

    Example:

    minimap2 -cx asm5 -t8 --cs ref.fa asm.fa > asm.paf
    sort -k6,6 -k8,8n asm.paf > asm.srt.paf
    k8 paftools.js call asm.srt.paf > asm.var.txt
  4. Map using a pre-computed index

    master

    To speed up mapping, you can create an index file (.mmi) first and then use that index for subsequent mapping tasks.

    1. Create the index:
    ./minimap2 -x map-ont -d MT-human-ont.mmi test/MT-human.fa
    1. Map using the index:
    ./minimap2 -a MT-human-ont.mmi test/MT-orang.fa > test.sam
    ./minimap2 -x map-ont -d MT-human-ont.mmi test/MT-human.fa
    ./minimap2 -a MT-human-ont.mmi test/MT-orang.fa > test.sam
  5. Map Nanopore direct-RNA reads

    master

    Direct-RNA reads are noisier and stranded. Use a shorter k-mer (-k14) for sensitivity and the -uf flag to force mapping to the forward transcript strand only. The --splice-flank=no option is also recommended for SIRV data.

    minimap2 -ax splice -k14 -uf SIRV_E2.fa SIRV_ont-drna.fa > aln.sam
  6. Install Mappy

    master

    Mappy is a Python binding for minimap2. It requires zlib. You can install it via pip or by building it from the source repository.

    Using pip:

    pip install --user mappy

    Building from source (requires Cython):

    git clone https://github.com/lh3/minimap2
    cd minimap2
    python setup.py install
    pip install --user mappy
  7. Map long mRNA/cDNA reads using splice presets

    master

    For RNA-seq data, use splice-aware presets. By default, -x splice assumes unknown read orientation and performs two rounds of alignment to infer it.

    Presets and Options

    • splice:hq: For PacBio Iso-seq or traditional cDNA. Use -uf to force forward strand consideration.
    • splice: For Nanopore 2D cDNA-seq.
    • splice with -uf -k14: For Nanopore Direct RNA-seq (smaller k-mer increases sensitivity).
    • --splice-flank=no: Use when mapping against SIRV control data to ignore non-conservative splicing signals.
    • -N: Tune the number of secondary alignments (default is 5).
    • -G: Set max intron length (default 200k). Avoid excessively large values.

    Using Gene Annotations

    You can prioritize annotated splice junctions using --junc-bed. This requires a BED file (e.g., generated via paftools.js gff2bed).

    # PacBio Iso-seq
    minimap2 -ax splice:hq -uf ref.fa iso-seq.fq > aln.sam
    
    # Nanopore 2D cDNA-seq
    minimap2 -ax splice ref.fa nanopore-cdna.fa > aln.sam
    
    # Nanopore Direct RNA-seq
    minimap2 -ax splice -uf -k14 ref.fa direct-rna.fq > aln.sam
    
    # Using annotations (requires BED file)
    paftools.js gff2bed anno.gff > anno.bed
    minimap2 -ax splice --junc-bed anno.bed ref.fa query.fa > aln.sam
  8. Install minimap2 and paftools.js

    master

    To install the minimap2 executables and the companion paftools.js script, download the release tarball, extract it, and add the directory to your PATH.

    # install minimap2 executables
    curl -L https://github.com/lh3/minimap2/releases/download/v2.31/minimap2-2.31_x64-linux.tar.bz2 | tar jxf -
    cp minimap2-2.31_x64-linux/{minimap2,k8,paftools.js} .
    export PATH="$PATH:"`pwd`                               # put the current directory on PATH
    
    # download example datasets
    curl -L https://github.com/lh3/minimap2/releases/download/v2.10/cookbook-data.tgz | tar zxf -
  9. Install minimap2

    master

    Precompiled Binaries (x86-64 Linux)

    You can download and extract precompiled binaries using curl and tar:

    curl -L https://github.com/lh3/minimap2/releases/download/v2.31/minimap2-2.31_x64-linux.tar.bz2 | tar -jxvf -
    ./minimap2-2.31_x64-linux/minimap2

    Compiling from Source

    Requires a C compiler, GNU make, and zlib development files.

    • Standard build: make
    • Disable SSE4 (slower): make sse2only=1 (useful if compilation errors occur)
    • 32-bit ARM (e.g., ARMv7): make arm_neon=1
    • 64-bit ARM (e.g., ARMv8): make arm_neon=1 aarch64=1
    • Using SIMDe (for various SIMD instruction sets): make -f Makefile.simde (combine with ARM flags as needed)
    curl -L https://github.com/lh3/minimap2/releases/download/v2.31/minimap2-2.31_x64-linux.tar.bz2 | tar -jxvf -
    ./minimap2-2.31_x64-linux/minimap2
  10. Perform cross-species full-genome alignment

    master

    Use the asm20 preset for cross-species alignment where divergence is up to 10%.

    Presets available:

    • asm5: divergence < 1%
    • asm10: divergence ~2%
    • asm20: divergence < 10%

    To call variants from the alignment, pipe the sorted PAF output to paftools.js call.

  11. Find long-read overlaps

    master

    Use the ava presets to find overlaps within the same dataset for assembly.

    • For PacBio: use ava-pb.
    • For Nanopore: use ava-ont. It is recommended to set a minimum overlap length with -r (e.g., -r 10000) to improve contiguity.