J_generateFigure10_s4_shuffle_LatPE.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. %% generate Figure 10 figure supplement 4
  2. clear all;clc;
  3. tic
  4. global Dura Baseline Tm Tbase BSIZE Tbin
  5. SAVE_FLAG=1;
  6. BSIZE=0.01;
  7. Dura=[-2 4]; Tm=Dura(1):BSIZE:Dura(2);
  8. Baseline=[-2 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %used to calculate Z-scores
  9. Tbin=-1:0.005:1; %window used to determine the optimal binsize
  10. PStat=0.05;
  11. prewin=[Dura(1) 0];
  12. postwin=[0 .5];
  13. postwin2=[Dura(1):0.05:Dura(2)]; %bounds should match Dura
  14. Slopebounds=[-.5:.05:.5];
  15. R2Bounds=[0:0.05:0.5];
  16. PvalBounds=[0:0.01:0.5];
  17. XI=[1 -1];
  18. RunRegress=1;
  19. SAVEFIG=1;
  20. %R=[];R.Ninfo={};
  21. NN=0;
  22. pre=[]; post=[];
  23. if ~exist('RAW'), load ('RAWextendedtraining.mat'); RAW=RAW; end
  24. if ~exist('R'), load('Rextendedtraining.mat'); R=R; end
  25. if RunRegress==0, load('C'); end
  26. load('Celltype_extendedTraining.mat')
  27. %path='C:\FRED\GALLO\Exps\2009-present GoNoGo\FINAL\PLX Files GoNoGo\GoNoGo NEX files\RESULTgn14.xls';
  28. %%
  29. for i=1:length(RAW)
  30. EvInd=strmatch('Last_LP',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  31. Rind=strmatch('FirstPE_PostRew',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  32. if sum(EvInd)~=0 && sum(Rind)~=0
  33. DS(i)=length(RAW(i).Erast{EvInd});
  34. Neur(i)=length(RAW(i).Nrast);
  35. end
  36. end
  37. MaxTrials=max(DS); MaxNeur=sum(Neur); MaxWin=length(postwin);
  38. if RunRegress
  39. % PREALLOCATING
  40. C.LatPE=NaN(MaxNeur, MaxTrials); % (neurons, trials)
  41. C.prePE=NaN(MaxNeur,MaxTrials); % (neurons, trials)
  42. C.postPE=NaN(MaxNeur,MaxTrials); % (neurons, trials)
  43. C.postwinPE=NaN(MaxNeur,MaxTrials,MaxWin); % (neurons, trials, windows)
  44. Cshuffle.LatPE=NaN(MaxNeur, MaxTrials); % (neurons, trials)
  45. Cshuffle.prePE=NaN(MaxNeur,MaxTrials); % (neurons, trials)
  46. Cshuffle.postPE=NaN(MaxNeur,MaxTrials); % (neurons, trials)
  47. Cshuffle.postwinPE=NaN(MaxNeur,MaxTrials,MaxWin); % (neurons, trials, windows)
  48. for i=1:length(RAW) %loops through sessions
  49. EvInd=strmatch('Last_LP',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  50. Rind=strmatch('FirstPE_PostRew',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  51. if sum(EvInd)~=0 && sum(Rind)~=0
  52. Dcell=MakePSR04(RAW(i).Erast(Rind),RAW(i).Erast{EvInd},[-1 10],{2,'first'});
  53. D=cell2mat(Dcell); %inds=find(~isnan(D));
  54. D(isnan(D))=60; %trials with no response are set to 10.25 to allow to use them in the correlation
  55. for j= 1:size(RAW(i).Nrast,1) %Number of neurons within sessions
  56. NN=NN+1;
  57. Cshuffle.LatPE(NN,1:length(RAW(i).Erast{EvInd}))=D;
  58. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% ref= last lever press / 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. Cshuffle.prePE(NN,m)=sum(PSR2{1,m}<prewin(2) & PSR2{1,m}>prewin(1))/(abs(prewin(2)-prewin(1))); %neurons, trials
  62. Cshuffle.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 on XX number of iterations of shuffled data
  69. XX=1000;
  70. for n=1:XX
  71. for NN=1:MaxNeur
  72. Cshuffle.LatShuffled(NN,:)=shuffle(Cshuffle.LatPE(NN,:));
  73. RegX=[Cshuffle.LatShuffled(NN,:)', ones(size(Cshuffle.LatShuffled,2),1)];
  74. [Cshuffle.SLOPEpostshuffled(NN,n),Cshuffle.STATSpostshuffled(NN,n)]= corr(Cshuffle.postPE1(NN,:)',Cshuffle.LatShuffled(NN,:)','rows','pairwise','type','Spearman');
  75. end
  76. fprintf('Shuffled Iteration # %d\n',n);
  77. % Saving data in C structure and excel file
  78. save('Cshuffle.mat', 'Cshuffle');
  79. end
  80. end
  81. %% Calculates number of significant correlations and the mean correlation coefficient for each
  82. selection1=R.Structure~=0;
  83. for q=1:length(Cshuffle.SLOPEpostshuffled(1,:))
  84. Cshuffle.SLOPEmeanshuffled(q)=nanmean(Cshuffle.SLOPEpostshuffled(selection1,q));
  85. Cshuffle.SIGshuffled(q)=sum(Cshuffle.STATSpostshuffled(selection1,q)<0.05);
  86. Cshuffle.NEGSIGshuffled(q)=sum(Cshuffle.STATSpostshuffled(selection1,q)<0.05 & Cshuffle.SLOPEpostshuffled(selection1,q)<0);
  87. end
  88. %% Calculates correlation coefficients and significant values for real data
  89. for NN=1:MaxNeur
  90. RegX=[Cshuffle.LatPE(NN,:)', ones(size(Cshuffle.LatPE,2),1)];
  91. [Cshuffle.SLOPEpre,Cshuffle.STATSpre]=corr(Cshuffle.prePE(NN,:)',Cshuffle.LatPE(NN,:)','rows','pairwise','type','Spearman');
  92. [Cshuffle.SLOPEpost(NN,:),Cshuffle.STATSpost(NN,:)]= corr(Cshuffle.postPE1(NN,:)',Cshuffle.LatPE(NN,:)','rows','pairwise','type','Spearman');
  93. [Cshuffle.SLOPEpostpre(NN,:),Cshuffle.STATSpostpre(NN,:)]= corr((Cshuffle.postPE1(NN,:)-Cshuffle.prePE(NN,:))',Cshuffle.LatPE(NN,:)','rows','pairwise','type','Spearman');
  94. fprintf('CORREL Neuron ID # %d\n',NN);
  95. end
  96. Cshuffle.SelectionLAT(:,1)=R.Structure==10 & R.TRN(:,1)~=0 & Cshuffle.STATSpost(:,1)<PStat;
  97. Cshuffle.SelectionLAT(:,2)=R.Structure==20 & R.TRN(:,1)~=0 & Cshuffle.STATSpost(:,1)<PStat;
  98. % Saving data in C structure and excel file
  99. save('Cshuffle.mat', 'Cshuffle');
  100. %% Figure of distributions of means correlation coefficients and number of significant neurons
  101. selection1=R.Structure==10 & Celltype(:,1)==1;
  102. selection2=R.Structure==20 & Celltype(:,1)==1;
  103. figure;
  104. % Plot of distribution of mean correlation coefficients for all of the
  105. % shuffled data
  106. subplot(2,2,3);
  107. histogram(Cshuffle.SLOPEmeanshuffled,100);
  108. hold on;
  109. MEANsel1=nanmean(Cshuffle.SLOPEpost(selection1,1));
  110. MEANsel2=nanmean(Cshuffle.SLOPEpost(selection2,1));
  111. plot([MEANsel1 MEANsel1], [0 50],'Color','b');
  112. plot([MEANsel2 MEANsel2], [0 50],'Color','r');
  113. xlabel('Mean correlation coefficient') % x-axis label
  114. ylabel('# of iterations')% y-axis label
  115. %Plot of distribution of number of significantly correlated neurons across
  116. %all the shuffled data sets
  117. subplot(2,2,4);
  118. histogram((Cshuffle.SIGshuffled*100/sum(Celltype(:,1)==1)),40);
  119. xlabel('% of significantly correlated unit') % x-axis label
  120. ylabel('# of iterations')% y-axis label
  121. hold on;
  122. SigNUM1=100*sum(Cshuffle.STATSpost(selection1,1)<0.05)/(sum(selection1));
  123. SigNUM2=100*sum(Cshuffle.STATSpost(selection2,1)<0.05)/(sum(selection2));
  124. plot([SigNUM1 SigNUM1], [0 100],'Color','b');
  125. plot([SigNUM2 SigNUM2], [0 100],'Color','r');
  126. propSigSh=Cshuffle.SIGshuffled*100/sum(Celltype(:,1)==1);
  127. OTpval(1,1)=(sum(propSigSh>SigNUM1)+1)/(length(propSigSh)+1);
  128. OTpval(2,1)=(sum(propSigSh>SigNUM2)+1)/(length(propSigSh)+1);
  129. % Plot of correlation coefficients from example shuffled data set
  130. subplot(2,2,1);
  131. sel1=R.Structure~=0;
  132. sel2=sel1 & Cshuffle.STATSpostshuffled(:,1)<PStat;
  133. xmin=floor(min(Cshuffle.SLOPEpostshuffled(sel1,1)));
  134. xmax=ceil(max(Cshuffle.SLOPEpostshuffled(sel1,1)));
  135. step=0.05;
  136. xcenters=xmin:step:xmax;
  137. Nsel1=hist(Cshuffle.SLOPEpostshuffled(sel1,1),xcenters);
  138. Nsel2=hist(Cshuffle.SLOPEpostshuffled(sel2,1),xcenters);
  139. hold on;
  140. bar(xcenters, Nsel1, 'w', 'EdgeColor', 'k', 'BarWidth', 1);alpha(0.3);
  141. bar(xcenters, Nsel2, 'k', 'EdgeColor', 'k', 'BarWidth', 1);
  142. plot([0 0], [0 30],'LineStyle',':','Color','k');
  143. MEANsel1=mean(Cshuffle.SLOPEpostshuffled(sel1,1));
  144. plot([MEANsel1 MEANsel1], [0 10],'Color','r');
  145. xlabel('% correlation coefficient shuffle data') % x-axis label
  146. ylabel('# of neurons')% y-axis label
  147. hold on;
  148. % %Plot of correlation coefficients with significance marked based on
  149. % %bootstrapping
  150. subplot(2,2,2);
  151. sel1=R.Structure~=0;
  152. sel2= sel1 & Cshuffle.STATSpost(:,1)<PStat;
  153. xmin=floor(min(Cshuffle.SLOPEpost(sel1,1)));
  154. xmax=ceil(max(Cshuffle.SLOPEpost(sel1,1)));
  155. step=0.05;
  156. xcenters=xmin:step:xmax;
  157. Nsel1=hist(Cshuffle.SLOPEpost(sel1,1),xcenters);
  158. Nsel2=hist(Cshuffle.SLOPEpost(sel2,1),xcenters);
  159. hold on;
  160. bar(xcenters, Nsel1, 'w', 'EdgeColor', 'k', 'BarWidth', 1);alpha(0.3);
  161. bar(xcenters, Nsel2, 'k', 'EdgeColor', 'k', 'BarWidth', 1);
  162. plot([0 0], [0 30],'LineStyle',':','Color','k');
  163. MEANsel1=mean(Cshuffle.SLOPEpost(sel2,1));
  164. plot([MEANsel1 MEANsel1], [0 10],'Color','r');
  165. xlabel('% correlation coefficient real data') % x-axis label
  166. ylabel('# of neurons')% y-axis label