BamTools provides both a programmer's API and an end-user's toolkit for handling BAM files.

Documentation

BamTools home
BamTools wiki
Original journal article

Slurm script

#!/bin/bash
#SBATCH --job-name=BamTools
#SBATCH --time=1:00:00
#SBATCH --partition=norm
#SBATCH --ntasks=1
#SBATCH --mem=32GB

module load bamtools
bamtools convert -format fastq -in in.bam -out out.fastq

Build instructions for those who are curious

git clone https://github.com/pezmaster31/bamtools.git
mkdir -p bamtools/build; cd bamtools/build
cmake -DCMAKE_INSTALL_PREFIX=/mnt/nasapps/production/bamtools/2.5.2 ..
make && make install