Concatenating subbricks across subjects into single nifti file

I performed subject-level analysis in AFNI for a specific task-based scan, which included standard steps in afni_proc.py such as motion correction, detrending, etc. In step, I also obtained t-statistics for event stimuli. Suppose the t-statistic spatial maps for a stimulus are in a subbrick labeled “stim1” in the stats/REML output file. I wanted to combined the “stim1” subbrick for each subject in the sample into a single volume named “sample.nii” that I plan to use as input into the Broccoli permutation testing software for group analysis. What is the best function for doing so that will guarantee the order of volumes is preserved? Thank you.

I would do something like this:


3dbucket -aglueto sample.nii subject1.stats_REML+tlrc.HEAD[stim1#0_Coef] \
subject2.stats_REML+tlrc.HEAD[stim1#0_Coef] \
... \
subjectX.stats_REML+tlrc.HEAD[stim1#0_Coef]

Or you could use a for loop in bash (or other scripting language):


for aSubject in subject*.stats_REML+tlrc.HEAD
do
       3dbucket -aglueto sample.nii ${aSubject}.stats_REML+tlrc.HEAD[stim1#0_Coef]
done

This can also be done via wildcards in AFNI directly, e.g.

3dbucket -prefix all_subj_stim1  'subject*.stats_REML+tlrc.HEAD[stim1#0_Coef]'

That would assume, of course, that subj*.stats_REML+tlrc.HEAD was the
desired ordering. Directories can be included, too.

  • rick

I had initially tried 3dbucket. However, the resultant NIFTI file (after applying 3dAFNItoNIFTI) was not readable by Broccoli:

The exact form of the call to Broccoli was:


# Combining Subject-level nifti files using 3dBucket
[ -f volumes.nii ] && rm volumes*
3dbucket -aglueto volumes sub_1.nii.gz sub_2.nii.gz  \
                sub_3.nii.gz  \
                sub_4.nii.gz  \
                sub_5.nii.gz  \
                sub_6.nii.gz  \
                sub_7.nii.gz  \
                sub_8.nii.gz  \
                sub_9.nii.gz  \
                sub_10.nii.gz
3dAFNItoNIFTI -overwrite volumes+tlrc
rm volumes+tlrc*
3dinfo -label volumes.nii

# Run Broccoli
RandomiseGroupLevel volumes.nii -mask TT_N27_3mm_mask.nii -design design_broccoli.mat -contrasts contrast_broccoli.con -teststatistics 0 -permutations 500 -output test_broccoli

With thiis, the permutation tests by broccoli yielded the following response:


Input data is a single volume, nothing to permute!

An alternative approach for creating a nifti file that Broccoli can read is 3dTcat. I didn’t realize it initially that 3dTcat appends new volumes to the beginning of the sample volume. So creating the appropriate sample volume requires adding subject-volumes in reverse order to the rows in the design matrix.


# Combining Subject-level nifti files using 3dTcat
CNT=0
[ -f volumes.nii ] && rm volumes*
3dcopy sub_10.nii.gz volumes
3dTcat sub_9.nii.gz   -glueto     volumes+tlrc
3dTcat sub_8.nii.gz   -glueto     volumes+tlrc
3dTcat sub_7.nii.gz   -glueto     volumes+tlrc
3dTcat sub_6.nii.gz   -glueto     volumes+tlrc
3dTcat sub_5.nii.gz   -glueto     volumes+tlrc
3dTcat sub_4.nii.gz   -glueto     volumes+tlrc
3dTcat sub_3.nii.gz   -glueto     volumes+tlrc
3dTcat sub_2.nii.gz   -glueto     volumes+tlrc
3dTcat sub_1.nii.gz  -glueto     volumes+tlrc

3dAFNItoNIFTI -overwrite volumes+tlrc
rm volumes+tlrc*
3dinfo -label volumes.nii

# Run Broccoli
RandomiseGroupLevel volumes.nii -mask TT_N27_3mm_mask.nii -design design_broccoli.mat -contrasts contrast_broccoli.con -teststatistics 0 -permutations 500 -output test_broccoli

That being said, Broccoli’s maintainer informed me that the “Clustsim” flag on 3dttest++ can generate non-parametric null distribution, which I was previously unaware of. However, including that flag leads to a crash on my system (the error message was placed in a different post).

Thank you for your attention.

The difference between 3dbucket and 3dTcat is the
index of the fourth dimension in a NIFTI dataset.
Since 3dTcat works, it looks like Broccoli expects
that dimension index to be 5, as a time axis, which
is reasonable (even though this is not really time).

Please consider not using -glueto or -aglueto, they
are just confusing and unnecessary. Try instead
something like:

3dTcat -prefix volumes.new.nii                           \
     sub_1.nii.gz sub_2.nii.gz sub_3.nii.gz sub_4.nii.gz \
     sub_5.nii.gz sub_6.nii.gz sub_7.nii.gz sub_8.nii.gz \
     sub_9.nii.gz sub_10.nii.gz

I would then expect volumes.new.nii to be directly
readable by Broccoli.

Note that if you named the datasets so that they
are alphabetical, i.e. with zero-padding:
sub_01.nii sub_02.nii … sub_09.nii sub_10.nii,
then it might be as simple as:

3dTcat -prefix volumes.new.nii sub_*.nii
  • rick