News ·

mmft 0.5: a fast FASTA/FASTQ toolkit, and how to use it

mmft, my minimal FASTA toolkit, is now a fast toolkit for FASTA, FASTQ and GenBank files, and a Rust library. Here is how to use it, and how it compares with seqkit, samtools and others.

mmft started life as my (Max’s) minimal FASTA toolkit. I was learning Rust and wanted to have my own toolkit, it wasn’t particularly great. Now I’ve been working with Claude to extend it, and optimise parsing as much as I can. It seems to be working pretty well. Now, over the last couple of releases it has become something more useful. It reads FASTA, FASTQ and GenBank/EMBL files, plain, gzipped or BGZF-compressed. Version 0.5.1 is out today on crates.io, with its parsers available as a Rust library too. Conda is on it’s way, and there are GitHub releases too.

I wanted mmft to be useful for humans, and also efficient for agentic coders.

This post is a tour: how to install it, the commands I use most, a worked example on organelle genomes, and the benchmarks, for anyone who wants to know whether it is worth switching.

Install

With a Rust toolchain:

cargo install mmft

Or download a prebuilt binary for Linux, macOS or Windows from the releases page. Every command has its own help and examples: mmft <command> -h.

Everyday use

Most commands take one or more files, or read stdin, and print tab-separated output (or BED, where that makes sense). Compression is detected from the bytes, not the file name, so the same command works on asm.fa, asm.fa.gz or a pipe.

mmft stats asm.fa.gz                          # N50/L50, auN, N90, contigs, gaps, GC, soft-masking
mmft len asm.fa.gz | sort -k2,2nr | head -5   # the five longest sequences
mmft extract asm.fa.gz chr1:1-1000 chrM       # regions (fast with a .fai/.gzi)
mmft extract -b genes.bed -N genome.fa        # BED regions, named by column 4
mmft windows -W 100k -s 50k asm.fa.gz         # 100 kb windows every 50 kb
mmft grep -f ids.txt -v reads.fq.gz           # drop listed reads; FASTQ stays FASTQ
mmft seq -m 1000 -i -w 0 asm.fa               # sequences of at least 1 kb, bare IDs, one line each
mmft dust asm.fa > dust.bed                   # low-complexity regions, as sdust finds them
mmft mask -b repeats.bed --hard asm.fa        # hard-mask repeats
mmft translate -T 5 -x cds.fa                 # invertebrate mitochondrial code, final stop dropped

A few things make it pleasant to use on genome releases:

  • Indexes are used when they exist. If there is a fresh .fai (and .gzi for a .fa.gz), len, names, num and n50 read only the index, and extract seeks straight to the bytes it needs, even inside a compressed file. mmft faidx writes samtools-compatible indexes, quickly.
  • Output named .gz is written as BGZF, so it can be indexed straight away. Converting a plain-gzip NCBI download into something indexable is one line:

    zcat ncbi.fna.gz | mmft seq -o asm.fa.gz && mmft faidx asm.fa.gz
    
  • FASTQ in gives FASTQ out from the record commands (grep, head, seq, sample, sort, dedup, extract, mask and others), with qualities following any cuts and reverse complements. mmft seq -a writes FASTA.
  • Threads by default. -j sets the number of threads. It defaults to the LSF job’s allocation on a cluster, otherwise up to 8.

A worked example: organelle genomes

Organelle genomes were the reason for several of the newer commands, because they are circular and annotated in GenBank files. Here is the Arabidopsis thaliana plastome from RefSeq (NC_000932.1.gb), alongside the mitochondrial and plastid contigs from a Darwin Tree of Life assembly in our organelle atlas.

GenBank files are read as FASTA, so every command works on them directly:

$ mmft len ath.mito.fa ath.pltd.fa NC_000932.1.gb
ath.mito.fa     ctg000001c      368828
ath.pltd.fa     ctg000001c      154475
NC_000932.1.gb  NC_000932.1     154478

features pulls annotated genes out of a GenBank file, including joins, complements and genes split across the genome. -l lists them as a table (I’ve cut off the ID and product columns here). rps12 is trans-spliced in plastids, and its two copies show up with their joined locations:

$ mmft features -t CDS -g rps12 -l NC_000932.1.gb | cut -f2-8
name   type  start  end     strand  length  location
rps12  CDS   69611  98793   -       372     complement(join(97999..98024,98562..98793,69611..69724))
rps12  CDS   69611  140650  .       372     join(complement(69611..69724),139856..140087,140625..140650)

$ mmft features -t CDS -g rps12 --translate NC_000932.1.gb | head -2
>NC_000932.1_rps12 CDS complement(join(97999..98024,98562..98793,69611..69724))
MPTIKQLIRNTRQPIRNVTKSPALRGCPQRRGTCTRVYTITPKKPNSALRKVARVRLTSG

-p takes the protein from the /translation qualifier instead, and mmft features -p -g rbcL refs/*.gb collects one gene’s proteins from a whole folder of GenBank files.

Circular genomes have no natural start, and an assembler’s start is arbitrary. rotate moves the start of a sequence to a position or a motif, on either strand. To start the DToL plastome where RefSeq does, take RefSeq’s first 30 bases and rotate to them; -t reports what moved:

$ start=$(mmft extract NC_000932.1.gb NC_000932.1:1-30 | tail -1)
$ mmft rotate -m $start -b -t ath.pltd.fa -o rotated.fa
ctg000001c      76      +       154475

Regions can run across the origin with -c, written either as START-END with the start after the end, or with the end past the length (the GFF convention):

$ mmft extract -c ath.mito.fa ctg000001c:368800-30
>ctg000001c:368800-30
TCCGTGGGCTAGTAAGATAACTATTGAGGTGAGGGTCCGAAGGAGATCTTTCACTATTT

and locate --circular finds motif hits that span it. locate handles IUPAC codes and both strands, -c counts hits, and the default output is BED6. Given several files, the counts start with the file name (new in 0.5.1), which matters here because both atlas contigs are called ctg000001c:

$ mmft locate -p GAATTC -c ath.mito.fa ath.pltd.fa
ath.mito.fa     ctg000001c      GAATTC  139
ath.pltd.fa     ctg000001c      GAATTC  104

How fast is it?

These numbers are from the README, measured with the input page-cached on an 8-thread share of a node on our farm. In summary:

compared with task mmft is
seqkit 2.10 reports on a 600 Mb genome (lengths, stats) 24–100× faster
samtools 1.21 building .fai / .gzi 15–30× faster
seqtk 1.4 reports / transforms on the same genome 16–100× / 3–12× faster
FasterFASTA 22 FASTA and FASTQ tasks faster on all 22: 1.2–20×, typically 2×
needletail 0.6 reading records from Rust 1.1–3× on one thread, 6–34× on eight

In more detail, on a 600 Mb fish genome (fMasArm1, 123 sequences):

task mmft 0.5 mmft 0.3.2 other tool compared with
lengths, plain FASTA 0.03 s 0.96 s 0.72 s seqkit fx2tab -nl
assembly stats, plain FASTA 0.04 s 4.2 s 4.1 s seqkit stats -a
assembly stats, BGZF 0.14 s no gzip 8.9 s seqkit stats -a -j 4
build .fai, plain FASTA 0.05 s   1.5 s samtools faidx
build .fai + .gzi, BGZF 0.17 s   2.5 s samtools faidx
8,720 regions from BGZF 0.43 s   0.66 s samtools faidx -r
reverse complement 0.18 s 1.56 s 2.45 s seqkit seq -rp
low-complexity regions 5.5 s   95 s, 126 s sdust, dustmasker
soft-mask 845k BED regions 0.81 s   3.9 s bedtools maskfasta -soft

The 0.3.2 column is the previous design, which used noodles for parsing; 0.4 replaced it with mmft’s own parser and I/O.

Some numbers on bigger and messier inputs:

  • A 14 Gb genome, stored as a 4.5 GB BGZF release on Lustre (Adonis annua, dmAdoAnnu1): mmft -j 8 stats takes 4.9 s using 118 MB of memory. With its index, mmft len takes 0.05 s, and extracting 1 Mb from the middle 0.1 s.
  • 8 million 150 bp reads (a 2.6 GB FASTQ): stats takes 0.8 s, FASTQ to FASTA 0.32 s, grep -f with 200,000 IDs 1.5 s, and dust 7.9 s (sdust: 139 s).
  • Against FasterFASTA on 1.18M E. coli reads, 8M short reads, the genome and Swiss-Prot, both tools gave the same output, and mmft was faster on all 22 tasks, on one thread as well as eight. dedup used 114–168 MB of memory where FasterFASTA used 0.7–1.1 GB.
  • A big thread allocation no longer costs memory. Since 0.5.1 the BGZF reader behind the record commands inflates on at most 8 threads and keeps at most two ~2 MiB jobs per thread, so seq -j 32 on a 600 Mb .fa.gz peaks at 77 MB instead of 333 MB, at the same speed.

Two caveats. These are timings with the file already in memory, so on a slow disk or a busy network filesystem, reading the file will often dominate. And plain (non-BGZF) gzip is decompressed on one thread, at about the speed of pigz -dc: around 2 s for a 600 Mb genome. BGZF files, which is what bgzip and the Tree of Life and INSDC releases use, are decompressed on all threads.

Where the speed comes from

Briefly, for the curious:

  • Every whole-file command uses every thread. Reports cut the input into chunks at arbitrary bytes, summarise each chunk independently and stitch the summaries together; for BGZF this happens in the same threads that decompress the blocks. Transforms cut records into 1 MiB pieces, so even a single chromosome is spread over all cores, and the pieces are written back in order.
  • No copying. The parser finds records with SIMD and hands out slices of its buffer, or of the memory-mapped file, rather than copying them.
  • Cache-sized work. Sequence is processed in 64 KiB pieces that stay in cache from reading to writing, and newline stripping, base counting, reverse complement and motif matching use AVX2 kernels.

The library

The parsers are a Rust crate too, without any of the command-line dependencies:

[dependencies]
mmft = { version = "0.5.1", default-features = false }

Reading records in order, from plain, gzipped or BGZF FASTA/FASTQ:

let mut r = mmft::open("reads.fq.gz")?;
while let Some(rec) = r.next()? {
    let id = String::from_utf8_lossy(rec.id());
    let gc = rec.bases().iter().filter(|&&b| b == b'G' || b == b'C').count();
    println!("{id}\t{}\t{gc}", rec.seq_len());
}

rec.bases() lends the sequence without line breaks, copying only when it has to. BGZF is inflated on up to 8 threads, with about 6 MiB of read-ahead each; mmft::open_with_threads(path, 1) (new in 0.5.1) uses one, which suits a program that already runs a reader per core.

To work on reads in parallel, fastq_map runs a function on every read across threads and hands the results back in input order:

let mut r = mmft::open("reads.fq.gz")?;
let (mut bases, mut good) = (0, 0);
mmft::fastq_map(
    &mut r,
    8,
    // on all threads: each read's length, and whether every base is Q20+
    |rec| (rec.seq.len(), rec.qual.iter().all(|&q| q >= b'5')),
    // back on this thread, in input order
    |(len, ok)| {
        bases += len;
        good += ok as usize;
        Ok(())
    },
)?;

There is also mmft::scan for per-record summaries on all threads, Indexed for region reads through .fai/.gzi, Out for writing FASTA, FASTQ or BGZF, and the sequence kernels (reverse complement, translation, DUST). Against needletail 0.6, counting G and C in every record:

input needletail Reader fastq_map, 8 threads
FASTQ, 1.18M E. coli reads 0.45 s 0.42 s 0.07 s
FASTQ, 8M reads (2.6 GB) 1.92 s 1.79 s 0.28 s
the same, BGZF 6.06 s 1.97 s 0.66 s
FASTA, 600 Mb genome 1.15 s 0.88 s  

Everything is documented, with tested examples, on docs.rs.

Conventions worth knowing

A few choices differ from other tools, so they are worth knowing before you compare outputs:

  • GC% is (G+C)/(A+C+G+T), so Ns don’t pull it down. seqkit divides by the full length instead, so its values for gappy assemblies are lower.
  • Regions are 1-based and inclusive (chr1:1-1000), as in samtools. BED in and out is 0-based and half-open.
  • IDs are the header up to the first space or tab.
  • FASTQ must be 4-line FASTQ (one line of sequence, one of qualities per record), which is what sequencers write.
  • Indexes are only used if they are fresh. A .fai older than its FASTA gives a warning and is ignored.

Try it

mmft is MIT-licensed and on GitHub, where the changelog has the details of what changed in 0.4, 0.5 and 0.5.1. If it is slower than something else on one of your tasks, or gives a different answer, please open an issue. I’d like to know.

All news