% Displays first and second-level SPM results. First level results are displayed
% with: ShowResults(subjectn, contrastn, [none/FDR/FWE],[treshold]).
% For second-level results, set subjectn to 'rfx'. Use ShowResults('list') to 
% list current first-level contrast names and numbers, or ('listrfx') to list 
% second-level contrasts. You can skip specifying correction and treshold -
% the script will use pre-specified default values instead.

% Example 1: ShowResults(1, 3, 'FWE', .05) brings up the F>S contrast for
% subject 1, FWE-corrected at p<.05.
% Example 2: ShowResults('rfx', 6) brings up the F>S second-level contrast
% at the default correction and treshold.

% Requires csl_getRes and csl_getSPM2 on the path or in the same directory.
% Based on print_results. See this page for files:
% http://imaging.mrc-cbu.cam.ac.uk/imaging/SpmBatchPrintContrasts 
% You will need to change some parameters below to run this on your data.

% This script makes some assumptions regarding your folder structure, e.g.
% a shared root for first- and second-level scripts, first-level results 
% in each participant's folder, second-level results in folders by contrast etc). 
% If things aren't working, this is where I'd look first.

% J Carlin 20/2/2009
% 24/2/2009 Updated for greater first-level contrast listing flexibility
% via SPM.xCon.name.

function ShowResults(subjectn,contrast,correction,treshold)

% --- PARAMETERS TO CHANGE --- %
addpath /imaging/jc01/UtilityScripts/BatchPrintSPMs/% Change to where this is saved
subjroot ='/imaging/jc01/Gaze01/mrdata/analysis'; % All other paths build on this
subjects = {'S01',... % Subject directories
	'S02',...
	'S03',...
	'S04',...
	'S05',...
	'S06',...
	'S07',...
	'S08',...
	% 'S09',...
	};
ana_dir = 'SPMsplitresults'; % First-level results directory within subject dir
SLdir = 'SecondLevel'; % directory in subjroot

% If correction and treshold aren't specified, use these defaults.
if nargin == 2
	correction = 'none';
	treshold = 0.001;
	display(['No correction or treshold specified. Default values are correction: ' correction ', p<' num2str(treshold)])
end

% --- END OF PARAMETERS --- %

% List of second level condition names from directory names
SLrawdirs = dir(fullfile(subjroot, SLdir));
% Skipping unix . and ..
% Adding some additional control by ensuring only directories are included
ic = 0;
for i=3:size(SLrawdirs,1)
	if SLrawdirs(i).isdir == 1
		ic=ic+1;
		SLdirs{ic} = SLrawdirs(i).name;
	end
end
SLcons = 1:1:size(SLdirs,2);


% List conditions
switch subjectn
	case 'list' % Below, we happily assume that subject 1 is representative of all.
		load(fullfile(subjroot,subjects{1},ana_dir,'SPM.mat'));
		con_n = 1:1:length(SPM.xCon);
		% This code could probably be made more efficient
		D=struct2cell(SPM.xCon); %Condition list to string
		E=D(1,1,:); %Take only name part
		F=shiftdim(E,2); %Rotate dimensions to put names in rows
		display(strcat(int2str(con_n'), ' = ', F))
		return
	case 'listrfx'
		display(strcat(int2str(SLcons'), ' = ', SLdirs'))
		return
end



spm_defaults;
global defaults
defaults.modality='FMRI';


%--------------------------------------------------------------------
%- xSPM is a structure for the parameters

xSPM = struct( ...
      'swd', '', ...       % full path to SPM.mat file
      'Ic', [], ...        % no of contrast (or contrasts for conjunction)
      'Im', [],...         % no of contrast to mask with. Empty for no masking
      'pm', [],...         % masking contrast uncorrected p
      'Ex', [],...         % whether masking is inclusive or exclusive
      'title', '',...      % if empty results in default contrast title
      'Mcp', correction,...    % Mutiple comp method: FWE|FDR|none
      'u', treshold,...        % threshold (corrected or uncorrected, as above)
      'k', 0);             % extent threshold
%--------------------------------------------------------------------

if subjectn == 'rfx' % second-level analysis
	contrastdir = SLdirs{contrast};
	xSPM.swd = fullfile(subjroot, SLdir, contrastdir); %analysis directory
	xSPM.spmmat = fullfile(subjroot, SLdir, contrastdir, 'SPM.mat'); % Get SPM path
	xSPM.Ic=1; % The contrast number is always one for RFX.
else % first-level analysis
	% Index to the right subject
	subj = subjects{subjectn};
	xSPM.swd = fullfile(subjroot, subj, ana_dir); %analysis directory
	xSPM.spmmat = fullfile(subjroot,subj,ana_dir, 'SPM.mat'); % Get SPM path
	% Get contrast - now only by number.
	xSPM.Ic=contrast; %Set the current contrast index	
end

[hReg,xSPM,SPM] = csl_getRes(xSPM);

% Assign some variables to the workspace - this enables the SPM results GUI buttons 
% (plot, overlays etc) to function properly.
assignin('base','xSPM', xSPM);
assignin('base','hReg', hReg);
assignin('base','SPM', SPM);

spm_list('List',xSPM,hReg);
