Neuroimaging Software

SpinLab Pipeline Builder: from four questions to a runnable neuroimaging pipeline

The SpinLab Pipeline Builder turns a research question into a processing script. In guided mode you answer four questions (what data you have, what you want to find out, where your files are, and where the script will run) and SpinLab assembles the steps, estimates the processing time and writes a Bash script that loops over every participant. The full builder gives access to every step and parameter, and exports to Bash, SLURM, Snakemake or Nextflow.

SpinLab Pipeline Builder guided mode with a structural pipeline
Guided mode of the Pipeline Builder: four questions on the left, and the resulting nine-step structural pipeline with estimated times on the right. Detail from spinlab.mrispin.com.

Step by step (guided mode)

  1. What data do you have? Choose structural scan, task fMRI, resting-state fMRI, diffusion, ASL perfusion or raw DICOM files.
  2. What do you want to find out? For a structural scan, for example: brain volumes and cortical thickness, a voxel-based grey-matter comparison between groups (VBM), or cortical surface measures only.
  3. Where are your files? Enter the BIDS folder, the results folder and, optionally, a list of participants (leave empty to process every sub- folder).
  4. Where will it run? One participant after another on your computer, several in parallel, or one job per participant on a SLURM cluster. Set the number of CPU cores.
  5. Build my script and copy or download it.

The pipeline it builds

For “brain volumes and cortical thickness” the Pipeline Builder assembles nine steps and estimates about 8 h 49 min per participant, most of it FreeSurfer:

#StepToolTypical time
1Check the BIDS layoutbids-validator1 min
2Even out brightness (bias field correction)ANTs N4BiasFieldCorrection5 min
3Remove the skullFSL BET2 min
4Line up with the MNI template (12 degrees of freedom)FSL FLIRT3 min
5Match brain shape in detailFSL FNIRT20 min
6Separate grey matter, white matter and CSFFSL FAST8 min
7Reconstruct the cortexFreeSurfer recon-allabout 8 h
8Subcortical volumes tableasegstats2table5 min
9Cortical thickness tableaparcstats2table5 min

The script, explained

Safety settings and variables

#!/bin/bash
set -euo pipefail
trap 'echo "ERROR on line $LINENO (exit $?)" >&2; exit 1' ERR

BIDS_DIR="/path/to/bids"
OUT_DIR="/path/to/derivatives"
NTHREADS=12

set -euo pipefail makes the script stop at the first failure instead of carrying on with missing files. The trap line prints the line number of any error, which makes failures easy to find in long logs. Set the two folders before running.

Finding the participants

SUBJECTS=($(ls -d "/path/to/bids"/sub-* 2>/dev/null | xargs -n1 basename))
if [ ${#SUBJECTS[@]} -eq 0 ]; then
    echo "ERROR: No sub-* folders found in /path/to/bids"
    exit 1
fi
for SUBJ in "${SUBJECTS[@]}"; do
    SUBJ_OUT="${OUT_DIR}/${SUBJ}"
    mkdir -p "$SUBJ_OUT"

The script lists every sub-* folder in the BIDS dataset and stops with a clear message if none is found. Each participant gets their own output folder.

Bias correction and brain extraction

    N4BiasFieldCorrection -d 3 -i "$T1" -o "$T1_N4" \
        -s 4 -c [50x50x50x50,0.0000001] -b [200]

    bet "$T1" "$T1_BRAIN" -f 0.4 -g 0 -B -m

N4 removes the slow brightness variation caused by the receive coil: -s 4 shrinks the image four times to speed up the fit, -c sets four levels of 50 iterations with a convergence threshold, and -b [200] sets the B-spline spacing in millimetres. BET then removes the skull: -f 0.4 is the fractional intensity threshold (smaller values keep more brain), -B applies extra bias and neck clean-up, and -m saves a binary brain mask.

Registration to MNI space

    flirt -in "$T1_BRAIN" -ref "$REF" -out "$T1_MNI_LIN" -omat "$AFF_MAT" \
        -dof 12 -cost corratio

    fnirt --in="${T1_BRAIN}" --aff="${AFF_MAT}" \
        --ref="$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz" \
        --cout="${WARPFIELD}" --iout="${T1_MNI_NL}" --config=T1_2_MNI152_2mm.cnf

FLIRT finds a 12-parameter affine transform (translation, rotation, scaling and shear) using the correlation-ratio cost function, which works well between T1 images. FNIRT starts from that matrix (--aff) and adds a non-linear warp using FSL’s standard configuration for T1-to-MNI registration.

Segmentation and FreeSurfer

    fast -t 1 -n 3 -o "${FAST_DIR}/${SUBJ}" "$T1_BRAIN"

    export SUBJECTS_DIR="/data/freesurfer"
    recon-all -s "${SUBJ}" -i "$T1" -all -parallel -openmp "${NTHREADS}"

FAST labels voxels into three tissue classes in a T1 image (-t 1 -n 3) and writes partial-volume maps such as _pve_1 for grey matter. recon-all -all runs the full FreeSurfer stream; -parallel -openmp uses several cores. FreeSurfer needs a free licence file.

Tables for statistics

    asegstats2table --subjects "${SUBJ}" --meas volume \
        --tablefile "/data/results/aseg_volumes.tsv" --skip
    for HEMI in lh rh; do
        aparcstats2table --subjects "${SUBJ}" --hemi "$HEMI" \
            --meas thickness --parc aparc \
            --tablefile "/data/results/aparc_${HEMI}_thickness.tsv" --skip
    done

These commands collect FreeSurfer results into tab-separated tables, one for subcortical volumes and one per hemisphere for cortical thickness in the Desikan–Killiany (aparc) atlas. --skip skips participants whose results are missing instead of stopping.

Before you run it

  • Replace the placeholder folders and check the output paths of the statistics tables.
  • Run it on one participant first and look at the outputs (brain mask, registration, segmentation) before processing everyone.
  • For a cluster, choose the SLURM option so that each participant runs as its own job.

References

  • Jenkinson M, Beckmann CF, Behrens TE, Woolrich MW, Smith SM. FSL. NeuroImage 2012;62(2):782–790.
  • Tustison NJ, Avants BB, Cook PA, et al. N4ITK: improved N3 bias correction. IEEE Trans Med Imaging 2010;29(6):1310–1320.
  • Fischl B. FreeSurfer. NeuroImage 2012;62(2):774–781.

Related articles

Last reviewed: 7 November 2026. SpinLab and SpinCalc are free tools developed by MRISpin. Screenshots show the tools as they were on this date and may change as the tools are updated. Generated scripts should be checked before use on real data.

MRISpin Team

MRISpin team combines decades of experience in MRI research, clinical practice, and science communication. Meet the people driving MRISpin’s mission.

Related Articles

Back to top button