Chapter 11 FreeSurfer v6 Thalamic Nuclei

On the remote computer, install the MATLAB runtime. You don’t have to do this step if you’ve already done this for the amygdala segmentation.

https://surfer.nmr.mgh.harvard.edu/fswiki/MatlabRuntime

cd ~/apps/freesurfer-dev
curl "https://surfer.nmr.mgh.harvard.edu/fswiki/MatlabRuntime?action=AttachFile&do=get&target=runtime2014bLinux.tar.gz" -o "runtime.tar.gz"
tar xvf runtime.tar.gz
rm runtime.tar.gz

11.1 Job Script

On the remote computer create job script.

vi ~/scripts/ACAP/fsThalamus-job.sh

Copy and paste.

#!/bin/bash

#SBATCH --time=05:00:00   # walltime
#SBATCH --ntasks=1   # number of processor cores (i.e. tasks)
#SBATCH --nodes=1   # number of nodes
#SBATCH --mem-per-cpu=16G  # memory per CPU core

# LOAD ENVIRONMENTAL VARIABLES
username=`id -un`
export FREESURFER_HOME=/home/${username}/apps/freesurfer-dev
source $FREESURFER_HOME/SetUpFreeSurfer.sh

# INSERT CODE, AND RUN YOUR PROGRAMS HERE
segmentThalamicNuclei.sh \
${1} \
/work/${username}/data/ACAP/mindboggle/freesurfer_subjects/

11.2 Batch Script

First check that your subjid are correct.

cd /work/ashley.ware/data/ACAP/mindboggle/
for subjid in $(ls freesurfer_subjects/ | grep -e "sub*"); do
  echo $subjid
done

If everything is correct then you can create your batch file.

Create batch script.

vi ~/scripts/ACAP/fsThalamus-batch.sh

Copy and paste.

#!/bin/bash
curTime=`date +"%Y%m%d-%H%M%S"`
username=`id -un`
mkdir -p ~/logfiles/ACAP/${curTime}
cd /work/${username}/data/ACAP/mindboggle/
for subjid in $(ls freesurfer_subjects/ | grep -e "sub*"); do
  sbatch \
  -o ~/logfiles/ACAP/${curTime}/output-${subjid}.txt \
  -e ~/logfiles/ACAP/${curTime}/error-${subjid}.txt \
  ~/scripts/ACAP/fsThalamus-job.sh \
  ${subjid}
  sleep 1
done

11.3 Submit Jobs with Batch Script

Submit jobs.

sh ~/scripts/ACAP/fsThalamus-batch.sh

You can check the status of your jobs by checking if it is running or pending.

squeue

You can check the outputs.

tail -n 5 ~/logfiles/ACAP/<date>/output*

11.4 Download Directory / Sync Directory with Arc

Back on your local computer, sync the data from the remote computer to your local computer.

rsync -rauv \
ashley.ware@arc.ucalgary.ca:/work/ashley.ware/data/ACAP/mindboggle/freesurfer_subjects/ \
/Volumes/bobo/data/ACAP/mindboggle/freesurfer_subjects/

11.5 Output

The output will consist of the follwing set of files, which can be found under the subject’s mri directory (in this example, $SUBJECTS_DIR/sub-001/mri/):

  • ThalamicNuclei.v10.T1.volumes.txt: this text file stores the estimated volumes of the thalamic nuclei.
  • ThalamicNuclei.v10.T1.mgz: stores the discrete segmentation volumes at subvoxel resolution (0.5 mm).
  • ThalamicNuclei.v10.T1.FSvoxelSpace.mgz: stores the discrete segmentation volume in the FreeSurfer voxel space (normally 1mm isotropic, unless higher resolution data was used in recon-all with the flag -cm).

It will also produce files in the stats folder. These can be compiled across subject with the following code:

SUBJECTS_DIR=/Volumes/bobo/data/ACAP/mindboggle/freesurfer_subjects/
cd ${SUBJECTS_DIR}

# LEFT THALAMUS
asegstats2table \
--subjectsfile subjects.txt \
--statsfile=thalamic-nuclei.lh.v10.T1.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/thalamicnuclei-lh-stats.csv

# RIGHT THALAMUS
asegstats2table \
--subjectsfile subjects.txt \
--statsfile=thalamic-nuclei.rh.v10.T1.stats \
--skip \
--delimiter comma \
--tablefile tablefiles/thalamicnuclei-rh-stats.csv

If you want to view the segmentation:

cd /Volumes/bobo/data/ACAP/mindboggle/freesurfer_subjects/sub-ACAP1001/mri
freeview -v nu.mgz -v ThalamicNuclei.v10.T1.mgz:colormap=lut