F_Figure5_Figure.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. %% Plotting Heatmaps and average PSTH for sequence of event in DT5, FR5 and FS5
  2. % edited on june 2019 to only include sucrose rats in DT5 data set
  3. % do average first/last 3 session on PSTH instead of all sessions
  4. clc;clear all; close all;
  5. load('R_Comparison_2task.mat')
  6. R2=R;
  7. task_name={'DT5','FS5'};
  8. task_Dataset={'R_DT5','R_FS5'};
  9. BSIZE=0.01; %Do not change
  10. Dura=[-25 20]; Tm=Dura(1):BSIZE:Dura(2);
  11. thresholdIPI=[1,2,1];
  12. nbLP=[2,3,2];
  13. %% --- --- Plotting color-coded and average PSTHs --- ---
  14. BrainRegion=[10 20]; %10 for DLS, 20 for DMS
  15. % ----------- COLORS ---------
  16. ColorsTask=[51/255 102/255 204/255,
  17. 255/255 153/255 51/255];
  18. Clim=[-2 2];%ColorMap
  19. Xevent=[-0.25 0.25];
  20. Yaxis=[-1 3];Xaxis=[1 357];
  21. Ishow=find(Tm>=Xevent(1) & Tm<=Xevent(2));
  22. % for stat
  23. Ishow1(1,:)=find(Tm>=Xevent(1) & Tm<=0);
  24. Ishow1(2,:)=find(Tm>=0 & Tm<=Xevent(2));
  25. Combined_Table_allTask=[];allTask_ID=[];
  26. %% ------------------------------------Main program ------------------------------------------------------------------------------------------------------------------------
  27. figure;
  28. for task=1:length(R)
  29. for event=1:3
  30. %generate concat file
  31. clear R Celltype SesCelltype
  32. load(task_Dataset{task})
  33. DLS_activity_first3=[];DMS_activity_first3=[];DLS_activity_last3=[]; DMS_activity_last3=[];DLSconcat_ACQ_all=[];DMSconcat_ACQ_all=[];DLS_Sess_all=[];DMS_Sess_all=[];
  34. for k=1:7
  35. clear DLSselectionTRN DMSselectionTRN selectionTRN DLS_activity DMS_activity
  36. for neuron=1:length(R2(task).Ses(k).IPI)
  37. ID_ShortIPI=R2(task).Ses(k).IPI{neuron,event}<thresholdIPI(event);
  38. if ~isempty(ID_ShortIPI)
  39. IPI(neuron,1)=nanmean(R2(task).Ses(k).IPI{neuron,event}(ID_ShortIPI,1),1);
  40. else
  41. IPI(neuron,1)=NaN;
  42. end
  43. end
  44. DLSselectionTRN=R.Ses(k).Coord(:,4)==BrainRegion(1) & R.Ses(k).TRN(:,1)~=0 & SesCelltype(k).Celltype(:,1)==1 & R2(task).Ses(k).NumberTrials(:,event)>5;
  45. DMSselectionTRN=R.Ses(k).Coord(:,4)==BrainRegion(2) & R.Ses(k).TRN(:,1)~=0 & SesCelltype(k).Celltype(:,1)==1 & R2(task).Ses(k).NumberTrials(:,event)>5;
  46. if ~isempty(R2(task).Ses(k).PSTHz{event})
  47. DLS_activity=R2(task).Ses(k).PSTHz{event}(DLSselectionTRN,Ishow);
  48. DMS_activity=R2(task).Ses(k).PSTHz{event}(DMSselectionTRN,Ishow);
  49. else
  50. DLS_activity=NaN(1,length(Ishow));
  51. DMS_activity=NaN(1,length(Ishow));
  52. end
  53. selectionTRN=R.Ses(k).Coord(:,4)~=0 & R.Ses(k).TRN(:,1)~=0 & SesCelltype(k).Celltype(:,1)==1 & R2(task).Ses(k).NumberTrials(:,event)>5;
  54. SelectionIPI=IPI(selectionTRN,1);
  55. ResponseRate{task}{k,event}=nbLP(event)./unique(SelectionIPI,'first');
  56. MeanRR{task}(k,event)=nanmean(ResponseRate{task}{k,event},1);
  57. semRR{task}(k,event)=nansem(ResponseRate{task}{k,event},1);
  58. if k<4 %first 3 DT5 sessions
  59. DLS_activity_first3=cat(1,DLS_activity_first3,DLS_activity);
  60. DMS_activity_first3=cat(1,DMS_activity_first3,DMS_activity);
  61. end
  62. if k>(length(R.Ses)-3)
  63. DLS_activity_last3=cat(1,DLS_activity_last3,DLS_activity);
  64. DMS_activity_last3=cat(1,DMS_activity_last3,DMS_activity);
  65. end
  66. DLSconcat_ACQ_all=cat(1,DLSconcat_ACQ_all,DLS_activity);
  67. DMSconcat_ACQ_all=cat(1,DMSconcat_ACQ_all,DMS_activity);
  68. DLS_Sess_all=cat(1,DLS_Sess_all,k*ones(size(DLS_activity,1),1));
  69. DMS_Sess_all=cat(1,DMS_Sess_all,k*ones(size(DMS_activity,1),1));
  70. time=[1:1:size(DLS_activity,2)];
  71. MeanDLS_ACQ{task}{event}(k,:)=nanmean(DLS_activity,1);
  72. MeanDMS_ACQ{task}{event}(k,:)=nanmean(DMS_activity,1);
  73. DLSsem1{task}{event}(k,:)=nansem(DLS_activity(:,time),1);
  74. DMSsem1{task}{event}(k,:)=nansem(DMS_activity(:,time),1);
  75. DLSnn{task}(k,event)=size(DLS_activity,1);
  76. DMSnn{task}(k,event)=size(DMS_activity,1);
  77. end
  78. TableAct_DLS{task,event}=DLSconcat_ACQ_all;
  79. TableAct_DMS{task,event}=DMSconcat_ACQ_all;
  80. TableSes_DLS{task,event}=DLS_Sess_all;
  81. TableSes_DMS{task,event}=DMS_Sess_all;
  82. % MeanDLS_ACQ_first3=nanmean(DLS_activity_first3,1);
  83. % MeanDMS_ACQ_first3=nanmean(DMS_activity_first3,1);
  84. % DLSsem1_first3=nansem(DLS_activity_first3,1);
  85. % DMSsem1_first3=nansem(DMS_activity_first3,1);
  86. %
  87. % MeanDLS_ACQ_last3=nanmean(DLS_activity_last3,1);
  88. % MeanDMS_ACQ_last3=nanmean(DMS_activity_last3,1);
  89. % DLSsem1_last3=nansem(DLS_activity_last3,1);
  90. % DMSsem1_last3=nansem(DMS_activity_last3,1);
  91. MeanDLS_ACQ_all=nanmean(DLSconcat_ACQ_all,1);
  92. MeanDMS_ACQ_all=nanmean(DMSconcat_ACQ_all,1);
  93. DLSsem1_all=nansem(DLSconcat_ACQ_all,1);
  94. DMSsem1_all=nansem(DMSconcat_ACQ_all,1);
  95. %% Heatmaps
  96. subplot(4,6,6+event+6*(task-1))
  97. imagesc(time,[1 size(MeanDLS_ACQ{task}{event},1)],MeanDLS_ACQ{task}{event},Clim); %colorbar;axis tight;
  98. colormap(parula);
  99. hold on
  100. plot([0 0],[size(MeanDLS_ACQ{task}{event},1)+0.5 0.5],'k')
  101. hold off
  102. set(gca,'XTick',0:25.5:size(MeanDLS_ACQ{task}{event},2));
  103. set(gca,'xticklabel',{'-0.25','0','0.25'})
  104. set(gca,'YTick',1:size(MeanDLS_ACQ{task}{event},1));
  105. set(gca,'yticklabel',DLSnn{task}(:,event))
  106. ylabel('sessions');
  107. subplot(4,6,9+event+6*(task-1))
  108. imagesc(time,[1 size(MeanDMS_ACQ{task}{event},1)],MeanDMS_ACQ{task}{event},Clim); %colorbar;axis tight;
  109. colormap(parula);
  110. hold on
  111. plot([0 0],[size(MeanDMS_ACQ{task}{event},1)+0.5 0.5],'k')
  112. set(gca,'XTick',0:25.5:size(MeanDMS_ACQ{task}{event},2));
  113. set(gca,'xticklabel',{'-0.25','0','0.25'})
  114. set(gca,'YTick',1:size(MeanDMS_ACQ{task}{event},1));
  115. set(gca,'yticklabel',DMSnn{task}(:,event))
  116. hold off
  117. subplot(4,6,18+event)
  118. DLSup=MeanDLS_ACQ_all+DLSsem1_all;
  119. DLSdown=MeanDLS_ACQ_all-DLSsem1_all;
  120. hold on;
  121. patch([time,time(end:-1:1)],[DLSup,DLSdown(end:-1:1)],ColorsTask(task,:),'EdgeColor','none');alpha(0.5);
  122. g1=plot(time,MeanDLS_ACQ_all,'Color',ColorsTask(task,:),'linewidth',1.5);
  123. plot([0 50],[0 0],'k','LineStyle',':')
  124. plot([25 25],[-1 2.5],'k','LineStyle',':')
  125. axis([0 51 -1 2.5]);
  126. set(gca,'XTick',0:25.5:size(MeanDMS_ACQ{task}{event},2));
  127. set(gca,'xticklabel',{'-0.25','0','0.25'})
  128. ylabel('z-score');
  129. ylim([-1 2.5]);
  130. subplot(4,6,21+event)
  131. DMSup=MeanDMS_ACQ_all+DMSsem1_all;
  132. DMSdown=MeanDMS_ACQ_all-DMSsem1_all;
  133. hold on;
  134. patch([time,time(end:-1:1)],[DMSup,DMSdown(end:-1:1)],ColorsTask(task,:),'EdgeColor','none');alpha(0.5);
  135. g2=plot(time,MeanDMS_ACQ_all,'Color',ColorsTask(task,:),'linewidth',1.5);
  136. plot([0 50],[0 0],'k','LineStyle',':')
  137. plot([25 25],[-1 2.5],'k','LineStyle',':')
  138. axis([0 51 -1 2.5]);
  139. set(gca,'XTick',0:25.5:size(MeanDMS_ACQ{task}{event},2));
  140. set(gca,'xticklabel',{'-0.25','0','0.25'})
  141. ylabel('z-score');
  142. hold off
  143. end
  144. end
  145. for event=1:3
  146. for task=1:2
  147. subplot(4,6,3+event)
  148. hold on
  149. er = errorbar(1:7,MeanRR{task}(:,event),semRR{task}(:,event),'-o','MarkerSize',4,'color',ColorsTask(task,:),'MarkerFaceColor',ColorsTask(task,:),'CapSize',0,'LineWidth',1);
  150. ylabel('response rate (Hz)');
  151. xlabel('sessions')
  152. axis([0 8 0 10])
  153. end
  154. end
  155. %% statistics
  156. filename = 'ResultStat_Activity_Fig5.xlsx';
  157. for event=1:3
  158. ActDLS_BothTask=cat(1,TableAct_DLS{:,event});
  159. ActDMS_BothTask=cat(1,TableAct_DMS{:,event});
  160. TableActDLS_BothTask=[nanmean(ActDLS_BothTask(:,1:25),2) nanmean(ActDLS_BothTask(:,26:51),2)];
  161. TableActDMS_BothTask=[nanmean(ActDMS_BothTask(:,1:25),2) nanmean(ActDMS_BothTask(:,26:51),2)];
  162. Task_ID_DLS=cat(1,zeros(size(TableAct_DLS{1,event},1),1),ones(size(TableAct_DLS{2,event},1),1));
  163. Task_ID_DMS=cat(1,zeros(size(TableAct_DMS{1,event},1),1),ones(size(TableAct_DMS{2,event},1),1));
  164. SessionsDLS_BothTask=cat(1,TableSes_DLS{:,event});
  165. SessionsDMS_BothTask=cat(1,TableSes_DMS{:,event});
  166. CatDLS{event}=cat(2,Task_ID_DLS,SessionsDLS_BothTask,TableActDLS_BothTask);
  167. CatDMS{event}=cat(2,Task_ID_DMS,SessionsDMS_BothTask,TableActDMS_BothTask);
  168. nbwin=[1:2];
  169. %DLS first
  170. tableSTAT=array2table(CatDLS{event},'VariableNames',{'task','session','win1','win2'});
  171. tableSTAT.session=categorical(tableSTAT.session);
  172. tableSTAT.task=categorical(tableSTAT.task);
  173. rm_reg = fitrm(tableSTAT,'win1-win2~task*session','WithinDesign',nbwin');
  174. stat(event).condition={'rmANOVA DLS'};
  175. stat(event).anovatbl_event = ranova(rm_reg);
  176. stat(event).Btw_ranovatbl_event = anova(rm_reg);
  177. writetable(stat(event).anovatbl_event,filename,'Sheet',event,'Range','A1')
  178. writetable(stat(event).Btw_ranovatbl_event,filename,'Sheet',event,'Range','J1')
  179. %then test DMS
  180. tableSTAT=array2table(CatDMS{event},'VariableNames',{'task','session','win1','win2'});
  181. tableSTAT.session=categorical(tableSTAT.session);
  182. tableSTAT.task=categorical(tableSTAT.task);
  183. rm_reg = fitrm(tableSTAT,'win1-win2~task*session','WithinDesign',nbwin');
  184. stat(event+3).condition={'rmANOVA DMS'};
  185. stat(event+3).anovatbl_event = ranova(rm_reg);
  186. stat(event+3).Btw_ranovatbl_event = anova(rm_reg);
  187. writetable(stat(event+3).anovatbl_event,filename,'Sheet',event+3,'Range','A1')
  188. writetable(stat(event+3).Btw_ranovatbl_event,filename,'Sheet',event+3,'Range','J1')
  189. end
  190. %Stat RR
  191. SesID=[];TableRR=[];taskID=[];
  192. for event=1:3
  193. for task=1:2
  194. SesID_sh=[];
  195. for k=1:7
  196. SesID=cat(1,SesID,k*ones(length(ResponseRate{task}{k,event}),1));
  197. TableRR=cat(1,TableRR,ResponseRate{task}{k,event});
  198. SesID_sh=cat(1,SesID_sh,k*ones(length(ResponseRate{task}{k,event}),1));
  199. end
  200. taskID=cat(1,taskID,task*ones(length(SesID_sh),1));
  201. end
  202. TableStat_RR{event}=cat(2,taskID,SesID,TableRR);
  203. stat(6+event).condition={'ANOVAN RR'};
  204. [~,stat(6+event).anovatbl_event,~]=anovan(TableStat_RR{event}(:,3),{TableStat_RR{event}(:,1),TableStat_RR{event}(:,2)},'varnames',{'task','session'},'display','off','model','full');
  205. xlswrite(filename,stat(6+event).anovatbl_event,event,'R1')
  206. end