Example use of DTIprep for DWI Quality Checking

In a previous post, we covered using DTIprep for preprocessing of diffusion weighted imaging (DWI) data.  I’ve written a quick script (below) to automate DTIprep for the purposes of running the default QC check.  My workflow has DICOM data automatically downloaded from our PACS server, then automatically converted to NIFTI format, and then organized in the file structure.  This program is just adding the QC checks for the diffusion data, whereas other scripts will preprocess the fMRI data using afni_proc.py.

The script itself can live anywhere and will create a temporary directory for any files used wherever the script is called from.  So if you were to call the script from your home directory like so:

dti_qc.sh /data/mri/subject001/dti/ep2ddiff.nii.gz \
/data/mri/subject001/dti/ep2ddiff.bvec \
/data/mri/subject001/dti/ep2ddiff.bval

Then the temporary file would be created in your home directory.  Obviously you can change this however you like.  My recent tests of XNAT and NiDB, which both allow for some pipeline processing have used an approach like the one in this script.

 

#!/bin/bash
current=`pwd`

#if on a Mac with DTIprep and Slicer installed
#assuming both in /Applications directory
#modify if elsewhere or on other OS.
export PATH=$PATH:/Applications/Slicer.app/Contents/lib/Slicer-4.4/cli-modules
export PATH=$PATH:/Applications/DTIPrep.app/Contents/MacOS
#some quick error checking
if [ $# -lt 3 ]; then
 echo "Usage: ./dti_qc <nifti_file> <bvecs> <bvals>"
 echo "Usage: ./dti_qc myfile.nii.gz myfile.bvec myfile.bval"
 exit;
fi
if [ ! -e $1 ]; then
 echo "$1 does not exist. Use a real file."
 exit;
fi
if [ ! -e $2 ]; then
 echo "$2 does not exist. Use a real file."
 exit;
fi
if [ ! -e $3 ]; then
 echo "$3 does not exist. Use a real file."
 exit;
fi
if [ `which DWIConvert | wc -l` -eq 0 ]; then
 echo "Cannot find DWIConvert, is it in your PATH?"
 exit;
fi
if [ `which DTIPrepExec | wc -l` -eq 0 ]; then
 echo "Cannot find DTIPrepExec, is it in your PATH?"
 exit;
fi
echo "Files found, proceeding with Quality Check"
#create a temporary working space with date & time
foldername=`date +"%m%d%y_%T" | sed 's/://g'`
mkdir dti.${foldername}
#copy over DWI image data and transpose bvals
3dcopy $1 dti.${foldername}/dwi.nii
1dtranspose $3 > dti.${foldername}/bval.txt
#transpose b-vectors, flip y gradient b/c Siemens...
1dDW_Grad_o_Mat \
-in_grad_rows $2 \
-out_grad_cols dti.${foldername}/bvec.txt \
-flip_y \
-keep_b0s
#change into working directory
cd dti.${foldername}
#convert DWI image data from NIFTI to NRRD
DWIConvert \
--inputVolume dwi.nii \
--inputBVectors bvec.txt \
--inputBValues bval.txt \
--conversionMode FSLToNrrd \
-o dwi.nrrd
#Run default QC check
DTIPrepExec \
-c \
-d \
-p test.xml \
-w dwi.nrrd \
--numberOfThreads 4
#Convert corrected DWI image data from NRRD to NIFTI
DWIConvert \
--inputVolume dwi_QCed.nrrd \
--outputVolume dwi_QCed.nii \
--outputBVectors dwi_QCed.bvec \
--outputBValues dwi_QCed.bval \
--conversionMode NrrdToFSL
echo "Total Good Gradients: `cat dwi_QCed.bvec | wc -l`"
#copy report back to original data location
cp *QCReport.txt ../
cd $current

Possible modifications include more automation and error checking, as well as someone could awk/grep through the DTIprep configuration file (XML) and change options as they wished before continuing on.  I’ve mostly been using this as a quick assessment of how the diffusion data looks, since DTIprep will remove gradients with too much motion, noise, bad stuff, the final count reported is the actual directions + 1 b0.

Comments are closed.