| Next revision | Previous revision |
| infra:tips_and_tricks:efficient_kraken2_multi-sample_execution [2026/11/13 08:49] – created davith | infra:tips_and_tricks:efficient_kraken2_multi-sample_execution [2026/11/13 09:11] (current) – davith |
|---|
| ===== Problem and solution ===== | ===== 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. | By default, ''kraken2'' loads its database into process-local RAM for every invocation, which requires substantial amount of time. Repeating this for many samples (*mhm* especially with the better db - Pracken (>400 GB)) the time to classify samples explodes, just because of repeated db-loading from scratch. |
| |
| 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. | 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((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. |
| | )), 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. | e.g. |
| | <code bash> |
| | kraken2 \ |
| | --db /mnt/nas1/db/kraken/pracken_20260708/ \ |
| | --paired \ |
| | --gzip-compressed \ |
| | --threads 8 \ |
| | --confidence 0.1 \ |
| | --use-names \ |
| | --memory-mapping \ |
| | --report sample_report.txt \ |
| | --output sample_output.txt \ |
| | sample_R1.fastq.gz sample_R2.fastq.gz |
| | </code> |
| | |
| | Example Kraken2 Script: |
| | * [[https://wiki.abi.am/scripts:kraken2|ABI Wiki's Kraken2 page]] |
| |
| References: | References: |
| * [[https://github.com/DerrickWood/kraken2/issues/719|GitHub issue #719]] — discusses the ability to load db only once for multiple samples. | * [[https://github.com/DerrickWood/kraken2/issues/719|GitHub issue #719]] — discusses the ability to load db only once for multiple samples. |
| * [[https://github.com/DerrickWood/kraken2/issues/451|GitHub issue #451]] — discusses memory mapping and I/O performance. | * [[https://github.com/DerrickWood/kraken2/issues/451|GitHub issue #451]] — discusses memory mapping and I/O performance. |
| | * [[https://wiki.abi.am/pipelines:metagenomic_classification|ABI Wiki's own metagenomic classification pipeline]] |
| |
| ===== 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. | |
| |
| <code> | |
| sample r1 r2 | |
| S01 /data/fastq/S01_R1.fastq.gz /data/fastq/S01_R2.fastq.gz | |
| S02 /data/fastq/S02.fastq.gz | |
| </code> | |
| |
| ===== 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. | |
| |
| <code bash> | |
| #!/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 | |
| </code> | |
| |
| Run with: | |
| |
| <code bash> | |
| mkdir -p log | |
| sbatch kraken2.sh --manifest data/meta/manifest.tsv --db pracken --out res_pracken | |
| </code> | |
| |