Batch Processing of MNI Normalization and Parcellation

Dear Brainstorm Experts,

I have several subjects for which I would like to:

  1. Load their structural T1w images.
  2. Compute the MNI normalization (e.g., using maff8).
  3. Reslice an MNI brain parcellation into the subject space.
  4. Save the parcellation in subject space as a NIfTI file.

I am familiar with performing these steps using the GUI. However, since I need to process around 40 subjects, I was wondering if there is an efficient way to handle this in batch mode?

Thank you in advance for your help.

Best regards,

Cristóbal

This can be done using scripting in Brainstorm:
See this tutorial:
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting

In short you would need to make a FOR loop across subjects, and perform the desired steps.

As per the tutorial above, the simpler way to create a script is to create a pipeline in the Brainstorm GUI and the generate a script from it.

For your four steps, the first 3 can done with the following processes:

  1. Step 1: Import > Import anatomy > Import MRI
    Pay attention to the file format that you plan to import

  2. Step 2: Import > Import anatomy > MNI normalization

  3. Step 3: Import > Import anatomy > Import MRI
    Note: the format must be ALL-MNI-ATLAS to indicate it is a MNI parcellation that will be sliced into the subject space

See an example of how the script looks for the first two steps in here:
brainstorm3/toolbox/script/tutorial_fem_tensors.m at 0a5cc2c803964970e7e0d30dc06004eb056f7019 · brainstorm-tools/brainstorm3 · GitHub

  1. Step 4: This cannot be done with a process, but once the other steps are done you can export to a NIfTI file as:
% Get Subject structure and their MRIs
sSubject = bst_get('Subject', 'SUBJECTNAME');

% Find the parcellation MRI filename in the array sSubject.Anatomy
iMniAtlas = find(contains({sSubject.Anatomy.Comment}, 'ATLASNAME', 'IgnoreCase',true));

% Save MNI atlas as NIfTI file 'new_MRI.nii'
export_mri(sSubject.Anatomy(iMniAtlas).FileName, 'FULLPATH/new_MRI.nii' );

Dear Raymundo

Thank you very much for all your advice.

I have written a script, and the resulting output looks good. Could you please take a look at my script to verify that everything is correct? In this script, I iterate over three subjects and use the Tian Subcortex parcellation in MNI space (Tian_Subcortex_S1_3T_1mm.nii.gz).

% Script generated by Brainstorm (26-Sep-2024)

% Input files
sFiles = [];
SubjectNames = {'100206','100307','100408'};
RawFiles = {...
    '/home/cris/Escritorio/ProbarPipelineBrainstorm/100206/T1w_acpc_dc_restore.nii.gz', ...
    '/home/cris/Escritorio/ProbarPipelineBrainstorm/100307/T1w_acpc_dc_restore.nii.gz', ...
    '/home/cris/Escritorio/ProbarPipelineBrainstorm/100408/T1w_acpc_dc_restore.nii.gz', ...
    '/home/cris/Escritorio/ProbarPipelineBrainstorm/Tian2020MSA/3T/Subcortex-Only/Tian_Subcortex_S1_3T_1mm.nii.gz'};

% Start a new report
bst_report('Start', sFiles);

for iSubject = 1:3   
% Process: Import MRI
sFiles = bst_process('CallProcess', 'process_import_mri', sFiles, [], ...
    'subjectname', SubjectNames{iSubject}, ...
    'mrifile',     {RawFiles{iSubject}, 'ALL'}, ...
    'nas',         [0, 0, 0], ...
    'lpa',         [0, 0, 0], ...
    'rpa',         [0, 0, 0], ...
    'ac',          [0, 0, 0], ...
    'pc',          [0, 0, 0], ...
    'ih',          [0, 0, 0]);

% Process: MNI normalization
sFiles = bst_process('CallProcess', 'process_mni_normalize', sFiles, [], ...
    'subjectname', SubjectNames{iSubject}, ...
    'method',      'maff8', ...  % maff8:Affine registration using SPM mutual information algorithm.Estimates a simple 4x4 linear transformation to the MNI space.Included in Brainstorm.
    'uset2',       0);

% Process: Import MRI
sFiles = bst_process('CallProcess', 'process_import_mri', sFiles, [], ...
    'subjectname', SubjectNames{iSubject}, ...
    'mrifile',     {RawFiles{4}, 'ALL-MNI-ATLAS'}, ...
    'nas',         [0, 0, 0], ...
    'lpa',         [0, 0, 0], ...
    'rpa',         [0, 0, 0], ...
    'ac',          [0, 0, 0], ...
    'pc',          [0, 0, 0], ...
    'ih',          [0, 0, 0]);

% Get Subject structure and their MRIs
sSubject = bst_get('Subject', SubjectNames{iSubject});
% Find the parcellation MRI filename in the array sSubject.Anatomy
iMniAtlas = find(contains({sSubject.Anatomy.Comment}, 'Tian_Subcortex_S1_3T_1mm (MNI-linear)', 'IgnoreCase',true));
% Save MNI atlas as NIfTI file 'new_MRI.nii'
export_mri(sSubject.Anatomy(iMniAtlas).FileName, strcat('/home/cris/Escritorio/ProbarPipelineBrainstorm/',SubjectNames{iSubject},'/Tian_Subcortex_S1_3T_1mm_volatlas.nii') );
end
% Save and display report
ReportFile = bst_report('Save', sFiles);
bst_report('Open', ReportFile);
% bst_report('Export', ReportFile, ExportDir);
% bst_report('Email', ReportFile, username, to, subject, isFullReport);

% Delete temporary files
% gui_brainstorm('EmptyTempFolder');

Best regards

Cristóbal

It looks good. Do you have any trouble at running it?
If any, I'd use bst_fullfile() or fullfile() to create the path for the output file instead of strcat when you call strcat.

Dear Raymundo,

I didn’t encounter any issues running the script. However, there is a small detail: it appears that the labels for the right hemisphere, which originally start at 1 in the MNI parcellation, are now starting at 9 in the subject parcellation. The MNI parcellation uses labels 1 to 8 for structures in the right hemisphere and 9 to 16 for those in the left hemisphere. Could MNI-reslicing be causing the labels to shift, starting the numbering from the left hemisphere?

captura

I’ve appended a figure for reference. In it, Tian_Subcortex_S1_3T_1mm.nii.gz represents the parcellation in MNI space, while Tian_Subcortex_S1_3T_1mm_volatlas.nii is the resulting parcellation in subject space (output from the script).

Best regards,

Cristóbal