Subject: Automating SUMA Script to Load Significant Clusters and Generate Brain Images

Hi AFNI Community,

I'm working on automating a script using SUMA to load significant clusters from a NIFTI file and generate brain images from various views. While I've been able to start AFNI and SUMA, and load underlay and overlay datasets, I'm encountering issues with correctly displaying the overlays and automating the generation of images.

Has anyone successfully automated this process or have insights on the best way to approach this? Any examples or advice would be greatly appreciated! Generating the figures manually is a very tedious process.


So you have volumetric data, and you want to view it onto the surface? (That is, you don't have data on the surface already to cluster?)

Would you be able to post your script so far? That might help get things running.

--pt

I have resting state fMRI data. I want to overlay my significant clusters on the inflated brain via SUMA. this is my script so far. It would be great if someone could help me troubleshoot this issue.

suma_path= 'path to your suma_MI_N27'
cluster_mask='path to your significant clusters '
underlay= 'path to your underlay.nii.gz'
output_dir= 'path to your output directory'

% Change to the output directory
if ~exist(output_dir, 'dir')
mkdir(output_dir);
end

% Echo the SUMA path
disp(['SUMA path is: ', suma_path]);

% Set the environment variable for SUMA max wait time
setenv('SUMA_DriveSumaMaxWait', '600'); % Increase wait time to 600 seconds

% Start AFNI in the background
disp('Starting AFNI...');
[status, cmdout] = system('afni -niml &');
if status ~= 0
disp('Error starting AFNI');
disp(cmdout);
return;
end

% Pause to allow AFNI to start
disp('Waiting for AFNI to start...');
pause(10); % Adjust wait time as needed

% Start SUMA with the specified spec file and surface volume
disp('Starting SUMA...');
suma_command = sprintf('suma -spec %s/MNI_N27_both.spec -sv %s/MNI_N27_SurfVol_UNCal+orig &', suma_path, suma_path);
[status, cmdout] = system(suma_command);
if status ~= 0
disp('Error starting SUMA');
disp(cmdout);
return;
end

% Pause to allow SUMA to start
disp('Waiting for SUMA to start...');
pause(10); % Adjust wait time as needed

% Load the underlay in SUMA
disp('Loading underlay...');
underlay_command = sprintf('DriveSuma -com viewer_cont -load_dset %s -switch_cmap GrayScale', underlay);
[status, cmdout] = system(underlay_command);
if status ~= 0
disp('Error loading underlay');
disp(cmdout);
return;
end

% Load the overlay in SUMA
disp('Loading overlay...');
overlay_command = sprintf('DriveSuma -com viewer_cont -load_dset %s -switch_cmap Spectrum:red_to_blue+gap', cluster_mask);
[status, cmdout] = system(overlay_command);
if status ~= 0
disp('Error loading overlay');
disp(cmdout);
return;
end

% Set SUMA to use the auto-record function
disp('Setting auto-record function...');
autorecord_command = 'DriveSuma -com viewer_cont -autorecord autorecord.A';
[status, cmdout] = system(autorecord_command);
if status ~= 0
disp('Error setting auto-record');
disp(cmdout);
return;
end

% Set the view angles and save images
views = {'left', 'right', 'medial', 'lateral'};
keys = {'r', 'l', 'ctrl+m', 'm'}; % Updated keys for medial and lateral views

for i = 1:length(views)
disp(['Setting view to ', views{i}, '...']);
view_command = sprintf('DriveSuma -com viewer_cont -key %s', keys{i});
[status, cmdout] = system(view_command);
if status ~= 0
disp(['Error setting view angle: ', views{i}]);
disp(cmdout);
return;
end
pause(2); % Wait for the view to update
disp('Recording view...');
record_command = 'DriveSuma -com viewer_cont -key ctrl+r';
[status, cmdout] = system(record_command);
if status ~= 0
disp('Error recording view');
disp(cmdout);
return;
end

% Check for the most recent image file
recent_files = dir('autorecord.*.jpg');
if isempty(recent_files)
    disp('No new images found.');
    continue;
end

[~, idx] = max([recent_files.datenum]);
latest_file = recent_files(idx).name;

% Move the recorded image to the output directory
movefile(latest_file, fullfile(output_dir, sprintf('comp8_%s_view.jpg', views{i})));

end

% Output completion message
disp('Figures have been saved using the auto-record function.');