UNCDEPENdLab / fmri.pipeline

R package for running full fMRI GLM analyses in FSL and other software.
GNU General Public License v3.0
4 stars 0 forks source link

fmri.pipeline

Installing package directly from Github:

library(devtools)

install_github("UNCDEPENdLab/fmri.pipeline")

R package for running full fMRI GLM analyses in FSL and other software.

overview

This folder contains all scripts for running a model-based fMRI analysis using FSL FILM GLS for level 1 analyses, OLS for level 2 analyses (combining runs), and and FLAME 1+2 for level 3 analyses. The pipeline is structured to run on a high-performance computing cluster using the TORQUE scheduler. The pipeline will spawn one job submission for each first-level (run) model, then setup job dependencies for the level 2 and level 3 analyses so that the pipeline completes in a funnel-like fashion (many first-level runs funneling into a few group models).

For the pipeline to work, processed MRI data should be structured as follows:

<fmri_dir>/<subject_id>/<expectdir>/<paradigm_name><run_number>/<expectfile>

For descriptions of these field names, see documentation below for run_fsl_pipeline.R.

In general, most scripts in the pipeline expect at least two environment variables to be set (passed using qsub -v in the cluster environment). The first, fsl_pipeline_file is the full path to the analysis specifications defined in run_fsl_pipeline.R and saved to an RData object in the configuration_files subfolder. The second, run_model_index specific element within l1_model_variants that is the current object of analysis. That is parallelization is achieved in part by spawning many jobs that are split in terms of which level 1 model is being executed. These environment variables should be passed by the push_pipeline function called in run_fsl_pipeline.R, but knowing about their existence is helpful for setting up or debugging subsidiary scripts (e.g., the beta extraction scripts).

core scripts

run_fsl_pipeline.R

This is the primary script for running the entire voxelwise GLM pipeline. It constructs a full model specification in a list called fsl_model_arguments, which is saved as an RData object and passed to subsidiary scripts. The core ingredients of that object are described below.

Once the fsl_model_arguments object is created, it is saved, then push_pipeline executes the full analysis pipeline based on the specification.

extract_sceptic_map_betas.R

This script looks for whole-brain-significant clusters in each covariate/predictor included in a level 3 analysis. It uses zthresh and clustsize settings in the fsl_model_arguments list to find the clusters, employing AFNI's 3dclust program specifically.

Importantly, for a three-level analysis in FSL, each level 1 contrast becomes the dependent variable in a level 3 model such that there are we need a third-level FLAME analysis for each level 1 contrast, yielding one .gfeat folder per level 1 contrast. This script anticipates this structure and walks over all level 1 and level 3 regressors.

This is accomplished by a for loop over the level 1 contrasts (for (l1 in 1:n_l1_copes) {), level 2 contrasts (for (l2 in 1:n_l2_contrasts) {), and level 3 contrats (for (l3 in 1:n_l3_copes) {). For each combination, 3dclust is run to identiy whole-brain significant clusters. The coordinates and labels are identified using AFNI whereami, then the cluster mask (integer-valued, one per cluster) is read into R. The mean regression coefficient for each subject and/or run (if calculate_l1_betas is TRUE) is extracted for each cluster. These are compiled into a long-form tidy data.frame (ending in 'betas.csv'). The script generates on cluster file per level 1 contrast, with columns in that file denoting level 2 and level 3 contrasts.

For each level 1 contrast, two tidy CSV are produced, one labeled with 'metadata' and the other with 'betas'.

extract_sceptic_map_betas_atlas.R

This script extracts voxelwise regression coefficients for each voxel within an a priori set of mask files, atlas_files. Whereas extract_sceptic_map_betas.R extracts one coefficient within each significant cluster, extract_sceptic_map_betas_atlas.R provides regression coefficients for each voxel in a mask. It produces csv files labeled with *atlas_betas.csv.gz for the regression coefficients or *atlas_zstats.csv.gz for the corresponding voxelwise z-statistics (from FSL level 1 analysis). The structure of these files is very close to the voxelwise deconvolution outputs from extract_sceptic_atlas_deconvolved.R, but those contain BOLD/neural estimates at each time point, while this script is just one coefficient for a single subject (from the second-level GLM). The files produced by this script formed the basis of voxelwise analyses along the hippocampal long axis depicted in Figure 2 of Dombrovski, Luna, and Hallquist.

extract_sceptic_atlas_deconvolved.R

This script walks over a set of atlas NIfTI files, atlas_files, runs a voxelwise deconvolution algorithm on each BOLD acquisition in the study (i.e., multiple runs per subject, multiple subjects). The algorithm used in the Dombrovski, Luna, & Hallquist paper is from Bush and Cisler (2013) and is called Bu12 in that paper. We have largely adapted an optimized C++ codebase provided by Keith Bush for speeding up the voxelwise calculation. This program, called deconvolvefilter, is committed in compiled form to Github, but is an ELF binary that will only run on Linux-based machines. The fallback position would be to run the deconvolve_nlreg function in the dependlab R package, which is the same algorithm, but a mixture of C++ and R (and slower).

The program produces two long-form data.frame for each run of data, one containing the deconvolved time series (labeled 'deconvolved') and the other containing the input timeseries (i.e., the preprocessed fMRI data without deconvolution; labeled 'original'). The structure of these data.frames is:

fsl_model_arguments object

The fsl_model_arguments file gets saved by run_fsl_pipeline.R and is a critical input to most subsidiary scripts in the pipeline and in additional analysis scripts. Its structure is as follows: