Note: This is written for FSL. You will need to have FSL installed on your computer. Feel free to write a SPM version!
This page will walk you through fMRI data analysis from beginning to end. What we'll cover:
- Transferring data from the Cluster
- Converting data to the format you need
- Data organization
- Preprocessing data
- Setting up regressors
- First level analysis (run level)
- Second level analysis (participant level)
- Group level analysis (all participants)
- Automating analyses through the command line
Use SSH to connect with the Discovery cluster and retrieve your data (see THE CLUSTER for more info).
Data from the DBIC comes in PAR/REC format. You'll need to convert it to NIFTI format to use in FSL. This page explains how to convert data to different formats.
Once you have the data in the lab's cluster folder, organize it into a systemic format. Do the same on your local computer. Suggested organization format:
It's also good to rename your runs to run1.nii.gz, run2.nii.gz, run3.nii.gz, and the mprage to anat.nii.gz
A. Anatomical Reorienting
For some reason, the images are collected in a weird orientation:
The first thing you need to do is to reorient them such that they match the standard template. Let's start with the anatomical.
Your reorientation script should look something like the example below. You can run your script from the command line (look here if you're not sure how). Make sure you have FSL open, or none of the FSL commands will work!
# navigate to where the anatomical is stored cd ../[project_name]/data/fmri/[subID]/anat # reorient fslreorient2std anat.nii.gz anat.nii.gz # check new orientation in fslview fslview anat
Don't they look much better now? BTW, this is a necessary step, it is not just for aesthetic reasons - otherwise you'll run into trouble with registration!
B. Anatomical Brain Extraction
One of the pre-processing steps in first-level analysis is the registration of the functional images to the higher resolution structural image (i.e. superimposing the each subject's BOLD signals onto their anatomical map). For the registration to work accurately, you must skull-strip (aka brain extract) the structural volume.
Navigate to the folder with the anatomical image. The following command will create a new file called anat_brain, which is the skull-stripped brain.
bet anat.nii.gz anat_brain -f 0.3
Always, ALWAYS check the new image to make sure the skull stripping worked properly. If you find the stripping to be too aggressive or lenient, tweak the "f" parameter.
C. Functional Reorienting
Reorient the functional data exactly as you did the anatomical - no need to brain extract/motion correct until the next stage
fslreorient2std run1.nii.gz run1.nii.gz
fslreorient2std run2.nii.gz run2.nii.gz
fslreorient2std run3.nii.gz run3.nii.gz
Setting Up Regressors
The goal is to have an explanatory variable (EV) file for each regressor for each run for each subject. For basic on/off designs, the user can choose preset waveforms. However, you'll likely need to input a custom waveform for each EV. We will use the custom 3-column format to specify our input waveforms. (You can read about this in the FSL GUI's balloon popup help.)
Each EV file is a .txt file consisting of triplets of numbers. The first number in the triplet is the onset of the period in seconds, the second number is the duration of the period in seconds, and the third number is the value of the input during that period.
In the following example, we will only have 2 EVs.
- EV1 = Positive Image
- EV2 = Neutral Image
In both cases, the first column is the time of image presentation, the second column is the duration of image presentation, and the 3rd number is a binary variable denoting if the image is positive/neutral.
You'll need to generate EV files particular to your experiment in MATLAB. Email the lab manager for an example EV generator script.
This section will walk you through each tab in the GUI for first-level analysis, i.e. the analysis for each individual run.
A. Data Tab
- Press "Select 4D data" and select the first run (don’t just type “run***.nii.gz” or you probably won’t end up setting the full pathname). If you have already done motion correction, make sure you select the motion-corrected version of the file.
- For the Output directory, name a to-be-created folder inside the subject’s data folder. If you follow the suggested data structure, it should be something like:
- Set the TR (repetition time) length - in this case, 2s.
- The high pass filter cutoff is preset to 100 sec. This is chosen to remove the low frequency trends (in this case, frequencies slower than 100 sec), and must be long enough to avoid removing the signal of interest. In general you need to ensure that this is not set lower than the duration of each block in your run.
B. Pre-stats Tab
- Check BET and Motion Correction
- Slice timing correction: You'll need to select the slice acquisition order that your data was acquired in. If you're not sure what order was used, this page explains how to find out. You can use the "TriggerTime" field in the DICOM head to confirm the slice acquisition timing.
C. Stats Tab
- Click "Full Model Set-Up"
- Create your EVs - in our case, we can name EV1 "Positive" amd EV2 "Negative"
- Click Basic Shape = Custom (3 column format)
- Filename = navigate to the EV file for the respective run
- Under the contrast & F-tests, create a contrast and add in the weights. For this example, we will create a contrast in which Pos = + 1 and Neu = - 1.
D. Poststats Tab
Defaults are usually fine - usually the only thing you might need to change is the threshold, but for the first-level it really doesn't matter.
E. Registration Tab
By default, FSL will register the middle-timepoint image (saved as example_func in the .feat output directory) directly to the standard space template. This isn’t really ideal, especially since we have a high-resolution structural of the subject. So turn on the "Main structural image" option so that the lowres fMRI image is first registered to a brain-extracted highres structural image from the same subject. This highres image is then registered to the standard space template, and then the two registrations are combined to give an example_func2standard.mat transform file which can be used later to resample the fMRI stats into standard space.
- Leave “Initial structural image” OFF.
- Turn ON “Main structural image” and choose the skull-stripped structural volume.
- Leave“Standard space” structural image ON
- for type of search, I would click 7 DOF - I have no idea what BBR is. If any one wants to try it out and compare, please do :)
Once you're done with all this, submit the job to FSL, sit back, and wait for it to churn out those first level analyses!
In the first-level analysis, we examined individual runs. Now in the second-level analysis, we'll look at the results across runs for one subject. Once again, open the FSL GUI. At the top, select higher-level analysis.
A. Data Tab
- Usually, we select "Inputs are lower-level feat Directories"
- Number of inputs = number of runs (and enter paths for respective runs)
- Output directory:
B. Stats Tab
- Select Fixed Effects. (A discussion of why we use Fixed effects vs. mixed effects is beyond the scope of this page. The rule of thumb is that when analyzing runs from 1 subject, we run Fixed Effects, but for Group Level Analysis, we run Mixed Effects.)
- Under Full Model Setup, make sure all runs have weights (usually just "1").
To visualize results for a particular subject, open fslview, open Standard Brain (MNI152_T1_2mm_brain), then load the file 2ndlevel.gfeat/cope1/thresh_zstat.nii.gz
This is the cluster-size corrected stats map. Note that when looking at data from a single subject, you rarely see significant activation. As such, you might want to look at stats/zstat.nii.gz, which will give you the unthresholded map where you can check for subthreshold activation. Just for kicks, here's the FWE cluster-size corrected image of a brain smiling at positive pictures :)
The steps are the same as the Second-Level Analyses, EXCEPT:
- Instead of loading the feat directory for individual runs, load in higher level feat directories for subjects
- Instead of Fixed Effects, use Mixed Effects.
See Andy's Brain Blog Group FEAT tutorial if you need more guidance.
As you may have figured out by now, there are two ways of interacting with FSL:
- Through the GUI
- Through running FSL scripts with the command line
Strategy 1 is useful for generating your initial analysis scripts. But once you have a script for one participant, there's no need to do all that button-mashing again for the other participants! Instead, you can simply swap in the participant ID in the analysis script. Better yet, you can write a bash script with a loop that swaps in all participant IDs and submits, say, 30 analyses to the cluster in quick succession. Then, with Strategy 2, you can simply run your "master script", sit back, and eat some popcorn (or whatever snack of your choosing).
Strategy 2 is explained in depth in another Andy's Brain Blog tutorial.
Adapted from a post by the wonderful Yuan Chang Leong