m_ThreeRewHist.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. %plotting reward response according to trial history
  2. clear all;
  3. load ('R_3R.mat');
  4. load ('RAW.mat');
  5. Xaxis=[-2 5];
  6. Ishow=find(R_3R.Param.Tm>=Xaxis(1) & R_3R.Param.Tm<=Xaxis(2));
  7. time1=R_3R.Param.Tm(Ishow);
  8. runanalysis=1;
  9. %get parameters
  10. trialsback=6; %how many trials back to look
  11. Baseline=R_3R.Param.BinBase; %For normalizing activity
  12. Window=[0.8 1.3]; %what window of activity is analyzed
  13. BinDura=R_3R.Param.BinDura;
  14. bins=R_3R.Param.bins;
  15. binint=R_3R.Param.binint;
  16. binstart=R_3R.Param.binstart;
  17. %colors
  18. sucrose=[0.7 0.3 0];
  19. maltodextrin=[.9 0.3 .9];
  20. water=[0.00 0.75 0.75];
  21. total=[0.3 0.1 0.8];
  22. exc=[0 113/255 188/255];
  23. inh=[240/255 0 50/255];
  24. %extra colors to make a gradient
  25. sucrosem=[0.8 0.7 0.2];
  26. sucrosel=[1 0.8 0.3];
  27. maltodextrinm=[.9 0.6 1];
  28. maltodextrinl=[1 0.8 1];
  29. waterm=[0.1 0.9 0.9];
  30. waterl=[0.3 1 1];
  31. NAc=[0.5 0.1 0.8];
  32. VP=[0.3 0.7 0.1];
  33. RD1P1=strcmp('RD1P1', R_3R.Erefnames);
  34. RD1P2=strcmp('RD1P2', R_3R.Erefnames);
  35. RD1P3=strcmp('RD1P3', R_3R.Erefnames);
  36. RD2P1=strcmp('RD2P1', R_3R.Erefnames);
  37. RD2P2=strcmp('RD2P2', R_3R.Erefnames);
  38. RD2P3=strcmp('RD2P3', R_3R.Erefnames);
  39. RD3P1=strcmp('RD3P1', R_3R.Erefnames);
  40. RD3P2=strcmp('RD3P2', R_3R.Erefnames);
  41. RD3P3=strcmp('RD3P3', R_3R.Erefnames);
  42. Sel = (R_3R.Ev(RD1P1).RespDir<2); %all neurons
  43. %% Plotting sucrose according to previous trial
  44. subplot(2,3,1);
  45. hold on;
  46. title(['Suc response'])
  47. %plot suc preferring neurons to suc
  48. psthE=nanmean(R_3R.Ev(RD1P1).PSTHz(Sel,Ishow),1);
  49. semE=nanste(R_3R.Ev(RD1P1).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  50. upE=psthE+semE;
  51. downE=psthE-semE;
  52. plot(time1,psthE,'Color',sucrose,'linewidth',1);
  53. %plot suc preferring neurons to malt
  54. psth2=nanmean(R_3R.Ev(RD1P2).PSTHz(Sel,Ishow),1);
  55. sem2=nanste(R_3R.Ev(RD1P2).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  56. up2=psth2+sem2;
  57. down2=psth2-sem2;
  58. plot(time1,psth2,'Color',sucrosem,'linewidth',1);
  59. %plot suc preferring neurons to water
  60. psth3=nanmean(R_3R.Ev(RD1P3).PSTHz(Sel,Ishow),1);
  61. sem3=nanste(R_3R.Ev(RD1P3).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  62. up3=psth3+sem3;
  63. down3=psth3-sem3;
  64. plot(time1,psth3,'Color',sucrosel,'linewidth',1);
  65. patch([time1,time1(end:-1:1)],[upE,downE(end:-1:1)],sucrose,'EdgeColor','none');alpha(0.5);
  66. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],sucrosem,'EdgeColor','none');alpha(0.5);
  67. patch([time1,time1(end:-1:1)],[up3,down3(end:-1:1)],sucrosel,'EdgeColor','none');alpha(0.5);
  68. plot([-2 5],[0 0],':','color','k');
  69. plot([0 0],[-2 8],':','color','k');
  70. axis([-2 5 -1 2.5]);
  71. ylabel('Z-score');
  72. legend('Suc after suc','Suc after mal','Suc after wat');
  73. xlabel('Seconds from RD');
  74. %% Plotting maltodextrin according to previous trial
  75. subplot(2,3,2);
  76. hold on;
  77. title(['Mal response'])
  78. %plot mal preferring neurons to suc
  79. psthE=nanmean(R_3R.Ev(RD2P1).PSTHz(Sel,Ishow),1);
  80. semE=nanste(R_3R.Ev(RD2P1).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  81. upE=psthE+semE;
  82. downE=psthE-semE;
  83. plot(time1,psthE,'Color',maltodextrin,'linewidth',1);
  84. %plot mal preferring neurons to malt
  85. psth2=nanmean(R_3R.Ev(RD2P2).PSTHz(Sel,Ishow),1);
  86. sem2=nanste(R_3R.Ev(RD2P2).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  87. up2=psth2+sem2;
  88. down2=psth2-sem2;
  89. plot(time1,psth2,'Color',maltodextrinm,'linewidth',1);
  90. %plot mal preferring neurons to water
  91. psth3=nanmean(R_3R.Ev(RD2P3).PSTHz(Sel,Ishow),1);
  92. sem3=nanste(R_3R.Ev(RD2P3).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  93. up3=psth3+sem3;
  94. down3=psth3-sem3;
  95. plot(time1,psth3,'Color',maltodextrinl,'linewidth',1);
  96. patch([time1,time1(end:-1:1)],[upE,downE(end:-1:1)],maltodextrin,'EdgeColor','none');alpha(0.5);
  97. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],maltodextrinm,'EdgeColor','none');alpha(0.5);
  98. patch([time1,time1(end:-1:1)],[up3,down3(end:-1:1)],maltodextrinl,'EdgeColor','none');alpha(0.5);
  99. plot([-2 5],[0 0],':','color','k');
  100. plot([0 0],[-2 8],':','color','k');
  101. axis([-2 5 -1 2.5]);
  102. ylabel('Z-score');
  103. legend('Mal after suc','Mal after mal','Mal after wat');
  104. xlabel('Seconds from RD');
  105. %% Plotting water according to previous trial
  106. subplot(2,3,3);
  107. hold on;
  108. title(['Water response'])
  109. %plot wat preferring neurons to suc
  110. psthE=nanmean(R_3R.Ev(RD3P1).PSTHz(Sel,Ishow),1);
  111. semE=nanste(R_3R.Ev(RD3P1).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  112. upE=psthE+semE;
  113. downE=psthE-semE;
  114. plot(time1,psthE,'Color',water,'linewidth',1);
  115. %plot wat preferring neurons to malt
  116. psth2=nanmean(R_3R.Ev(RD3P2).PSTHz(Sel,Ishow),1);
  117. sem2=nanste(R_3R.Ev(RD3P2).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  118. up2=psth2+sem2;
  119. down2=psth2-sem2;
  120. plot(time1,psth2,'Color',waterm,'linewidth',1);
  121. %plot wat preferring neurons to water
  122. psth3=nanmean(R_3R.Ev(RD3P3).PSTHz(Sel,Ishow),1);
  123. sem3=nanste(R_3R.Ev(RD3P3).PSTHz(Sel,Ishow),1); %calculate standard error of the mean
  124. up3=psth3+sem3;
  125. down3=psth3-sem3;
  126. plot(time1,psth3,'Color',waterl,'linewidth',1);
  127. patch([time1,time1(end:-1:1)],[upE,downE(end:-1:1)],water,'EdgeColor','none');alpha(0.5);
  128. patch([time1,time1(end:-1:1)],[up2,down2(end:-1:1)],waterm,'EdgeColor','none');alpha(0.5);
  129. patch([time1,time1(end:-1:1)],[up3,down3(end:-1:1)],waterl,'EdgeColor','none');alpha(0.5);
  130. plot([-2 5],[0 0],':','color','k');
  131. plot([0 0],[-2 8],':','color','k');
  132. axis([-2 5 -1 2.5]);
  133. ylabel('Z-score');
  134. legend('Wat after suc','Wat after mal','Wat after wat');
  135. xlabel('Seconds from RD');
  136. %% linear model for impact of previous rewards
  137. %reset
  138. NN=0;
  139. EvMeanz=0;
  140. if runanalysis==1
  141. for i=1:length(RAW) %loops through sessions
  142. if strcmp('TH',RAW(i).Type(1:2))
  143. %events
  144. RD=strmatch('RD',RAW(i).Einfo(:,2),'exact');
  145. R1=strmatch('Reward1Deliv',RAW(i).Einfo(:,2),'exact');
  146. R2=strmatch('Reward2Deliv',RAW(i).Einfo(:,2),'exact');
  147. R3=strmatch('Reward3Deliv',RAW(i).Einfo(:,2),'exact');
  148. %reset
  149. X=[];
  150. Y=[];
  151. %set up the matrix with outcome identity for each session
  152. rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
  153. rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
  154. rewards3=cat(2,RAW(i).Erast{R3,1}(:,1),-1*ones(length(RAW(i).Erast{R3,1}(:,1)),1));
  155. rewards=cat(1,rewards1,rewards2,rewards3);
  156. [rewards(:,1),ind]=sort(rewards(:,1));
  157. rewards(:,2)=rewards(ind,2);
  158. for k=trialsback+1:length(RAW(i).Erast{RD,1}(:,1))
  159. time=RAW(i).Erast{RD,1}(k,1);
  160. entry=find(rewards(:,1)==time);
  161. for m=1:trialsback+1
  162. X(k-trialsback,m)=rewards(entry+1-m,2);
  163. end
  164. end
  165. for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
  166. NN=NN+1;
  167. rewspk=0;
  168. basespk=0;
  169. %get mean baseline firing for all reward trials
  170. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Baseline,{2});% makes trial by trial rasters for baseline
  171. for y= 1:B1n
  172. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  173. end
  174. Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
  175. Bmean=nanmean(Bhz);
  176. Bstd=nanstd(Bhz);
  177. %get trial by trial firing rate for all reward trials
  178. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
  179. for y= 1:EVn
  180. rewspk(y,1)=sum(EVcell{1,y}>Window(1));
  181. end
  182. Y=((rewspk(trialsback+1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
  183. %true data
  184. linmdl{NN,1}=fitlm(X,Y);%,'CategoricalVars',[1:trialsback+1]);
  185. R_3R.RewHist.LinMdlWeights(NN,1:trialsback+1)=linmdl{NN,1}.Coefficients.Estimate(2:trialsback+2)';
  186. R_3R.RewHist.LinMdlPVal(NN,1:trialsback+1)=linmdl{NN,1}.Coefficients.pValue(2:trialsback+2)';
  187. %shuffled
  188. YSh=Y(randperm(length(Y)));
  189. linmdlSh{NN,1}=fitlm(X,YSh);%,'CategoricalVars',[1:trialsback+1]);
  190. R_3R.RewHist.LinMdlWeightsSh(NN,1:trialsback+1)=linmdlSh{NN,1}.Coefficients.Estimate(2:trialsback+2)';
  191. R_3R.RewHist.LinMdlPValSh(NN,1:trialsback+1)=linmdlSh{NN,1}.Coefficients.pValue(2:trialsback+2)';
  192. fprintf('Neuron # %d\n',NN);
  193. end
  194. end
  195. end
  196. end
  197. %% plot linear model coefficients
  198. %Plot all neurons
  199. Sel = (R_3R.Ev(RD1P1).RespDir<2); %all neurons
  200. %coefficients for each trial
  201. subplot(2,3,4);
  202. hold on;
  203. errorbar(0:trialsback,nanmean(R_3R.RewHist.LinMdlWeights(Sel,1:trialsback+1),1),nanste(R_3R.RewHist.LinMdlWeights(Sel,1:trialsback+1),1),'color',VP,'linewidth',1);
  204. errorbar(0:trialsback,nanmean(R_3R.RewHist.LinMdlWeightsSh(Sel,1:trialsback+1),1),nanste(R_3R.RewHist.LinMdlWeights(Sel,1:trialsback+1),1),'color','k','linewidth',1);
  205. xlabel('Trials back');
  206. ylabel('Mean coefficient weight');
  207. title('Linear model coefficients');
  208. legend('True','Shuff');
  209. axis([-1 trialsback+1 -1 4]);
  210. plot([-1 trialsback+1],[0 0],':','color','k','linewidth',0.75);
  211. % %stats to check if VP is greater than NAc
  212. % [c,~,~,~]=multcompare(R_3R.RewHist.LinCoeffStats{1,2},'dimension',[1,2,3],'display','off');
  213. % for i=1:trialsback+1
  214. %
  215. % %NAc vs VP
  216. % Cel=c(:,1)==4*(i-1)+1 & c(:,2)==4*(i-1)+3;
  217. % if c(Cel,6)<0.05 R_3R.RewHist.LinCoeffMultComp(i,1)=1; else R_3R.RewHist.LinCoeffMultComp(i,1)=0; end
  218. % R_3R.RewHist.LinCoeffMultComp(i,2)=c(Cel,6);
  219. %
  220. % %VP vs shuff
  221. % Cel=c(:,1)==4*(i-1)+1 & c(:,2)==4*(i-1)+2;
  222. % if c(Cel,6)<0.05 R_3R.RewHist.LinCoeffMultComp(i,3)=1; else R_3R.RewHist.LinCoeffMultComp(i,3)=0; end
  223. % R_3R.RewHist.LinCoeffMultComp(i,4)=c(Cel,6);
  224. %
  225. % %NAc vs shuff
  226. % Cel=c(:,1)==4*(i-1)+3 & c(:,2)==4*(i-1)+4;
  227. % if c(Cel,6)<0.05 R_3R.RewHist.LinCoeffMultComp(i,5)=1; else R_3R.RewHist.LinCoeffMultComp(i,5)=0; end
  228. % R_3R.RewHist.LinCoeffMultComp(i,6)=c(Cel,6);
  229. % end
  230. % subplot(2,4,4);
  231. % hold on;
  232. % plot([0:trialsback]-0.15,(R_3R.RewHist.LinCoeffMultComp(:,3)-0.5)*5,'*','color',VP); %VP vs shuff
  233. % plot([0:trialsback],(R_3R.RewHist.LinCoeffMultComp(:,5)-0.5)*5,'*','color',NAc); %NAc vs shuff
  234. % plot([0:trialsback]+0.15,(R_3R.RewHist.LinCoeffMultComp(:,1)-0.5)*5,'*','color','k'); %VP vs NAc
  235. %number of neurons with significant weights
  236. subplot(2,3,5);
  237. hold on;
  238. plot(0:trialsback,sum(R_3R.RewHist.LinMdlPVal(Sel,1:trialsback+1)<0.05,1)/sum(Sel),'color',VP,'linewidth',1);
  239. plot(0:trialsback,sum(R_3R.RewHist.LinMdlPValSh(Sel,1:trialsback+1)<0.05,1)/sum(Sel),'color',VP/3,'linewidth',1);
  240. axis([-1 trialsback+1 0 1]);
  241. xlabel('Trials back');
  242. ylabel('Fraction of the population');
  243. title('Reward-modulated neurons');
  244. legend('True','Shuff');
  245. % %Chi squared stat for each trial
  246. % for i=1:trialsback+1
  247. % [~,R_3R.RewHist.LinMdlX2all(i,1),R_3R.RewHist.LinMdlX2all(i,2)]=crosstab(cat(1,R_3R.RewHist.LinMdlPVal(Sel,i)<0.05,R_3R.RewHist.LinMdlPValSh(Sel,i)<0.05),cat(1,VPneurons,VPneurons+2));
  248. % [~,R_3R.RewHist.LinMdlX2region(i,1),R_3R.RewHist.LinMdlX2region(i,2)]=crosstab(R_3R.RewHist.LinMdlPVal(Sel,i)<0.05,VPneurons);
  249. % end
  250. % %plot([0:trialsback]-0.1,(R_3R.LinMdlX2all(:,2)<0.05)-0.52,'*','color',NAc);
  251. % plot([0:trialsback],(R_3R.RewHist.LinMdlX2region(:,2)<0.05&R_3R.RewHist.LinMdlX2all(:,2)<0.05)-0.52,'*','color','k');