Skip to content

Module 5 FreeSurfer GLM

Jared Tanner edited this page Feb 17, 2026 · 7 revisions

Exporting FreeSurfer stats/metrics

Walk-through video here (most of this is repeated in the Zoom link below, if you prefer). I recommend watching this and then skipping the first part of the Zoom meeting.

https://youtu.be/nqWz6PY7XrQ

Commands of interest

https://surfer.nmr.mgh.harvard.edu/fswiki/asegstats2table

https://surfer.nmr.mgh.harvard.edu/fswiki/aparcstats2table

Reminder: You will need to set your FreeSurfer subjects directory: export SUBJECTS_DIR=/path/to/directory

Group Analysis

A previous recorded class Zoom recording (watch at 1.5 speed) will provide an overview of FreeSurfer stats exporting and what a GLM is: https://ufl.zoom.us/rec/share/Sj4uL2Vl9dtC5S7alkZj6sfRJGdRnSVZsO7KfUBWm_5H64hmxaoU1NIhgUTsvfG3.LUeIv6Wuzs9hQ3BW

Follow along with the tutorial to learn how to perform a group analysis (first 26 minutes or so): https://youtu.be/pEwirpN_3-Q?si=KNm83ogdXEiZWk4G

Link to the tutorial that I follow in the above video: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/GroupAnalysis

Here are the commands I use in the video (with some modifications to match the 2026 class data structure)

Steps to run through the FreeSurfer GLM tutorial in a HiPerGator Desktop session.

Some of the paths may be different from year to year. Make sure the paths match where the data are this year and where your directory is.

I recommend running this in a HiPerGator Desktop session. Request 8 GB of memory and at least 3 hours of time.

cd /blue/psy4930/share/data/Module5 #Go into the Module 5 directory

cp tutorial_data.tar.gz /blue/psy4930/share/students/{your gatorlink id} #Copy tutorial_data.tar.gz from Module5 into your personal directory. You could instead copy it to /blue/psy4930/{your gatorlink id}

cd /blue/psy4930/share/students/{your gatorlink id} #Go into your directory. Make sure you change the {your gatorlink id} to your id.

tar -xzvf tutorial_data.tar.gz && rm tutorial_data.tar.gz #extract the tutorial data and then delete the .tar.gz file. This will take a while to complete

cd tutorial_data_20190918_1558 #Go into the tutorial data directory

rm -fr dicom_challenge/ diffusion_recons/ diffusion_tutorial/ fsfast-functional/ fsfast-tutorial.subjects/ practice_with_data/ #Remove some unnecessary data to keep more space available

cd tutorial_data_20190918_1558/buckner_data/tutorial_subjs/group_analysis_tutorial #change into the group analysis tutorial directory

module load freesurfer/7.4.1 #Make FreeSurfer 7.4.1 available to use

export SUBJECTS_DIR=$PWD #set FreeSurfer's subjects directory to your current location. Alternatively, you can put in the whole path after the equal sign

echo $SUBJECTS_DIR #This will verify that SUBJECTS_DIR really is your current location. Make sure the path printed to the screen is where you think you are.

cd glm && ls #go in the GLM directory and display contents. The && runs the ls command if the previous command successfully completed.

Preprocessing for the analysis has already been done.

You do not need to run what's in the next block of code but are welcome to do so

mris_preproc --fsgd gender_age.fsgd \
--target fsaverage \
--hemi lh \
--meas thickness \
--out lh.gender_age.thickness.00.mgh

mri_surf2surf --hemi lh \
--s fsaverage \
--sval lh.gender_age.thickness.00.mgh \
--fwhm 10 \
--cortex \
--tval lh.gender_age.thickness.10.mgh

This is the end of the code that has already been run. Run everything after this.

This next command runs the general linear model, which is a linear regression. What you are testing is if there is a relationship between cortical thickness and age, adjusting for sex

mri_glmfit \
--y lh.gender_age.thickness.10.mgh \
--fsgd gender_age.fsgd dods \
--C lh-Avg-thickness-age-Cor.mtx \
--surf fsaverage lh \
--cortex \
--glmdir lh.gender_age.glmdir

View the analysis results

freeview -f $SUBJECTS_DIR/fsaverage/surf/lh.inflated:annot=aparc.annot:annot_outline=1:overlay=lh.gender_age.glmdir/lh-Avg-thickness-age-Cor/sig.mgh:overlay_threshold=1,5 -viewport 3d -layout 1

Stats and Group Cortex GLM Assignment

  1. Using the data you previously processed for Module3 (e.g., /blue/psy4930/share/students/[your gatorlink]/ADNI_bids/derivatives/freesurfer), export aseg and aparc stats for subjects. For the homework you will submit one Excel or csv file with the aseg, and left and right aparc stats for the subjects.

Hint: you will need to set your SUBJECTS_DIR first. Also, if your processing for that assignment did not complete, this step will fail. If you have issues, contact me and I can provide data.

Example here for how to export the aseg stats: https://surfer.nmr.mgh.harvard.edu/fswiki/asegstats2table

And here for the aparc (cortical) stats: https://surfer.nmr.mgh.harvard.edu/fswiki/aparcstats2table

  1. For the group analysis, your assignment will be to use the tutorial data to run an analysis on the left hemisphere. Create an FSGD file and contrasts for a thickness study. An example is in the tutorial dataset (called gender_age.fsgd) but you will create a new analysis by changing Age to some other continuous variable and modifying the values. An example is if you wanted to assess if there was a relationship between running speed and cortical thickness. You can be creative. You can create a copy of the .fsgd file and modify and rename it. You will also create a copy and rename the contrasts file (lh-Avg-thickness-age-Cor.mtx). You are welcome to modify this as appropriate.

First, copy the tutorial dataset into your directory (you should have done this already when going through the module).

For this part of the assignment, you can refer to this tutorial video (start at about 26 minutes): https://youtu.be/pEwirpN_3-Q?si=KNm83ogdXEiZWk4G

What you submit

  1. An Excel or CSV file with the aseg and aparc values from both brains.
  2. The new fsgd file (sample one in buckner_data/tutorial_subjs/group_analysis_tutorial/glm/)
  3. The new contrasts file (sample one in the same location as above)
  4. Two screenshots of the results displayed in Freeview.

Clone this wiki locally