F_generateFigure3-4.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. %% generate figure 3, 4 and S4
  2. % fourrier analysis - hierarchical clustering - Classification approach on
  3. % extended training dataset
  4. % statistics for the extended training dataset
  5. clc;clear;close all;
  6. load('Rextendedtraining_light.mat');
  7. load('RatID_extendedtraining.mat'); %% column 1 = rat ID, column 2 = habit ID; 0=goal-directed / 1=habit
  8. load('Celltype_extendedTraining.mat');
  9. load('TRN_DT5_extendedtrain.mat')
  10. timeconcat=[1:1:357];
  11. BrainRegion=[10 20];
  12. Erefnames=Erefnames(1:7);
  13. Xaxis1=[-0.25 0.25]; Yaxis=[-4 8];
  14. Xaxis2=[1 357]; Yaxis2=[-2 6];
  15. Ishow=find(Tm>=Xaxis1(1) & Tm<=Xaxis1(2));
  16. % ----------- COLORS ---------
  17. myblue=[0 113/255 188/255];
  18. mypurple=[200/255 0 255/255];
  19. mydarkblue=[0 0/255 255/255];
  20. TickSize=[0.015 0.02];
  21. Clim=[-5 5];
  22. %% preparation of data
  23. %normalization based on max
  24. for k=1:length(Erefnames)
  25. Ev(k).PSTH_norm1=normalize(Ev(k).PSTHraw,Ev(k).PSTHrawBL,1);
  26. end
  27. %concat with normalized data
  28. DSconcat_OT = Ev(7).PSTH_norm1(:,Ishow);
  29. for i=1:6
  30. DSconcat_OT = cat(2,DSconcat_OT,Ev(i).PSTH_norm1(:,Ishow));
  31. end
  32. % concat with Zscore
  33. DSconcat_OTz = Ev(7).PSTHz(:,Ishow);
  34. for i=1:6
  35. DSconcat_OTz = cat(2,DSconcat_OTz,Ev(i).PSTHz(:,Ishow));
  36. end
  37. selection=Coord(:,4)~=0 & Celltype(:,1)==1;% & TRN(:,1)~=0; %exclude NaN values
  38. selectionID=find(Coord(:,4)~=0 & Celltype(:,1)==1);% & TRN(:,1)~=0);
  39. outID=find(Coord(:,4)==0 | Celltype(:,1)~=1);% & TRN(:,1)==0);
  40. DSconcat_OTshort=DSconcat_OTz(selection,:);
  41. %% Clustering on phasic and non phasic neurons
  42. close all;
  43. f_low=1;f_high=4;
  44. %fourier analysis
  45. [X_all,f_all,P_all,Nt_all]=myfft(DSconcat_OTshort',0.01,1);
  46. total_power=sum(P_all);
  47. P_all_norm = bsxfun(@rdivide,P_all, total_power);
  48. a=find(f_all>f_low);b=find(f_all<f_high);
  49. lower_window_sum_all=sum(P_all_norm(1:a(1),:));
  50. upper_window_sum_all=sum(P_all_norm(a(1):b(end),:));
  51. ratio_all=lower_window_sum_all./upper_window_sum_all;
  52. %hierarchical clustering
  53. X_ratio=[[lower_window_sum_all]' [upper_window_sum_all]']; %#ok<NBRAK>
  54. eva_Ch_ratio = evalclusters(X_ratio,'linkage','CalinskiHarabasz','KList',[1:10]);
  55. c_ratio=eva_Ch_ratio.OptimalY; % if wat to use optimal number of cluster
  56. Z = linkage(X_ratio,'ward'); % X is the data set, neuron in rows, features in columns
  57. c = cluster(Z,'Maxclust',3); % determine the number cluster here, if you don't use optimalK
  58. D = pdist(X_ratio);
  59. leafOrder = optimalleaforder(Z,D);
  60. cutoff = median([Z(end-2,3) Z(end-1,3)]);
  61. phasicID(selectionID,1)=c_ratio;
  62. phasicID(outID,1)=NaN;
  63. %% heatmap results clustering
  64. figure(1)
  65. for i=1:2
  66. ind=find(phasicID==i);
  67. t=num2str(i);
  68. Clim=[-5 5];
  69. DSconcat_OT_class=[];SORTimg=[];MaxVal=[];MeanVal=[]; MaxTime=[];TMP=[];NNid=[];
  70. DSconcat_OT_class=DSconcat_OT(ind,:);
  71. for NN=1:size(DSconcat_OT_class,1)
  72. MeanVal(NN,1)=mean(DSconcat_OT_class(NN,52:306),2);
  73. [MaxVal(NN,1),MaxInd]=max(DSconcat_OT_class(NN,:));
  74. MaxTime(NN,1)=timeconcat(timeconcat(1)+MaxInd-1);
  75. if i==2
  76. MaxTimePhasic=MaxTime;
  77. end
  78. end
  79. if i==1
  80. TMP=MeanVal;
  81. subplot(10,4,[17 18 21 22 25 26 29 30 33 34 37 38])
  82. elseif i==2
  83. TMP=MeanVal;
  84. subplot(10,4,[19 20 23 24 27 28 31 32 35 36 39 40])
  85. end
  86. TMP(isnan(TMP))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  87. [~,SORTimg]=sort(TMP);
  88. %NNid=cell2mat(Ninfo(phasic_neuron_selection,3));NNid2=NNid(SORTimg);
  89. DSconcat_OT_class=DSconcat_OT_class(SORTimg,:);
  90. imagesc(timeconcat,[1 size(DSconcat_OT_class,1)],DSconcat_OT_class); %colorbar;axis tight;
  91. colormap(jet);
  92. hold on
  93. plot([51 51],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  94. plot([102 102],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  95. plot([153 153],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  96. plot([204 204],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  97. plot([255 255],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  98. plot([306 306],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  99. set(gca,'XTick',[0:25.5:357]);
  100. hold off
  101. end
  102. %% heatmap phasic versus non phasic neurons
  103. phasicID(phasicID(:,1)==3,1)=1;
  104. subplot(10,4,[9 13])
  105. dendrogram(Z,'ColorThreshold',cutoff, 'Reorder',leafOrder);
  106. for j=1:2 % loop thru structure
  107. for k=1:max(phasicID(:,1)) % loop thru class
  108. DSselection(:,1)=Coord(:,4)==BrainRegion(j) & phasicID(:,1)==k;
  109. DSselectionNumber(j,k)=sum(DSselection);
  110. DSselectionProp(j,k)=100*sum(DSselection)/sum(Coord(:,4)==BrainRegion(j) & Celltype(:,1)==1);
  111. end
  112. end
  113. subplot(10,4,[10 14])
  114. b2=bar(DSselectionProp,1,'stacked');
  115. %% Figure scatter plot Hierarchical clustering based on fourier
  116. for i=1:length(c_ratio)
  117. if c_ratio(i,1)==2
  118. color(i,:)=[255/255 0/255 0/255]; %phasic red
  119. elseif c_ratio(i,1)==1
  120. color(i,:)=[0 255/255 0/255]; %sustained green
  121. end
  122. end
  123. subplot(10,4,[1 2 5 6])
  124. scatter(lower_window_sum_all,upper_window_sum_all,[],color)
  125. hold on
  126. title(strcat('hierarchical clustering'))
  127. xlabel('lower_window_sum_all') % x-axis label
  128. ylabel('upper_window_sum_all,ordinates')% y-axis label
  129. hold off
  130. c_ratio(c_ratio(:,1)==3,1)=1;
  131. Xlimit=find(f_all<30);
  132. subplot(10,4,[3 4 7 8 11 12 15 16])
  133. P_all_norm=P_all_norm';
  134. selectionPhasic=c_ratio(:,1)==2;
  135. selectionNonPhasic=c_ratio(:,1)==1;
  136. Phasicmean=nanmean(P_all_norm(selectionPhasic,Xlimit'),1);
  137. Phasicsem=nanste(P_all_norm(selectionPhasic,Xlimit'),1);
  138. Phasicup=Phasicmean+Phasicsem;
  139. Phasicdown=Phasicmean-Phasicsem;
  140. nonphasicmean=nanmean(P_all_norm(selectionNonPhasic,Xlimit'),1);
  141. nonphasicsem=nanste(P_all_norm(selectionNonPhasic,Xlimit'),1);
  142. nonphasicup=nonphasicmean+nonphasicsem;
  143. nonphasicdown=nonphasicmean-nonphasicsem;
  144. allmean=nanmean(P_all_norm(:,Xlimit'),1);
  145. allsem=nanste(P_all_norm(:,Xlimit'),1);
  146. allup=allmean+allsem;
  147. alldown=allmean-allsem;
  148. timeX=1:1:length(Xlimit);
  149. hold on
  150. patch([timeX,timeX(end:-1:1)],[Phasicup,Phasicdown(end:-1:1)],myblue,'EdgeColor','none');
  151. alpha(0.5);
  152. g1=plot(timeX,Phasicmean,'Color',myblue,'linewidth',1.5);
  153. patch([timeX,timeX(end:-1:1)],[nonphasicup,nonphasicdown(end:-1:1)],'r','EdgeColor','none');alpha(0.5);
  154. g2=plot(timeX,nonphasicmean,'r','linewidth',1.5);
  155. plot([0 30], [0 0],'LineStyle',':','Color','k');
  156. set(gca,'XTick',[0:15:60]);
  157. hold off
  158. figure(4)
  159. subplot(10,4,[3 4 7 8 11 12 15 16])
  160. hold on
  161. patch([timeX,timeX(end:-1:1)],[allup,alldown(end:-1:1)],myblue,'EdgeColor','none');
  162. alpha(0.5);
  163. g1=plot(timeX,allmean,'Color',myblue,'linewidth',1.5);
  164. plot([0 30], [0 0],'LineStyle',':','Color','k');
  165. set(gca,'XTick',[0:15:225]);
  166. hold off
  167. %% Plot heatmap phasic neurons, separation DMS/DLS,
  168. PhasicNeurons_ID(:,1)=find(phasicID==2);
  169. PhasicNeurons_ID(:,2)=Coord(PhasicNeurons_ID(:,1),4);
  170. PhasicNeurons_ID(:,3)=RatID(PhasicNeurons_ID(:,1),1);
  171. DSconcat_OT_phasic=DSconcat_OTz(PhasicNeurons_ID(:,1),:);
  172. DLSselection=PhasicNeurons_ID(:,2)~=0 & PhasicNeurons_ID(:,2)==10;
  173. DMSselection=PhasicNeurons_ID(:,2)~=0 & PhasicNeurons_ID(:,2)==20;
  174. % sorting DLS
  175. DSconcat_OT_classDLS=DSconcat_OT_phasic(DLSselection,:);
  176. for NN=1:size(DSconcat_OT_classDLS,1)
  177. [MaxValDLS(NN,1),MaxInd]=max(DSconcat_OT_classDLS(NN,:));
  178. MaxTimeDLS(NN,1)=timeconcat(timeconcat(1)+MaxInd-1);
  179. end
  180. TMPdls=MaxTimeDLS;
  181. TMPdls(isnan(TMPdls))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  182. [~,SORTimgDLS]=sort(TMPdls);
  183. DSconcat_OT_classDLS=DSconcat_OT_classDLS(SORTimgDLS,:);
  184. % sorting DMS
  185. DSconcat_OT_classDMS=DSconcat_OT_phasic(DMSselection,:);
  186. for NN=1:size(DSconcat_OT_classDMS,1)
  187. [MaxValDMS(NN,1),MaxInd]=max(DSconcat_OT_classDMS(NN,:));
  188. MaxTimeDMS(NN,1)=timeconcat(timeconcat(1)+MaxInd-1);
  189. end
  190. TMPdms=MaxTimeDMS;
  191. TMPdms(isnan(TMPdms))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  192. [~,SORTimgDMS]=sort(TMPdms);
  193. DSconcat_OT_classDMS=DSconcat_OT_classDMS(SORTimgDMS,:);
  194. % plot heatmap
  195. figure(2)
  196. subplot(9,4,[1 2 5 6 9 10])
  197. imagesc(timeconcat,[1 size(DSconcat_OT_classDLS,1)],DSconcat_OT_classDLS,Clim); %colorbar;axis tight;
  198. colormap(jet);
  199. hold on
  200. plot([51 51],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  201. plot([102 102],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  202. plot([153 153],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  203. plot([204 204],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  204. plot([255 255],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  205. plot([306 306],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  206. set(gca,'XTick',[0:25.5:357]);
  207. hold off
  208. subplot(9,4,[17 18 21 22 25 26])
  209. imagesc(timeconcat,[1 size(DSconcat_OT_classDMS,1)],DSconcat_OT_classDMS,Clim); %colorbar;axis tight;
  210. colormap(jet);
  211. hold on
  212. plot([51 51],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  213. plot([102 102],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  214. plot([153 153],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  215. plot([204 204],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  216. plot([255 255],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  217. plot([306 306],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  218. set(gca,'XTick',[0:25.5:357]);
  219. hold off
  220. %% Hierarchical clustering of phasic neurons based on Seqeunce-related activity
  221. LIwin=[26:51];%post lever insertion window
  222. LPwin=[52:280];%pre-1stLP to pre-lastLP window
  223. PEwin=[307:332];% pre-PE window
  224. binLI=mean(DSconcat_OT(:,LIwin),2);
  225. binLP=mean(DSconcat_OT(:,LPwin),2);
  226. binPE=mean(DSconcat_OT(:,PEwin),2);
  227. binnedDS=cat(2,binLI,binLP,binPE);
  228. sel=Coord(:,4)~=0 & Celltype==1 & phasicID==2;
  229. X=binnedDS(sel,:);
  230. Z = linkage(X,'ward');
  231. c = cluster(Z,'Maxclust',4);
  232. eva = evalclusters(X,'linkage','CalinskiHarabasz','KList',[1:10]); %% You can change the criterion here. There are 3 other options. Gap, 'Silhouette' , 'DaviesBouldin'. You can do help evalclusters to use this code.
  233. D = pdist(X);
  234. leafOrder = optimalleaforder(Z,D);
  235. cutoff = median([Z(end-2,3) Z(end-1,3)]);
  236. subplot(9,4,[29 33])
  237. dendrogram(Z,'ColorThreshold',cutoff, 'Reorder',leafOrder);
  238. ClassPHAS(:,1)=eva.OptimalY;
  239. ClassPHAS(:,2:4)=X;
  240. %% eva has the results of fitting the clusters. eva.OptimalK tells us how many clusters fit this data best based on the criterion used.
  241. %% eva.OptimalY tells us the cluster identities of each point.
  242. %% CH criterion clustering labels
  243. ind=[];
  244. DSconcat_OT_Phasic=DSconcat_OTz(sel,:);
  245. for i=1:max(eva.OptimalY)
  246. DSconcat_OT_classDLS=[]; SORTimgDLS=[];MaxValDLS=[];TMPdls=[];
  247. DSconcat_OT_classDMS=[]; SORTimgDMS=[];MaxValDMS=[];TMPdms=[];
  248. ind=find(eva.OptimalY==i);
  249. PhasicNeurons_ID(:,1)=find(phasicID~=1 & ~isnan(phasicID));
  250. PhasicNeurons_ID(:,2)=Coord(PhasicNeurons_ID(:,1),4);
  251. PhasicNeurons_ID(:,3)=eva.OptimalY;
  252. DLSselectionP=PhasicNeurons_ID(:,2)==10 & PhasicNeurons_ID(:,3)==i;
  253. DMSselectionP=PhasicNeurons_ID(:,2)==20 & PhasicNeurons_ID(:,3)==i;
  254. % sorting DLS
  255. DSconcat_OT_classDLS=DSconcat_OT_Phasic(DLSselectionP,:);
  256. for NN=1:size(DSconcat_OT_classDLS,1)
  257. MaxValDLS(NN,1)=mean(DSconcat_OT_classDLS(NN,52:306),2);
  258. end
  259. TMPdls=MaxValDLS;
  260. TMPdls(isnan(TMPdls))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  261. [~,SORTimgDLS]=sort(TMPdls);
  262. DSconcat_OT_classDLS=DSconcat_OT_classDLS(SORTimgDLS,:);
  263. % sorting DMS
  264. DSconcat_OT_classDMS=DSconcat_OT_Phasic(DMSselectionP,:);
  265. for NN=1:size(DSconcat_OT_classDMS,1)
  266. MaxValDMS(NN,1)=mean(DSconcat_OT_classDMS(NN,52:306),2);
  267. end
  268. TMPdms=MaxValDMS;
  269. TMPdms(isnan(TMPdms))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  270. [~,SORTimgDMS]=sort(TMPdms);
  271. DSconcat_OT_classDMS=DSconcat_OT_classDMS(SORTimgDMS,:);
  272. for j=1:2
  273. if j==1
  274. DSconcat_OT_class_DLSDMS=DSconcat_OT_classDLS;
  275. else
  276. DSconcat_OT_class_DLSDMS=DSconcat_OT_classDMS;
  277. end
  278. subplot(9,4,[3+(i-1)*12+(j-1)*4 4+(i-1)*12+(j-1)*4])
  279. imagesc(timeconcat,[1 size(DSconcat_OT_class_DLSDMS,1)],DSconcat_OT_class_DLSDMS,Clim);
  280. colormap(jet);
  281. hold on
  282. plot([51 51],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  283. plot([102 102],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  284. plot([153 153],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  285. plot([204 204],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  286. plot([255 255],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  287. plot([306 306],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  288. set(gca,'XTick',[0:25.5:357]);
  289. hold off
  290. % plot average PSTH
  291. subplot(9,4,[11+(i-1)*12 12+(i-1)*12])
  292. DLSpsth=nanmean(DSconcat_OT_classDLS(:,timeconcat),1);
  293. DLSsem=nanste(DSconcat_OT_classDLS(:,timeconcat),1);
  294. DLSup=DLSpsth+DLSsem;
  295. DLSdown=DLSpsth-DLSsem;
  296. DMSpsth=nanmean(DSconcat_OT_classDMS(:,timeconcat),1);
  297. DMSsem=nanste(DSconcat_OT_classDMS(:,timeconcat),1);
  298. DMSup=DMSpsth+DMSsem;
  299. DMSdown=DMSpsth-DMSsem;
  300. hold on
  301. patch([timeconcat,timeconcat(end:-1:1)],[DLSup,DLSdown(end:-1:1)],myblue,'EdgeColor','none');alpha(0.5);
  302. g1=plot(timeconcat,DLSpsth,'Color',myblue,'linewidth',1.5);
  303. patch([timeconcat,timeconcat(end:-1:1)],[DMSup,DMSdown(end:-1:1)],'r','EdgeColor','none');alpha(0.5);
  304. g2=plot(timeconcat,DMSpsth,'r','linewidth',1.5);
  305. plot([0 357], [0 0],'LineStyle',':','Color','k');
  306. plot([51 51],Yaxis,'LineStyle',':','Color','k')
  307. plot([102 102],Yaxis,'LineStyle',':','Color','k')
  308. plot([153 153],Yaxis,'LineStyle',':','Color','k')
  309. plot([204 204],Yaxis,'LineStyle',':','Color','k')
  310. plot([255 255],Yaxis,'LineStyle',':','Color','k')
  311. plot([306 306],Yaxis,'LineStyle',':','Color','k')
  312. axis([Xaxis2,Yaxis]);
  313. set(gca,'XTick',[0:25.5:357]);
  314. hold off
  315. end
  316. end
  317. %% proportion of neurons in each classes / plot stack bars / 2*3 classes with separation phasic / non phasic neurons
  318. DSselection=[];
  319. for j=1:2 % loop thru structure
  320. for k=1:6 % loop thru class
  321. DSselection(:,1)=PhasicNeurons_ID(:,2)==BrainRegion(j) & PhasicNeurons_ID(:,3)==k;
  322. DSselectionNumber(j,k)=sum(DSselection);
  323. DSselectionProp(j,k)=100*sum(DSselection)/sum(PhasicNeurons_ID(:,2)==BrainRegion(j));
  324. end
  325. end
  326. subplot(9,4,[30 34])
  327. b1=bar(DSselectionProp,1,'stacked');
  328. %% Hierarchical clustering of Nonphasic neurons based on Seqeunce-related activity
  329. figure
  330. sel=Coord(:,4)~=0 & Celltype==1 & phasicID==1;
  331. X=binnedDS(sel,:);
  332. Z = linkage(X,'ward');
  333. c = cluster(Z,'Maxclust',4);
  334. eva = evalclusters(X,'linkage','CalinskiHarabasz','KList',[1:10]); %% You can change the criterion here. There are 3 other options. Gap, 'Silhouette' , 'DaviesBouldin'. You can do help evalclusters to use this code.
  335. D = pdist(X);
  336. leafOrder = optimalleaforder(Z,D);
  337. cutoff = median([Z(end-2,3) Z(end-1,3)]);
  338. subplot(6,4,[17 21])
  339. dendrogram(Z,'ColorThreshold',cutoff, 'Reorder',leafOrder);
  340. ClassNONPHASIC(:,1)=eva.OptimalY;
  341. ClassNONPHASIC(:,2:4)=X;
  342. save('ClassHC.mat','ClassPHAS','ClassNONPHASIC')
  343. %% eva has the results of fitting the clusters. eva.OptimalK tells us how many clusters fit this data best based on the criterion used.
  344. %% eva.OptimalY tells us the cluster identities of each point.
  345. %% CH criterion clustering labels
  346. ind=[];
  347. DSconcat_OT_NonPhasic=DSconcat_OTz(sel,:);
  348. DLSsel=Coord(:,4)==10 & Celltype==1 & phasicID==1;
  349. DMSsel=Coord(:,4)==20 & Celltype==1 & phasicID==1;
  350. % sorting all neurons
  351. DSconcat_OT_NPclassDLS=DSconcat_OTz(DLSsel,:);
  352. for NN=1:size(DSconcat_OT_NPclassDLS,1)
  353. MeanValdls(NN,1)=mean(DSconcat_OT_NPclassDLS(NN,52:306),2);
  354. end
  355. TMPdls=MeanValdls;
  356. TMPdls(isnan(TMPdls))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  357. [~,SORTimgDLS]=sort(TMPdls);
  358. DSconcat_OT_NPclassDLS=DSconcat_OT_NPclassDLS(SORTimgDLS,:);
  359. DSconcat_OT_NPclassDMS=DSconcat_OTz(DMSsel,:);
  360. for NN=1:size(DSconcat_OT_NPclassDMS,1)
  361. MeanValdms(NN,1)=mean(DSconcat_OT_NPclassDMS(NN,52:306),2);
  362. end
  363. TMPdms=MeanValdms;
  364. TMPdms(isnan(TMPdms))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  365. [~,SORTimgDMS]=sort(TMPdms);
  366. DSconcat_OT_NPclassDMS=DSconcat_OT_NPclassDMS(SORTimgDMS,:);
  367. % plot heatmap
  368. subplot(6,4,[1 2 5 6])
  369. imagesc(timeconcat,[1 size(DSconcat_OT_NPclassDLS,1)],DSconcat_OT_NPclassDLS,Clim); %colorbar;axis tight;
  370. colormap(jet);
  371. hold on
  372. plot([51 51],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  373. plot([102 102],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  374. plot([153 153],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  375. plot([204 204],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  376. plot([255 255],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  377. plot([306 306],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  378. set(gca,'XTick',[0:25.5:357]);
  379. hold off
  380. subplot(6,4,[9 10 13 14])
  381. imagesc(timeconcat,[1 size(DSconcat_OT_NPclassDMS,1)],DSconcat_OT_NPclassDMS,Clim); %colorbar;axis tight;
  382. colormap(jet);
  383. hold on
  384. plot([51 51],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  385. plot([102 102],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  386. plot([153 153],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  387. plot([204 204],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  388. plot([255 255],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  389. plot([306 306],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  390. set(gca,'XTick',[0:25.5:357]);
  391. hold off
  392. for i=1:max(eva.OptimalY)
  393. DSconcat_OT_classDLS=[]; SORTimgDLS=[];MaxValDLS=[];TMPdls=[];
  394. DSconcat_OT_classDMS=[]; SORTimgDMS=[];MaxValDMS=[];TMPdms=[];
  395. ind=find(eva.OptimalY==i);
  396. NPhasicNeurons_ID(:,1)=find(phasicID==1 & ~isnan(phasicID));
  397. NPhasicNeurons_ID(:,2)=Coord(NPhasicNeurons_ID(:,1),4);
  398. NPhasicNeurons_ID(:,3)=eva.OptimalY;
  399. DLSselectionNP=NPhasicNeurons_ID(:,2)==10 & NPhasicNeurons_ID(:,3)==i;
  400. DMSselectionNP=NPhasicNeurons_ID(:,2)==20 & NPhasicNeurons_ID(:,3)==i;
  401. % sorting DLS
  402. DSconcat_OT_classDLS=DSconcat_OT_NonPhasic(DLSselectionNP,:);
  403. for NN=1:size(DSconcat_OT_classDLS,1)
  404. MaxValDLS(NN,1)=mean(DSconcat_OT_classDLS(NN,52:306),2);
  405. end
  406. TMPdls=MaxValDLS;
  407. TMPdls(isnan(TMPdls))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  408. [~,SORTimgDLS]=sort(TMPdls);
  409. DSconcat_OT_classDLS=DSconcat_OT_classDLS(SORTimgDLS,:);
  410. % sorting DMS
  411. DSconcat_OT_classDMS=DSconcat_OT_NonPhasic(DMSselectionNP,:);
  412. for NN=1:size(DSconcat_OT_classDMS,1)
  413. MaxValDMS(NN,1)=mean(DSconcat_OT_classDMS(NN,52:306),2);
  414. end
  415. TMPdms=MaxValDMS;
  416. TMPdms(isnan(TMPdms))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  417. [~,SORTimgDMS]=sort(TMPdms);
  418. DSconcat_OT_classDMS=DSconcat_OT_classDMS(SORTimgDMS,:);
  419. for j=1:2
  420. if j==1
  421. DSconcat_OT_class_DLSDMS= DSconcat_OT_classDLS;
  422. else
  423. DSconcat_OT_class_DLSDMS= DSconcat_OT_classDMS;
  424. end
  425. subplot(6,4,[3+(i-1)*12+(j-1)*4 4+(i-1)*12+(j-1)*4])
  426. imagesc(timeconcat,[1 size(DSconcat_OT_class_DLSDMS,1)],DSconcat_OT_class_DLSDMS,Clim);
  427. colormap(jet);
  428. hold on
  429. plot([51 51],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  430. plot([102 102],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  431. plot([153 153],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  432. plot([204 204],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  433. plot([255 255],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  434. plot([306 306],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  435. set(gca,'XTick',[0:25.5:357]);
  436. hold off
  437. % plot average PSTH
  438. subplot(6,4,[11+(i-1)*12 12+(i-1)*12])
  439. DLSpsth=nanmean(DSconcat_OT_classDLS(:,timeconcat),1);
  440. DLSsem=nanste(DSconcat_OT_classDLS(:,timeconcat),1);
  441. DLSup=DLSpsth+DLSsem;
  442. DLSdown=DLSpsth-DLSsem;
  443. DMSpsth=nanmean(DSconcat_OT_classDMS(:,timeconcat),1);
  444. DMSsem=nanste(DSconcat_OT_classDMS(:,timeconcat),1);
  445. DMSup=DMSpsth+DMSsem;
  446. DMSdown=DMSpsth-DMSsem;
  447. hold on
  448. patch([timeconcat,timeconcat(end:-1:1)],[DLSup,DLSdown(end:-1:1)],myblue,'EdgeColor','none');alpha(0.5);
  449. g1=plot(timeconcat,DLSpsth,'Color',myblue,'linewidth',1.5);
  450. patch([timeconcat,timeconcat(end:-1:1)],[DMSup,DMSdown(end:-1:1)],'r','EdgeColor','none');alpha(0.5);
  451. g2=plot(timeconcat,DMSpsth,'r','linewidth',1.5);
  452. plot([0 357], [0 0],'LineStyle',':','Color','k');
  453. plot([51 51],Yaxis,'LineStyle',':','Color','k')
  454. plot([102 102],Yaxis,'LineStyle',':','Color','k')
  455. plot([153 153],Yaxis,'LineStyle',':','Color','k')
  456. plot([204 204],Yaxis,'LineStyle',':','Color','k')
  457. plot([255 255],Yaxis,'LineStyle',':','Color','k')
  458. plot([306 306],Yaxis,'LineStyle',':','Color','k')
  459. axis([Xaxis2,Yaxis]);
  460. set(gca,'XTick',[0:25.5:357]);
  461. hold off
  462. end
  463. end
  464. %% proportion of neurons in each classes / plot stack bars / 2*3 classes with separation phasic / non phasic neurons
  465. DSselection=[];
  466. for j=1:2 % loop thru structure
  467. for k=1:6 % loop thru class
  468. DSselection(:,1)=NPhasicNeurons_ID(:,2)==BrainRegion(j) & NPhasicNeurons_ID(:,3)==k;
  469. DSselectionNumberNP(j,k)=sum(DSselection);
  470. DSselectionPropNP(j,k)=100*sum(DSselection)/sum(NPhasicNeurons_ID(:,2)==BrainRegion(j));
  471. end
  472. end
  473. subplot(6,4,[18 22])
  474. b1=bar(DSselectionPropNP,1,'stacked');
  475. %% make table for statistics
  476. load('RatID_OT.mat');
  477. Class_OT(1:length(Coord(:,4)),1)=phasicID;
  478. Class_OT(PhasicNeurons_ID(:,1),2)=PhasicNeurons_ID(:,3);
  479. Class_OT(NPhasicNeurons_ID(:,1),2)=NPhasicNeurons_ID(:,3)+3;
  480. DSmeanz_OT=[];
  481. DSmeanz_OT = cat(2,DSmeanz_OT,Ev(7).Meanz(:,1)); % use meanZ instead of average of bin
  482. for k=1:5
  483. DSmeanz_OT = cat(2,DSmeanz_OT,Ev(k).MeanzPRE(:,1));
  484. DSmeanz_OT = cat(2,DSmeanz_OT,Ev(k).Meanz(:,1));
  485. end
  486. DSmeanz_OT = cat(2,DSmeanz_OT,Ev(6).MeanzPRE(:,1));
  487. table_OT=cat(2,RatID,Coord(:,4),Celltype(:,1),Class_OT,DSmeanz_OT, TRN);
  488. save('table_OT.mat','table_OT')
  489. nbevent=[1:12];
  490. selectionSTAT=table_OT(:,3)~=0 & table_OT(:,4)==1 & TRN(:,1)~=0;
  491. tableSTAT=array2table(table_OT(selectionSTAT,1:18),'VariableNames',{'rat','habit','region','celltype','phasicID','class','win1','win2','win3','win4','win5','win6','win7','win8','win9','win10','win11','win12'});
  492. rm = fitrm(tableSTAT,'win1-win12~region','WithinDesign',nbevent');
  493. ranovatbl_event = ranova(rm);
  494. Btw_ranovatbl_event = anova(rm);
  495. %control for effect of habit
  496. rm = fitrm(tableSTAT,'win1-win12~region*habit','WithinDesign',nbevent');
  497. ranovatbl_event_habit = ranova(rm);
  498. Btw_ranovatbl_event_habit = anova(rm);
  499. for i=1:max(table_OT(:,6))
  500. selectionSTAT=table_OT(:,3)~=0 & table_OT(:,4)==1 & table_OT(:,6)==i;
  501. tableSTAT=array2table(table_OT(selectionSTAT,1:18),'VariableNames',{'rat','habit','region','celltype','phasicID','class','win1','win2','win3','win4','win5','win6','win7','win8','win9','win10','win11','win12'});
  502. rm = fitrm(tableSTAT,'win1-win12~region','WithinDesign',nbevent');
  503. stat(i).ranovatbl_event = ranova(rm);
  504. stat(i).Btw_ranovatbl_event = anova(rm);
  505. rm = fitrm(tableSTAT,'win1-win12~region*habit','WithinDesign',nbevent');
  506. stat(i).ranovatbl_event_Habit = ranova(rm);
  507. stat(i).Btw_ranovatbl_event_Habit = anova(rm);
  508. end