function out = flyY120DataWorkup( data , varargin)
% Reads in the N x 15 cell array of individual fly Y-maze data, returns a
% summary and plots of the number of flies run per line, etc, as well as
% numerous statistics on each fly, and reworked single fly data with
% switchiness and clumpiness. varargin should be 0 for normal usage.

runMode=1;

if runMode==2
    strainCol=5;
else
    strainCol=7;
end

numFlies=size(data,1);
strainList=[{0}];

for i=1:numFlies
    
    %     disp(i);
    
    
    
    if runMode==2
        turnSeq=data{i,6};
        turnTimes=data{i,7};
        data{i,12}=length(turnSeq);
        data{i,13}=mean(turnSeq);
    else
        turnSeq=data{i,14};
        turnTimes=data{i,15};
    end
   
    
    
    ITIs=(turnTimes(2:end)-turnTimes(1:end-1))/1000;
    
    %     data{i,16}=sum((turnSeq(1:end-1)+turnSeq(2:end))==1)/min([sum(turnSeq) length(turnSeq)-sum(turnSeq)]);%switchiness
    pOne=sum(turnSeq)/length(turnSeq);
    pZero=1-pOne;
    data{i,16}=sum((turnSeq(1:end-1)+turnSeq(2:end))==1)/(2*pOne*pZero*length(turnSeq));
    data{i,17}=mad(ITIs,1)/(7200/length(turnSeq));%clumpiness
    %     data{i,18}=sum(ITIs(ITIs>300))/(7200/length(turnSeq));%sleep
    %     data{i,18}=sum((turnSeq(1:end-1)+turnSeq(2:end))==1)/(min([pZero pOne])*length(turnSeq));
    
    N1=100;
    scoreTemp=zeros(N1,1);
    %     for j=1:N1
    %         ITIsTemp=ITIs(randperm(length(ITIs)));
    %         ITIsTemp=ITIsTemp(1:49);
    %         [H,P,KSSTAT] = kstest(zscore(ITIsTemp));
    %         scoreTemp(j)=KSSTAT;
    %     end
    
    %     [H,P,KSSTAT] = kstest(zscore(ITIs));
    data{i,18}=mean(scoreTemp);
    
    temp=data{i,strainCol};
    
    
    if ischar(temp)
        strainList(i,1)={strtrim(temp)};
    elseif iscell(temp)
        strainList(i,1)=temp;
    else
        strainList{i,1}=num2cell(temp);
    end
end

[strainList , ~, Bind]=unique(strainList(:,1));

strainTable=cell(size(strainList,1),2);
strainTable(:,1)=strainList;

strainN=206;
tick=1;
temp2=zeros(4,4,strainN);
for i=1:size(strainList,1)
    strainTable{i,2}=sum(Bind==i);
    
    numTurns=cell2mat(data(Bind==i,12));
    turnBias=cell2mat(data(Bind==i,13));
    switchiness=cell2mat(data(Bind==i,16));
    clumpiness=cell2mat(data(Bind==i,17));
    clumpiness2=cell2mat(data(Bind==i,18));
    
    if i<=strainN && length(numTurns)>=50;
        temp=[turnBias numTurns switchiness clumpiness];
        temp2(:,:,tick)=corr(temp);
        tick=tick+1;
    end
    
    strainTable{i,3}=median(turnBias);
    strainTable{i,4}=median(numTurns);
    strainTable{i,5}=median(switchiness);
    strainTable{i,6}=median(clumpiness);
    %     strainTable{i,7}=median(sleeping);
    
    strainTable{i,7}=mad(turnBias,1);
    strainTable{i,8}=mad(numTurns,1);
    strainTable{i,9}=mad(switchiness,1);
    strainTable{i,10}=mad(clumpiness,1);
    %     strainTable{i,12}=mad(sleeping,1);
    %     strainTable{i,11}=median(sleeping);%old switchiness
    %     strainTable{i,12}=mad(sleeping,1);%old switchiness
    
    strainTable{i,11}=median(clumpiness2);
    strainTable{i,12}=mad(clumpiness2,1);
    strainTable{i,13}=cell2mat(data(Bind==i,13));
    
    disp(strainList(i));
    disp(class(strainList(i)));
    disp(i);
    
    if runMode==2
        if isequal(strainList(i),{'cbd X BK'}); out.cbdhet=switchiness; end;
        if isequal(strainList(i),{'cbd'}); out.cbd=switchiness; end;
        if isequal(strainList(i),{'Ten-a GE1914 (parent of deficiency)'}); out.defPar=switchiness; end;
        if isequal(strainList(i),{'Df(x)ten-a'}); out.def=switchiness; end;
        
    end
    
    counts(i)=sum(Bind==i);
end

temp2(:,:,tick:end)=[];

ranksTemp=[1:size(strainList,1);counts]';
ranksTemp=sortrows(ranksTemp,2);

hist(counts,1:10:200);
xlim([0 200])

out.summary=strainTable(ranksTemp(:,1),:);
out.newData=data;
temp2(isnan(temp2))=0;
out.corrs=temp2;

if varargin{1}==1
    
    out.xTabMAD=[];
    out.xTabN=[];
    labels={};
    labels{1}='45';
    labels{2}='105';
    labels{3}='535';
    labels{4}='796';
    
    for i=1:4
        for j=1:4
            
            if i==j
                targetStr=[labels{i} 'x' labels{i}];
            else
                targetStr=['F' labels{i} 'x' 'M' labels{j}];
            end
            
            
            for k=1:size(out.summary,1);
                
                if isequal(out.summary{k,1},targetStr)
                    out.xTabMAD(i,j)=out.summary{k,7};
                    out.xTabN(i,j)=out.summary{k,2};
                    out.xTabMeanBias(i,j)=out.summary{k,3};
                end
                
            end
        end
    end
    
    
elseif varargin{1}==2
    strainTable=cell(size(strainList,1),2);
    strainTable(:,1)=strainList;
    for i=1:size(strainList,1)
    turnBias=cell2mat(data(Bind==i,13));
    strainTable{i,2}=turnBias;
    end
    out=strainTable;
end



% for i=2:numFlies
%     hitBool=0;
%     if class(data{i,strainCol})=='cell'
%         targetStrain=cell2mat(data{i,strainCol});
%     else
%         targetStrain=data{i,strainCol};
%     end
%     for j=1:size(strainList,1)
%         if isequal(strtrim(targetStrain),strtrim(strainList{j}) )
%             hitBool=1;
%         end
%     end
%     if hitBool==0
%         strainList=[strainList;targetStrain {0}];
%         wb=waitbar(i/numFlies,wb,'Generating List of Strains');
%     end
% end
%
% strainList
%
% wb=waitbar(0,'Counting Flies per Strain');
%
% for i=1:numFlies
%     if class(data{i,strainCol})=='cell'
%         targetStrain=cell2mat(data{i,strainCol});
%     else
%         targetStrain=data{i,strainCol};
%     end
%     whereStrain=where(strainList{:,1}==targetStrain)
% end