Thanks, Paul.
We are using the MRICron for visual inspection of fMRI data.
" Well, that looks like it could just be the CSF in the midsagittal part of the brain"
Above statement of yours is scaring me because of the whole intention of doing all these steps is to retain the brain activities present in the whole brain for extracting ALFF values.
My main concern is why the skull is appearing in the RSFC files?
Here I have attached the overlay of ‘run_01_ALFF+tlrc’ and the final anatomical image. It clearly shows the presence of skull in the RSFC file. It started to appear only after I used @SSwarper function. For your reference, I have pasted the proc codes here.
#!/bin/tcsh -xef
echo "auto-generated by afni_proc.py, Wed Mar 27 16:32:15 2019"
echo "(version 6.32, February 22, 2019)"
echo "execution started: `date`"
# to execute via tcsh:
# tcsh -xef proc.CSRI002 |& tee output.proc.CSRI002
# to execute via bash:
# tcsh -xef proc.CSRI002 2>&1 | tee output.proc.CSRI002
# =========================== auto block: setup ============================
# script setup
# take note of the AFNI version
afni -ver
# check that the current AFNI version is recent enough
afni_history -check_date 17 Jan 2019
if ( $status ) then
echo "** this script requires newer AFNI binaries (than 17 Jan 2019)"
echo " (consider: @update.afni.binaries -defaults)"
exit
endif
# the user may specify a single subject to run with
if ( $#argv > 0 ) then
set subj = $argv[1]
else
set subj = CSRI002
endif
# assign output directory name
set output_dir = $subj.results
# verify that the results directory does not yet exist
if ( -d $output_dir ) then
echo output dir "$subj.results" already exists
exit
endif
# set list of runs
set runs = (`count -digits 2 1 1`)
# create results and stimuli directories
mkdir $output_dir
mkdir $output_dir/stimuli
# copy anatomy to results dir
3dcopy anatSS.CSRI002.nii $output_dir/anatSS.CSRI002
# copy anatomical follower datasets into the results dir
3dcopy CSRI002_SurfVol.nii $output_dir/copy_af_anat_w_skull
3dcopy aparc.a2009s+aseg.nii $output_dir/copy_af_aaseg
3dcopy aparc.a2009s+aseg.nii $output_dir/copy_af_aeseg
3dcopy 001_vent.nii $output_dir/copy_af_FSvent
3dcopy 002_wm.nii $output_dir/copy_af_FSWe
# copy external -tlrc_NL_warped_dsets datasets
3dcopy anatQQ.CSRI002.nii $output_dir/anatQQ.CSRI002
3dcopy anatQQ.CSRI002.aff12.1D $output_dir/anatQQ.CSRI002.aff12.1D
3dcopy anatQQ.CSRI002_WARP.nii $output_dir/anatQQ.CSRI002_WARP.nii
# ============================ auto block: tcat ============================
# apply 3dTcat to copy input dsets to results dir,
# while removing the first 3 TRs
3dTcat -prefix $output_dir/pb00.$subj.r01.tcat CSRI-002-1-WAS-Rest.nii'[3..$]'
# and make note of repetitions (TRs) per run
set tr_counts = ( 297 )
# -------------------------------------------------------
# enter the results directory (can begin processing data)
cd $output_dir
# ========================== auto block: outcount ==========================
# data check: compute outlier fraction for each volume
touch out.pre_ss_warn.txt
foreach run ( $runs )
3dToutcount -automask -fraction -polort 4 -legendre \
pb00.$subj.r$run.tcat+orig > outcount.r$run.1D
# outliers at TR 0 might suggest pre-steady state TRs
if ( `1deval -a outcount.r$run.1D"{0}" -expr "step(a-0.4)"` ) then
echo "** TR #0 outliers: possible pre-steady state TRs in run $run" \
>> out.pre_ss_warn.txt
endif
end
# catenate outlier counts into a single time series
cat outcount.r*.1D > outcount_rall.1D
# get run number and TR index for minimum outlier volume
set minindex = `3dTstat -argmin -prefix - outcount_rall.1D\'`
set ovals = ( `1d_tool.py -set_run_lengths $tr_counts \
-index_to_run_tr $minindex` )
# save run and TR indices for extraction of vr_base_min_outlier
set minoutrun = $ovals[1]
set minouttr = $ovals[2]
echo "min outlier: run $minoutrun, TR $minouttr" | tee out.min_outlier.txt
# ================================ despike =================================
# apply 3dDespike to each run
foreach run ( $runs )
3dDespike -NEW -nomask -prefix pb01.$subj.r$run.despike \
pb00.$subj.r$run.tcat+orig
end
# ================================= tshift =================================
# time shift data so all slice timing is the same
foreach run ( $runs )
3dTshift -TR 2 -tzero 0 -tpattern seq-z -quintic -prefix pb02.$subj.r$run.tshift \
pb01.$subj.r$run.despike+orig
end
# --------------------------------
# extract volreg registration base
3dbucket -prefix vr_base_min_outlier \
pb02.$subj.r$minoutrun.tshift+orig"[$minouttr]"
# ================================= align ==================================
# for e2a: compute anat alignment transformation to EPI registration base
# (new anat will be current anatSS.CSRI002+orig)
align_epi_anat.py -anat2epi -anat anatSS.CSRI002+orig \
-suffix _al_junk \
-epi vr_base_min_outlier+orig -epi_base 0 \
-epi_strip 3dAutomask \
-anat_has_skull no \
-giant_move -cost lpc+ZZ -check_flip \
-volreg off -tshift off
# ================================== tlrc ==================================
# nothing to do: have external -tlrc_NL_warped_dsets
# warped anat : anatQQ.CSRI002+tlrc
# affine xform : anatQQ.CSRI002.aff12.1D
# non-linear warp : anatQQ.CSRI002_WARP.nii
# ================================= volreg =================================
# align each dset to base volume, to anat, warp to tlrc space
# verify that we have a +tlrc warp dataset
if ( ! -f anatQQ.CSRI002+tlrc.HEAD ) then
echo "** missing +tlrc warp dataset: anatQQ.CSRI002+tlrc.HEAD"
exit
endif
# register and warp
foreach run ( $runs )
# register each volume to the base image
3dvolreg -verbose -zpad 1 -base vr_base_min_outlier+orig \
-1Dfile dfile.r$run.1D -prefix rm.epi.volreg.r$run \
-cubic \
-1Dmatrix_save mat.r$run.vr.aff12.1D \
pb02.$subj.r$run.tshift+orig
# create an all-1 dataset to mask the extents of the warp
3dcalc -overwrite -a pb02.$subj.r$run.tshift+orig -expr 1 \
-prefix rm.epi.all1
# catenate volreg/epi2anat/tlrc xforms
cat_matvec -ONELINE \
anatQQ.CSRI002.aff12.1D \
anatSS.CSRI002_al_junk_mat.aff12.1D -I \
mat.r$run.vr.aff12.1D > mat.r$run.warp.aff12.1D
# apply catenated xform: volreg/epi2anat/tlrc/NLtlrc
# then apply non-linear standard-space warp
3dNwarpApply -master anatQQ.CSRI002+tlrc -dxyz 3.5 \
-source pb02.$subj.r$run.tshift+orig \
-nwarp "anatQQ.CSRI002_WARP.nii mat.r$run.warp.aff12.1D" \
-prefix rm.epi.nomask.r$run
# warp the all-1 dataset for extents masking
3dNwarpApply -master anatQQ.CSRI002+tlrc -dxyz 3.5 \
-source rm.epi.all1+orig \
-nwarp "anatQQ.CSRI002_WARP.nii mat.r$run.warp.aff12.1D" \
-interp cubic \
-ainterp NN -quiet \
-prefix rm.epi.1.r$run
# make an extents intersection mask of this run
3dTstat -min -prefix rm.epi.min.r$run rm.epi.1.r$run+tlrc
end
# make a single file of registration params
cat dfile.r*.1D > dfile_rall.1D
# compute motion magnitude time series: the Euclidean norm
# (sqrt(sum squares)) of the motion parameter derivatives
1d_tool.py -infile dfile_rall.1D -set_nruns 1 \
-derivative -collapse_cols euclidean_norm \
-write motion_${subj}_enorm.1D
# ----------------------------------------
# create the extents mask: mask_epi_extents+tlrc
# (this is a mask of voxels that have valid data at every TR)
# (only 1 run, so just use 3dcopy to keep naming straight)
3dcopy rm.epi.min.r01+tlrc mask_epi_extents
# and apply the extents mask to the EPI data
# (delete any time series with missing data)
foreach run ( $runs )
3dcalc -a rm.epi.nomask.r$run+tlrc -b mask_epi_extents+tlrc \
-expr 'a*b' -prefix pb03.$subj.r$run.volreg
end
# warp the volreg base EPI dataset to make a final version
cat_matvec -ONELINE \
anatQQ.CSRI002.aff12.1D \
anatSS.CSRI002_al_junk_mat.aff12.1D -I > mat.basewarp.aff12.1D
3dNwarpApply -master anatQQ.CSRI002+tlrc -dxyz 3.5 \
-source vr_base_min_outlier+orig \
-nwarp "anatQQ.CSRI002_WARP.nii mat.basewarp.aff12.1D" \
-prefix final_epi_vr_base_min_outlier
# create an anat_final dataset, aligned with stats
3dcopy anatQQ.CSRI002+tlrc anat_final.$subj
# record final registration costs
3dAllineate -base final_epi_vr_base_min_outlier+tlrc -allcostX \
-input anat_final.$subj+tlrc |& tee out.allcostX.txt
# -----------------------------------------
# warp anat follower datasets (non-linear)
# first perform any pre-warp erode operations
3dmask_tool -input copy_af_FSvent+orig -dilate_input -1 \
-prefix copy_af_FSvent_erode
3dmask_tool -input copy_af_FSWe+orig -dilate_input -1 \
-prefix copy_af_FSWe_erode
# and apply any warp operations
3dNwarpApply -source copy_af_anat_w_skull+orig \
-master anat_final.$subj+tlrc \
-ainterp wsinc5 -nwarp anatQQ.CSRI002_WARP.nii \
anatQQ.CSRI002.aff12.1D \
-prefix follow_anat_anat_w_skull
3dNwarpApply -source copy_af_aaseg+orig \
-master anat_final.$subj+tlrc \
-ainterp NN -nwarp anatQQ.CSRI002_WARP.nii \
anatQQ.CSRI002.aff12.1D \
-prefix follow_ROI_aaseg
3dNwarpApply -source copy_af_aeseg+orig \
-master pb03.$subj.r01.volreg+tlrc \
-ainterp NN -nwarp anatQQ.CSRI002_WARP.nii \
anatQQ.CSRI002.aff12.1D \
-prefix follow_ROI_aeseg
3dNwarpApply -source copy_af_FSvent_erode+orig \
-master pb03.$subj.r01.volreg+tlrc \
-ainterp NN -nwarp anatQQ.CSRI002_WARP.nii \
anatQQ.CSRI002.aff12.1D \
-prefix follow_ROI_FSvent
3dNwarpApply -source copy_af_FSWe_erode+orig \
-master pb03.$subj.r01.volreg+tlrc \
-ainterp NN -nwarp anatQQ.CSRI002_WARP.nii \
anatQQ.CSRI002.aff12.1D \
-prefix follow_ROI_FSWe
# ================================== blur ==================================
# blur each volume of each run
foreach run ( $runs )
3dBlurToFWHM -FWHM 6.0 -prefix pb04.$subj.r$run.blur \
pb03.$subj.r$run.volreg+tlrc
end
# ================================== mask ==================================
# create 'full_mask' dataset (union mask)
foreach run ( $runs )
3dAutomask -prefix rm.mask_r$run pb04.$subj.r$run.blur+tlrc
end
# create union of inputs, output type is byte
3dmask_tool -inputs rm.mask_r*+tlrc.HEAD -union -prefix full_mask.$subj
# ---- create subject anatomy mask, mask_anat.$subj+tlrc ----
# (resampled from tlrc anat)
3dresample -master full_mask.$subj+tlrc -input anatQQ.CSRI002+tlrc \
-prefix rm.resam.anat
# convert to binary anat mask; fill gaps and holes
3dmask_tool -dilate_input 5 -5 -fill_holes -input rm.resam.anat+tlrc \
-prefix mask_anat.$subj
# compute tighter EPI mask by intersecting with anat mask
3dmask_tool -input full_mask.$subj+tlrc mask_anat.$subj+tlrc \
-inter -prefix mask_epi_anat.$subj
# compute overlaps between anat and EPI masks
3dABoverlap -no_automask full_mask.$subj+tlrc mask_anat.$subj+tlrc \
|& tee out.mask_ae_overlap.txt
# note Dice coefficient of masks, as well
3ddot -dodice full_mask.$subj+tlrc mask_anat.$subj+tlrc \
|& tee out.mask_ae_dice.txt
# ---- create group anatomy mask, mask_group+tlrc ----
# (resampled from tlrc base anat, MNI152_2009_template_SSW.nii.gz)
3dresample -master full_mask.$subj+tlrc -prefix ./rm.resam.group \
-input /home/naren123/abin/MNI152_2009_template_SSW.nii.gz
# convert to binary group mask; fill gaps and holes
3dmask_tool -dilate_input 5 -5 -fill_holes -input rm.resam.group+tlrc \
-prefix mask_group
# ================================= scale ==================================
# scale each voxel time series to have a mean of 100
# (be sure no negatives creep in)
# (subject to a range of [0,200])
foreach run ( $runs )
3dTstat -prefix rm.mean_r$run pb04.$subj.r$run.blur+tlrc
3dcalc -a pb04.$subj.r$run.blur+tlrc -b rm.mean_r$run+tlrc \
-c mask_epi_extents+tlrc \
-expr 'c * min(200, a/b*100)*step(a)*step(b)' \
-prefix pb05.$subj.r$run.scale
end
# ================================ regress =================================
# compute de-meaned motion parameters (for use in regression)
1d_tool.py -infile dfile_rall.1D -set_nruns 1 \
-demean -write motion_demean.1D
# compute motion parameter derivatives (for use in regression)
1d_tool.py -infile dfile_rall.1D -set_nruns 1 \
-derivative -demean -write motion_deriv.1D
# convert motion parameters for per-run regression
1d_tool.py -infile motion_demean.1D -set_nruns 1 \
-split_into_pad_runs mot_demean
1d_tool.py -infile motion_deriv.1D -set_nruns 1 \
-split_into_pad_runs mot_deriv
# ------------------------------
# create ROI PC ort sets: FSvent
# create a time series dataset to run 3dpc on...
# detrend, so principal components are not affected
foreach run ( $runs )
3dTproject -polort 4 -prefix rm.det_pcin_r$run \
-input pb03.$subj.r$run.volreg+tlrc
end
# catenate runs
3dTcat -prefix rm.det_pcin_rall rm.det_pcin_r*+tlrc.HEAD
# make ROI PCs : FSvent
3dpc -mask follow_ROI_FSvent+tlrc -pcsave 3 \
-prefix ROIPC.FSvent rm.det_pcin_rall+tlrc
# ------------------------------
# run the regression analysis
3dDeconvolve -input pb05.$subj.r*.scale+tlrc.HEAD \
-ortvec ROIPC.FSvent_vec.1D ROIPC.FSvent \
-ortvec mot_demean.r01.1D mot_demean_r01 \
-ortvec mot_deriv.r01.1D mot_deriv_r01 \
-polort 4 \
-num_stimts 0 \
-fout -tout -x1D X.xmat.1D -xjpeg X.jpg \
-fitts fitts.$subj \
-errts errts.${subj} \
-x1D_stop \
-bucket stats.$subj
# -- use 3dTproject to project out regression matrix --
3dTproject -polort 0 -input pb05.$subj.r*.scale+tlrc.HEAD \
-ort X.xmat.1D -prefix errts.${subj}.tproject
# if 3dDeconvolve fails, terminate the script
if ( $status != 0 ) then
echo '---------------------------------------'
echo '** 3dDeconvolve error, failing...'
echo ' (consider the file 3dDeconvolve.err)'
exit
endif
# display any large pairwise correlations from the X-matrix
1d_tool.py -show_cormat_warnings -infile X.xmat.1D |& tee out.cormat_warn.txt
# display degrees of freedom info from X-matrix
1d_tool.py -show_df_info -infile X.xmat.1D |& tee out.df_info.txt
# create an all_runs dataset to match the fitts, errts, etc.
3dTcat -prefix all_runs.$subj pb05.$subj.r*.scale+tlrc.HEAD
# --------------------------------------------------
# generate fast ANATICOR result: errts.$subj.fanaticor+tlrc
# --------------------------------------------------
# fast ANATICOR: generate local FSWe time series averages
# create catenated volreg dataset
3dTcat -prefix rm.all_runs.volreg pb03.$subj.r*.volreg+tlrc.HEAD
# mask white matter before blurring
3dcalc -a rm.all_runs.volreg+tlrc -b follow_ROI_FSWe+tlrc \
-expr "a*bool(b)" -datum float -prefix rm.all_runs.volreg.mask
# generate ANATICOR voxelwise regressors via blur
3dmerge -1blur_fwhm 30 -doall -prefix Local_FSWe_rall \
rm.all_runs.volreg.mask+tlrc
# -- use 3dTproject to project out regression matrix --
3dTproject -polort 0 -input pb05.$subj.r*.scale+tlrc.HEAD \
-dsort Local_FSWe_rall+tlrc \
-ort X.xmat.1D -prefix errts.$subj.fanaticor
# --------------------------------------------------
# for each run, bandpass with 3dRSFC and calculate parameters
mkdir RSFC
set b0 = 0
set b1 = -1
# run index is 1 digit (wary of octal), file name run is 2
foreach rind ( `count -digits 1 1 $#runs` )
set reps = $tr_counts[$rind]
@ b1 += $reps
3dRSFC -prefix RSFC/run_$runs[$rind] -nodetrend \
0.01 0.1 errts.$subj.fanaticor+tlrc"[$b0..$b1]"
@ b0 += $reps
end
# copy main LFF results out, catenated back into one dataset
# (this basically replaces the input errts dataset)
3dTcat -prefix RSFC_LFF_rall_$subj RSFC/run*_LFF+tlrc.HEAD
# --------------------------------------------------
# create a temporal signal to noise ratio dataset
# signal: if 'scale' block, mean should be 100
# noise : compute standard deviation of errts
3dTstat -mean -prefix rm.signal.all all_runs.$subj+tlrc
3dTstat -stdev -prefix rm.noise.all RSFC_LFF_rall_$subj+tlrc
3dcalc -a rm.signal.all+tlrc \
-b rm.noise.all+tlrc \
-c mask_epi_anat.$subj+tlrc \
-expr 'c*a/b' -prefix TSNR.$subj
# ---------------------------------------------------
# compute and store GCOR (global correlation average)
# (sum of squares of global mean of unit errts)
3dTnorm -norm2 -prefix rm.errts.unit RSFC_LFF_rall_$subj+tlrc
3dmaskave -quiet -mask full_mask.$subj+tlrc rm.errts.unit+tlrc \
> gmean.errts.unit.1D
3dTstat -sos -prefix - gmean.errts.unit.1D\' > out.gcor.1D
echo "-- GCOR = `cat out.gcor.1D`"
# ---------------------------------------------------
# compute correlation volume
# (per voxel: average correlation across masked brain)
# (now just dot product with average unit time series)
3dcalc -a rm.errts.unit+tlrc -b gmean.errts.unit.1D -expr 'a*b' -prefix rm.DP
3dTstat -sum -prefix corr_brain rm.DP+tlrc
# compute 2 requested correlation volume(s)
# create correlation volume corr_af_aeseg
3dcalc -a follow_ROI_aeseg+tlrc -b full_mask.$subj+tlrc -expr 'a*b' \
-prefix rm.fm.aeseg
3dmaskave -q -mask rm.fm.aeseg+tlrc rm.errts.unit+tlrc > mean.unit.aeseg.1D
3dcalc -a rm.errts.unit+tlrc -b mean.unit.aeseg.1D \
-expr 'a*b' -prefix rm.DP.aeseg
3dTstat -sum -prefix corr_af_aeseg rm.DP.aeseg+tlrc
# create correlation volume corr_af_FSvent
3dcalc -a follow_ROI_FSvent+tlrc -b full_mask.$subj+tlrc -expr 'a*b' \
-prefix rm.fm.FSvent
3dmaskave -q -mask rm.fm.FSvent+tlrc rm.errts.unit+tlrc > mean.unit.FSvent.1D
3dcalc -a rm.errts.unit+tlrc -b mean.unit.FSvent.1D \
-expr 'a*b' -prefix rm.DP.FSvent
3dTstat -sum -prefix corr_af_FSvent rm.DP.FSvent+tlrc
# --------------------------------------------------------
# compute sum of non-baseline regressors from the X-matrix
# (use 1d_tool.py to get list of regressor colums)
set reg_cols = `1d_tool.py -infile X.xmat.1D -show_indices_interest`
3dTstat -sum -prefix sum_ideal.1D X.xmat.1D"[$reg_cols]"
# also, create a stimulus-only X-matrix, for easy review
1dcat X.xmat.1D"[$reg_cols]" > X.stim.xmat.1D
# ============================ blur estimation =============================
# compute blur estimates
touch blur_est.$subj.1D # start with empty file
# create directory for ACF curve files
mkdir files_ACF
# -- estimate blur for each run in epits --
touch blur.epits.1D
# restrict to uncensored TRs, per run
foreach run ( $runs )
set trs = `1d_tool.py -infile X.xmat.1D -show_trs_uncensored encoded \
-show_trs_run $run`
if ( $trs == "" ) continue
3dFWHMx -detrend -mask mask_epi_anat.$subj+tlrc \
-ACF files_ACF/out.3dFWHMx.ACF.epits.r$run.1D \
all_runs.$subj+tlrc"[$trs]" >> blur.epits.1D
end
# compute average FWHM blur (from every other row) and append
set blurs = ( `3dTstat -mean -prefix - blur.epits.1D'{0..$(2)}'\'` )
echo average epits FWHM blurs: $blurs
echo "$blurs # epits FWHM blur estimates" >> blur_est.$subj.1D
# compute average ACF blur (from every other row) and append
set blurs = ( `3dTstat -mean -prefix - blur.epits.1D'{1..$(2)}'\'` )
echo average epits ACF blurs: $blurs
echo "$blurs # epits ACF blur estimates" >> blur_est.$subj.1D
# -- estimate blur for each run in errts --
touch blur.errts.1D
# restrict to uncensored TRs, per run
foreach run ( $runs )
set trs = `1d_tool.py -infile X.xmat.1D -show_trs_uncensored encoded \
-show_trs_run $run`
if ( $trs == "" ) continue
3dFWHMx -detrend -mask mask_epi_anat.$subj+tlrc \
-ACF files_ACF/out.3dFWHMx.ACF.errts.r$run.1D \
RSFC_LFF_rall_$subj+tlrc"[$trs]" >> blur.errts.1D
end
# compute average FWHM blur (from every other row) and append
set blurs = ( `3dTstat -mean -prefix - blur.errts.1D'{0..$(2)}'\'` )
echo average errts FWHM blurs: $blurs
echo "$blurs # errts FWHM blur estimates" >> blur_est.$subj.1D
# compute average ACF blur (from every other row) and append
set blurs = ( `3dTstat -mean -prefix - blur.errts.1D'{1..$(2)}'\'` )
echo average errts ACF blurs: $blurs
echo "$blurs # errts ACF blur estimates" >> blur_est.$subj.1D
# ================== auto block: generate review scripts ===================
# generate a review script for the unprocessed EPI data
gen_epi_review.py -script @epi_review.$subj \
-dsets pb00.$subj.r*.tcat+orig.HEAD
# generate scripts to review single subject results
# (try with defaults, but do not allow bad exit status)
gen_ss_review_scripts.py \
-errts_dset RSFC_LFF_rall_$subj+tlrc.HEAD -exit0 \
-ss_review_dset out.ss_review.$subj.txt \
-write_uvars_json out.ss_review_uvars.json
# ========================== auto block: finalize ==========================
# remove temporary files
\rm -f rm.*
# if the basic subject review script is here, run it
# (want this to be the last text output)
if ( -e @ss_review_basic ) then
./@ss_review_basic |& tee out.ss_review.$subj.txt
# generate html ss review pages
# (akin to static images from running @ss_review_driver)
apqc_make_tcsh.py -review_style basic -subj_dir . \
-uvar_json out.ss_review_uvars.json
tcsh @ss_review_html |& tee out.review_html
apqc_make_html.py -qc_dir QC_$subj
echo "\nconsider running: \n\n afni_open -b $subj.results/QC_$subj/index.html\n"
endif
# return to parent directory (just in case...)
cd ..
echo "execution finished: `date`"
Regards
-Korann