====== runFREE.sh Example script ====== Run freesurfer on the same raw image that was used for first (not the nifti file, but the .dcm used to make the nifti file). * freesurfer converts the image from dicom, to a coronally sliced .mgz file the following code is a sample script that will run all stages of freesurfer segmentation: #!/bin/bash #### add the paths to freesurfer if not already recognized and execute the set-up script #### #export FREESURFDIR=/usr/local/packages/freesurfer/ #. ${FREESURFDIR}/SetUpFreeSurfer.sh SUBJ=$1 SERIES=$2 RAWDIR=/path/to/raw/data/$SUBJ/series${SERIES} FREEOUT=/path/to/your/freesurfer/SUBJECTS_DIR ## execute the variable, freesurfer requires this to be set, unless using absolute paths ### export SUBJECTS_DIR=$FREEOUT cd $SUBJECTS_DIR ### creates the freesurfer data structure for your subject, converts the raw data to mgz, sets your subject name recon-all -i $RAWDIR/*${SUBJ}_${SERIES}_01_00001.dcm -s $SUBJ ### runs all stages of freesurfer segmentation on your subject recon-all -all -s $SUBJ to actually run the above script: bash -x runFREE SUBJ# SERIES# ex:bash -x runFREE 20080227_12345 300 of course everything in the runFREE.sh script can be run from the command line as well. ====== Convert FreeSurfer Segmentation ====== once the freesurfer segmentation has completed, you need to convert the segmentations from freesurfer space back to the original space. * tkregister2 is used to get the conversion matrix * mri_label2vol is used to apply this matrix to the aseg.mgz segmentation label * by using this method there is no interpolation, and the segmentations retain their original volume labels below is a sample script that will obtain the transformation matrix, apply it to the segmentation, then convert the segmentation to LAS orientation (remember first segmentation is in LAS). also, it includes the extraction of the individual regions of interest from the large aseg volume that contains all ROIs. then using a similar method as first, they are numbered 1 for L_side and 2 for R_side to make keeping track of them easier. conveniently if you are directly comparing freesurfer and first segmentations they are labeled with the same [[http://www.fmrib.ox.ac.uk/fsl/first/cma_subcortical_label.html|values]] by default. if you are extracting other regions that first does not segment, the label values can be seen in the '${SUBJECTS_DIR}//stats/aseg.stats' file #!/bin/bash SUBJ=$1 FREEDIR=~/path/to/freesurfer/data/$SUBJ/mri OUTDIR=~/path/to/put/resulting/ROI/files/$SUBJ mkdir -p $OUTDIR ##create a registration matrix from coronal to original space tkregister2 --mov $FREEDIR/rawavg.mgz --targ $FREEDIR/aseg.mgz --regheader --reg $OUTDIR/register.dat --noedit ##convert the aseg into original space mri_label2vol --seg $FREEDIR/aseg.mgz --temp $FREEDIR/rawavg.mgz --reg $OUTDIR/register.dat --o $OUTDIR/aseg.native.nii.gz ##put the native space aseg into LAS fslwrapbxh $OUTDIR #creates bxh headers bxhreorient --orientation=LAS $OUTDIR/aseg.native.bxh $OUTDIR/tmp.bxh #converts to LAS orientation bxh2analyze -s --niigz $OUTDIR/tmp.bxh $OUTDIR/aseg.native.LAS #converts to .nii.gz ## by thresholding just above and below the label value, then dividing by the value you can extract a specific ROI and set its value to 1 ##create ROIs for Amyg fslmaths $OUTDIR/aseg.native.LAS -thr 17.99 -uthr 18.01 -div 18 $OUTDIR/tmp_L_Amyg_free fslmaths $OUTDIR/tmp_L_Amyg_free -div $OUTDIR/tmp_L_Amyg_free $OUTDIR/tmp_L_Amyg_free fslmaths $OUTDIR/aseg.native.LAS -thr 53.99 -uthr 54.01 -div 54 $OUTDIR/tmp_R_Amyg_free ## then by multiplying the R ROI by 2, that changes its label value to 2 before adding L and R together fslmaths $OUTDIR/tmp_R_Amyg_free -div $OUTDIR/tmp_R_Amyg_free -mul 2 -add $OUTDIR/tmp_L_Amyg_free $OUTDIR/amyg_free ##create ROIs for Hipp fslmaths $OUTDIR/aseg.native.LAS -thr 16.99 -uthr 17.01 -div 17 $OUTDIR/tmp_L_Hipp_free fslmaths $OUTDIR/tmp_L_Hipp_free -div $OUTDIR/tmp_L_Hipp_free $OUTDIR/tmp_L_Hipp_free fslmaths $OUTDIR/aseg.native.LAS -thr 52.99 -uthr 53.01 -div 53 $OUTDIR/tmp_R_Hipp_free fslmaths $OUTDIR/tmp_R_Hipp_free -div $OUTDIR/tmp_R_Hipp_free -mul 2 -add $OUTDIR/tmp_L_Hipp_free $OUTDIR/hipp_free ##create ROIs for Accu fslmaths $OUTDIR/aseg.native.LAS -thr 25.99 -uthr 26.01 -div 26 $OUTDIR/tmp_L_Accu_free fslmaths $OUTDIR/tmp_L_Accu_free -div $OUTDIR/tmp_L_Accu_free $OUTDIR/tmp_L_Accu_free fslmaths $OUTDIR/aseg.native.LAS -thr 57.99 -uthr 58.01 -div 58 $OUTDIR/tmp_R_Accu_free fslmaths $OUTDIR/tmp_R_Accu_free -div $OUTDIR/tmp_R_Accu_free -mul 2 -add $OUTDIR/tmp_L_Accu_free $OUTDIR/accu_free ##create ROIs for Caud fslmaths $OUTDIR/aseg.native.LAS -thr 10.99 -uthr 11.01 -div 11 $OUTDIR/tmp_L_Caud_free fslmaths $OUTDIR/tmp_L_Caud_free -div $OUTDIR/tmp_L_Caud_free $OUTDIR/tmp_L_Caud_free fslmaths $OUTDIR/aseg.native.LAS -thr 49.99 -uthr 50.01 -div 50 $OUTDIR/tmp_R_Caud_free fslmaths $OUTDIR/tmp_R_Caud_free -div $OUTDIR/tmp_R_Caud_free -mul 2 -add $OUTDIR/tmp_L_Caud_free $OUTDIR/caud_free ## removes all temporary files from above rm $OUTDIR/tmp* by using fslstats -V, you can obtain the count of the voxels with the appropriate label. * this can be done from the command line, or included in the above script touch $OUTDIR/free_vols.txt for input in amyg_free hipp_free accu_free caud_free; do echo "$OUTDIR/$input L" >> $OUTDIR/free_vols.txt fslstats $OUTDIR/$input -u 1.1 -V >> $OUTDIR/free_vols.txt echo "$OUTDIR/$input R" >> $OUTDIR/free_vols.txt fslstats $OUTDIR/$input -l 1.1 -V >> $OUTDIR/free_vols.txt done