J_generateFigure10_correlPElatency.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. %% generate Figure 10 and Figure 10 supplement 1 - Correlation analysis
  2. %firing analysis post lever retraction and correl with PE latency
  3. %Analysis on all neurons without DLS/DMS dissociation
  4. %histogram for correlated neurons in each regions, in each class
  5. clear all;clc;close all
  6. tic
  7. global Dura Baseline Tm Tbase BSIZE Tbin
  8. SAVE_FLAG=1;
  9. BSIZE=0.01;
  10. Dura=[-2 4]; Tm=Dura(1):BSIZE:Dura(2);
  11. Baseline=[-2 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %used to calculate Z-scores
  12. Tbin=-1:0.005:1; %window used to determine the optimal binsize
  13. PStat=0.05;
  14. prewin=[Dura(1) 0];
  15. postwin=[0 .5];
  16. prewinPE=[-0.5 0];
  17. postwin2=[Dura(1):0.05:Dura(2)]; %bounds should match Dura
  18. Slopebounds=[-1.5:0.25:1.5];
  19. R2Bounds=[0:0.05:0.5];
  20. PvalBounds=[0:0.01:0.5];
  21. Struct=10;
  22. XI=1;
  23. RunRegress=1;
  24. SAVEFIG=1;
  25. %R=[];R.Ninfo={};
  26. NN=0;
  27. pre=[]; post=[];
  28. if ~exist('RAW'), load ('RAWextendedtraining.mat'); RAW=RAW; end
  29. if ~exist('Ev'), load('Rextendedtraining_light.mat'); end
  30. load('C');
  31. if RunRegress
  32. % PREALLOCATING
  33. for i=1:length(RAW)
  34. EvInd=strmatch('Last_LP',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  35. Rind=strmatch('FirstPE_PostRew',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  36. if sum(EvInd)~=0 && sum(Rind)~=0
  37. DS(i)=length(RAW(i).Erast{EvInd});
  38. Neur(i)=length(RAW(i).Nrast);
  39. end
  40. end
  41. MaxTrials=max(DS); MaxNeur=sum(Neur); MaxWin=length(postwin);
  42. C.LatPE=NaN(MaxNeur, MaxTrials); % (neurons, trials)
  43. C.prePE=NaN(MaxNeur,MaxTrials); % (neurons, trials)
  44. C.postPE=NaN(MaxNeur,MaxTrials); % (neurons, trials)
  45. C.postwinPE=NaN(MaxNeur,MaxTrials,MaxWin); % (neurons, trials, windows)
  46. %
  47. for i=1:length(RAW) %loops through sessions
  48. EvInd=strmatch('Last_LP',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  49. Rind=strmatch('FirstPE_PostRew',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  50. if sum(EvInd)~=0 && sum(Rind)~=0
  51. Dcell=MakePSR04(RAW(i).Erast(Rind),RAW(i).Erast{EvInd},[-1 10],{2,'first'});
  52. D=cell2mat(Dcell); %inds=find(~isnan(D));
  53. %D(isnan(D))=60; %trials with no response are set to 10.25 to allow to use them in the correlation
  54. for j= 1:size(RAW(i).Nrast,1) %Number of neurons within sessions
  55. NN=NN+1;
  56. C.LatPE(NN,1:length(RAW(i).Erast{EvInd}))=D;
  57. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% ref= last lever press / makes trial by trail rasters.
  58. [PSR3,N3]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Rind},Dura,{2});% ref= post-reward PE / makes trial by trail rasters.
  59. for m=1:size(RAW(i).Erast{Rind},1) %loops through trials
  60. PElatency=RAW(i).Erast{Rind}(m)-RAW(i).Erast{EvInd}(m);
  61. C.prePE(NN,m)=sum(PSR2{1,m}<prewin(2) & PSR2{1,m}>prewin(1))/(abs(prewin(2)-prewin(1))); %neurons, trials
  62. C.postPE1(NN,m)=sum(PSR2{1,m}<postwin(2) & PSR2{1,m}>postwin(1))/(abs(postwin(2)-postwin(1))); %When last LP is ref, postwin is 0-0.5s post LP
  63. end %m loop
  64. fprintf('Neuron ID # %d\n',NN);
  65. end %j loop
  66. end %if
  67. end %i loop
  68. %% Runs the regression analysis
  69. for NN=1:MaxNeur
  70. RegX=[C.LatPE(NN,:)', ones(size(C.LatPE,2),1)];
  71. [C.SLOPEprePE,C.STATSprePE]=corr(C.prePE(NN,:)',C.LatPE(NN,:)','rows','pairwise','type','Spearman');
  72. [C.SLOPEpostPE1(NN,:),C.STATSpostPE1(NN,:)]= corr(C.postPE1(NN,:)',C.LatPE(NN,:)','rows','pairwise','type','Spearman');
  73. [C.SLOPEpostprePE(NN,:),C.STATSpostprePE(NN,:)]= corr((C.postPE(NN,:)-C.prePE(NN,:))',C.LatPE(NN,:)','rows','pairwise','type','Spearman');
  74. fprintf('CORREL Neuron ID # %d\n',NN);
  75. end
  76. save('C.mat', 'C');
  77. end
  78. %% --------------Plots distribution of correlation variables SINGLE WINDOW -------------
  79. % POST WINDOW
  80. figure(1);
  81. for i=1:2
  82. sel1=Coord(:,4)==i*10;
  83. sel2= sel1 & C.STATSpostPE1(:,1)<PStat;
  84. h=subplot(2,2,i);
  85. xmin=floor(min(C.SLOPEpostPE1(sel1,1)));
  86. xmax=ceil(max(C.SLOPEpostPE1(sel1,1)));
  87. step=0.05;
  88. xcenters=xmin:step:xmax;
  89. Nsel1=hist(C.SLOPEpostPE1(sel1,1),xcenters);
  90. Nsel2=hist(C.SLOPEpostPE1(sel2,1),xcenters);
  91. hold on;
  92. bar(xcenters, Nsel1, 'w', 'EdgeColor', 'k', 'BarWidth', 1);alpha(0.3);
  93. bar(xcenters, Nsel2, 'k', 'EdgeColor', 'k', 'BarWidth', 1);
  94. plot([0 0], [0 30],'LineStyle',':','Color','k');
  95. %axis([-10 10 0 20]) %note: their is an outlier @-14 for shell
  96. pval1=signrank(C.SLOPEpostPE1(sel1,1));
  97. pval2=signrank(C.SLOPEpostPE1(sel2,1));
  98. MEANsel1=mean(C.SLOPEpostPE1(sel2,1));
  99. plot([MEANsel1 MEANsel1], [0 10],'Color','r');
  100. %text(-5.8,9.8,sprintf('p=%d', pval1), 'Parent',h);
  101. %text(-5.8,8.8,sprintf('p=%d', pval2), 'Parent',h);
  102. subplot(2,2,i+2)
  103. [N,xcenters2]=hist(C.STATSpostPE1(sel2,1),20);
  104. bar(xcenters2, N, 'k', 'EdgeColor', 'k', 'BarWidth', 1); alpha(0.3);
  105. axis ([0 ceil(max(xcenters2)*10)/10 0 ceil(max(N))+1]);
  106. end
  107. save('C.mat', 'C');