This is an old revision of the document!
Table of Contents
Kraken2: Efficient Multi-Sample Classification
Problem and solution
By default, kraken2 loads its database into process-local RAM for every invocation. Repeating this for many samples adds substantial startup time, especially with Pracken (>400 GB) rather than a smaller standard K2 database.
Use –memory-mapping to access the database through file-backed memory mapping instead of copying the entire index into each process's RAM. The operating system can reuse cached database pages between samples, reducing repeated loading overhead.
Note: This does not keep a Kraken2 process or mapping alive between runs. Benefits depend on cache availability and disk speed. On slow or network storage, random disk I/O may make classification slower.
References:
- Official Kraken2 manual — explains what
–memory-mappingdoes. - GitHub issue #719 — discusses the ability to load db only once for multiple samples.
- GitHub issue #451 — discusses memory mapping and I/O performance.
Input manifest
The script reads a headered, tab-separated manifest.tsv with three columns: sample, r1, and r2. These specify a sample identifier and paths to gzipped FASTQ files. Leave r2 empty for single-end reads. Absolute paths are recommended.
sample r1 r2 S01 /data/fastq/S01_R1.fastq.gz /data/fastq/S01_R2.fastq.gz S02 /data/fastq/S02.fastq.gz
Example SLURM script
The following script processes samples sequentially, supports single- and paired-end reads, writes per-sample outputs and reports, and skips completed samples.
Adjust database paths and SLURM resources for your environment. Use separate output directories for different databases, since completion markers are keyed only by sample ID.
#!/usr/bin/env bash #SBATCH --job-name=kraken2 #SBATCH --output=log/kraken2_%j.log #SBATCH --mem=450G #SBATCH --cpus-per-task=8 set -euo pipefail PRACKEN_DB="/mnt/nas1/db/kraken/pracken_20260708/" K2_DB="/mnt/nas1/db/kraken/k2_standard_20260226/" MANIFEST="data/meta/manifest.tsv" DB_NAME="pracken" KRAKEN_DB_OVERRIDE="" THREADS=8 OUT="res_pracken" while [[ $# -gt 0 ]]; do case "$1" in --manifest) MANIFEST="$2"; shift 2 ;; --db) DB_NAME="$2"; shift 2 ;; --kraken-db) KRAKEN_DB_OVERRIDE="$2"; shift 2 ;; --threads) THREADS="$2"; shift 2 ;; --out) OUT="$2"; shift 2 ;; *) echo "Unknown arg: $1" >&2; exit 1 ;; esac done case "$DB_NAME" in pracken) KRAKEN_DB="$PRACKEN_DB" ;; k2) KRAKEN_DB="$K2_DB" ;; *) echo "Unknown --db '$DB_NAME'" >&2; exit 1 ;; esac [[ -n "$KRAKEN_DB_OVERRIDE" ]] && KRAKEN_DB="$KRAKEN_DB_OVERRIDE" echo "Starting Kraken2 batch run with THREADS=$THREADS on MANIFEST=$MANIFEST DB=$KRAKEN_DB" mkdir -p log awk 'NR>1' "$MANIFEST" | while IFS=$'\t' read -r sample r1 r2; do [[ -n "${sample:-}" && -n "${r1:-}" ]] || { echo "Bad manifest row for sample '$sample'" continue } rep="$OUT/kraken2/reports/${sample}_report.txt" out="$OUT/kraken2/outputs/${sample}_output.txt" doneflag="$OUT/kraken2/.done/${sample}" if [[ -f "$doneflag" && -s "$rep" ]]; then echo "Skip $sample (done)" continue fi if [[ ! -f "$r1" ]]; then echo "ERROR: r1 not found for $sample: $r1" >&2 continue fi if [[ -n "${r2:-}" && ! -f "$r2" ]]; then echo "ERROR: r2 listed but not found for $sample: $r2" >&2 continue fi mkdir -p "$(dirname "$rep")" "$(dirname "$out")" "$(dirname "$doneflag")" # File-backed mapping allows OS cache reuse across runs. if [[ -n "${r2:-}" && -f "$r2" ]]; then echo "$sample: Kraken2 (paired)" kraken2 --db "$KRAKEN_DB" --confidence 0.1 --use-names --memory-mapping --paired \ --gzip-compressed --threads "$THREADS" \ --report "$rep" --output "$out" "$r1" "$r2" else echo "$sample: Kraken2 (single)" kraken2 --db "$KRAKEN_DB" --confidence 0.1 --use-names --memory-mapping \ --gzip-compressed --threads "$THREADS" \ --report "$rep" --output "$out" "$r1" fi touch "$doneflag" done
Run with:
mkdir -p log sbatch kraken2.sh --manifest data/meta/manifest.tsv --db pracken --out res_pracken
