Chapter 5 T1 Preprocessing Steps

After you finish running the preprocessing steps on your T1 images, these are the image you will ALWAYS use for analyses. When you run FreeSufer, antsCorticalThickness, MindBoggle, etc. this is the default T1 image used. Log onto the remote computer.

5.1 Job Script

Create script.

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

Copy and paste code.

#!/bin/bash

#SBATCH --time=00:30: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 ANTSPATH=/home/${username}/apps/ants/bin/
PATH=${ANTSPATH}:${PATH}

# INSERT CODE, AND RUN YOUR PROGRAMS HERE
DATA_DIR=/work/${username}/BIDS/ACAP/${1}/${2}/anat
OUTPUT_DIR=/work/${username}/data/ACAP/prepAnat/${1}_${2}
mkdir -p ${OUTPUT_DIR}

## CROP
c3d \
${DATA_DIR}/${1}_${2}_T1w_RAS.nii \
-trim 20vox \
-o ${OUTPUT_DIR}/cropped.nii.gz

## N4 BIAS FIELD CORRECTION
N4BiasFieldCorrection \
-v -d 3 \
-i  ${OUTPUT_DIR}/cropped.nii.gz \
-o ${OUTPUT_DIR}/n4.nii.gz \
-s 4 -b [200] -c [50x50x50x50,0.000001]

## RESAMPLE
c3d \
${OUTPUT_DIR}/n4.nii.gz \
-resample-mm 1x1x1mm \
-o ${OUTPUT_DIR}/${1}_${2}_T1w.nii.gz

5.2 Batch Script

First check that your subjid and ses are correct.

for i in $(find /work/ashley.ware/BIDS/ACAP -name "*T1w_RAS.nii"); do
  subjid=`echo $i | cut -d "/" -f6`
  ses=`echo $i | cut -d "/" -f7`
  echo $subjid
  echo $ses
done

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

Create script.

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

Copy and paste code.

#!/bin/bash
curTime=`date +"%Y%m%d-%H%M%S"`
username=`id -un`
mkdir -p ~/logfiles/ACAP/${curTime}
for i in $(find /work/${username}/BIDS/ACAP -name "*T1w_RAS.nii"); do
  subjid=`echo $i | cut -d "/" -f6`
  ses=`echo $i | cut -d "/" -f7`
  sbatch \
  -o ~/logfiles/ACAP/${curTime}/output-${subjid}_${ses}.txt \
  -e ~/logfiles/ACAP/${curTime}/error-${subjid}_${ses}.txt \
  ~/scripts/ACAP/preprocess-job.sh \
  ${subjid} \
  ${ses}
  sleep 1
done

5.3 Submit Jobs with Batch Script

sh ~/scripts/ACAP/preprocess-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 1 ~/logfiles/ACAP/<date>/output*

5.4 Clean Up Directory

You don’t need the interm files and those can be removed.

find /work/ashley.ware/data/ACAP/prepAnat/ -type f -name "n4.nii.gz" -exec rm {} \;
find /work/ashley.ware/data/ACAP/prepAnat/ -type f -name "cropped.nii.gz" -exec rm {} \;

5.5 Directory Structure

The final data structure will look like this.

/work/ashley.ware/data/ACAP/prepAnat/
  |–– sub-ACAP1001_ses-BL/
    |–– sub-ACAP1001_ses-BL_T1w.nii.gz
  |–– sub-ACAP1001_ses-FU3/
    |–– sub-ACAP1001_ses-FU3_T1w.nii.gz

5.6 Download Directory / Sync Directory with Arc

Back on your local computer make sure you have the directory already.

mkdir -p /Volumes/bobo/data/ACAP/prepAnat

Then sync the data from the remote computer to your local computer.

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