function  seqLR = PLE_sequenLR(S)

%PLE_SEQUENLR -- Frequencies as a fun. of previous stimulus and response.
%
% seqLR = PLE_sequenLR(S)
%
% A variant of PLE_sequential that works with categories "Left" and
% "Right" instead of "congruent" and "incongruent".
%
% The function returns a structure array SEQLR with fields 'sbj','group',
% and 'counts', where each COUNTSLR is a 144x14 matrix with columns:
%  1. backgr -- [-15,+15] background noise orientation (in degrees)
%  2. prev_contrast -- [.10 .15 .23] peak contrast of previous stimulus
%  3. prev_Gabor -- [-10,+10] orientation of the previous Gabor target
%  4. prev_resp -- [1="Left",2="Right"] previous response
%  5. prev_correct = (prev_resp==prev_Gabor) -- [0=incorrect,1=correct]
%  6. curr_contrast -- [.10 .15 .23] peak contrast of current stimulus
%  7. curr_Gabor -- [-10,+10] orientation of the current Gabor target
%  8. N_total -- total number of cases in the condition specified by 1.-7.
%  9. N_Left -- number of cases with current response = "Left"
% 10. N_Right -- number of cases w curr resp = "Right" (==N_total-N_Left)
% 11. N_correct -- number of correct responses
% 12. P_Left = N_Left/N_total -- probability of responding "Left"
% 13. P_Right= N_Right/N_total -- P("Right")=1-P("congruent")
% 14. P_correct = N_correct/N_total -- probability correct
%
%  See also PLE_SEQUENTIAL, PLEDATA_BY_SBJ, XTAB.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.0 $  $Date: 2003/01/02 10:51 $

for k = 1:length(S)
  seqLR(k).sbj = S(k).sbj ;
  seqLR(k).group = S(k).group ;
  seqLR(k).countsLR = do_one(S(k).data) ;   % <-- MAIN WORK
  %- Collapse BACKGROUND  (column 1)
  foo = seqLR(k).countsLR(:,1:11) ;   % [144x11]
  foo = foo(1:72,:)+foo(73:144,:) ;   % [backr=-15] + [backgr=+15]
  foo(:,1:7) = foo(:,1:7)./2 ;        % foo(:,1) must be 0 throughout
  Ps = foo(:,9:11)./repmat(foo(:,8),1,3) ;
  seqLR(k).counts72 = [foo Ps] ;      % [72x14]
  %- Collapse PREV_CONTRAST  (column 2)
  foo = foo(1:24,:)+foo(25:48,:)+foo(49:72,:) ;     % .10 + .15 + .23
  foo(:,1:7) = foo(:,1:7)./3 ;        % foo(:,2) must be .16 throughout
  Ps = foo(:,9:11)./repmat(foo(:,8),1,3) ;
  seqLR(k).counts24 = [foo Ps] ;      % [24x14]
  %- Collapse CURR_CONTRAST (column 6)
  idx = [1 2  7 8  13 14  19 20]' ;
  foo = foo(idx,:)+foo(idx+2,:)+foo(idx+4,:) ;      % .10 + .15 + .23
  foo(:,1:7) = foo(:,1:7)./3 ;        % foo(:,6) must be .16 throughout
  Ps = foo(:,9:11)./repmat(foo(:,8),1,3) ;
  seqLR(k).counts08 = [foo Ps] ;      % [08x14]
  %- Collapse CURR_GABOR  (column 7)
  idx = [1 3 5 7]' ;
  foo = foo(idx,:)+foo(idx+1,:) ;     % [cG=-10] + [cG=+10]
  foo(:,1:7) = foo(:,1:7)./2 ;        % foo(:,7) must be 0 throughout
  Ps = foo(:,9:11)./repmat(foo(:,8),1,3) ;
  seqLR(k).counts04 = [foo Ps] ;      % [04x14]
end

%--- Return SEQLR
%%%% End of the main function

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DATA is a matrix with 1 row per trial and 10 cols (see PLEDATA_BY_SBJ):
%  1=block [1:32], 2=noise_degr [-15,+15], 3=Cpeak [.10 .15 .23],
%  4=offset [-111,+111], 5=Gabor_degr [-10,+10], 6=congr [0,1]
%  7=resp [0,1,2], 8=correct [0,1], 9=RT [msec], 10=exitcode [0,1,2]
function  countsLR = do_one(data)

%-- Retrieve the relevant variables (i.e. columns)
d = data(:,[1,2,3,5,7,10]) ;    % [blk,backgr,contrast,Gabor,resp,exitcode]

%-- Put "previous" trials on the left of "current" trials
d = [[[0 0 0 0 0 1];d(1:end-1,:)] , d] ;

%-- Reject all trials with missing values (EXITCODE > 0)
idx = find((d(:,6)==0) & (d(:,12)==0)) ;
d = d(idx,[1:5,7:11]) ;

%-- Reject all trials at block boundaries (PREV_BLOCK~=CURR_BLOCK)
idx = find(d(:,1)==d(:,6)) ;
d = d(idx,[2:5,8:10]) ;      % [bkgr,prCp,prGab,prResp,crCp,crGab,crResp]

%-- Cross-tabulate CURR_RESP of the other six (independent) variables
[count_currLR,vals] = xtab2(d(:,1:6),d(:,7)) ;       % [144x6]
if (length(vals{2})~=2)   % vals{2} must be [1 2], not [-9 1 2] or whatnot
  error('Invalid response value(s): [',num2str(vals{2}),']') ;
end
IVs = vals{1} ;           % indep.vars: [bkgr,prCp,prGab,prResp,crCp,crGab]
N = sum(count_currLR,2) ; % total number of cases per condition

%-- Correct responses
LR = IVs(:,3)./20 + 1.5 ;        % prev_Gabor: {-10,+10}-->{1,2}
prev_correct = (IVs(:,4)==LR) ;  % prev_resp == prev_Gabor, [144x1]
LR = d(:,6)./20 + 1.5 ;          % curr_Gabor: {-10,+10}-->{1,2}
curr_correct = (d(:,7)==LR) ;    % curr_resp == curr_Gabor, [9568x1]

%-- Cross-tabulate CURR_CORRECT of the other six (independent) variables
[count_corr,vals] = xtab2(d(:,1:6),curr_correct) ;
if (length(vals{2})~=2)   % vals{2} must be [1 2], not [-9 1 2] or whatnot
  error('Invalid response value(s): [',num2str(vals{2}),']') ;
end
if (any(sum(count_corr,2)~=N))
  error('Mistaken partitioning into correct vs. incorrect responses.') ;
end
count_corr = count_corr(:,2) ;   % 1st column counts INcorrect responses

%-- Calculate probabilities
Ns = [count_currLR,count_corr] ;   % [N_Left,N_Right,N_correct]
Ps = Ns ./ repmat(N,1,3) ;

%-- Put everything together and return
countsLR = [IVs(:,1:4) prev_correct IVs(:,5:6) N Ns Ps] ;

%--- Return COUNTSLR
%%%% End of subfunction DO_ONE

%%%%% End of PLE_SEQUENLR.M
