clear 
%% mice
Files_mouse=dir(['*m.mat']);  %only mice

AllSessions_mouse=[];
for i=1:length(Files_mouse)
   temp=load(Files_mouse(i).name); 
   AllSessions_mouse=[AllSessions_mouse struct2cell(temp)] ;
end
AllCellsStat_mouse=[];
for i=1:length(AllSessions_mouse)
    AllCellsStat_mouse=[AllCellsStat_mouse AllSessions_mouse{i}];
end

pval_all_mouse=[];
pval_int_mouse=[];
pval_pyr_mouse=[];
for i=1:length(AllCellsStat_mouse)
    if ~cellfun(@isempty,AllCellsStat_mouse(i)) & AllCellsStat_mouse{i}.meanFiringRatePerSecNoStim>=0.5
        pval_all_mouse=[pval_all_mouse AllCellsStat_mouse{i}.pSN];
        if AllCellsStat_mouse{i}.cellType=="pyr"
                pval_pyr_mouse=[pval_pyr_mouse AllCellsStat_mouse{i}.pSN];
        elseif AllCellsStat_mouse{i}.cellType=="int"
                pval_int_mouse=[pval_int_mouse AllCellsStat_mouse{i}.pSN];
        end
    end
end

SuppCell_p05_mouse={};
ExctCell_p05_mouse={};
for i=1:length(AllCellsStat_mouse)
    if ~cellfun(@isempty,AllCellsStat_mouse(i)) & AllCellsStat_mouse{i}.meanFiringRatePerSecNoStim>=0.5
        if AllCellsStat_mouse{i}.pSN<0.05
            if AllCellsStat_mouse{i}.meanFiringRatePerSecNoStim>AllCellsStat_mouse{i}.meanFiringRatePerSecStim
            SuppCell_p05_mouse=[SuppCell_p05_mouse AllCellsStat_mouse{i}] ;
            elseif AllCellsStat_mouse{i}.meanFiringRatePerSecNoStim<AllCellsStat_mouse{i}.meanFiringRatePerSecStim
            ExctCell_p05_mouse=[ExctCell_p05_mouse AllCellsStat_mouse{i}] ;
            
        end
        end
    end
end


perctage_allcells_mouse=length(find(pval_all_mouse<0.05))/length(pval_all_mouse)*100
perctage_intcells_mouse=length(find(pval_int_mouse<0.05))/length(pval_int_mouse)*100
perctage_pyrcells_mouse=length(find(pval_pyr_mouse<0.05))/length(pval_pyr_mouse)*100

totNumCells_mouse=length(pval_all_mouse)
affectedNumcells_mouse=length(find(pval_all_mouse<0.05))
perctage_ExctCell_mouse=length(ExctCell_p05_mouse)/affectedNumcells_mouse*100
perctage_SuppCell_mouse=length(SuppCell_p05_mouse)/affectedNumcells_mouse*100

%% rats
Files_rat=dir(['*r.mat']);  %only rats

AllSessions_rat=[];
for i=1:length(Files_rat)
   temp=load(Files_rat(i).name); 
   AllSessions_rat=[AllSessions_rat struct2cell(temp)] ;
end
AllCellsStat_rat=[];
for i=1:length(AllSessions_rat)
    AllCellsStat_rat=[AllCellsStat_rat AllSessions_rat{i}];
end

pval_all_rat=[];
pval_int_rat=[];
pval_pyr_rat=[];
for i=1:length(AllCellsStat_rat)
    if ~cellfun(@isempty,AllCellsStat_rat(i)) & AllCellsStat_rat{i}.meanFiringRatePerSecNoStim>=0.5
        pval_all_rat=[pval_all_rat AllCellsStat_rat{i}.pSN];
        if AllCellsStat_rat{i}.cellType=="pyr"
                pval_pyr_rat=[pval_pyr_rat AllCellsStat_rat{i}.pSN];
        elseif AllCellsStat_rat{i}.cellType=="int"
                pval_int_rat=[pval_int_rat AllCellsStat_rat{i}.pSN];
        end
    end
end

SuppCell_p05_rat={};
ExctCell_p05_rat={};
for i=1:length(AllCellsStat_rat)
    if ~cellfun(@isempty,AllCellsStat_rat(i)) & AllCellsStat_rat{i}.meanFiringRatePerSecNoStim>=0.5
        if AllCellsStat_rat{i}.pSN<0.05
            if AllCellsStat_rat{i}.meanFiringRatePerSecNoStim>AllCellsStat_rat{i}.meanFiringRatePerSecStim
            SuppCell_p05_rat=[SuppCell_p05_rat AllCellsStat_rat{i}] ;
            elseif AllCellsStat_rat{i}.meanFiringRatePerSecNoStim<AllCellsStat_rat{i}.meanFiringRatePerSecStim
            ExctCell_p05_rat=[ExctCell_p05_rat AllCellsStat_rat{i}] ;
            
        end
        end
    end
end




perctage_allcells_rat=length(find(pval_all_rat<0.05))/length(pval_all_rat)*100
perctage_intcells_rat=length(find(pval_int_rat<0.05))/length(pval_int_rat)*100
perctage_pyrcells_rat=length(find(pval_pyr_rat<0.05))/length(pval_pyr_rat)*100

pval_int_mouse_rat=[pval_int_mouse pval_int_rat];
perctage_intcells_all=length(find(pval_int_mouse_rat<0.05))/length(pval_int_mouse_rat)*100;

pval_pyr_mouse_rat=[pval_pyr_mouse pval_pyr_rat];
perctage_pyrcells_all=length(find(pval_pyr_mouse_rat<0.05))/length(pval_pyr_mouse_rat)*100;

totNumCells_rat=length(pval_all_rat)
affectedNumcells_rat=length(find(pval_all_rat<0.05))
perctage_ExctCell_rat=length(ExctCell_p05_rat)/affectedNumcells_rat*100
perctage_SuppCell_rat=length(SuppCell_p05_rat)/affectedNumcells_rat*100
save(['CoparisionResults' datestr(now,'mmddyyHHMMSS') '.mat'],'-v7.3')

%% make plot
fig1=figure(); hold on;

% add mice data
for i=1:length(AllCellsStat_mouse)
    if ~cellfun(@isempty,AllCellsStat_mouse(i)) & AllCellsStat_mouse{i}.meanFiringRatePerSecNoStim>=0.5 

        if AllCellsStat_mouse{i}.cellType=='pyr' & AllCellsStat_mouse{i}.pSN<0.05
             scatter(AllCellsStat_mouse{i}.meanFiringRatePerSecNoStim,AllCellsStat_mouse{i}.meanFiringRatePerSecStim,80,'filled'...
             ,'MarkerFaceColor',[0.85 0.85 0.85],'MarkerEdgeColor',[0 0 1]);
        elseif AllCellsStat_mouse{i}.cellType=='int' & AllCellsStat_mouse{i}.pSN<0.05
             scatter(AllCellsStat_mouse{i}.meanFiringRatePerSecNoStim,AllCellsStat_mouse{i}.meanFiringRatePerSecStim,80,'filled'...
             ,'MarkerFaceColor',[0.85 0.85 0.85],'MarkerEdgeColor',[1 0 0]);  
        else 
            scatter(AllCellsStat_mouse{i}.meanFiringRatePerSecNoStim,AllCellsStat_mouse{i}.meanFiringRatePerSecStim,80,'filled'...
            ,'MarkerFaceColor',[0.85 0.85 0.85],'MarkerEdgeColor',[0.85 0.85 0.85])
        end
    end
end

% add rats data
for i=1:length(AllCellsStat_rat)
    if ~cellfun(@isempty,AllCellsStat_rat(i)) & AllCellsStat_rat{i}.meanFiringRatePerSecNoStim>=0.5 

        if AllCellsStat_rat{i}.cellType=='pyr' & AllCellsStat_rat{i}.pSN<0.05
             scatter(AllCellsStat_rat{i}.meanFiringRatePerSecNoStim,AllCellsStat_rat{i}.meanFiringRatePerSecStim,80,'d','filled'...
             ,'MarkerFaceColor',[0.75, 0.75, 0],'MarkerEdgeColor',[0 0 1]);
        elseif AllCellsStat_rat{i}.cellType=='int' & AllCellsStat_rat{i}.pSN<0.05
             scatter(AllCellsStat_rat{i}.meanFiringRatePerSecNoStim,AllCellsStat_rat{i}.meanFiringRatePerSecStim,80,'d','filled'...
             ,'MarkerFaceColor',[0.75, 0.75, 0],'MarkerEdgeColor',[1 0 0]);  
        else 
            scatter(AllCellsStat_rat{i}.meanFiringRatePerSecNoStim,AllCellsStat_rat{i}.meanFiringRatePerSecStim,80,'d','filled'...
            ,'MarkerFaceColor',[0.75, 0.75, 0],'MarkerEdgeColor',[0.75, 0.75, 0])
        end
    end
end

set(gca, 'XScale', 'log')
set(gca, 'YScale', 'log')
line ([min(xlim) max(xlim)],[min(ylim) max(ylim)])
axis square
xlabel('Mean Firing Rate during RF-OFF (Hz)')
ylabel('Mean Firing Rate during RF-ON (Hz)')
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
set(fig1,'Renderer', 'painters')
saveas(gca,fullfile(cd,['AllCells.fig']))
saveas(gca,fullfile(cd,['AllCells.pdf']))