FreeSurfer terminal commands


Useful freesurfer commands to perform various actions. 

What is freesurfer?

FreeSurfer is a software package for the analysis and visualization of structural and functional neuroimaging data from cross-sectional or longitudinal studies. It is developed by the Laboratory for Computational Neuroimaging at the Athinoula A. Martinos Center for Biomedical Imaging. FreeSurfer is the structural MRI analysis software of choice for the Human Connectome Project.

Personally I started using freesurfer only a few months ago. At the beginning, I found this package very non-intuitive and complicated. They are open source and their support forum is great, however I failed to find a page that addresses to the beginners questions. Here I am sharing the notes I took while learning how to use this package. If you have questions you can ask me via contact page, even if I am not an expert yet, I enjoy challenging questions. 


# Open Terminal #
This is the very first step, on Mac open your terminal and after setting your path as indicated, enter the lines to the terminal. This step lets you teach the paths (freesurfer application path, subject folders, the path of the SetUpFreeSurfer.sh) to your bash system. 
export ´FREESURFER_HOME=/*data address*/freesurfer_v53/freesurfer

export SUBJECTS_DIR=/*data address*/fsAnatomical

source $FREESURFER_HOME/SetUpFreeSurfer.sh 

PATH=$PATH:/Applications/MATLAB_R2015a.app/bin

I found the screenshot below via google images and reflects how my terminal appeared after setting these paths and since then every time I open a new terminal tab. 

Fully automated cortical reconstruction with anatomical image
cd /*data address*/Retinotopy/SUBJID/mri/orig

mri_convert SUBJID.nii 001.mgz

recon-all -s SUBJID -all

Coregistration between structural and functional data

#Manual registration

tkregister2 --mov *functional data* --s SUBJID --regheader --reg  SUBJID/stats/register.dat 
#Automatic registration
bbregister --mov *functional data* --bold \
    --s SUBJID \
    --init-fsl --reg $SUBJECTS_DIR/SUBJID/stats/register.dat
# To check results, run:
tkregisterfv --mov *functional data* --reg $SUBJECTS_DIR/SUBJID/stats/register.dat --surfs
 

Overlay functional data on the cortical surface

“tksurfer” function is the one I use the most often, as I recently realized. This function allows you to overlay volume based masks (ROIs, spmT maps, etc) onto inflated subject surface. Yet, once the interface is ready, you are allowed to shift to pial surface view as well.

tksurfer SUBJID lh inflated -overlay /*path to the spmT volume file*/


e.g. /*first level SPM folder path*/spmT_0001.nii

Export labels to volum based ROIs

Labels are the default region of interest output for the freesurfer. Therefore, they are surface based and not compatible to view with SPM, mriCRO(n). As you might be already used to nifti (*.nii) files, they are volume based. The code below is to convert a surface based label to volume based nifti file. For this we use “mri_label2vol” function.

mri_label2vol --label /*data file*/SUBJID/label/lh.V1_exvivo.thresh.label --temp /*functional image to register on the same space*/ --reg /*data file*/SUBJID/stats/register.dat --fillthresh 0 --subject SUBJID --hemi lh --o /*target destination for the new ROI*/lh_SUBJID_V1.nii