fMRI Preprocessing using SPM 12

John Muschelli

2019-01-08

Overview of spm12r functions

Requires MATLAB (installs SPM to R library)

Kirby21 Data used

We will use the subject 113 from the Kirby21 data set to illustrate some basic operations of functional magnetic resonance imaging (fMRI). We will load in the T1 anatomical image and the fMRI from the respective packages.

library(kirby21.t1)
library(kirby21.fmri)
install_dir = tempdir()
outdir = tempfile()
dir.create(outdir)
stopifnot(download_fmri_data(outdir = outdir))
## cloning into '/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba063861b5c/kirby21.fmri'...
## Receiving objects:   1% (1/98),    8 kb
## Receiving objects:  11% (11/98),    8 kb
## Receiving objects:  21% (21/98),    8 kb
## Receiving objects:  31% (31/98),    8 kb
## Receiving objects:  41% (41/98),    8 kb
## Receiving objects:  51% (50/98), 132726 kb
## Receiving objects:  61% (60/98), 267438 kb
## Receiving objects:  71% (70/98), 267438 kb
## Receiving objects:  81% (80/98), 267438 kb
## Receiving objects:  91% (90/98), 267438 kb
## Receiving objects: 100% (98/98), 267438 kb, done.
stopifnot(download_t1_data(outdir = outdir))
## cloning into '/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba0796e680f/kirby21.t1'...
## Receiving objects:   1% (2/117),    8 kb
## Receiving objects:  11% (13/117),    8 kb
## Receiving objects:  21% (25/117),    8 kb
## Receiving objects:  31% (37/117),    8 kb
## Receiving objects:  41% (48/117),    8 kb
## Receiving objects:  51% (60/117), 30704 kb
## Receiving objects:  61% (72/117), 46544 kb
## Receiving objects:  71% (84/117), 46544 kb
## Receiving objects:  81% (95/117), 46544 kb
## Receiving objects:  91% (107/117), 46544 kb
## Receiving objects: 100% (117/117), 46544 kb, done.
functional = get_fmri_filenames(ids = 113, visit = 1, outdir = outdir)
anatomical = get_t1_filenames(ids = 113, visit = 1, outdir = outdir)
files = c(anatomical = anatomical,
          functional = functional)
files
##                                                                                                     anatomical 
##   "/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba07b27947e/visit_1/113/113-01-T1.nii.gz" 
##                                                                                                     functional 
## "/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba07b27947e/visit_1/113/113-01-fMRI.nii.gz"

We know the repetition time (TR) is 2 seconds for this data. It may be encoded in the NIfTI file itself or come from a parameter file from the scanner. We will drop the first 20 seconds to allow for signal stabilization.

library(neurobase)
## Loading required package: oro.nifti
## oro.nifti 0.9.11
tr = 2 # seconds
DROP = 10 # 20 seconds for stabilization

fmri = readnii(files["functional"])

times = (DROP + 1):ntim(fmri)
run_fmri = copyNIfTIHeader(fmri, fmri[,,,times], drop = TRUE)

Now run_fmri contains a nifti object with the first 10 volumes dropped. We will pass this into the subsequent functions.

Checking MATLAB

As SPM requires MATLAB and calls all the functions through the matlabr package, we will have checks in this vignette/workflow that the user has MATLAB. The have_matlab() function returns a logical that is TRUE when matlabr can find MATLAB to run the subsequent commands.

library(matlabr)
have_matlab()
have_matlab()
## [1] TRUE

If this is not TRUE, almost none of the functionality below will run because it would simply result in errors.

Overall Processing

We will show how to do spatial realignment, slice-timing correction, spatial normalization to the MNI template (2 different ways), and spatial smoothing. Overall, there are many different ways to order these operations, with different options, so this represents just one way to organize a preprocessing pipeline.

Image Realignment

Realignment is referring to in this case as within-subject registration of the 4D fMRI data.

## # Getting Number of Time Points
## # Reslice is mean
## [1] "spm"      "script"   "outfiles" "rp"       "mean"     "mat"
##  [1] "spm.spatial.realign.estwrite.data"            
##  [2] "spm.spatial.realign.estwrite.eoptions.quality"
##  [3] "spm.spatial.realign.estwrite.eoptions.sep"    
##  [4] "spm.spatial.realign.estwrite.eoptions.fwhm"   
##  [5] "spm.spatial.realign.estwrite.eoptions.rtm"    
##  [6] "spm.spatial.realign.estwrite.eoptions.interp" 
##  [7] "spm.spatial.realign.estwrite.eoptions.wrap"   
##  [8] "spm.spatial.realign.estwrite.eoptions.weight" 
##  [9] "spm.spatial.realign.estwrite.roptions.which"  
## [10] "spm.spatial.realign.estwrite.roptions.interp" 
## [11] "spm.spatial.realign.estwrite.roptions.wrap"   
## [12] "spm.spatial.realign.estwrite.roptions.mask"   
## [13] "spm.spatial.realign.estwrite.roptions.prefix"
## cloning into '/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12'...
## Receiving objects:   1% (65/6496),   18 kb
## Receiving objects:  11% (715/6496),  259 kb
## Receiving objects:  21% (1365/6496), 3167 kb
## Receiving objects:  31% (2014/6496), 8349 kb
## Receiving objects:  41% (2664/6496), 10204 kb
## Receiving objects:  51% (3313/6496), 11596 kb
## Receiving objects:  61% (3963/6496), 32906 kb
## Receiving objects:  71% (4613/6496), 37689 kb
## Receiving objects:  81% (5262/6496), 59573 kb
## Receiving objects:  91% (5912/6496), 71860 kb
## Receiving objects: 100% (6496/6496), 129916 kb, done.
## # Getting Number of Time Points
## 
## # Reslice is mean
## # Running matlabbatch job
## # Creating matlabbatch job
## # Adding SPMDIR: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12\n
## Running job: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba0140cb031.m, which calls /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba03662dad4.m
## Command run is:
## matlab  -nodesktop -nosplash -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba0140cb031.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## # Result is 0
## [1] "spm"      "script"   "outfiles" "rp"       "mean"     "mat"     
## [7] "result"

Overall the spm12_realign does the realignment. There is some discussion of performing realignment before slice-timing correction because estimation of motion parameters may be skewed after slice-timing correction. We see that the output realigned has the output 4D fMRI data (outfiles), the realignment parameters (rp), voxel-wise mean after realignment (mean), and the matrix of transformations for the 4D series (mat).

Reading in the RP file

Here we can read in the rp file to show the estimated parameters. These can be used as regressors in motion correction for further analyses.

##                x            y             z          roll         pitch
## [1,]  0.00000000  0.000000000 -4.410886e-16 -2.507721e-37  2.507721e-37
## [2,] -0.03250006  0.022276041  6.105989e-02 -3.616417e-04 -1.389442e-04
## [3,]  0.02884634 -0.005311455  5.649025e-02 -3.774759e-04 -1.798493e-04
## [4,]  0.02379671 -0.056644719  2.574345e-02  5.180747e-05 -1.756995e-04
## [5,]  0.04293237 -0.056942376  2.414558e-02 -2.210120e-05 -8.367122e-05
## [6,]  0.02858868 -0.038461745  8.289177e-02 -2.785027e-04 -3.532936e-04
##                yaw
## [1,]  6.288666e-74
## [2,] -2.732142e-04
## [3,]  1.048090e-04
## [4,] -6.489734e-06
## [5,]  2.291027e-04
## [6,] -8.279382e-06
## [1] 200   6

Slice-Timing Correction

A slice-timing correction does interpolation since each slice was not actually taken at the same time point, but a shifted time point over the course of an entire TR. The correction requires you to input the reference slice (in this case the median, ref_slice), the repetition time (tr), time between the first and the last slice within one scan (ta), and the order the slices were acquired. In our case, it was done in an ascending, contiguous order, so we created the slice order as such. If you used descending or interleaved acquisition, then this must be changed accordingly.

## [1] "spm"                 "script"              "orig_filename"      
## [4] "base_name"           "temporary_directory" "outfile"
## [1] "spm.temporal.st.scans"    "spm.temporal.st.nslices" 
## [3] "spm.temporal.st.tr"       "spm.temporal.st.ta"      
## [5] "spm.temporal.st.so"       "spm.temporal.st.refslice"
## [7] "spm.temporal.st.prefix"
## # Running matlabbatch job
## # Creating matlabbatch job
## # Adding SPMDIR: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12\n
## Running job: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba055ccb9b1.m, which calls /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba07639f042.m
## Command run is:
## matlab  -nodesktop -nosplash -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba055ccb9b1.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## # Result is 0
## [1] "/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/afilebba053f1c6cb.nii"

We see the output aimg has the filename of the slice-timing corrected 4D image. We also read in the mean image into a nifti object (mean_nifti).

Spatial Normalization

AC-PC Alignment

For the subsequent image normalization steps, SPM assumes the data is aligned along the anterior commissure (AC) posterior commissure (PC) line (AC-PC). The acpc_reorient function (based on nii_setOrigin from Dr. Chris Rorden) will do this. The syntax is that the first file (mean_img) is used to estimate the line/plane and the subsequent files are reoriented using this estimation (aimg). These are changes to the header of the image and the image with the new header is written to the same file as the input file.

## 
##  #Reorientation /private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/RtmpuSoCbO/meanfilebba053f1c6cb.nii
## Script created: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba025a7fcdc.m
## Command run is:
## matlab  -nodesktop -nosplash -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba025a7fcdc.m'); catch err, disp(err.message); exit(1); end; exit(0);"
##   /private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/RtmpuSoCbO/meanfilebba053f1c6cb.nii 
## "/private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/RtmpuSoCbO/meanfilebba053f1c6cb.nii" 
##             /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/afilebba053f1c6cb.nii 
##    "/private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/RtmpuSoCbO/afilebba053f1c6cb.nii"

Direct Normalization

Here, we will spatially normalize the fMRI to the MNI template using spm12_normalize. Here we specify a standard bounding box for a 2mm MNI template. We are taking the mean image and directly registering it to the MNI template (T1-weighted), and applying that transform to the other.files, in this case the mean image and the 4D fMRI image.

##      [,1] [,2] [,3]
## [1,]  -90 -126  -72
## [2,]   90   90  108
## # Adding SPMDIR: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12
## # Running script /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m
## which calls /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Normalize_Estimate_and_Write_job.m
## Command run is:
## matlab  -nodesktop -nosplash -singleCompThread -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## # Result is 0
## [1] "spm"      "script"   "outfiles" "result"

We see tbe output direct_norm is a list with the output files (outfiles). The order of these files is the same as the order of those put in. In this case, the first file of outfiles is the normalized mean image and second is the normalized 4D image. Here we read in the spatially normalized mean image to compare to the template later.

Indirect Normalization

Indirect normalization refers to spatially normalizing the co-registered anatomical T1-weighted image to the MNI template. This transformation is applied to the mean image and 4D fMRI image. This is also referred to as Unified Segmentation.

Anatomical MRI Coregistration to Mean fMRI

Here we will perform the registration of the T1-weighted anatomical image into the space of the mean fMRI image after realignment. This is referred to as “co-registration” as it is within-subject registration, but across modalities (where we referred to within-subject, within-modality as realignment).

Here, we also reorient the anatomical image the AC-PC line. We then perform the coregistration using spm12_coregister, where the fixed image is the mean image and the moving image is the anatomical.

## [1] "/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/RtmpuSoCbO/113-01-T1.nii"
## attr(,"nbrOfBytes")
## [1] 44564832
## 
##  #Reorientation /private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/RtmpuSoCbO/113-01-T1.nii
## Script created: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba0e10e410.m
## Command run is:
## matlab  -nodesktop -nosplash -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba0e10e410.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## Checking Filenames
## # Running matlabbatch job
## # Creating matlabbatch job
## # Adding SPMDIR: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12\n
## Running job: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba0579cebc0.m, which calls /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba038cbfa49.m
## Command run is:
## matlab  -nodesktop -nosplash -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/filebba0579cebc0.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## # Result is 0
## # Removing scripts

We see the anatomical image has been transformed and resliced into the mean fMRI image space (and thus has the resolution of that image).

Anatomical MRI Segmentation (and Spatial Normalize Estimation)

Here we perform the segmentation of the co-registered anatomical image from above. This will segment the image into 6 different regions, where the regions are gray matter, white matter, cerebrospinal fluid (CSF), bone, soft tissue, and the background. You should inspect these visually before using them to ensure quality and no reordering due to artifacts.

## # Adding SPMDIR: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12
## # Running script /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m
## which calls /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Segment_job.m
## Command run is:
## matlab  -nodesktop -nosplash -singleCompThread -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## # Result is 0
## [1] "spm"                 "script"              "result"             
## [4] "outfiles"            "outmat"              "deformation"        
## [7] "inverse_deformation"

In order to segment the image, SPM spatially normalizes the image to the MNI template, however. This transformation will be the one we use to transform the fMRI data to MNI space. We see in seg_reg a deformation file, which is the transformation. We also see the output segmentation files of the probability of each class, in native space. We only tend to care about the first 3 categories.

“Hard” Segmentations

If you would like to convert these class probabilities to a “hard” segmentation, you must read the data in and use spm_probs_to_seg for that. In spm_probs_to_seg, each voxel is assigned the class with the maximum probability, with options on how to choose ties (first is default). We can then remove the labels for bone, soft tissue, and background (> 3).

Applying Spatial Normalization Transformation

Now that we have estimated the transformation from the T1 image, we can take that deformation and apply it to the fMRI data using spm12_normalize_write. Again, we are registering to the MNI template and will use a standard bounding box. We pass the anatomical, mean fMRI, and 4D fMRI data in to be transformed.

## # Adding SPMDIR: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12
## # Running script /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m
## which calls /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Normalize_Write_job.m
## Command run is:
## matlab  -nodesktop -nosplash -singleCompThread -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## # Result is 0
## # Removing scripts
## [1] "spm"          "script"       "deformation"  "other_fnames"
## [5] "outfiles"     "result"

Now we have the indirect spatially normalized data in MNI template space

Spatial Smoothing

Here we will perform spatial smoothing of the 4D fMRI data in template space. We can set the full-width half max (FWHM) for the Gaussian smoother. The relationship between the FWHM and the Gaussian standard deviation is:

\[ FWHM = \sigma \sqrt{8 \log(2)} \] where \(\log\)` is the natural log.

## # Adding SPMDIR: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12
## # Running script /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m
## which calls /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Smooth_job.m
## Command run is:
## matlab  -nodesktop -nosplash -singleCompThread -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## # Result is 0
## # Removing scripts

In many applications, this is the data you will use for post-processing and analysis. Motion correction has usually been applied above, but some motion correct this data as well.

## # Adding SPMDIR: /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/spm12
## # Running script /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m
## which calls /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Smooth_job.m
## Command run is:
## matlab  -nodesktop -nosplash -singleCompThread -nodisplay -r  "try, run('/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpuSoCbO/Executable.m'); catch err, disp(err.message); exit(1); end; exit(0);"
## # Result is 0
## # Removing scripts

Here we can smooth the mean image in MNI space. We could have also calculated a new mean image and smoothed that. Or we could have calculated a mean of the smoothed 4D data. They all may be slightly different.

Bibliography