J_generateFigure10_s2_shuffle_LatLP1.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. %% generate Figure 10 figure supplement 2
  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. %%
  28. for i=1:length(RAW)
  29. EvInd=strmatch('LeverInsertion',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  30. Rind=strmatch('First_LP',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  31. if sum(EvInd)~=0 && sum(Rind)~=0
  32. DS(i)=length(RAW(i).Erast{EvInd});
  33. Neur(i)=length(RAW(i).Nrast);
  34. end
  35. end
  36. MaxTrials=max(DS); MaxNeur=sum(Neur); MaxWin=length(postwin);
  37. if RunRegress
  38. % PREALLOCATING
  39. Cshuffle.Lat=NaN(MaxNeur, MaxTrials); % (neurons, trials)
  40. Cshuffle.pre=NaN(MaxNeur,MaxTrials); % (neurons, trials)
  41. Cshuffle.post=NaN(MaxNeur,MaxTrials); % (neurons, trials)
  42. Cshuffle.postwin=NaN(MaxNeur,MaxTrials,MaxWin); % (neurons, trials, windows)
  43. for i=1:length(RAW) %loops through sessions
  44. EvInd=strmatch('LeverInsertion',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  45. Rind=strmatch('First_LP',RAW(i).Einfo(:,2),'exact'); %finds the index of the event
  46. if sum(EvInd)~=0 && sum(Rind)~=0
  47. Dcell=MakePSR04(RAW(i).Erast(Rind),RAW(i).Erast{EvInd},[-1 60],{2,'first'});
  48. D=cell2mat(Dcell); %inds=find(~isnan(D));
  49. D(isnan(D))=60; %trials with no response are set to 60 to allow to use them in the correlation
  50. for j= 1:size(RAW(i).Nrast,1) %Number of neurons within sessions
  51. NN=NN+1;
  52. Cshuffle.Lat(NN,1:length(RAW(i).Erast{EvInd}))=D;
  53. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trail rasters. PSR1 is a cell(neurons, trials)
  54. for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
  55. Cshuffle.pre(NN,m)=sum(PSR2{1,m}<prewin(2) & PSR2{1,m}>prewin(1))/(abs(prewin(2)-prewin(1))); %neurons, trials
  56. Cshuffle.post(NN,m)=sum(PSR2{1,m}<postwin(2) & PSR2{1,m}>postwin(1))/(abs(postwin(2)-postwin(1)));
  57. for k=1:(length(postwin2)-1) %loops through time windows.
  58. Cshuffle.postwin(NN,m,k)=sum(PSR2{1,m}<postwin2(k+1) & PSR2{1,m}>postwin2(k))/(abs(postwin2(k+1)-postwin2(k)));
  59. end %k loop
  60. end %m loop
  61. fprintf('Neuron ID # %d\n',NN);
  62. end %j loop
  63. end %if
  64. end %i loop
  65. %% Runs the regression analysis on XX number of iterations of shuffled data
  66. XX=1000;
  67. for n=1:XX
  68. for NN=1:MaxNeur
  69. Cshuffle.LatShuffled(NN,:)=shuffle(Cshuffle.Lat(NN,:));
  70. RegX=[Cshuffle.LatShuffled(NN,:)', ones(size(Cshuffle.LatShuffled,2),1)];
  71. [Cshuffle.SLOPEpostshuffled(NN,n),Cshuffle.STATSpostshuffled(NN,n)]= corr(Cshuffle.post(NN,:)',Cshuffle.LatShuffled(NN,:)','rows','pairwise','type','Spearman');
  72. end
  73. fprintf('Shuffled Iteration # %d\n',n);
  74. % Saving data in C structure and excel file
  75. save('Cshuffle.mat', 'Cshuffle');
  76. end
  77. end
  78. %% Calculates number of significant correlations and the mean correlation coefficient for each
  79. selection1=R.Structure~=0;
  80. for q=1:length(Cshuffle.SLOPEpostshuffled(1,:))
  81. Cshuffle.SLOPEmeanshuffled(q)=nanmean(Cshuffle.SLOPEpostshuffled(selection1,q));
  82. Cshuffle.SIGshuffled(q)=sum(Cshuffle.STATSpostshuffled(selection1,q)<0.05);
  83. Cshuffle.NEGSIGshuffled(q)=sum(Cshuffle.STATSpostshuffled(selection1,q)<0.05 & Cshuffle.SLOPEpostshuffled(selection1,q)<0);
  84. end
  85. %% Calculates correlation coefficients and significant values for real data
  86. for NN=1:MaxNeur
  87. RegX=[Cshuffle.Lat(NN,:)', ones(size(Cshuffle.Lat,2),1)];
  88. [Cshuffle.SLOPEpre,Cshuffle.STATSpre]=corr(Cshuffle.pre(NN,:)',Cshuffle.Lat(NN,:)','rows','pairwise','type','Spearman');
  89. [Cshuffle.SLOPEpost(NN,:),Cshuffle.STATSpost(NN,:)]= corr(Cshuffle.post(NN,:)',Cshuffle.Lat(NN,:)','rows','pairwise','type','Spearman');
  90. [Cshuffle.SLOPEpostpre(NN,:),Cshuffle.STATSpostpre(NN,:)]= corr((Cshuffle.post(NN,:)-Cshuffle.pre(NN,:))',Cshuffle.Lat(NN,:)','rows','pairwise','type','Spearman');
  91. fprintf('CORREL Neuron ID # %d\n',NN);
  92. end
  93. Cshuffle.SelectionLAT(:,1)=R.Structure==10 & R.TRN(:,1)~=0 & Cshuffle.STATSpost(:,1)<PStat;
  94. Cshuffle.SelectionLAT(:,2)=R.Structure==20 & R.TRN(:,1)~=0 & Cshuffle.STATSpost(:,1)<PStat;
  95. % Saving data in C structure and excel file
  96. save('Cshuffle.mat', 'Cshuffle');
  97. %% Figure of distributions of means correlation coefficients and number of significant neurons
  98. selection1=R.Structure==10 & Celltype(:,1)==1;
  99. selection2=R.Structure==20 & Celltype(:,1)==1;
  100. figure;
  101. % Plot of distribution of mean correlation coefficients for all of the
  102. % shuffled data
  103. subplot(2,2,3);
  104. histogram(Cshuffle.SLOPEmeanshuffled,100);
  105. hold on;
  106. MEANsel1=nanmean(Cshuffle.SLOPEpost(selection1,1));
  107. MEANsel2=nanmean(Cshuffle.SLOPEpost(selection2,1));
  108. plot([MEANsel1 MEANsel1], [0 50],'Color','b');
  109. plot([MEANsel2 MEANsel2], [0 50],'Color','r');
  110. xlabel('Mean correlation coefficient') % x-axis label
  111. ylabel('# of iterations')% y-axis label
  112. %Plot of distribution of number of significantly correlated neurons across
  113. %all the shuffled data sets
  114. subplot(2,2,4);
  115. histogram((Cshuffle.SIGshuffled*100/sum(Celltype(:,1)==1)),40);
  116. xlabel('% of significantly correlated unit') % x-axis label
  117. ylabel('# of iterations')% y-axis label
  118. hold on;
  119. SigNUM1=100*sum(Cshuffle.STATSpost(selection1,1)<0.05)/(sum(selection1));
  120. SigNUM2=100*sum(Cshuffle.STATSpost(selection2,1)<0.05)/(sum(selection2));
  121. plot([SigNUM1 SigNUM1], [0 100],'Color','b');
  122. plot([SigNUM2 SigNUM2], [0 100],'Color','r');
  123. propSigSh=Cshuffle.SIGshuffled*100/sum(Celltype(:,1)==1);
  124. OTpval(1,1)=(sum(propSigSh>SigNUM1)+1)/(length(propSigSh)+1);
  125. OTpval(2,1)=(sum(propSigSh>SigNUM2)+1)/(length(propSigSh)+1);
  126. % Plot of correlation coefficients from example shuffled data set
  127. subplot(2,2,1);
  128. sel1=R.Structure~=0;
  129. sel2=sel1 & Cshuffle.STATSpostshuffled(:,1)<PStat;
  130. xmin=floor(min(Cshuffle.SLOPEpostshuffled(sel1,1)));
  131. xmax=ceil(max(Cshuffle.SLOPEpostshuffled(sel1,1)));
  132. step=0.05;
  133. xcenters=xmin:step:xmax;
  134. Nsel1=hist(Cshuffle.SLOPEpostshuffled(sel1,1),xcenters);
  135. Nsel2=hist(Cshuffle.SLOPEpostshuffled(sel2,1),xcenters);
  136. hold on;
  137. bar(xcenters, Nsel1, 'w', 'EdgeColor', 'k', 'BarWidth', 1);alpha(0.3);
  138. bar(xcenters, Nsel2, 'k', 'EdgeColor', 'k', 'BarWidth', 1);
  139. plot([0 0], [0 30],'LineStyle',':','Color','k');
  140. MEANsel1=mean(Cshuffle.SLOPEpostshuffled(sel1,1));
  141. plot([MEANsel1 MEANsel1], [0 10],'Color','r');
  142. hold on;
  143. xlabel('% correlation coefficient shuffle data') % x-axis label
  144. ylabel('# of neurons')% y-axis label
  145. % %Plot of correlation coefficients with significance marked based on
  146. % %bootstrapping
  147. subplot(2,2,2);
  148. sel1=R.Structure~=0;
  149. sel2= sel1 & Cshuffle.STATSpost(:,1)<PStat;
  150. xmin=floor(min(Cshuffle.SLOPEpost(sel1,1)));
  151. xmax=ceil(max(Cshuffle.SLOPEpost(sel1,1)));
  152. step=0.05;
  153. xcenters=xmin:step:xmax;
  154. Nsel1=hist(Cshuffle.SLOPEpost(sel1,1),xcenters);
  155. Nsel2=hist(Cshuffle.SLOPEpost(sel2,1),xcenters);
  156. hold on;
  157. bar(xcenters, Nsel1, 'w', 'EdgeColor', 'k', 'BarWidth', 1);alpha(0.3);
  158. bar(xcenters, Nsel2, 'k', 'EdgeColor', 'k', 'BarWidth', 1);
  159. plot([0 0], [0 30],'LineStyle',':','Color','k');
  160. MEANsel1=mean(Cshuffle.SLOPEpost(sel2,1));
  161. plot([MEANsel1 MEANsel1], [0 10],'Color','r');
  162. xlabel('% correlation coefficient real data') % x-axis label
  163. ylabel('# of neurons')% y-axis label