Samtools is a toolkit for processing high-throughput sequencing data and is widely used in bioinformatics. This article introduces how to build a Samtools container with Singularity, so that Samtools can be used across systems and users while maintaining a consistent bioinformatics analysis environment.

The base Singularity environment used in this article is Ubuntu 22.04 LTS. Although Singularity can also build containers directly from yml or def files, I prefer sandbox mode for creating new containers. Sandbox mode is easier to operate and debug, and it makes it easier to solve problems encountered during installation. The installation workflow inside a sandbox is also similar to installing software on a normal Linux system, which reduces unnecessary learning cost. For users who only occasionally build new containers, sandbox mode is often a more cost-effective choice.

1. First, download a Singularity base container, or download a preferred base container from the official library:

Bash
# Pull the base Ubuntu container
singularity pull library://library/default/ubuntu

2. Generate a sandbox from the base container:

Bash
# Build the sandbox container
singularity build --sandbox samtools/ ubuntu_latest.sif

3. Create an installation script for Samtools. Here it is named SamtoolsInstall.sh. This article uses source compilation; Samtools can also be installed directly with Conda:

Bash
set -e
# Update apt
apt update

# Install Samtools dependencies
apt-get install -y zlib1g-dev
apt-get install -y libbz2-dev
apt install -y liblzma-dev
apt-get install -y libncurses5-dev

# Install tools
apt install -y wget
apt install -y bzip2
apt-get install -y gcc
apt-get install -y make

# Download Samtools
wget https://github.com/samtools/samtools/releases/download/1.19/samtools-1.19.tar.bz2
# Extract Samtools to /opt; /opt can store third-party software
tar -jxvf samtools-1.19.tar.bz2 -C /opt
# Enter the Samtools directory
cd /opt/samtools-1.19
# Generate the Makefile. configure checks libraries, headers, dependencies, and other required information before generating a Makefile
./configure --prefix=/opt/samtools-1.19
# Compile
make
# Install
make install
# Add environment variables to the Singularity container
echo 'export PATH="/opt/samtools-1.19/bin:$PATH"' >> /.singularity.d/env/10-docker2singularity.sh

# Remove unnecessary packages and cache files to reduce the Singularity container size
apt remove -y wget
apt remove -y bzip2
apt-get remove -y gcc
apt-get remove -y make
apt-get autoremove -y
rm -rf /var/lib/apt/lists/*

For Conda users, installing Samtools with Conda is often easier. Here, source compilation is used because we are building a Singularity container and want to reduce the final image size as much as possible. The commands above also work for compiling and installing Samtools on Ubuntu, but in that case the final commands that remove software and cached files should not be executed. Also, add Samtools to the environment variables according to your own system configuration.

4. Call SamtoolsInstall.sh inside the sandbox:

Bash
# Install Samtools inside the sandbox
singularity exec --writable samtools/ sh SamtoolsInstall.sh
# Test Samtools
singularity exec samtools/ samtools --help

You can also enter the sandbox with shell mode, so that you can operate inside the sandbox as if it were a Linux system:

Bash
# Enter the sandbox
singularity shell --writable samtools/

5. Package the sandbox into a static SIF file:

Bash
# Generate a static SIF file
singularity build samtools.sif samtools/
# Test Samtools
singularity exec samtools.sif samtools --help

At this point, the Samtools container has been created. You can execute Samtools with the static SIF file, or share the SIF file wherever it is needed.