💬 In this tutorial we get a bit deeper with using Singularity containers.
To run these exercises you need to container image tutorial.sif
. If you did not
already download it in the previous tutorial, download it now:
wget https://a3s.fi/saren-2001659-pub/tutorial.sif
💬 Singularity containers have their own internal file system that is separate from the host file system.
💭 In most real-world use cases you probably want to access the host file system to read and write files.
💡 This is done with command line argument --bind
(or -B
). The basic syntax is
--bind /path/in/host:/path/inside/container
.
💭 Some remarks:
➖ The bind path does not need to exist inside the container – it is created if necessary.
➖ More than one bind pair can be specified.
➖ The option is available for all the run methods described in the previous tutorial.
sinteractive
:
sinteractive --account project_xxxx # Change the xxxx for the project number
bind
:
export SCRATCH=/scratch/project_xxxx/yourcscusername # Replace xxxx and yourcscusername
singularity exec tutorial.sif ls $SCRATCH
The container can not see the host directory, so you will get a “No such file or directory” error.
/scratch
to directory /scratch
inside the container:
singularity exec --bind $SCRATCH:/scratch tutorial.sif ls /scratch
singularity exec --bind /scratch:/scratch tutorial.sif ls $SCRATCH
/scratch
.💡 You can use bind
to set the container, for example, to find input data or configuration files from a certain directory.
$SCRATCH
into directory /input
:
singularity exec --bind $SCRATCH:/input tutorial.sif ls /input
singularity_wrapper
, it will take care of the binds for most common use cases:
singularity_wrapper exec tutorial.sif ls $SCRATCH
$SING_IMAGE
is set, you don’t even need to provide path to the image file:
export SING_IMAGE=$PWD/tutorial.sif
singularity_wrapper exec ls $SCRATCH
‼️ Since some modules set $SING_IMAGE
when loaded, it is a good idea to start with module purge
or unset SING_IMAGE
if you plan to use it, to make sure correct image is used.
💬 Some software may reguire some environment variables to be set, e.g. to point to some reference data or a configuration file.
💬 Most environment variables set on the host are inherited by the container.
--cleanenv
host environment is not inherited by the container.💬 To set an environment variable specifically inside the container, you can set an environment variable $SINGULARITYENV_xxx
(where xxx is the variable name) on the host before invoking the container.
export TEST1="value1"
export SINGULARITYENV_TEST2="value2"
env |grep TEST
singularity exec tutorial.sif env |grep TEST
singularity exec --cleanenv tutorial.sif env |grep TEST
$TEST1
and $SINGULARITYENV_TEST2
.$TEST1
(inherited from host) and $TEST2
(specifically set inside the container by setting $SINGULARITYENV_TEST2
on host).$TEST2
. singularity exec tutorial.sif echo $TEST1
singularity exec tutorial.sif echo $TEST2
$TEST2
has not been set on host. (It was SINGULARITYENV_TEST2="value2"
remember?)💬 Our test container includes program hello2
, but it is not in the $PATH
.
find
inside the container:
singularity exec tutorial.sif find / -type f -name "hello2" 2>/dev/null
singularity exec tutorial.sif /found/me/hello2
$PATH
inside the container:
export SINGULARITYENV_PREPEND_PATH=/found/me
singularity exec tutorial.sif hello2
💡 If you can’t locate the desired binary with find
, you can always use singularity shell
to explore the container.
💡 Singularity exec
is the run method you will typically use in a batch job script.
test.sh
:
module load nano # The computing node does not have nano by default
nano test.sh
Copy the following contents into the file and change “project_xxxx” to the correct project name:
#!/bin/bash
#SBATCH --job-name=test
#SBATCH --account=project_xxxx
#SBATCH --partition=test
#SBATCH --time=00:01:00
#SBATCH --mem=1G
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
singularity exec tutorial.sif hello_world
💬 In nano you can use ctrl + o
to save and ctrl + x
to exit.
sbatch test.sh
Some software available in CSC has bee installed as containers. In this example we will run one such application.
wget https://a3s.fi/containers-workflows/data.tar.gz
tar xf data.tar.gz
This should get you two FASTQ files: B1_sub_R1.fq
and B1_sub_R2.fq
cutadapt.sh
:
nano cutadapt.sh
Copy the following contents into the file and change “project_xxxx” to the correct project name:
#!/bin/bash
#SBATCH --job-name=test
#SBATCH --account=project_xxxx
#SBATCH --partition=test
#SBATCH --time=00:10:00
#SBATCH --mem=1G
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
# Load the software module
module load cutadapt/3.4
# The module sets $SING_IMAGE, so we can omit image file in command
singularity_wrapper exec cutadapt \
-a ^GTGCCAGCMGCCGCGGTAA...ATTAGAWACCCBDGTAGTCC \
-A ^GGACTACHVGGGTWTCTAAT...TTACCGCGGCKGCTGGCAC \
-m 215 \
-M 285 \
--discard-untrimmed \
-o B1_sub_R1_trimmed.fq \
-p B1_sub_R2_trimmed.fq \
B1_sub_R1.fq B1_sub_R2.fq
💬 Note that in the above example the command line is quite long, so we have used escape charecter “\” to escape (i.e. ignore) new line characters. From the computer’s point of view the whole singularity_wrapper command is just a single line.
💬 This way of splitting the command can improve readability and make it easier to edit parameters, but one has to be careful with the notation, or some of the command may be omitted. Make sure “\” precedes the newline directly. If there is a e.g. a space between the “\” and the newline, the space gets escaped, not the newline, and the line gets cut.
sbatch cutadapt.sh
In this case the module also includes a wrapper script that allows us to run the program with just cutadapt
.
Modify the batch job script so that you change singularity_wrapper exec cutadapt
to cutadapt
and re-submit.
Does it still work?
You can take a look at the cutadapt wrapper script to see how it’s done.
First find out the path to the script:
which cutadapt
You can then take a look at it using the path:
less /appl/soft/bio/cutadapt/bin/cutadapt
💡 For more information on batch jobs, please see CSC Docs pages.
💬 This tutorial is meant as a brief introduction to get you started.
☝🏻 When searching the internet for instruction, pay attention that the instructions are for the same version of Singularity that you are using. There has been some command syntax changes etc. between the versions, so older instructions may not work with copy-paste.
💡 For authoritative instructions see Singularity documentation.