

function varargout = QueryROI(varargin)
% TAL_ROI Application M-file for QueryROI.fig
%    FIG = TAL_ROI launch QueryROI GUI.
%    TAL_ROI('callback_name', ...) invoke the named callback.
%
% This is part of a GUI that enables the user to select combinations
% of Brain regions, and then retrieves the corresponding ROIs from
% the Talairach Database. These ROIS are stored as *.img and *.hdr files
% in the current working directory.
%
% The resultant ROI from the Talairach Database will contain:
% the Union of selections within a brain region, and
% the Intersection of selections between brain regions.
% An error may be generated if the selected regions do not intersect.
%
%    Pavithra Srinivasan, August 1st, 2002, Thu.
% [The retrieval is implemented by roi_generate.m, written by Kalina Christoff]
% Last Modified by GUIDE v2.0 31-Jul-2002 06:36:42

if nargin == 0  % LAUNCH GUI

	fig = openfig(mfilename,'reuse');

	% Generate a structure of handles to pass to callbacks, and store it. 
	handles = guihandles(fig);
	guidata(fig, handles);

	if nargout > 0
		varargout{1} = fig;
	end

elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

	try
		if (nargout)
			[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
		else
			feval(varargin{:}); % FEVAL switchyard
		end
	catch
		disp(lasterr);
	end

end


%| ABOUT CALLBACKS:
%| GUIDE automatically appends subfunction prototypes to this file, and 
%| sets objects' callback properties to call them through the FEVAL 
%| switchyard above. This comment describes that mechanism.
%|
%| Each callback subfunction declaration has the following form:
%| <SUBFUNCTION_NAME>(H, EVENTDATA, HANDLES, VARARGIN)
%|
%| The subfunction name is composed using the object's Tag and the 
%| callback type separated by '_', e.g. 'slider2_Callback',
%| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'.
%|
%| H is the callback object's handle (obtained using GCBO).
%|
%| EVENTDATA is empty, but reserved for future use.
%|
%| HANDLES is a structure containing handles of components in GUI using
%| tags as fieldnames, e.g. handles.figure1, handles.slider2. This
%| structure is created at GUI startup using GUIHANDLES and stored in
%| the figure's application data using GUIDATA. A copy of the structure
%| is passed to each callback.  You can store additional information in
%| this structure at GUI startup, and you can change the structure
%| during callbacks.  Call guidata(h, handles) after changing your
%| copy to replace the stored original so that subsequent callbacks see
%| the updates. Type "help guihandles" and "help guidata" for more
%| information.
%|
%| VARARGIN contains any extra arguments you have passed to the
%| callback. Specify the extra arguments by editing the callback
%| property in the inspector. By default, GUIDE sets the property to:
%| <MFILENAME>('<SUBFUNCTION_NAME>', gcbo, [], guidata(gcbo))
%| Add any extra arguments after the last argument, before the final
%| closing parenthesis.



% --------------------------------------------------------------------
function varargout = hemisphere_listbox_Callback(h, eventdata, handles, varargin)




% --------------------------------------------------------------------
function varargout = lobe_listbox_Callback(h, eventdata, handles, varargin)




% --------------------------------------------------------------------
function varargout = gyrus_listbox_Callback(h, eventdata, handles, varargin)




% --------------------------------------------------------------------
function varargout = ttype_listbox_Callback(h, eventdata, handles, varargin)




% --------------------------------------------------------------------
function varargout = brodmann_listbox_Callback(h, eventdata, handles, varargin)



% --------------------------------------------------------------------
function varargout = pushbutton1_Callback(h, eventdata, handles, varargin)

    % Each of the following values is a vector containing listbox-indices selcted by the user:
    val_hemi  = get( handles.hemisphere_listbox, 'Value');
    val_lobe  = get( handles.lobe_listbox, 'Value');
    val_gyrus = get( handles.gyrus_listbox, 'Value');
    val_ttype = get( handles.ttype_listbox, 'Value');
    val_brod  = get( handles.brodmann_listbox, 'Value');
    
    % The following labels hold the Talairach labels corresponding to ALL the listbox-indices:
    hemi_TalLabels =  strvcat( 'null', 'L', 'R', 'Lbst', 'Rbst', 'Lcbl', ...
                                                'Rcbl', 'IH' );
                                        
    lobe_TalLabels =  strvcat( 'null', 'FLb', 'PLb', 'TLb', 'OLb', 'LmLb', ...
                        'FTSp', 'ALb', 'PoLb', 'MdBr', 'MO', ...
                        'Pons', 'SubLb' );
                                      
    gyrus_TalLabels = strvcat( 'null', 'sFG', 'mFG', 'iFG', 'meFG', 'OG', ...
                        'RG', 'PrCG', 'PcL', 'ACgG', 'SubcG', ...
                        'CgG', 'PCgG', 'PoCG', 'iPL', 'sPL', ...
                        'AngG', 'SMrgG', 'sTG', 'mTG', 'iTG', ...                 
                        'trTG', 'PHpG', 'Cu', 'PrCu', 'FusG', ...
                        'Unc', 'sOG', 'mOG', 'iOG', 'LngG', 'Ins', ...
                        'Cl', 'Cd', 'LntN', 'ExtraN', 'Th', ...
                        'SubG', 'LV', '3V', '4V', ...
                        'CulVerm', 'iSLL', 'CerebTons', 'CerebLing', ...
                        'Uvul', 'UvulVerm', 'PyrVerm', 'TubVerm', ...
                        'Decl', 'DeclVerm', 'Culm', 'Nodule', ...
                        'Tuber', 'Fastig', 'Pyr' );
               
    ttype_TalLabels  = strvcat( 'null', 'gray', 'white', 'CSF' );

    brod_TalLabels   = strvcat('null', 'BA1','BA2', 'BA3', 'BA4', 'BA5', 'BA6', 'BA7', ...
                               'BA8', 'BA9', 'BA10', 'BA11', 'BA13', 'BA17', ...
                               'BA18', 'BA19', 'BA20', 'BA21', 'BA22', 'BA23', ...
                               'BA24', 'BA25', 'BA27', 'BA28', 'BA29', 'BA30', ...
                               'BA31', 'BA32', 'BA33', 'BA34', 'BA35', 'BA36', ...
                               'BA37', 'BA38', 'BA39', 'BA40', 'BA41', 'BA42', ...
                               'BA43', 'BA44', 'BA45', 'BA46', 'BA47', 'BCd', ...
                               'HCd', 'TCd', 'Pu', 'GPe', 'GPi', 'Amg', 'Hi', ...
                               'HypTh', 'Mmb', 'RN', 'SThN', 'SN', 'An', 'LGn', ...
                               'MGn', 'LDn', 'LPn', 'DMn', 'Mn', 'Pul', 'VAn', ...
                               'VLn', 'VPLn', 'VPMn', 'AC', 'CC', 'Fx', 'OpCh', ...
                               'OpTr', 'Dent');

    % These codes are those Talairach labels corresponding to those indices/regions
    % selected by the user:
    hemi_codes  = hemi_TalLabels(val_hemi, :);
    lobe_codes  = lobe_TalLabels(val_lobe, :);
    gyrus_codes = gyrus_TalLabels(val_gyrus, :);
    ttype_codes = ttype_TalLabels(val_ttype, :);
    brod_codes  = brod_TalLabels(val_brod, :);
    
    % A matrix that holds all the selected Talairach labels:
    TalLabels   = strvcat(hemi_codes, lobe_codes, gyrus_codes, ttype_codes, brod_codes);
    NUM_REGIONS = size(TalLabels, 1);
    
    % Search and eliminate 'null' labels [i.e., regions where the user has not
    % made any selections:
    FinalTalLabels = [];
    for i = 1:NUM_REGIONS
        if ( strcmp((cellstr(TalLabels(i,:))), ('null')) == 0 )
            FinalTalLabels = strvcat(FinalTalLabels, TalLabels(i,:));
        end        
    end

    TalInput = (cellstr(FinalTalLabels))
    
    roi_generate(TalInput{:})
        