Chapter 3 DICOM to NIfTI

Set up directories on your local storage system. I keep the raw DICOMs organized as they are in a separate location than BIDS organized NIFTI images, as well as analyzed images (e.g. freesufer, afq, etc).

mkdir -p /path/to/DICOM/ACAP
mkdir -p /path/to/BIDS/ACAP
mkdir -p /path/to/data/ACAP

The DICOM directory is where the raw DICOMs will be stored. BIDS organized NIFTI images are located under the BIDS directory. Any analyzed data is under data.

If your data will be stored on an external harddrive and you’ve named that harddrive, bobo, then the path to that HD is /Volumes/bobo/. Therefore you can make the above directories:

mkdir -p /Volumes/bobo/DICOM/ACAP
mkdir -p /Volumes/bobo/BIDS/ACAP
mkdir -p /Volumes/bobo/data/ACAP

3.1 Convert T1 image

Assuming the raw DICOM files are located at /Volumes/bobo/raw/ACAP. You can run the following code to convert the raw DICOMS to NIFTI format. This will convert DICOM to NIFTI and organize the NIFTI files in BIDS format.

Sometimes with larger and messier datasets where sequences could have been run multiple times, you’ll want to generate a text file that lists all the DICOM directories and scrub that file first before running dcm2niix. It is a lot easier to edit the text file then search through hundreds of directories trying to weed out bad images. You can use Mango app to view the raw DICOMs and check which one’s you’d like to keep if there’s multiple for a subject. Optimally, you will want one T1 per participant.

find /Volumes/bobo/DICOM/ACAP -type d \
-name "PUAx_FSPGR_BRAVO*" > /Volumes/bobo/DICOM/ACAP/ACAP_MPRAGE.txt

Instead of finding files on your system, you can just loop through each line of your text file and convert those DICOMs to NIFTI. Double check the subject IDs first.

cd /Volumes/bobo/BIDS/ACAP/
for i in `cat /Volumes/bobo/DICOM/ACAP/ACAP_MPRAGE.txt`; do 
  subjid=`echo $i | cut -d "/" -f6`
  echo $subjid
done

Once you’ve confirmed that you have the correct subject IDs you can run the code.

cd /Volumes/bobo/BIDS/ACAP/
for i in `cat /Volumes/bobo/DICOM/ACAP/ACAP_MPRAGE.txt`; do 
  subjid=`echo $i | cut -d "/" -f6`
  mkdir -p /Volumes/bobo/BIDS/ACAP/sub-${subjid:0:8}/ses-${subjid:9}/anat
  
  dcm2niix \
  -o sub-${subjid:0:8}/ses-${subjid:9}/anat/ \
  -x n \
  -f sub-${subjid:0:8}_ses-${subjid:9}_T1w \
  -z n \
  $i
done

3.2 Covert T2 image

Create a text file to list all the T2 flair directories.

find /Volumes/bobo/DICOM/ACAP -type d \
-name "Ax_T2_FLAIR*" > /Volumes/bobo/DICOM/ACAP/ACAP_T2FLAIR.txt

After cleaning the text file, loop through and generate your NIFTI images.

cd /Volumes/bobo/BIDS/ACAP/
for i in `cat /Volumes/bobo/DICOM/ACAP/ACAP_T2FLAIR.txt`; do 
  subjid=`echo $i | cut -d "/" -f6`
  mkdir -p /Volumes/bobo/BIDS/ACAP/sub-${subjid:0:8}/ses-${subjid:9}/anat
  
  dcm2niix \
  -o sub-${subjid:0:8}/ses-${subjid:9}/anat/ \
  -x n \
  -f sub-${subjid:0:8}_ses-${subjid:9}_T2w \
  -z y \
  $i
done

3.3 Convert DWI images

Create a text file to list all the DWI b900 directories. It is ok to keep multiple runs of the same sequence as it is easier to verify scan quality later.

find /Volumes/bobo/DICOM/ACAP -type d \
-name "DTI_30_dir_b900*" > /Volumes/bobo/DICOM/ACAP/ACAP_DWIb900.txt

After cleaning the text file, loop through and generate your NIFTI images.

cd /Volumes/bobo/BIDS/ACAP/
for i in `cat /Volumes/bobo/DICOM/ACAP/ACAP_DWIb900.txt`; do 
  subjid=`echo $i | cut -d "/" -f6`
  mkdir -p /Volumes/bobo/BIDS/ACAP/sub-${subjid:0:8}/ses-${subjid:9}/dwi
  
  dcm2niix \
  -o sub-${subjid:0:8}/ses-${subjid:9}/dwi/ \
  -x n \
  -f sub-${subjid:0:8}_ses-${subjid:9}_acq-b900_dwi \
  -z y \
  $i
done

Do the same thing for your b2000 sequences.

find /Volumes/bobo/DICOM/ACAP -type d \
-name "DTI_30_dir_b2000*" > /Volumes/bobo/DICOM/ACAP/ACAP_DWIb2000.txt

Use BBEdit to split your file into two so one contains unique DICOM directories and a second file contains duplicates. Hopefully we don’t have more than two scans per participant. After cleaning the text file, loop through and generate your NIFTI images.

cd /Volumes/bobo/BIDS/ACAP/
for i in `cat /Volumes/bobo/DICOM/ACAP/ACAP_DWIb2000.txt`; do 
  subjid=`echo $i | cut -d "/" -f6`
  mkdir -p /Volumes/bobo/BIDS/ACAP/sub-${subjid:0:8}/ses-${subjid:9}/dwi
  
  dcm2niix \
  -o sub-${subjid:0:8}/ses-${subjid:9}/dwi/ \
  -x n \
  -f sub-${subjid:0:8}_ses-${subjid:9}_acq-b2000_dwi \
  -z y \
  $i
done

3.4 ACPC Alignment

Before you can upload your images and start preprocessing the T1 images, you need to run ACPC alignment on your local computer.

for i in $(find /Volumes/bobo/BIDS/ACAP -type f -name "*T1w.nii"); do
  acpcdetect \
  -noppm -nopng -notxt \
  -i $i
  rm $(dirname $i)/*FSL.mat
  rm $(dirname $i)/*.mrx
done

Open a Finder window and go to ~/BIDS/ACAP.

Search for ‘RAS’ and then select the ‘ACAP’ folder to view a list of ACPC aligned images. Press the spacebar to quickview the images. Double check for:

  1. Images with too much motion, remove these from the BIDS directory
  2. Images not aligned correctly, rerun acpcdetect

To re-run acpcdetect, you will need to create a text file with the correct AC and PC location using ITK-SNAP. Create a file named, ‘landmark.txt’, located in the BIDS folder of the participant you need to fix. The first line will by (i, j, k) coordinates for anterior commissure. The second line will be the coordinates for posterior commissure. The third line will be the coordinates for vertex of the superior pontine sulcus.

256 274 109
256 216 109
258 223 86

To run the code change into the participant directory and run acpcdetect.

cd /Volumes/bobo/BIDS/ACAP/sub-ACAP1180/ses-BL/anat/
acpcdetect \
-lm landmark.txt \
-noppm -nopng -notxt \
-i sub-ACAP1180_ses-BL_T1w.nii
rm *FSL.mat
rm *.mrx

3.5 Setting up your Remote System

Log onto Helix.

ssh ashley.ware@arc.ucalgary.ca

Create the following directories.

mkdir -p /work/ashley.ware/BIDS/ACAP
mkdir -p /work/ashley.ware/data/ACAP
mkdir -p /home/ashley.ware/scripts/ACAP
mkdir -p /home/ashley.ware/logfiles/ACAP

Log off Helix.

exit

3.6 Upload Directory / Sync Directory with Arc

Before you run rsync, let’s create a file that lists hidden system files that you don’t want to sync between your local and remote computers.

vi ~/.rsyncExclude.txt

In your file, add this name.

.DS_Store

Then you can sync your files and not include these unnecssary files.

rsync -rauv \
--exclude-from ~/.rsyncExclude.txt \
/Volumes/bobo/BIDS/ACAP/ \
ashley.ware@arc.ucalgary.ca:/work/ashley.ware/BIDS/ACAP/