F_generateFigure3and5.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. %% generate figure 3, 5 and Figure 3 - supplement figure 1
  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. Eventnames={'';'LI';'';'LP1';'';'LP2';'';'LP3';'';'LP4';'';'LP5';'';'PE'};
  17. % ----------- COLORS ---------
  18. myblue=[0 113/255 188/255];
  19. mypurple=[200/255 0 255/255];
  20. mydarkblue=[0 0/255 255/255];
  21. TickSize=[0.015 0.02];
  22. Clim=[-5 5];
  23. %% preparation of data
  24. %normalization based on max
  25. for k=1:length(Erefnames)
  26. Ev(k).PSTH_norm1=normalize(Ev(k).PSTHraw,Ev(k).PSTHrawBL,1);
  27. end
  28. %concat with normalized data
  29. DSconcat_OT = Ev(7).PSTH_norm1(:,Ishow);
  30. for i=1:6
  31. DSconcat_OT = cat(2,DSconcat_OT,Ev(i).PSTH_norm1(:,Ishow));
  32. end
  33. % concat with Zscore
  34. DSconcat_OTz = Ev(7).PSTHz(:,Ishow);
  35. for i=1:6
  36. DSconcat_OTz = cat(2,DSconcat_OTz,Ev(i).PSTHz(:,Ishow));
  37. end
  38. selection=Coord(:,4)~=0 & Celltype(:,1)==1;% & TRN(:,1)~=0; %exclude NaN values
  39. selectionID=find(Coord(:,4)~=0 & Celltype(:,1)==1);% & TRN(:,1)~=0);
  40. outID=find(Coord(:,4)==0 | Celltype(:,1)~=1);% & TRN(:,1)==0);
  41. DSconcat_OTshort=DSconcat_OTz(selection,:);
  42. %% Clustering on phasic and non phasic neurons
  43. close all;
  44. f_low=1;f_high=4;
  45. %fourier analysis
  46. [X_all,f_all,P_all,Nt_all]=myfft(DSconcat_OTshort',0.01,1);
  47. total_power=sum(P_all);
  48. P_all_norm = bsxfun(@rdivide,P_all, total_power);
  49. a=find(f_all>f_low);b=find(f_all<f_high);
  50. lower_window_sum_all=sum(P_all_norm(1:a(1),:));
  51. upper_window_sum_all=sum(P_all_norm(a(1):b(end),:));
  52. ratio_all=lower_window_sum_all./upper_window_sum_all;
  53. %hierarchical clustering
  54. X_ratio=[[lower_window_sum_all]' [upper_window_sum_all]']; %#ok<NBRAK>
  55. eva_Ch_ratio = evalclusters(X_ratio,'linkage','CalinskiHarabasz','KList',[1:10]);
  56. c_ratio=eva_Ch_ratio.OptimalY; % if wat to use optimal number of cluster
  57. Z = linkage(X_ratio,'ward'); % X is the data set, neuron in rows, features in columns
  58. c = cluster(Z,'Maxclust',3); % determine the number cluster here, if you don't use optimalK
  59. D = pdist(X_ratio);
  60. leafOrder = optimalleaforder(Z,D);
  61. cutoff = median([Z(end-2,3) Z(end-1,3)]);
  62. phasicID(selectionID,1)=c_ratio;
  63. phasicID(outID,1)=NaN;
  64. %% heatmap results clustering
  65. figure(1)
  66. for i=1:2
  67. ind=find(phasicID==i);
  68. t=num2str(i);
  69. Clim=[-5 5];
  70. DSconcat_OT_class=[];SORTimg=[];MaxVal=[];MeanVal=[]; MaxTime=[];TMP=[];NNid=[];
  71. DSconcat_OT_class=DSconcat_OT(ind,:);
  72. for NN=1:size(DSconcat_OT_class,1)
  73. MeanVal(NN,1)=mean(DSconcat_OT_class(NN,52:306),2);
  74. [MaxVal(NN,1),MaxInd]=max(DSconcat_OT_class(NN,:));
  75. MaxTime(NN,1)=timeconcat(timeconcat(1)+MaxInd-1);
  76. if i==2
  77. MaxTimePhasic=MaxTime;
  78. end
  79. end
  80. if i==1
  81. TMP=MeanVal;
  82. subplot(5,3,[4 7 10 13])
  83. elseif i==2
  84. TMP=MeanVal;
  85. subplot(5,3,[5 8 11 14])
  86. end
  87. TMP(isnan(TMP))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  88. [~,SORTimg]=sort(TMP);
  89. %NNid=cell2mat(Ninfo(phasic_neuron_selection,3));NNid2=NNid(SORTimg);
  90. DSconcat_OT_class=DSconcat_OT_class(SORTimg,:);
  91. imagesc(timeconcat,[1 size(DSconcat_OT_class,1)],DSconcat_OT_class); %colorbar;axis tight;
  92. colormap(jet);
  93. hold on
  94. plot([51 51],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  95. plot([102 102],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  96. plot([153 153],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  97. plot([204 204],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  98. plot([255 255],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  99. plot([306 306],[size(DSconcat_OT_class,1)+0.5 0.5],'k')
  100. set(gca,'XTick',[0:25.5:357]);
  101. set(gca,'xticklabel',Eventnames)
  102. hold off
  103. end
  104. %% heatmap phasic versus non phasic neurons
  105. phasicID(phasicID(:,1)==3,1)=1;
  106. subplot(5,3,2)
  107. dendrogram(Z,'ColorThreshold',2, 'Reorder',leafOrder);
  108. for j=1:2 % loop thru structure
  109. for k=1:max(phasicID(:,1)) % loop thru class
  110. DSselection(:,1)=Coord(:,4)==BrainRegion(j) & phasicID(:,1)==k;
  111. DSselectionNumber(j,k)=sum(DSselection);
  112. DSselectionProp(j,k)=100*sum(DSselection)/sum(Coord(:,4)==BrainRegion(j) & Celltype(:,1)==1);
  113. end
  114. end
  115. subplot(5,3,3)
  116. X = categorical({'DLS','DMS'});
  117. b2=bar(X,DSselectionProp,'stacked');
  118. ylabel('% neurons');
  119. legend('phasic', 'nonphasic')
  120. %% Figure scatter plot Hierarchical clustering based on fourier
  121. for i=1:length(c_ratio)
  122. if c_ratio(i,1)==2
  123. color(i,:)=[255/255 0/255 0/255]; %phasic red
  124. elseif c_ratio(i,1)==1
  125. color(i,:)=[0 255/255 0/255]; %sustained green
  126. end
  127. end
  128. subplot(5,3,1)
  129. scatter(lower_window_sum_all,upper_window_sum_all,[],color)
  130. hold on
  131. title(strcat('hierarchical clustering'))
  132. xlabel('Power low freq') % x-axis label
  133. ylabel('Power int freq')% y-axis label
  134. legend('phasic', 'nonphasic')
  135. hold off
  136. %% Plot heatmap phasic neurons, separation DMS/DLS,
  137. PhasicNeurons_ID(:,1)=find(phasicID==2);
  138. PhasicNeurons_ID(:,2)=Coord(PhasicNeurons_ID(:,1),4);
  139. PhasicNeurons_ID(:,3)=RatID(PhasicNeurons_ID(:,1),1);
  140. DSconcat_OT_phasic=DSconcat_OTz(PhasicNeurons_ID(:,1),:);
  141. DLSselection=PhasicNeurons_ID(:,2)~=0 & PhasicNeurons_ID(:,2)==10;
  142. DMSselection=PhasicNeurons_ID(:,2)~=0 & PhasicNeurons_ID(:,2)==20;
  143. % sorting DLS
  144. DSconcat_OT_classDLS=DSconcat_OT_phasic(DLSselection,:);
  145. for NN=1:size(DSconcat_OT_classDLS,1)
  146. [MaxValDLS(NN,1),MaxInd]=max(DSconcat_OT_classDLS(NN,:));
  147. MaxTimeDLS(NN,1)=timeconcat(timeconcat(1)+MaxInd-1);
  148. end
  149. TMPdls=MaxTimeDLS;
  150. TMPdls(isnan(TMPdls))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  151. [~,SORTimgDLS]=sort(TMPdls);
  152. DSconcat_OT_classDLS=DSconcat_OT_classDLS(SORTimgDLS,:);
  153. % sorting DMS
  154. DSconcat_OT_classDMS=DSconcat_OT_phasic(DMSselection,:);
  155. for NN=1:size(DSconcat_OT_classDMS,1)
  156. [MaxValDMS(NN,1),MaxInd]=max(DSconcat_OT_classDMS(NN,:));
  157. MaxTimeDMS(NN,1)=timeconcat(timeconcat(1)+MaxInd-1);
  158. end
  159. TMPdms=MaxTimeDMS;
  160. TMPdms(isnan(TMPdms))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  161. [~,SORTimgDMS]=sort(TMPdms);
  162. DSconcat_OT_classDMS=DSconcat_OT_classDMS(SORTimgDMS,:);
  163. % plot heatmap
  164. figure(2)
  165. subplot(9,4,[1 2 5 6 9 10])
  166. imagesc(timeconcat,[1 size(DSconcat_OT_classDLS,1)],DSconcat_OT_classDLS,Clim); %colorbar;axis tight;
  167. colormap(jet);
  168. hold on
  169. plot([51 51],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  170. plot([102 102],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  171. plot([153 153],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  172. plot([204 204],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  173. plot([255 255],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  174. plot([306 306],[size(DSconcat_OT_classDLS,1)+0.5 0.5],'k')
  175. set(gca,'XTick',[0:25.5:357]);
  176. set(gca,'xticklabel',Eventnames)
  177. ylabel('DLS neurons')
  178. hold off
  179. subplot(9,4,[17 18 21 22 25 26])
  180. imagesc(timeconcat,[1 size(DSconcat_OT_classDMS,1)],DSconcat_OT_classDMS,Clim); %colorbar;axis tight;
  181. colormap(jet);
  182. hold on
  183. plot([51 51],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  184. plot([102 102],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  185. plot([153 153],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  186. plot([204 204],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  187. plot([255 255],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  188. plot([306 306],[size(DSconcat_OT_classDMS,1)+0.5 0.5],'k')
  189. set(gca,'XTick',[0:25.5:357]);
  190. set(gca,'xticklabel',Eventnames)
  191. ylabel('DMS neurons')
  192. hold off
  193. %% Hierarchical clustering of phasic neurons based on Sequence-related activity
  194. LIwin=[26:51];%post lever insertion window
  195. LPwin=[52:280];%pre-1stLP to pre-lastLP window
  196. PEwin=[307:332];% pre-PE window
  197. binLI=mean(DSconcat_OT(:,LIwin),2);
  198. binLP=mean(DSconcat_OT(:,LPwin),2);
  199. binPE=mean(DSconcat_OT(:,PEwin),2);
  200. binnedDS=cat(2,binLI,binLP,binPE);
  201. sel=Coord(:,4)~=0 & Celltype==1 & phasicID==2;
  202. X=binnedDS(sel,:);
  203. Z = linkage(X,'ward');
  204. c = cluster(Z,'Maxclust',4);
  205. 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.
  206. D = pdist(X);
  207. leafOrder = optimalleaforder(Z,D);
  208. cutoff = median([Z(end-2,3) Z(end-1,3)]);
  209. subplot(9,4,[29 33])
  210. dendrogram(Z,'ColorThreshold',cutoff, 'Reorder',leafOrder);
  211. ClassPHAS(:,1)=eva.OptimalY;
  212. ClassPHAS(:,2:4)=X;
  213. %% eva has the results of fitting the clusters. eva.OptimalK tells us how many clusters fit this data best based on the criterion used.
  214. %% eva.OptimalY tells us the cluster identities of each point.
  215. %% CH criterion clustering labels
  216. ind=[];
  217. DSconcat_OT_Phasic=DSconcat_OTz(sel,:);
  218. for i=1:max(eva.OptimalY)
  219. DSconcat_OT_classDLS=[]; SORTimgDLS=[];MaxValDLS=[];TMPdls=[];
  220. DSconcat_OT_classDMS=[]; SORTimgDMS=[];MaxValDMS=[];TMPdms=[];
  221. ind=find(eva.OptimalY==i);
  222. PhasicNeurons_ID(:,1)=find(phasicID~=1 & ~isnan(phasicID));
  223. PhasicNeurons_ID(:,2)=Coord(PhasicNeurons_ID(:,1),4);
  224. PhasicNeurons_ID(:,3)=eva.OptimalY;
  225. DLSselectionP=PhasicNeurons_ID(:,2)==10 & PhasicNeurons_ID(:,3)==i;
  226. DMSselectionP=PhasicNeurons_ID(:,2)==20 & PhasicNeurons_ID(:,3)==i;
  227. % sorting DLS
  228. DSconcat_OT_classDLS=DSconcat_OT_Phasic(DLSselectionP,:);
  229. for NN=1:size(DSconcat_OT_classDLS,1)
  230. MaxValDLS(NN,1)=mean(DSconcat_OT_classDLS(NN,52:306),2);
  231. end
  232. TMPdls=MaxValDLS;
  233. TMPdls(isnan(TMPdls))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  234. [~,SORTimgDLS]=sort(TMPdls);
  235. DSconcat_OT_classDLS=DSconcat_OT_classDLS(SORTimgDLS,:);
  236. % sorting DMS
  237. DSconcat_OT_classDMS=DSconcat_OT_Phasic(DMSselectionP,:);
  238. for NN=1:size(DSconcat_OT_classDMS,1)
  239. MaxValDMS(NN,1)=mean(DSconcat_OT_classDMS(NN,52:306),2);
  240. end
  241. TMPdms=MaxValDMS;
  242. TMPdms(isnan(TMPdms))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  243. [~,SORTimgDMS]=sort(TMPdms);
  244. DSconcat_OT_classDMS=DSconcat_OT_classDMS(SORTimgDMS,:);
  245. for j=1:2
  246. if j==1
  247. DSconcat_OT_class_DLSDMS=DSconcat_OT_classDLS;
  248. else
  249. DSconcat_OT_class_DLSDMS=DSconcat_OT_classDMS;
  250. end
  251. subplot(9,4,[3+(i-1)*12+(j-1)*4 4+(i-1)*12+(j-1)*4])
  252. imagesc(timeconcat,[1 size(DSconcat_OT_class_DLSDMS,1)],DSconcat_OT_class_DLSDMS,Clim);
  253. colormap(jet);
  254. hold on
  255. plot([51 51],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  256. plot([102 102],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  257. plot([153 153],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  258. plot([204 204],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  259. plot([255 255],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  260. plot([306 306],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  261. set(gca,'XTick',[0:25.5:357]);
  262. set(gca,'xticklabel',Eventnames)
  263. hold off
  264. % plot average PSTH
  265. subplot(9,4,[11+(i-1)*12 12+(i-1)*12])
  266. DLSpsth=nanmean(DSconcat_OT_classDLS(:,timeconcat),1);
  267. DLSsem=nanste(DSconcat_OT_classDLS(:,timeconcat),1);
  268. DLSup=DLSpsth+DLSsem;
  269. DLSdown=DLSpsth-DLSsem;
  270. DMSpsth=nanmean(DSconcat_OT_classDMS(:,timeconcat),1);
  271. DMSsem=nanste(DSconcat_OT_classDMS(:,timeconcat),1);
  272. DMSup=DMSpsth+DMSsem;
  273. DMSdown=DMSpsth-DMSsem;
  274. hold on
  275. patch([timeconcat,timeconcat(end:-1:1)],[DLSup,DLSdown(end:-1:1)],myblue,'EdgeColor','none');alpha(0.5);
  276. g1=plot(timeconcat,DLSpsth,'Color',myblue,'linewidth',1.5);
  277. patch([timeconcat,timeconcat(end:-1:1)],[DMSup,DMSdown(end:-1:1)],'r','EdgeColor','none');alpha(0.5);
  278. g2=plot(timeconcat,DMSpsth,'r','linewidth',1.5);
  279. plot([0 357], [0 0],'LineStyle',':','Color','k');
  280. plot([51 51],Yaxis,'LineStyle',':','Color','k')
  281. plot([102 102],Yaxis,'LineStyle',':','Color','k')
  282. plot([153 153],Yaxis,'LineStyle',':','Color','k')
  283. plot([204 204],Yaxis,'LineStyle',':','Color','k')
  284. plot([255 255],Yaxis,'LineStyle',':','Color','k')
  285. plot([306 306],Yaxis,'LineStyle',':','Color','k')
  286. axis([Xaxis2,Yaxis]);
  287. set(gca,'XTick',[0:25.5:357]);
  288. set(gca,'xticklabel',Eventnames)
  289. hold off
  290. end
  291. end
  292. %% proportion of neurons in each classes / plot stack bars / 2*3 classes with separation phasic / non phasic neurons
  293. DSselection=[];
  294. for j=1:2 % loop thru structure
  295. for k=1:3 % loop thru class
  296. DSselection(:,1)=PhasicNeurons_ID(:,2)==BrainRegion(j) & PhasicNeurons_ID(:,3)==k;
  297. DSselectionNumber(j,k)=sum(DSselection);
  298. DSselectionProp(j,k)=100*sum(DSselection)/sum(PhasicNeurons_ID(:,2)==BrainRegion(j));
  299. end
  300. end
  301. subplot(9,4,[30 34])
  302. X=categorical({'DLS','DMS'});
  303. b1=bar(X,DSselectionProp,'stacked');
  304. legend('Start','Stop','Middle')
  305. %% Hierarchical clustering of Nonphasic neurons based on Seqeunce-related activity
  306. figure
  307. sel=Coord(:,4)~=0 & Celltype==1 & phasicID==1;
  308. X=binnedDS(sel,:);
  309. Z = linkage(X,'ward');
  310. c = cluster(Z,'Maxclust',4);
  311. 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.
  312. D = pdist(X);
  313. leafOrder = optimalleaforder(Z,D);
  314. cutoff = median([Z(end-2,3) Z(end-1,3)]);
  315. subplot(6,4,[17 21])
  316. dendrogram(Z,'ColorThreshold',5, 'Reorder',leafOrder);
  317. ClassNONPHASIC(:,1)=eva.OptimalY;
  318. ClassNONPHASIC(:,2:4)=X;
  319. save('ClassHC.mat','ClassPHAS','ClassNONPHASIC')
  320. %% eva has the results of fitting the clusters. eva.OptimalK tells us how many clusters fit this data best based on the criterion used.
  321. %% eva.OptimalY tells us the cluster identities of each point.
  322. %% CH criterion clustering labels
  323. ind=[];
  324. DSconcat_OT_NonPhasic=DSconcat_OTz(sel,:);
  325. DLSsel=Coord(:,4)==10 & Celltype==1 & phasicID==1;
  326. DMSsel=Coord(:,4)==20 & Celltype==1 & phasicID==1;
  327. % sorting all neurons
  328. DSconcat_OT_NPclassDLS=DSconcat_OTz(DLSsel,:);
  329. for NN=1:size(DSconcat_OT_NPclassDLS,1)
  330. MeanValdls(NN,1)=mean(DSconcat_OT_NPclassDLS(NN,52:306),2);
  331. end
  332. TMPdls=MeanValdls;
  333. TMPdls(isnan(TMPdls))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  334. [~,SORTimgDLS]=sort(TMPdls);
  335. DSconcat_OT_NPclassDLS=DSconcat_OT_NPclassDLS(SORTimgDLS,:);
  336. DSconcat_OT_NPclassDMS=DSconcat_OTz(DMSsel,:);
  337. for NN=1:size(DSconcat_OT_NPclassDMS,1)
  338. MeanValdms(NN,1)=mean(DSconcat_OT_NPclassDMS(NN,52:306),2);
  339. end
  340. TMPdms=MeanValdms;
  341. TMPdms(isnan(TMPdms))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  342. [~,SORTimgDMS]=sort(TMPdms);
  343. DSconcat_OT_NPclassDMS=DSconcat_OT_NPclassDMS(SORTimgDMS,:);
  344. % plot heatmap
  345. subplot(6,4,[1 2 5 6])
  346. imagesc(timeconcat,[1 size(DSconcat_OT_NPclassDLS,1)],DSconcat_OT_NPclassDLS,Clim); %colorbar;axis tight;
  347. colormap(jet);
  348. hold on
  349. plot([51 51],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  350. plot([102 102],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  351. plot([153 153],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  352. plot([204 204],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  353. plot([255 255],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  354. plot([306 306],[size(DSconcat_OT_NPclassDLS,1)+0.5 0.5],'k')
  355. set(gca,'XTick',[0:25.5:357]);
  356. set(gca,'xticklabel',Eventnames)
  357. hold off
  358. subplot(6,4,[9 10 13 14])
  359. imagesc(timeconcat,[1 size(DSconcat_OT_NPclassDMS,1)],DSconcat_OT_NPclassDMS,Clim); %colorbar;axis tight;
  360. colormap(jet);
  361. hold on
  362. plot([51 51],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  363. plot([102 102],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  364. plot([153 153],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  365. plot([204 204],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  366. plot([255 255],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  367. plot([306 306],[size(DSconcat_OT_NPclassDMS,1)+0.5 0.5],'k')
  368. set(gca,'XTick',[0:25.5:357]);
  369. set(gca,'xticklabel',Eventnames)
  370. hold off
  371. for i=1:max(eva.OptimalY)
  372. DSconcat_OT_classDLS=[]; SORTimgDLS=[];MaxValDLS=[];TMPdls=[];
  373. DSconcat_OT_classDMS=[]; SORTimgDMS=[];MaxValDMS=[];TMPdms=[];
  374. ind=find(eva.OptimalY==i);
  375. NPhasicNeurons_ID(:,1)=find(phasicID==1 & ~isnan(phasicID));
  376. NPhasicNeurons_ID(:,2)=Coord(NPhasicNeurons_ID(:,1),4);
  377. NPhasicNeurons_ID(:,3)=eva.OptimalY;
  378. DLSselectionNP=NPhasicNeurons_ID(:,2)==10 & NPhasicNeurons_ID(:,3)==i;
  379. DMSselectionNP=NPhasicNeurons_ID(:,2)==20 & NPhasicNeurons_ID(:,3)==i;
  380. % sorting DLS
  381. DSconcat_OT_classDLS=DSconcat_OT_NonPhasic(DLSselectionNP,:);
  382. for NN=1:size(DSconcat_OT_classDLS,1)
  383. MaxValDLS(NN,1)=mean(DSconcat_OT_classDLS(NN,52:306),2);
  384. end
  385. TMPdls=MaxValDLS;
  386. TMPdls(isnan(TMPdls))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  387. [~,SORTimgDLS]=sort(TMPdls);
  388. DSconcat_OT_classDLS=DSconcat_OT_classDLS(SORTimgDLS,:);
  389. % sorting DMS
  390. DSconcat_OT_classDMS=DSconcat_OT_NonPhasic(DMSselectionNP,:);
  391. for NN=1:size(DSconcat_OT_classDMS,1)
  392. MaxValDMS(NN,1)=mean(DSconcat_OT_classDMS(NN,52:306),2);
  393. end
  394. TMPdms=MaxValDMS;
  395. TMPdms(isnan(TMPdms))=0; %To place the neurons with no onset/duration/peak at the top of the color-coded map
  396. [~,SORTimgDMS]=sort(TMPdms);
  397. DSconcat_OT_classDMS=DSconcat_OT_classDMS(SORTimgDMS,:);
  398. for j=1:2
  399. if j==1
  400. DSconcat_OT_class_DLSDMS= DSconcat_OT_classDLS;
  401. else
  402. DSconcat_OT_class_DLSDMS= DSconcat_OT_classDMS;
  403. end
  404. subplot(6,4,[3+(i-1)*12+(j-1)*4 4+(i-1)*12+(j-1)*4])
  405. imagesc(timeconcat,[1 size(DSconcat_OT_class_DLSDMS,1)],DSconcat_OT_class_DLSDMS,Clim);
  406. colormap(jet);
  407. hold on
  408. plot([51 51],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  409. plot([102 102],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  410. plot([153 153],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  411. plot([204 204],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  412. plot([255 255],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  413. plot([306 306],[size(DSconcat_OT_class_DLSDMS,1)+0.5 0.5],'k')
  414. set(gca,'XTick',[0:25.5:357]);
  415. set(gca,'xticklabel',Eventnames)
  416. hold off
  417. if i==1
  418. Yaxis=[-3 2];
  419. else
  420. Yaxis=[-1 15];
  421. end
  422. % plot average PSTH
  423. subplot(6,4,[11+(i-1)*12 12+(i-1)*12])
  424. DLSpsth=nanmean(DSconcat_OT_classDLS(:,timeconcat),1);
  425. DLSsem=nanste(DSconcat_OT_classDLS(:,timeconcat),1);
  426. DLSup=DLSpsth+DLSsem;
  427. DLSdown=DLSpsth-DLSsem;
  428. DMSpsth=nanmean(DSconcat_OT_classDMS(:,timeconcat),1);
  429. DMSsem=nanste(DSconcat_OT_classDMS(:,timeconcat),1);
  430. DMSup=DMSpsth+DMSsem;
  431. DMSdown=DMSpsth-DMSsem;
  432. hold on
  433. patch([timeconcat,timeconcat(end:-1:1)],[DLSup,DLSdown(end:-1:1)],myblue,'EdgeColor','none');alpha(0.5);
  434. g1=plot(timeconcat,DLSpsth,'Color',myblue,'linewidth',1.5);
  435. patch([timeconcat,timeconcat(end:-1:1)],[DMSup,DMSdown(end:-1:1)],'r','EdgeColor','none');alpha(0.5);
  436. g2=plot(timeconcat,DMSpsth,'r','linewidth',1.5);
  437. plot([0 357], [0 0],'LineStyle',':','Color','k');
  438. plot([51 51],Yaxis,'LineStyle',':','Color','k')
  439. plot([102 102],Yaxis,'LineStyle',':','Color','k')
  440. plot([153 153],Yaxis,'LineStyle',':','Color','k')
  441. plot([204 204],Yaxis,'LineStyle',':','Color','k')
  442. plot([255 255],Yaxis,'LineStyle',':','Color','k')
  443. plot([306 306],Yaxis,'LineStyle',':','Color','k')
  444. axis([Xaxis2,Yaxis]);
  445. set(gca,'XTick',[0:25.5:357]);
  446. set(gca,'xticklabel',Eventnames)
  447. hold off
  448. end
  449. end
  450. %% proportion of neurons in each classes / plot stack bars / 2*3 classes with separation phasic / non phasic neurons
  451. DSselection=[];
  452. for j=1:2 % loop thru structure
  453. for k=1:6 % loop thru class
  454. DSselection(:,1)=NPhasicNeurons_ID(:,2)==BrainRegion(j) & NPhasicNeurons_ID(:,3)==k;
  455. DSselectionNumberNP(j,k)=sum(DSselection);
  456. DSselectionPropNP(j,k)=100*sum(DSselection)/sum(NPhasicNeurons_ID(:,2)==BrainRegion(j));
  457. end
  458. end
  459. subplot(6,4,[18 22])
  460. X=categorical({'DLS','DMS'});
  461. b1=bar(X,DSselectionPropNP,'stacked');
  462. legend('INH','EXC');
  463. %% make table for statistics
  464. load('RatID_extendedtraining.mat');
  465. Class_OT(1:length(Coord(:,4)),1)=phasicID;
  466. Class_OT(PhasicNeurons_ID(:,1),2)=PhasicNeurons_ID(:,3);
  467. Class_OT(NPhasicNeurons_ID(:,1),2)=NPhasicNeurons_ID(:,3)+3;
  468. DSmeanz_OT=[];
  469. DSmeanz_OT = cat(2,DSmeanz_OT,Ev(7).Meanz(:,1)); % use meanZ instead of average of bin
  470. for k=1:5
  471. DSmeanz_OT = cat(2,DSmeanz_OT,Ev(k).MeanzPRE(:,1));
  472. DSmeanz_OT = cat(2,DSmeanz_OT,Ev(k).Meanz(:,1));
  473. end
  474. DSmeanz_OT = cat(2,DSmeanz_OT,Ev(6).MeanzPRE(:,1));
  475. table_OT=cat(2,RatID,Coord(:,4),Celltype(:,1),Class_OT,DSmeanz_OT, TRN);
  476. save('table_OT.mat','table_OT')
  477. nbevent=[1:12];
  478. selectionSTAT=table_OT(:,3)~=0 & table_OT(:,4)==1 & TRN(:,1)~=0;
  479. 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'});
  480. rm = fitrm(tableSTAT,'win1-win12~region','WithinDesign',nbevent');
  481. ranovatbl_event = ranova(rm);
  482. Btw_ranovatbl_event = anova(rm);
  483. %control for effect of habit
  484. rm = fitrm(tableSTAT,'win1-win12~region*habit','WithinDesign',nbevent');
  485. ranovatbl_event_habit = ranova(rm);
  486. Btw_ranovatbl_event_habit = anova(rm);
  487. for i=1:max(table_OT(:,6))
  488. selectionSTAT=table_OT(:,3)~=0 & table_OT(:,4)==1 & table_OT(:,6)==i;
  489. 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'});
  490. rm = fitrm(tableSTAT,'win1-win12~region','WithinDesign',nbevent');
  491. stat(i).ranovatbl_event = ranova(rm);
  492. stat(i).Btw_ranovatbl_event = anova(rm);
  493. rm = fitrm(tableSTAT,'win1-win12~region*habit','WithinDesign',nbevent');
  494. stat(i).ranovatbl_event_Habit = ranova(rm);
  495. stat(i).Btw_ranovatbl_event_Habit = anova(rm);
  496. end
  497. %% permutation test
  498. allsamples=X_ratio;
  499. for p=1:10000
  500. shuffsamples=c_ratio(randperm(length(c_ratio)));
  501. MeanCluster1(p,:)=mean(allsamples(shuffsamples==1,:),1);
  502. MeanCluster2(p,:)=mean(allsamples(shuffsamples==2,:),1);
  503. DistCluster(p,1) = pdist([MeanCluster1(p,:);MeanCluster2(p,:)],'euclidean');
  504. end
  505. MeanP=mean(allsamples(c_ratio==2,:),1);
  506. MeanNP=mean(allsamples(c_ratio==1,:),1);
  507. RealDist1=pdist([MeanP;MeanNP],'euclidean');
  508. Classpval=(sum(RealDist1>DistCluster)+1)/(length(DistCluster)+1);
  509. figure;
  510. hist(DistCluster);
  511. hold on;
  512. plot([RealDist1 RealDist1],[0 4000]);
  513. xlabel('Between-cluster distance')
  514. ylabel('# iterations')
  515. text(0.225,100,'Phasic vs Non-phasic classification','color','b','rotation',90);
  516. %% Clustering on phasic and non phasic neurons
  517. rangeHighFreq=[2:0.1:8];
  518. rangeLowFreq=[0.3:0.1:1.1];
  519. nbClassif2=0;
  520. for i=1:length(rangeHighFreq)
  521. for j=1:length(rangeLowFreq)
  522. f_low=rangeLowFreq(j);
  523. f_high=rangeHighFreq(i);
  524. %fourier analysis
  525. [X_all,f_all,P_all,Nt_all]=myfft(DSconcat_OTshort',0.01,0);
  526. total_power=sum(P_all);
  527. P_all_norm = bsxfun(@rdivide,P_all, total_power);
  528. a=find(f_all>f_low);b=find(f_all<f_high);
  529. lower_window_sum_all=sum(P_all_norm(1:a(1),:));
  530. upper_window_sum_all=sum(P_all_norm(a(1):b(end),:));
  531. ratio_all=lower_window_sum_all./upper_window_sum_all;
  532. %hierarchical clustering
  533. X_ratio=[[lower_window_sum_all]' [upper_window_sum_all]']; %#ok<NBRAK>
  534. eva_Ch_ratio = evalclusters(X_ratio,'linkage','CalinskiHarabasz','KList',[1:10]);
  535. OptimalK(i,j)=eva_Ch_ratio.OptimalK;
  536. c_ratio3=eva_Ch_ratio.OptimalY;
  537. if eva_Ch_ratio.OptimalK==2
  538. MeanP=mean(X_ratio(c_ratio3==2,:),1);
  539. MeanNP=mean(X_ratio(c_ratio3==1,:),1);
  540. RealDist(i,j)=pdist([MeanP;MeanNP],'euclidean');
  541. nbClassif2=nbClassif2+1;
  542. OptimalY(:,nbClassif2)=eva_Ch_ratio.OptimalY;
  543. end
  544. end
  545. end
  546. figure
  547. [X,Y] = meshgrid(rangeLowFreq,rangeHighFreq);
  548. pcolor(X,Y,OptimalK)
  549. colorbar
  550. xlabel('Lower frequency limit (Hz)')
  551. ylabel('Higher frequency limit (Hz)')
  552. corrOptimalK=OptimalK(:,1:8);
  553. percentK2=100*sum(sum(corrOptimalK==2))/(size(corrOptimalK,1)*size(corrOptimalK,2));
  554. percentK3=100*sum(sum(corrOptimalK==3))/(size(corrOptimalK,1)*size(corrOptimalK,2));
  555. distancesDist=reshape(RealDist,size(RealDist,1)*size(RealDist,2),1);
  556. figure;
  557. hist(distancesDist(distancesDist(:,1)~=0,1));
  558. hold on
  559. hist(DistCluster);
  560. plot([RealDist1 RealDist1],[0 4000]);
  561. xlabel('Between-cluster distance')
  562. ylabel('# iterations')
  563. text(0.225,100,'Phasic vs Non-phasic classification','color','b','rotation',90);
  564. A(1,1)=length(find(OptimalK==2));
  565. A(1,2)=length(find(OptimalK==3));
  566. A(1,3)=size(OptimalK,1)*size(OptimalK,2);
  567. A(2,1)=100*A(1,1)./A(1,3);
  568. A(2,2)=100*A(1,2)./A(1,3);
  569. A(2,3)=100*(A(1,1)+A(1,2))./A(1,3);
  570. %% look at 2 classes when OptimalK=2
  571. for j=1:size(OptimalY,2)
  572. for i=1:2
  573. NbNeuronClass(j,i)=sum(OptimalY(:,j)==i);
  574. end
  575. NbNeuronClass(j,:)=sort(NbNeuronClass(j,:),2);
  576. end
  577. figure
  578. histogram(NbNeuronClass(:,1))
  579. hold on
  580. histogram(NbNeuronClass(:,2))
  581. hold off
  582. xlabel('number of neurons in each class')
  583. ylabel('# iterations')
  584. legend('Non-Phasic','Phasic')