An Example SLURM File: Running kb count
Let us consider a hypothetical scRNA-seq experiment with four samples, each processed using the 10X Chromium GEM-X v4 chemistry and sequenced using two sequencing lanes. We want to run kb count on each sample, and we have already generated the necessary index files using kb ref. Below is an example SLURM job script that we could use to run kb count for all four samples in a single job array. The following tutorial assumes that you have a basic understanding of SLURM and how to submit jobs, as covered in our Intro to HPC. If you are new to SLURM, we recommend reviewing that tutorial before proceeding with this example.
We will be working under the assumption that our files are organized within the following working tree:
/resnick/groups/<groupname>
├── <username>
| ├── my_project
| | ├── fastqs ## Contains the FASTQ files
| | ├── index ## Contains the pre-built kallisto index and t2g file
├── index.idx ## The pre-built kallisto index file generated by `kb ref`
├── t2g.txt ## The transcript-to-gene mapping file generated by `kb ref`
| | ├── slurm ## Contains your job scripts
| | | ├── job.sbatch ## The job script to be submitted
| | | ├── logs ## To store SLURM logs
| | └── kb_out ## To store kb-python outputs
|In this example:
- The
fastqs/directory contains raw sequencing files. - The
index/directory contains the pre-built kallisto index and transcript-to-gene mapping file. - The
slurm/directory contains your job submission scripts. - The
slurm/logs/directory stores standard output and error logs generated by SLURM. - The
kb_out/directory stores the results generated bykb count.
It is generally good practice to organize your files by project and store your SLURM files within the project directory.
Example SLURM File
#!/bin/bash
#SBATCH --job-name=kb_example
#SBATCH --account=<groupname>
#SBATCH -p expansion
#SBATCH --time=06:00:00
#SBATCH --cpus-per-task=8
#SBATCH --mem=20G
#SBATCH --array=0-3%2
#SBATCH --chdir=/resnick/groups/<groupname>/<username>/my_project
#SBATCH --output=slurm/logs/%x_%A_%a.log
#SBATCH --error=slurm/logs/%x_%A_%a.err
set -euo pipefail
# ---- software environment ----
# Adjust this path if your conda install lives elsewhere
source ~/miniconda3/etc/profile.d/conda.sh
conda activate kb-python
# ---- project directories ----
FASTQ_DIR="fastqs"
OUT_DIR="kb_out"
INDEX_DIR="index"
IDX="${INDEX_DIR}/index.idx"
T2G="${INDEX_DIR}/t2g.txt"
# ---- sample list for the job array ----
SAMPLES=( "S1" "S2" "S3" "S4" )
SAMPLE="${SAMPLES[$SLURM_ARRAY_TASK_ID]}"
# ---- FASTQ inputs (example: two lanes) ----
L1R1="${FASTQ_DIR}/${SAMPLE}_L001_R1_001.fastq.gz"
L1R2="${FASTQ_DIR}/${SAMPLE}_L001_R2_001.fastq.gz"
L2R1="${FASTQ_DIR}/${SAMPLE}_L002_R1_001.fastq.gz"
L2R2="${FASTQ_DIR}/${SAMPLE}_L002_R2_001.fastq.gz"
KB_OUT_DIR="${OUT_DIR}/${SAMPLE}"
mkdir -p "${KB_OUT_DIR}"
# ---- check that all input files are present ----
for f in "${L1R1}" "${L1R2}" "${L2R1}" "${L2R2}" "${IDX}" "${T2G}"; do
[[ -s "$f" ]] || { echo "Missing file: $f" >&2; exit 1; }
done
# ---- run kb-python ----
srun kb count \
-i "${IDX}" \
-g "${T2G}" \
-x 10XV4 \
-o "${KB_OUT_DIR}" \
--h5ad \
-t "${SLURM_CPUS_PER_TASK}" \
"${L1R1}" "${L1R2}" "${L2R1}" "${L2R2}"Resource Request Summary
This SLURM script requests:
- 8 CPU cores
- 20 GB of RAM
- A maximum runtime of 6 hours
- Execution on the expansion partition
- Charges to the
account - Execution within the
my_projectdirectory - Logging of standard output and error to the
slurm/logs/directory named according to the job name%x, job ID%A, and array index%a.
We also use the directive:
#SBATCH --array=0-3%2This line tells SLURM to create four copies of the job (indexed from 0 to 3) in what is called a “job array”. A job array allows us to run the same script for multiple samples without having to create separate job files for each sample. Note that each job in the array will be allocated its own set of resources (8 CPUs, 20 GB RAM, etc.) as specified in the script. We can access the index of the current job in the array using the environment variable SLURM_ARRAY_TASK_ID, which we use to select the appropriate sample from our SAMPLES array.
We use the %2 part to limit the number of concurrent jobs to 2, which means that at most 2 jobs from the array will run at the same time. This can help manage resource usage and prevent overloading the system. For kb count, which can be resource-intensive, we don’t recommend running more than 2–4 jobs concurrently.
Why These Resources? The resource requests in this script are based on the expected computational requirements of running kb count on a single sample. The kb count command can be computationally intensive, particularly for large datasets, and benefits from parallel processing.
We request 8 CPU cores so that kb count can take advantage of multithreading, which can significantly reduce runtime. In practice, using 8–16 threads is often sufficient for most single-cell datasets.
The 20 GB memory allocation is a conservative estimate intended to ensure the job has enough RAM. In many cases, kb count uses approximately 5–10 GB of memory for a single sample, but requesting additional memory provides a safety margin and accounts for system overhead.
The 6-hour runtime is also a conservative estimate based on typical runtimes for similar datasets. Actual runtime depends on factors such as input size, sequencing depth, and cluster performance.
When requesting resources, it is important to strike a balance. Underestimating resource needs can cause jobs to fail, while overestimating them may increase queue wait times and reduce overall cluster efficiency. As you gain experience, you can use tools such as sacct to review the resource usage of completed jobs and adjust your requests accordingly.
Script Set-Up
Before running the analysis itself, the script includes a small setup section designed to make the workflow more robust and reproducible. These steps are not strictly required, but they are strongly recommended for reliable HPC usage.
First, we use set -euo pipefail to ensure that the script exits immediately if any command fails, if we try to use an undefined variable, or if any part of a pipeline fails. This helps catch errors early and prevents the script from continuing in an inconsistent state.
Next, we activate the software environment:
# Direct the script to the location of your conda installation. Adjust this path if your conda install lives elsewhere.
source ~/miniconda3/etc/profile.d/conda.sh
# Activate your environment containing kb-python. Here we assume the environment is named "kb-python".
conda activate kb-pythonThis loads the Conda environment containing kb-python and its dependencies. Using Conda environments is considered best practice because it ensures that:
- the correct software versions are used
- dependencies are consistent
- analyses remain reproducible
- scripts behave the same across systems
Without an explicitly defined environment, differences in software versions or system configurations can lead to unexpected behavior or inconsistent results. If you are unfamiliar with Conda, see our Conda Tutorial.
Altogether, these setup steps help ensure that your SLURM job runs smoothly and produces reliable results.
Defining Path Variables
Now we arrive at the section of the script where we define variables for our file paths. This is a critical step in writing HPC scripts, as it promotes readability and makes it easier to modify the script if your project structure changes.
FASTQ_DIR="fastqs" ## Directory containing FASTQ files
OUT_DIR="kb_out" ## Directory to store kb-python outputs
INDEX_DIR="index" ## Directory containing the pre-built index and t2g files
IDX="${INDEX_DIR}/index.idx" ## Path to the pre-built kallisto index file
T2G="${INDEX_DIR}/t2g.txt" ## Path to the transcript-to-gene mapping file The script then defines the sample list:
SAMPLES=( "S1" "S2" "S3" "S4" )
SAMPLE="${SAMPLES[$SLURM_ARRAY_TASK_ID]}"Here, the Bash array provides the mapping between SLURM’s job array index and your sample names. Each job selects a single sample based on its assigned array index, allowing the same script to process multiple datasets automatically.
Based on the sample name, we then define the paths to the corresponding FASTQ files for each sample. In this example, we assume that each sample has two sequencing lanes, resulting in four FASTQ files per sample (R1 and R2 for each lane):
L1R1="${FASTQ_DIR}/${SAMPLE}_L001_R1_001.fastq.gz"
L1R2="${FASTQ_DIR}/${SAMPLE}_L001_R2_001.fastq.gz"
L2R1="${FASTQ_DIR}/${SAMPLE}_L002_R1_001.fastq.gz"
L2R2="${FASTQ_DIR}/${SAMPLE}_L002_R2_001.fastq.gz"Creating Output Directories
Once the core directories are defined, the script constructs the expected FASTQ filenames and prepares the output directory:
KB_OUT_DIR="${OUT_DIR}/${SAMPLE}"
mkdir -p "${KB_OUT_DIR}"Creating output directories explicitly is good practice because it prevents jobs from failing due to missing folders. The -p option ensures the command succeeds even if the directory already exists.
Checking Input Files
Before launching a computationally expensive job, the script verifies that all required files are present:
for f in "${L1R1}" "${L1R2}" "${L2R1}" "${L2R2}" "${IDX}" "${T2G}"; do
[[ -s "$f" ]] || { echo "Missing file: $f" >&2; exit 1; }
doneThis is an important safeguard. HPC jobs can run for hours, and a simple typo in a file path can otherwise lead to wasted compute time or incomplete results. By checking that each file exists and is non-empty, the script fails immediately if something is wrong.
Running kb count
The final line executes the analysis:
srun kb count ...The srun command launches the program within the resources allocated by SLURM. The arguments passed to kb count specify the index, transcript-to-gene mapping, sequencing technology, and output directory.
Notably, the script uses:
-t "${SLURM_CPUS_PER_TASK}"
This ensures that kb-python uses exactly the number of threads requested in the SLURM header. This pattern makes the script flexible — if you change the resource request, the program automatically adapts.
Adapting This Script for Your Own Data
Although this example uses a specific directory structure, dataset, and analysis tool, the general principles can be applied to any HPC job script. When adapting this script for your own data, consider the following:
- Resource Requests: Adjust the CPU, memory, and time requests based on the expected requirements of your analysis. Use past job data to inform these decisions with tools like
sacct. - Directory Structure: Define path variables at the top of your script to reflect your project’s organization. This makes it easier to modify paths if your structure changes.
- Sample List: Use a Bash array to manage your sample names and link them to SLURM array indices. This allows you to run the same script for multiple samples without duplication.
- Input Validation: Always include checks to ensure that required files are present before running the analysis. This can save time and prevent errors.
Though optional, we also recommend setting up a software environment and activating it within your script. This promotes reproducibility and ensures that your analysis runs with the correct software versions and dependencies.