Lesson 2: Useful Unix & Running Jobs on Biowulf
Learning Objectives
- Use pipes, grep, and text utilities to process files.
- Load software modules.
- Run interactive sessions with
sinteractive. - Write and submit a batch job with
sbatch.
Part 1 — Powerful Unix Tools
Combining commands with pipes |
The pipe (|) sends the output of one command as the input to the next. This lets you chain tools together into a mini-pipeline.
cat sample.fastq | head -8 # view just the first 8 lines of a FASTQ file
ls -l | wc -l # count the number of items in a directory
Redirecting output
ls -l > filelist.txt # write output to a file (overwrite)
ls -l >> filelist.txt # append output to an existing file
Check-your-learning
What is the difference between > and >>? What happens if you use > on a file that already exists?
Searching files with grep
grep searches a file for lines matching a pattern:
grep "ATCG" sample.fastq # find lines containing "ATCG"
grep -c "ATCG" sample.fastq # count matching lines
grep -i "atcg" sample.fastq # case-insensitive search
grep -v "^@" sample.fastq # show lines that do NOT start with @
In FASTQ files, sequence identifier lines start with @. Using grep to find or exclude these is a common bioinformatics task.
Check-your-learning
In a FASTQ file, every 4th line starting from line 1 is a sequence header (@). Write a grep command to count only header lines.
sort and uniq
sort filelist.txt # sort lines alphabetically
sort -k5 -n filelist.txt # sort by column 5 numerically
sort filelist.txt | uniq # remove duplicate lines
sort filelist.txt | uniq -c # count occurrences of each unique line
Part 2 — The Module System
Biowulf has 600+ pre-installed programs. You load them with module:
module avail # list all available software (long!)
module avail sratoolkit # search for a specific tool
module load sratoolkit # load the tool into your environment
module list # see what you have loaded
module unload sratoolkit # unload a module
Why modules?
Different analyses require different software versions. The module system lets you load exactly what you need without conflicts.
Check-your-learning
Type module avail samtools. What versions are available? How would you load a specific version?
Part 3 — Running Jobs on Biowulf
On a shared HPC system, you cannot simply run heavy programs on the login node. You must submit jobs through the Slurm scheduler (Simple Linux Utility for Resource Management). Slurm manages the queue and allocates resources fairly.
Three ways to run work on Biowulf
| Method | Command | Best for |
|---|---|---|
| Interactive session | sinteractive |
Testing, debugging, short tasks |
| Batch job | sbatch script.sh |
Long-running analyses |
| Job array (swarm) | swarm -f commands.txt |
Many similar jobs in parallel |
Interactive sessions with sinteractive
sinteractive # default: 1 core, 1.5 GB RAM, 8 hrs
sinteractive --cpus-per-task=4 --mem=8g # request more resources
hostname # confirm you're on a compute node
# do your work...
exit # return to login node when done
Check-your-learning
Why is it important NOT to run CPU-intensive jobs on the login node?
Writing a batch script with sbatch
Batch jobs run in the background without you needing to stay connected. You write a script that tells Biowulf what resources you need and what to run.
nano myfirstjob.sh
Paste this into the file:
#!/bin/bash
#SBATCH --cpus-per-task=2
#SBATCH --mem=4g
#SBATCH --time=01:00:00
#SBATCH --partition=student
#SBATCH --job-name=my_first_job
#SBATCH --output=myfirstjob_%j.log
# Your commands go below this line
echo "Hello from Biowulf compute node!"
hostname
date
Save and exit (Ctrl+X, Y, Enter), then submit:
sbatch myfirstjob.sh
Monitoring your jobs
squeue -u $USER # list your running/pending jobs
scancel JOBID # cancel a job by its ID
cat myfirstjob_JOBID.log # view job output after it finishes
Check-your-learning
In the script above, what does #SBATCH --time=01:00:00 do? What happens to your job if it exceeds this limit?
Part 4 — Quick Practice
- Start an
sinteractivesession on the student partition. - Load the
sratoolkitmodule. - Run
fastq-dump --versionto confirm it loaded. - Exit the interactive session.
Lesson 2 — End-of-Lesson Quiz
- What does the pipe
|do? Give an example using two commands. - What is the difference between
>and>>when redirecting output? - Write a grep command to find all lines in
sample.fastqthat start with@. - What command lists all software modules currently loaded in your environment?
- What is Slurm, and why does Biowulf use it?
- What is the difference between
sinteractiveandsbatch? - In a batch script, what does the
#SBATCHline do? - After submitting a job with
sbatch, how do you check whether it is still running? - What does
--partition=studentspecify in a Slurm job script? - You submitted a job but realize you made a mistake in the script. What command cancels a running job?