Note: This is written for FSL. You will need to have FSL installed on your computer. Feel free to write a SPM or AFNI version!
This page will walk you through fMRI data analysis from beginning to end. What we'll cover:
- Converting data to BIDS format
- Transferring data from Rolando
- 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
Converting Data to BIDS
Once you've finished collecting your data, email Yarik (director of the Center for Open Neuroscience) for help converting your data to BIDS format. Give him the path to your raw data—should be something like: /Meyer/Eleanor/1033-emporient
Conveniently, Yarik's pipeline also converts the data from PAR/REC files to nifti files, which is the format you'll need for analyses.
After each scan, MRI data gets exported to a computer called Rolando. We can’t actually run any analyses on here. Instead, we need to transfer it from Rolando to somewhere we can run our analyses: either 1) our lab’s file system, Flash, or 2) our lab share on the Discovery cluster.
From Rolando to Flash
- Log on to Rolando by opening a terminal window (on the server) and typing:
- ssh firstname.lastname@example.org
- You'll be prompted to enter your password
- Next, locate your data by typing:
- cd /inbox/BIDS/Meyer/YourName/your-study
- If you type ‘ls’ you should see all of the scans you’ve collected. (If you’re missing subjects, you may need to send SIDs and accession numbers to Yarik and request that he convert them to BIDS format.)
- To copy scans over to Flash, move up one directory (cd ../) and type:
- rsync -Lr your-study/ /flash/meyer/YourName/your-study/BIDS
- This will copy over any files from the subject directory on Rolando that are missing from your existing BIDS directory for your study, on flash. the nice thing about rsync is that it will only copy over new files, so there is no concern about being redundant
- The r flag makes sure it copies over the files recursively (including files embedded within subdirectories)
- The L flag makes sure it copies the original files and not symbolic links.
- Log on to Rolando by opening a terminal window (on the server) and typing:
From Rolando to Discovery
- Make sure you have a personal folder set up on the lab share on Discovery. See The Cluster for more info on logging into Discovery
- Copy the path to the folder you want to store your data in on Discovery—should be something like: /dartfs-hpc/rc/lab/M/MeyerM/YourName/NameOfStudy
- Log into Rolando and navigate to the folder containing your study (instructions above)
- Transfer your files to your desired folder on Discovery using the following command:
- scp -r FolderOnRolando yourDartmouthID@ndoli.dartmouth.edu:/path/to/your/folder/on/Discovery
- You'll be prompted to enter your password for Discovery, which may be different from your password for Rolando
Note: if you need to connect to Rolando or Discovery from off-campus, see The Cluster for instructions.
Once you have the data in the lab's Discovery share, you'll want to organize your study folder into a systemic format. Your fMRI data should already be in BIDS format. Suggested folder organization:
A. Anatomical Reorienting
Try opening a few subjects' anatomical images in FSLview. They may have come off the scanner in a weird orientation:
If so, 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
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