Kraken2: Multi-Sample SLURM Script

The following example runs Kraken2 on multiple samples sequentially within one SLURM job. It supports paired-end and single-end reads, generates a report and classification output for each sample, and skips samples already marked as completed. The –memory-mapping option allows the operating system to reuse cached database pages across invocations1).

Input manifest

The script expects a tab-separated manifest.tsv with a header and three columns:

  • sample — unique sample identifier.
  • r1 — path to the first (or only) gzipped FASTQ file.
  • r2 — path to the second FASTQ file for paired-end reads; leave empty for single-end reads.

Absolute file paths are recommended. For example (columns must be separated by tabs, not spaces):

sample	r1	r2
S01	/data/fastq/S01_R1.fastq.gz	/data/fastq/S01_R2.fastq.gz
S02	/data/fastq/S02.fastq.gz	

Databases

Prebuilt Kraken2 databases are available from Ben Langmead's Index Zone, including Standard K2 and PrackenDB.

  • K2 Standard: General-purpose database containing RefSeq bacteria, archaea, viruses, plasmids, the human genome, and UniVec Core.
  • PrackenDB: Larger database2) (>400 GB unpacked locally), containing reference assemblies of bacteria, archaea, protists, fungi, viruses, and the human genome. Unlike Standard K2, it generally retains only one representative genome per species.

Available on ABI's server (as of 13/11/2026):

Database Version Server path
K2 Standard 2026-02-26 /mnt/nas1/db/kraken/k2_standard_20260226/
PrackenDB 2026-07-08 (local directory) /mnt/nas1/db/kraken/pracken_20260708/

Specify the database directory using Kraken2's –db argument.

Example SLURM script

Save as kraken2.sh. Adjust the database paths, SLURM memory/CPU requests, and output paths to your environment. Use a different output directory for each database, because completion markers are keyed by sample name, not database.

#!/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

Submit the job

Create the log directory before submitting, since SLURM needs it for the job log:

mkdir -p log
sbatch kraken2.sh --manifest data/meta/manifest.tsv --db pracken --out res_pracken

To use the standard K2 database, specify –db k2 and a separate –out directory (for example, res_k2). To use a custom database path, provide –kraken-db /path/to/database.

2)
PrackenDB's larger taxonomic coverage and species-level representation may be advantageous for certain analyses, but the choice of database depends on the research objective.