% MarsBaR batch script to make ROIs
% See http://marsbar.sourceforge.net
%Adapted by J Grahn

 root_dir = '/imaging/jessica/Beat/';

% Directory to store (and load) ROI files
roi_dir = fullfile(root_dir, 'RFX/ROIs');

% MarsBaR version check
if isempty(which('marsbar'))
  error('Need MarsBaR on the path');
end
v = str2num(marsbar('ver'));
if v < 0.35
  error('Batch script only works for MarsBaR >= 0.35');
end
marsbar('on');  % needed to set paths etc

% Set up the SPM defaults, just in case
spm('defaults', 'fmri');
%Optional: this is a list of coordinates from which Marsbar will make
%sphere ROIs. You can specify details of each ROI separately instead of
%using a list like this (look at commented out script below the for:end
%loop.
sphere_centres = {[-6;6;54]
[6;3;63]
[6;15;48]
[-45;-3;45]
[-42;3;27]
[-39;-24;51]
[-48;-42;21]
[48;6;24]
[54;0;45]
[54;12;15]
[66;-36;15]
[66;-24;3]
[51;-24;3]
[30;24;0]
[30;-57;-27]
[42;-66;-27]
[42;-39;45]
[36;-45;45]
[-24;-69;-51]
[-27;-60;-27]
[33;-87;-6]
[24;-90;-6]
[-24;-93;-3]
[24;-66;-51]
[33;-60;-51]
[39;36;27]
[18;6;-3]
[-54;-21;3]
[-6;-18;-3]
[-36;30;21]
[-36;-60;-9]}
%Make sphere ROIs from list given above.
for x = 1:size(sphere_centres)
area =['roi' ]
sphere_centre = sphere_centres{x}'
sphere_radius = 5
sphere_roi = maroi_sphere(struct('centre', sphere_centre, 'radius', sphere_radius));
% Give it a name
details = [area int2str(sphere_centre) int2str(sphere_radius)]
details = details(details ~= ' ')
roitosave = label(sphere_roi, details)
% save ROI to MarsBaR ROI file, in current directory
detailsmat = [details, '_roi.mat']
saveroi(roitosave, fullfile(roi_dir,detailsmat ));
% also Save as image that can be viewed in MRIcroN
detailsimg = [details, '_roi.nii']
save_as_image(roitosave, detailsimg);
end
%*********************************************
%if instead you need to make a bunch of different types of ROIs, comment
%out the entire for end loop above, and use this part of the script instead.
%%See maroi_box and maroi_img (in marsbar directory) for other types of ROIs


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%THIS SECTION NAMES ROIS AS THEIR COORDINATES AND RADII

% %Make sphere ROI
% area = 'SMA_10sph'
% sphere_centre = [-8 2 72];
% sphere_radius = 10
% sphere_roi = maroi_sphere(struct('centre', sphere_centre, 'radius', sphere_radius));
% % Give it a name
% details = [area int2str(sphere_centre) int2str(sphere_radius)]
% details = details(details ~= ' ')
% roitosave = label(sphere_roi, details)
% % save ROI to MarsBaR ROI file, in current directory, just to show how
% detailsmat = [details, '_roi.mat']
% saveroi(roitosave, fullfile(roi_dir,detailsmat ));
% % Save as image
% detailsimg = [details, '_roi.nii']
% save_as_image(roitosave, detailsimg);
% 
% 
% %Make sphere ROI
% area = 'LIFG_5sph'
% sphere_centre = [-52 13 14];
% sphere_radius = 5
% sphere_roi = maroi_sphere(struct('centre', sphere_centre, 'radius', sphere_radius));
% % Give it a name
% details = [area int2str(sphere_centre) int2str(sphere_radius)]
% details = details(details ~= ' ')
% roitosave = label(sphere_roi, details)
% % save ROI to MarsBaR ROI file, in current directory, just to show how
% detailsmat = [details, '_roi.mat']
% saveroi(roitosave, fullfile(roi_dir,detailsmat ));
% % Save as image
% detailsimg = [details, '_roi.nii']
% save_as_image(roitosave, detailsimg);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %THIS SECTION NAMES ROIS BY HAND, AND STORES THEM SO THEY CAN BE COMBINED
% 
% %Make sphere ROI
% sphere_centre = [-6 -4 66];
% sphere_radius = 4
% sphere_roi = maroi_sphere(struct('centre', sphere_centre, 'radius', sphere_radius));;
% % Give it a name
% roitosave = label(sphere_roi, 'LSMAsphere_4mm_highres_-6_-4_66')
% LSMAroi = roitosave
% % save ROI to MarsBaR ROI file, in current directory, just to show how
% saveroi(roitosave, fullfile(roi_dir, 'LSMAsphere_4mm_highres_-6_-4_66_roi.mat'));
% 
% % Save as image
% save_as_image(roitosave, 'LSMAsphere_4mm_highres_-6_-4_66_roi.nii');
% 
% %Make sphere ROI
% sphere_centre = [6 0 66];
% sphere_radius = 4
% sphere_roi = maroi_sphere(struct('centre', sphere_centre, 'radius', sphere_radius));;
% % Give it a name
% roitosave = label(sphere_roi, 'RSMAsphere_4mm_highres_6_0_66')
% RSMAroi = roitosave
% % save ROI to MarsBaR ROI file, in current directory, just to show how
% saveroi(roitosave, fullfile(roi_dir, 'RSMAsphere_4mm_highres_6_0_66_roi.mat'));
% 
% % Save as image
% save_as_image(roitosave, 'RSMAsphere_4mm_highres_6_0_66_roi.nii');

%THIS COMBINES ALL NAMED ROIS SPECIFIED ABOVE INTO 1 BIG ROI

% All_rois = RSMAroi + LSMAroi;
% % Give it a name
% All_rois = label(All_rois, 'RSMA+LSMA_4mmhighres_roi');
% 
% % save ROI to MarsBaR ROI file, in current directory
% saveroi(All_rois, fullfile(roi_dir, 'RLSMA_4mmhighres_roi.mat'));
% 
% % Save as image
% save_as_image(All_rois, RLSMA_4mmhighres_roi.nii');