function [hReg,SPM,VOL,xX,xCon,xSDM]=spm_tbx_roi
% SPM region-of-interest (ROI) analysis toolbox
% Russ Poldrack, 5/15/00
% - provides a set of tools for ROI analysis of fMRI data within SPM.
%  
% NOTES ON ROI ANALYSIS:
% In order to ensure that all of the necessary data are available for 
% ROI analysis, you should set fMRI_UFp = 1.0 in your active spm_defaults.m 
%
% Options:
% - Define functional ROIs (spm_roi_define)
%	uses SPM statistical analysis to identify spherical regions
%	of interest at the location of each statistical maximum, and writes
%	mask images for these ROIs.  This module should be fully functional
%	for both event-related and epoch-based designs.
% - View an ROI 
%	View the anatomical location of an ROI determined using spm_roi_define.
% - Plot activity in ROI
%	Plots activity across conditions with an ROI.  
% - List ROI information
% 	List location and size of all ROIs.
% - Compute signal change
%       Extracts signal change and save to file.
%
% CVS repository and development information can be found at:
% http://spm-toolbox.sourceforge.net 
%
% The SPM ROI toolbox is distributed under the GNU Public License.
% Please see spm_LICENSE.man in the SPM distribution for full details.
% This software is meant for research use only.  You must not use this
% software for clinical purposes under any circumstances.
%

global BCH; %- used as a flag to know if we are in batch mode or not.
SCCSid  = '2.31';


%-GUI setup
%-----------------------------------------------------------------------
SPMid = spm('FnBanner',mfilename,SCCSid);
[Finter,Fgraph,CmdLine] = spm('FnUIsetup','fMRI stats model setup',0);
spm_help('!ContextHelp',mfilename)


% Input user action
%=======================================================================
MType = {'Define functional ROIs',...
	 'Define spherical ROIs',...
           'View ROI(s)',...
	   'List ROI information',...
           'Compute ROI data','Plot data from ROI'};
MT    = spm_input('What would you like to do?',1,'m',MType,...
                  'batch',{},'types');

%-Initialise output arguments in case return early
xX   = [];
Sess = [];

switch MT
%-----------------------------------------------------------------------

	case 1
	% define functional roi's
	%---------------------------------------------------------------
	spm_clf(Finter);
	radius=spm_input('Radius of ROI (in mm)','+1');
        spm_roi_define(radius,Finter);
	return
	
	case 2
	% define spherical ROI at MNI location
	%---------------------------------------------------------------
	spm_clf(Finter);
	radius=spm_input('Radius of ROI (in mm)','+1');
	loc=spm_input('MNI coord of ROI center [x y z]','+1');
	img=spm_get(1,'mask.img','choose mask image for this subject');
	% input checking
        spm_roi_sphere_mask(loc,radius,img);
	return


	case 3
	% view an ROI anatomically
	%---------------------------------------------------------------
	spm_clf(Finter);

	spm_roi_view;
	
        return

  	case 4
	% list info on all ROIs
	%---------------------------------------------------------------
	spm_clf(Finter);
        Fgraph = spm_figure('FindWin','Graphics');
	roi_info   = spm_get(1,'roi.mat','select an *con.mat file');
	spm_roi_list(roi_info);
      	return

	case 5
	% Compute timecourse using FIR selective averaging 
	%---------------------------------------------------------------
 
	spm_clf(Finter);
        Fgraph = spm_figure('FindWin','Graphics');
	spm_results_ui('Clear',Fgraph);
	roistruct=struct('roifile',[],'spmfile',[]);
	roistruct.roifile  = spm_get([1,1000],'*clust*.img','select cluster image file(s)');
	roistruct.spmfile  = spm_get(1,'SPM.mat','select an SPM.mat file');
	AdjTypes={'Condition','Filter','Raw'};
	AdjFlags={'cond','filt','raw'};
	roistruct.adjust_flag = spm_input('Adjustment:','+1','b',AdjTypes,AdjFlags,1);
	ter_flag=spm_input('Temporal resolution','+1','b',{'TR','specify'},{'TR','specify'},1);
	if strcmp(ter_flag,'specify'),
	  roistruct.TER=spm_input('Effective temporal resolution','+1');
	end;
	
        roidata = spm_roi_compute(roistruct);
	return;
	
        case 6
	% Plot data from computed ROI
	%---------------------------------------------------------------
 	spm_clf(Finter);
	infile=spm_get(1,'*.txt','Select an output file for plotting');
	spm_roi_plot(infile);
end












