Tuesday, November 1, 2011

A bash script to do DTI analysis.

Tzipi, who is our new post-doc from Israel, is a newbie to Linux.  She is interested in DTI and fMRI studies.

For helping her and also for my own good (convenience for future analysis), I wrote a nice small script to do DTI analysis. I would like to distribute it here freely.  I am a big fan of open-source.  Thus, feel free to use it.  Well, if you want to sponsor my research, also feel free to support me!



Downloads:
My script:  yw_dti_pipeline_for1subject.sh
A sample data: Test data

I tried to test Tzipi's data.  Well, the DTI protocol is weird compared with regular 32 dir data from philips 3T.  I need to change the script a little bit for her.

===================================================================
Updated Nov.2. 2011  script downloading: yw_dti_for1subject_v20111102.sh
Tzipi's bvals & bvecs:  bvals  bvecs
Tzipi's sample data: Tzipi test data

Small perl programs to share:
(1) convert bvals and bvecs into one file for AFNI 3dDWItoDT:  fsl_bvals_bvecs2afni.zip
(2) convert bvals and bvecs into one file for DTIStudio: fsl_bvals_bvecs2dtistudio.zip
(3) rotate the vecs: rotate_bvecs

Download all together:  All Together!

Please report bugs to Yingying at yingying.wang@cchmc.org or leave your comments here.
====================================================================

You can modify it  according to your own needs.  This is a very basic one.

#! /bin/bash

# get dti from dwi using FSL or AFNI pipeline
# Yingying [yingying.wang@cchmc.org]
# Advisor: Scott K. Holland [scott.holland@cchmc.org]
# 9.30.2011

# get started
echo "Welcome to DTI processing pipeline Beta 1.0! Any questions, please contact Yingying at 513-636-3495 or yingying.wang@cchmc.org"
echo "please enter the subject's folder name (highly recommend to use the subject id as the folder name):"
read -e subid
# check directory exist
if [[ ! -d "$subid" ]]
then
    echo "Oops, subject name given but the directory \"$subid\" does not exist! Please double check your subject folder name."
    exit 1
fi

echo "Checking the integrity of your data (we need subject's DWI nii image, bveces and bvals)"

orig_image=`imglob $subid/data_orig`

if [[ -z "orig_image" ]]
then
    echo "The subject's directory is there but the data_orig image is not"
    exit 1
fi

if [[ ! -f "$subid/data_orig.bvals" ]] || [[ ! -f "$subid/data_orig.bvecs" ]]
then
    echo "Either one of these files does not exist $subid/data_orig.bvals $subid/data_orig.bvecs, they should both exist"
    exit 1
fi

if [[ -z `imglob $subid/data_notrace` ]]
then
    remtrace=0
    echo "Pre-processing is almost ready ... check for the DWI and make sure the data bvecs is right"
    echo "Do you need to remove the trace volume?(No:0,YES:1)default:No"
    read -e remtrace
    if [[ $remtrace -eq 1 ]]
    then
        echo "Accordint to your input $remtrace, Your option is YES"
        echo "Now dealing with the tract volume"
        orig_vol=`fslsize $subid/data_orig | grep -e "^dim4" | awk '{ print $2 }'`
        newvol_notrace=`expr $orig_vol - 1`
        fslroi $subid/data_orig $subid/data_notrace 0 $newvol_notrace
        notrace_foreddy="data_notrace"
        cut -f `seq -s, 1 $newvol_notrace` -d \  $subid/data_orig.bvals > $subid/bvals
        cut -f`seq -s, 1 $newvol_notrace` -d \  $subid/data_orig.bvecs > $subid/bvecs
        echo "Congratulations! Trace volume has been successfully removed!"
    else
        echo "No trace volume according to your input $remtrace"
        notrace_foreddy="data_orig"
        cp $subid/data_orig.bvals $subid/bvals
        cp $subid/data_orig.bvecs $subid/bvecs
    fi
fi

if [[ -z `imglob $subid/data` ]]
then
    echo "Now eddy_current correction starts ... it may take a while, go to get a cup of coffee or chat with Dr. Holland, Ha Ha ..."
    cd $subid/
    eddy_correct $notrace_foreddy data 0
    rm -f bvecs_old
    rotate_bvecs data.ecclog bvecs
    cd ../
    echo "Successfully done!"
fi

if [[ -z `imglob $subid/nodif` ]]
then
    echo "Get the B0 image"
    #make the first volume the nodif (B=0 reference)
    fslroi $subid/data $subid/nodif 0 1
    echo "B0 image has been generated!"
fi

if [[ -z `imglob $subid/nodif_brain_mask` ]]
then
    # default mask threshold
    betthr=0.3
    echo "Please input a threshold for the brain mask(default:0.3):"
    read -e betthr
    echo "making a brain mask using threshold \"$betthr\"..."
    bet $subid/nodif $subid/nodif_brain -R -f $betthr -m
    echo "Brain mask is done!"
fi

echo "The real DTI process is ready to go!"
echo "Do you want to use FSL(option:1),AFNI(option:2),Both(option:3)"
read -e opts
case $opts in
1 )
    # make folder to put fsl results in
    rm -fr $subid/fsl_results
    rm -fr $subid/tmp
    mkdir -p $subid/fsl_results
    mkdir -p $subid/tmp
   
    echo "FSL processing is undergoing ..."
    dtifit -k $subid/data -o $subid/fsl_results/${subid}_dtifit -m $subid/nodif_brain_mask -r $subid/bvecs -b $subid/bvals -w 1> $subid/tmp/fsl_stdoutput 2> $subid/tmp/fsl_errors
   
    imcp $subid/fsl_results/${subid}_dtifit_L1 $subid/fsl_results/${subid}_dtifit_AD 1>> $subid/tmp/fsl_stdoutput 2>> $subid/tmp/fsl_errors
   
    fslmaths $subid/fsl_results/${subid}_dtifit_L2 -add $subid/fsl_results/${subid}_dtifit_L3 -div 2 $subid/fsl_results/${subid}_dtifit_RD 1>> $subid/tmp/fsl_stdoutput 2>> $subid/tmp/fsl_errors
   
    if [[ -s $subid/tmp/fsl_errors ]]
    then
        echo "Something is wrong:"
        echo `cat $subid/tmp/fsl_errors`
    else
        echo "YEAH! FSL process is done! See you soon! :-)"
    fi
    ;;
   
2 )
    # make AFNI folder and put AFNI results there   
    rm -fr $subid/afni_results
    mkdir -p $subid/afni_results
    rm -fr $subid/tmp
    mkdir -p $subid/tmp
   
    echo "AFNI processing is undergoing ..."
   
    # preparation: copy files to the afni_results folder
    imcp $subid/data $subid/afni_results/data 1> $subid/tmp/afni_stdoutput 2> $subid/tmp/afni_errors
    imcp $subid/nodif_brain_mask $subid/afni_results/nodif_brain_mask 1>> $subid/tmp/afni_stdoutput 2>> $subid/tmp/afni_errors
   
    # use nodif as the anatomical image
    imcp $subid/nodif_brain $subid/afni_results/nodif_brain 1>> $subid/tmp/afni_stdoutput 2>> $subid/tmp/afni_errors
   
    # convert bvals and bvecs to afni format
    cd $subid
    bvals_bvecs_fsl2afni.pl > afni_results/grad.afni
   
    cd afni_results
    fslchfiletype NIFTI data 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI nodif_brain_mask 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI nodif_brain 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    # copy data to AFNI format
    3dcopy data.nii data 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dcopy nodif_brain_mask.nii mask 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dcopy nodif_brain.nii b0_anat 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    # compute dti from dwis
    3dDWItoDT -prefix DT -mask mask+orig -reweight -max_iter 10 -max_iter_rw 10 -eigs grad.afni data+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_L1 -fbuc 'DT+orig[6]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_L2 -fbuc 'DT+orig[7]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_L3 -fbuc 'DT+orig[8]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_FA -fbuc 'DT+orig[18]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_MD -fbuc 'DT+orig[19]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    # Get AD and RD from L1, L2, L3
    3dcopy ${subid}_afni_L1+orig ${subid}_afni_AD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dcalc -a ${subid}_afni_L2+orig -b ${subid}_afni_L3+orig -expr '(a+b)/2' -prefix ${subid}_afni_RD 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    # convert to nii
    3dAFNItoNIFTI ${subid}_afni_L1+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dAFNItoNIFTI ${subid}_afni_L2+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dAFNItoNIFTI ${subid}_afni_L3+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dAFNItoNIFTI ${subid}_afni_FA+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dAFNItoNIFTI ${subid}_afni_MD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    fslchfiletype NIFTI_GZ ${subid}_afni_L1 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI_GZ ${subid}_afni_L2 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI_GZ ${subid}_afni_L3 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI_GZ ${subid}_afni_FA 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI_GZ ${subid}_afni_MD 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    imcp ${subid}_afni_L1 ${subid}_afni_AD 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_errors
   
    fslmaths ${subid}_afni_L2 -add ${subid}_afni_L3 -div 2 ${subid}_afni_RD.nii.gz 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_errors
   
    if [[ -s ../tmp/afni_errors ]]
    then
        echo "Something is wrong:"
        echo `cat ../tmp/afni_errors`
    else
        echo "AFNI process is done without Talairach! See you soon! :-)"
    fi
   
    echo "Do you want to Talairach(No:0,YES:1) default:No"
    read -e opts2   
    case $opts2 in
    0 )   
        echo "No talariach will be applied!"
        echo "Job is Done!"
        exit 1
        ;;
    1 )
        echo "Apply Talairach ...It is time for a break."
        # Get afni folder
        afnidirs=`which afni`
       
        # First, Talairach of anatomical image
        @auto_tlrc -base ${afnidirs%/afni}/TT_icbm452+tlrc -input b0_anat+orig 1> ../tmp/afni_stdoutput 2> ../tmp/afni_stdoutput
       
        # Talairach transform
        @auto_tlrc -apar b0_anat+tlrc -input $subid_afni_AD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
        @auto_tlrc -apar b0_anat+tlrc -input $subid_afni_FA+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
        @auto_tlrc -apar b0_anat+tlrc -input $subid_afni_MD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
        @auto_tlrc -apar b0_anat+tlrc -input $subid_afni_RD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
       
        if [[ -s ../tmp/afni_errors ]]
        then
            echo "Something is wrong:"
            echo `cat ../tmp/afni_errors`
        else
            echo "Successfully Talairached! See you soon! :-)"   
        fi
        exit 1
        ;;
    * ) echo "Sorry, invalid option!"
        exit 1
        ;;
    esac
    ;;
3 )
    # make folder to put fsl results in
    rm -fr $subid/fsl_results
    rm -fr $subid/tmp
    mkdir -p $subid/fsl_results
    mkdir -p $subid/tmp
   
    echo "FSL processing is undergoing ..."
    dtifit -k $subid/data -o $subid/fsl_results/${subid}_dtifit -m $subid/nodif_brain_mask -r $subid/bvecs -b $subid/bvals -w 1> $subid/tmp/fsl_stdoutput 2> $subid/tmp/fsl_errors
   
    imcp $subid/fsl_results/${subid}_dtifit_L1 $subid/fsl_results/${subid}_dtifit_AD 1>> $subid/tmp/fsl_stdoutput 2>> $subid/tmp/fsl_errors
   
    fslmaths $subid/fsl_results/${subid}_dtifit_L2 -add $subid/fsl_results/${subid}_dtifit_L3 -div 2 $subid/fsl_results/${subid}_dtifit_RD 1>> $subid/tmp/fsl_stdoutput 2>> $subid/tmp/fsl_errors
   
    if [[ -s $subid/tmp/fsl_errors ]]
    then
        echo "Something is wrong:"
        echo `cat $subid/tmp/fsl_errors`
    else
        echo "YEAH! FSL process is done! Next is AFNI!"
    fi
   
        # make AFNI folder and put AFNI results there   
    rm -fr $subid/afni_results
    mkdir -p $subid/afni_results
    rm -fr $subid/tmp
    mkdir -p $subid/tmp
   
    echo "AFNI processing is undergoing ..."
   
    # preparation: copy files to the afni_results folder
    imcp $subid/data $subid/afni_results/data 1> $subid/tmp/afni_stdoutput 2> $subid/tmp/afni_errors
    imcp $subid/nodif_brain_mask $subid/afni_results/nodif_brain_mask 1>> $subid/tmp/afni_stdoutput 2>> $subid/tmp/afni_errors
   
    # use nodif as the anatomical image
    imcp $subid/nodif_brain $subid/afni_results/nodif_brain 1>> $subid/tmp/afni_stdoutput 2>> $subid/tmp/afni_errors
   
    # convert bvals and bvecs to afni format
    cd $subid
    bvals_bvecs_fsl2afni.pl > afni_results/grad.afni
   
    cd afni_results
    fslchfiletype NIFTI data 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI nodif_brain_mask 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI nodif_brain 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    # copy data to AFNI format
    3dcopy data.nii data 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dcopy nodif_brain_mask.nii mask 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dcopy nodif_brain.nii b0_anat 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    # compute dti from dwis
    3dDWItoDT -prefix DT -mask mask+orig -reweight -max_iter 10 -max_iter_rw 10 -eigs grad.afni data+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_L1 -fbuc 'DT+orig[6]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_L2 -fbuc 'DT+orig[7]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_L3 -fbuc 'DT+orig[8]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_FA -fbuc 'DT+orig[18]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dbucket -prefix ${subid}_afni_MD -fbuc 'DT+orig[19]' 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    # Get AD and RD from L1, L2, L3
    3dcopy ${subid}_afni_L1+orig ${subid}_afni_AD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dcalc -a ${subid}_afni_L2+orig -b ${subid}_afni_L3+orig -expr '(a+b)/2' -prefix ${subid}_afni_RD 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    # convert to nii
    3dAFNItoNIFTI ${subid}_afni_L1+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dAFNItoNIFTI ${subid}_afni_L2+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dAFNItoNIFTI ${subid}_afni_L3+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dAFNItoNIFTI ${subid}_afni_FA+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    3dAFNItoNIFTI ${subid}_afni_MD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    fslchfiletype NIFTI_GZ ${subid}_afni_L1 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI_GZ ${subid}_afni_L2 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI_GZ ${subid}_afni_L3 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI_GZ ${subid}_afni_FA 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
    fslchfiletype NIFTI_GZ ${subid}_afni_MD 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
   
    imcp ${subid}_afni_L1 ${subid}_afni_AD 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_errors
   
    fslmaths ${subid}_afni_L2 -add ${subid}_afni_L3 -div 2 ${subid}_afni_RD.nii.gz 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_errors
   
    if [[ -s ../tmp/afni_errors ]]
    then
        echo "Something is wrong:"
        echo `cat ../tmp/afni_errors`
    else
        echo "AFNI process is done without Talairach! See you soon! :-)"
    fi
   
    echo "Do you want to Talairach(No:0,YES:1) default:No"
    read -e opts2   
    case $opts2 in
    0 )   
        echo "No talariach will be applied!"
        echo "Job is Done!"
        exit 1
        ;;
    1 )
        echo "Apply Talairach ...It is time for a break."
        # Get afni folder
        afnidirs=`which afni`
       
        # First, Talairach of anatomical image
        @auto_tlrc -base ${afnidirs%/afni}/TT_icbm452+tlrc -input b0_anat+orig 1> ../tmp/afni_stdoutput 2> ../tmp/afni_stdoutput
       
        # Talairach transform
        @auto_tlrc -apar b0_anat+tlrc -input $subid_afni_AD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
        @auto_tlrc -apar b0_anat+tlrc -input $subid_afni_FA+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
        @auto_tlrc -apar b0_anat+tlrc -input $subid_afni_MD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
        @auto_tlrc -apar b0_anat+tlrc -input $subid_afni_RD+orig 1>> ../tmp/afni_stdoutput 2>> ../tmp/afni_stdoutput
       
        if [[ -s ../tmp/afni_errors ]]
        then
            echo "Something is wrong:"
            echo `cat ../tmp/afni_errors`
        else
            echo "Successfully Talairached! See you soon! :-)"   
        fi
        exit 1
        ;;
    * ) echo "Sorry, invalid option!"
        exit 1
        ;;
    esac
    ;;
* )
    echo "Sorry, invalid option!"
    exit 1
    ;;               
esac

Found a good website (sharing with you):  http://www.psychology.gatech.edu/cabi/Resources/DTI/index.shtml

No comments:

Post a Comment