Freesurfer on ARC

From RCSWiki
Jump to navigation Jump to search

Freesurfer is a collection of neuroimaging software that can be used to process Magnetic Resonance Imaging files like DICOMs and NIFTIs. Detailed documentation can be found on the Freesurfer website. Most simple workflows that are scalable on ARC use recon-all to perform a reconstruction of the MRI image from the collection of slices. This is done by parallelizing over scans or subjects.

Freesurfer Singularity Containers

The more recent versions of Freesurfer installed on ARC have been built as Singularity containers based off of the official public Freesurfer docker release: [1]

Currently, on ARC there are containers for several versions of Freesurfer available. The containers have integrated license files. These containers cannot be used for commercial or clinical purposes. We strongly encourage you to submit a request for your own license so that you have registered with the makers of Freesurfer: [2]


The full path to the directory with the container files is /global/software/freesurfer/containers.

To see what versions of containerized Freesurfer are available, use

$ ls -l /global/software/freesurfer/containers/

-rwxr-xr-x 1 drozmano drozmano 4358946816 Oct  3 15:32 freesurfer-7.1.1.sif
-rwxr-xr-x 1 drozmano drozmano 6402535424 Oct  3 15:31 freesurfer-7.3.2.sif
-rwxr-xr-x 1 drozmano drozmano 9342550016 Oct  3 15:31 freesurfer-7.4.1.sif

Running commands from the containers

You can run a command from the container of your choice with the command line like this:

$ apptainer exec container.sif <command> [command arguments]

For example, you want to use the container with Freesurfer 7.3.2, freesurfer-7.3.2.sif, and you want to check the version of the recon-all command. This is how you can do that:

$ apptainer exec /global/software/freesurfer/containers/freesurfer-7.3.2.sif  recon-all --version
freesurfer-linux-centos8_x86_64-7.3.2-20220804-6354275

Freesurfer Interactive Job

The main command that can be called from an interactive job is singularity shell:

[userrname@arc ~]$ salloc --partition=single --time=2:0:0 --nodes=1 --ntasks=1 --cpus-per-task=8 --mem=0
salloc: Pending job allocation 8430460
salloc: job 8430460 queued and waiting for resources
salloc: job 8430460 has been allocated resources
salloc: Granted job allocation 8430460
salloc: Waiting for resource configuration
salloc: Nodes cn056 are ready for job
[username@cn056 ~]$ CONTAINER=/global/software/freesurfer/7.1.1/containers/freesurfer711.simg
[username@cn056 ~]$ singularity shell $CONTAINER
Singularity> recon-all --version
freesurfer-linux-centos7_x86_64-7.1.1-20200723-8b40551

Once you have a prompt inside the container you can run any non-graphical command that is part of freesurfer. We do not have instructions for running freeview yet.

Freesurfer Sample Script

As MRI reconstruction is memory intensive in general and highly serial for a single scan when using Freesurfer, we encourage users to submit jobs in a non-interactive mode. This wiki page will include a simple example for running one reconstruction and a more complex example for submitting many reconstructions as a job array.

Single Scan Script

For the simple case, we will use a specification of the time, memory, and CPU requirements and leave off a partition specification. Here we are assuming that any partition on the default partition list (mostly cpu2019-like hardware as of October 2021) will suffice. This is a sample recon-all command. You can replace it with any Freesurfer command that is appropriate to your work.

#!/bin/bash
#SBATCH --time=24:0:0 
#SBATCH --nodes=1 
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=1  
#SBATCH --mem=16G

CONTAINER=/global/software/freesurfer/7.1.1/containers/freesurfer711.simg

#These paths don't need to be in your home dir
subject=subject/specific/dir/name
dicom_file=file_name_to_process.dcm
subjects=~/path/to/parent/subjects/dir
subject=~/path/to/output/dir

singularity exec -B $output_dir -B $subjects_dir $CONTAINER recon-all -cw256 -subject ${subjects_dir}/${subject} -i ${subjects_dir}/${subject}/${dicom_file} -all -sd ${output_dir}/

Multiple Scans Reconstruction in Parallel with a Job Array

This script supposes that you have a fixed output directory, a fixed parent directory where the source data lives, a file consisting of a list of child directory names to be processed, and that the same file name should be passed as input for each child directory. The specifics of your research data management scheme will be particular to your project and the corresponding changes that will be required will depend on how you have organized your work. However, this is primarily a matter

#!/bin/bash
#SBATCH --time=24:0:0 
#SBATCH --nodes=1 
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=1  
#SBATCH --mem=16G
#SBATCH --array=0-99

CONTAINER=/global/software/freesurfer/7.1.1/containers/freesurfer711.simg

((i=SLURM_ARRAY_TASK_ID-1))

###This section collects the paths relevant to a specific task (i.e. processing a particular image and will change from workflow to workflow.

#check existence of subjects directory list file
subjects_file=/path/to/list/of/specific/subject/directories
if [ ! -e "${subjects_file}" ]; then
  >&2 echo "error: subjects_file does not exist"
  exit 1
fi

# read the subjects from the subjects file
IFS=$'\n'; subjects=( $(cat "${subjects_file}") );

#populate the parent directory for data, the child directory, the file to process, and the output directory
subject=${subjects[${i}]}
dicom_file=file_name_to_process.dcm
subjects=/path/to/parent/subjects/dir
output_dir=/path/to/output/dir

#This is the actual processing command with task specific variables filled in
singularity exec -B $output_dir -B $subjects_dir $CONTAINER recon-all -cw256 -subject ${subjects_dir}/${subject} -i ${subjects_dir}/${subject}/${dicom_file} -all -sd ${output_dir}/