all_behavior.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. function BehavStats=all_behavior(RAWblocks, RAWCued, plot_region, plot_sub_figures, perform_stats, Colors)
  2. %% interspersed vs blocks
  3. %subselect sessions of interest
  4. if strcmp(plot_region,'Both')
  5. RAW=RAWblocks;
  6. else
  7. for i=1:length(RAWblocks)
  8. name=char(RAWblocks(i).Region);
  9. region{i,1}=name(1:2);
  10. end
  11. region_sessions = strcmp(plot_region,region);
  12. RAW=RAWblocks(region_sessions);
  13. end
  14. % Latency
  15. % Find the indicies of all blocks and interspersed sessions
  16. blocked_sessions = strcmp('B',{RAW.Blocks})';
  17. interspersed_sessions = strcmp('I',{RAW.Blocks})';
  18. num_sessions = length(RAW);
  19. num_trials = 60;
  20. % preallocate arrays
  21. latencyR1 = nan(num_sessions, num_trials);
  22. latencyR2 = nan(num_sessions, num_trials);
  23. fraction_completed = zeros(num_sessions,2); %suc,malt
  24. rat_name = cell(num_sessions,1);
  25. for session = 1:num_sessions
  26. % Make logical index vectors to call every event type
  27. PER1 = strcmp('PER1',RAW(session).Einfo(:,2)); %finds the index of the event
  28. PER2 = strcmp('PER2',RAW(session).Einfo(:,2)); %finds the index of the event
  29. CueR1 = strcmp('CueR1',RAW(session).Einfo(:,2)); %finds the index of the event
  30. CueR2 = strcmp('CueR2',RAW(session).Einfo(:,2)); %finds the index of the event
  31. PECue = strcmp('PECue',RAW(session).Einfo(:,2)); %finds the index of the event
  32. % Latency Stuff
  33. suc_latencies = cell2mat(MakePSR04(RAW(session).Erast(PECue),RAW(session).Erast{CueR1},[0 20],{2,'first'}));
  34. malt_latencies = cell2mat(MakePSR04(RAW(session).Erast(PECue),RAW(session).Erast{CueR2},[0 20],{2,'first'}));
  35. latencyR1(session,1:length(suc_latencies)) = suc_latencies;
  36. latencyR2(session,1:length(malt_latencies)) = malt_latencies;
  37. % Trial Completion Stuff
  38. suc_completed = length(RAW(session).Erast{PER1,1});
  39. suc_total = length(RAW(session).Erast{CueR1,1});
  40. malt_completed = length(RAW(session).Erast{PER2,1});
  41. malt_total = length(RAW(session).Erast{CueR2,1});
  42. fraction_completed(session,:) = [suc_completed/suc_total,malt_completed/malt_total];
  43. % Rat names
  44. name=char(RAW(session).Ninfo(1,1));
  45. rat_name{session,1} = name(1:3);
  46. end
  47. % Latency stuff
  48. median_latencyR1 = median(latencyR1,2,'omitnan');
  49. median_latencyR2 = median(latencyR2,2,'omitnan');
  50. % Average for rat
  51. unique_rat_names = unique(rat_name);
  52. rat_latency_data = zeros(length(unique_rat_names),4);
  53. rat_completion_data = zeros(length(unique_rat_names),4);
  54. rat_latency_data_sem = zeros(length(unique_rat_names),4);
  55. rat_completion_data_sem = zeros(length(unique_rat_names),4);
  56. for rat = 1:length(unique_rat_names)
  57. rat_index = strcmp(unique_rat_names{rat}, rat_name);
  58. rat_latency_data(rat,:) = [mean(median_latencyR1(interspersed_sessions & rat_index)),... % Sucrose inter
  59. mean(median_latencyR2(interspersed_sessions & rat_index)),... % Malto inter
  60. mean(median_latencyR1(blocked_sessions & rat_index)),... % Sucrose block
  61. mean(median_latencyR2(blocked_sessions & rat_index))]; % Malto block
  62. rat_completion_data(rat,:) = [mean(fraction_completed(interspersed_sessions & rat_index, 1)),... % Sucrose inter
  63. mean(fraction_completed(interspersed_sessions & rat_index, 2)),... % Malto inter
  64. mean(fraction_completed(blocked_sessions & rat_index, 1)),... % Sucrose block
  65. mean(fraction_completed(blocked_sessions & rat_index, 2))]; % Malto block
  66. rat_latency_data_sem(rat,:) = [nanste(median_latencyR1(interspersed_sessions & rat_index),1),... % Sucrose inter
  67. nanste(median_latencyR2(interspersed_sessions & rat_index),1),... % Malto inter
  68. nanste(median_latencyR1(blocked_sessions & rat_index),1),... % Sucrose block
  69. nanste(median_latencyR2(blocked_sessions & rat_index),1)]; % Malto block
  70. rat_completion_data_sem(rat,:) = [nanste(fraction_completed(interspersed_sessions & rat_index, 1),1),... % Sucrose inter
  71. nanste(fraction_completed(interspersed_sessions & rat_index, 2),1),... % Malto inter
  72. nanste(fraction_completed(blocked_sessions & rat_index, 1),1),... % Sucrose block
  73. nanste(fraction_completed(blocked_sessions & rat_index, 2),1)]; % Malto block
  74. end
  75. mean_latency_data = [mean(median_latencyR1(interspersed_sessions)),... % Sucrose inter
  76. mean(median_latencyR2(interspersed_sessions)),... % Malto inter
  77. mean(median_latencyR1(blocked_sessions)),... % Sucrose block
  78. mean(median_latencyR2(blocked_sessions));... % Malto block
  79. nanste(median_latencyR1(interspersed_sessions),1),... % Sucrose inter
  80. nanste(median_latencyR2(interspersed_sessions),1),... % Malto inter
  81. nanste(median_latencyR1(blocked_sessions),1),... % Sucrose block
  82. nanste(median_latencyR2(blocked_sessions),1)]; % Malto block
  83. mean_completion_data = [mean(fraction_completed(interspersed_sessions, 1)),... % Sucrose inter
  84. mean(fraction_completed(interspersed_sessions, 2)),... % Malto inter
  85. mean(fraction_completed(blocked_sessions, 1)),... % Sucrose block
  86. mean(fraction_completed(blocked_sessions, 2));... % Malto block
  87. nanste(fraction_completed(interspersed_sessions, 1),1),... % Sucrose inter
  88. nanste(fraction_completed(interspersed_sessions, 2),1),... % Malto inter
  89. nanste(fraction_completed(blocked_sessions, 1),1),... % Sucrose block
  90. nanste(fraction_completed(blocked_sessions, 2),1)]; % Malto block
  91. % make figure
  92. figure('Units','inches','Position',[3,3,8,5]);
  93. % Latency subplot
  94. mean_data = {mean_latency_data, mean_completion_data};
  95. rat_data = {rat_latency_data, rat_completion_data, rat_latency_data_sem, rat_completion_data_sem};
  96. color = {Colors('sucrose'),Colors('maltodextrin'),Colors('blockrose'),Colors('blockrodextrin')};
  97. titles = {'Latency','Completion'};
  98. for plot_type = 1:2
  99. subplot(3,4,[5+(plot_type-1)*2 6+(plot_type-1)*2])
  100. hold on
  101. for rat = 1:length(rat_latency_data)
  102. xvals=[0.9 1.9]+(rat-1)*0.2/(length(unique_rat_names)-1);
  103. errorbar(xvals,rat_data{plot_type}(rat,1:2),rat_data{plot_type+2}(rat,1:2),'Color',[.7 .7 .7])
  104. errorbar(xvals+2,rat_data{plot_type}(rat,3:4),rat_data{plot_type+2}(rat,3:4),'Color',[.7 .7 .7])
  105. end
  106. for i = 1:4
  107. errorbar(i,mean_data{plot_type}(1,i),mean_data{plot_type}(2,i),'o','Color',color{i},'MarkerFaceColor',color{i})
  108. end
  109. if plot_type == 1
  110. % plot([1,2],[5,5],'k')
  111. % plot([3,4],[4.5,4.5],'k')
  112. % plot([1.5,3.5],[5.5,5.5],'k')
  113. % plot([1.5,1.5],[5.5,5],'k')
  114. % plot([3.5,3.5],[5.5,4.5],'k')
  115. % text(2.5, 5.7, '*','Color','k','FontSize',16)
  116. % text(2.98, 1.77, '#','Color','k') %This is to note that the two sucrose latencies are significantly different, which should be explained in the figure legend.
  117. axis([0.5 9.5 0 10]);
  118. ylabel('Latency (sec)');
  119. else
  120. axis([0.5 9.5 0 1]);
  121. ylabel('Fraction of trials completed')
  122. end
  123. end
  124. subplot(3,4,1:3)
  125. sessions_to_use = [1 8];
  126. % figure;
  127. session_end = strcmp('MedEnd',RAW(sessions_to_use(1)).Einfo(:,2)); %finds the index of the event
  128. stop(1) = RAW(sessions_to_use(1)).Erast{session_end,1};
  129. session_end = strcmp('MedEnd',RAW(sessions_to_use(2)).Einfo(:,2)); %finds the index of the event
  130. stop(2) = RAW(sessions_to_use(2)).Erast{session_end,1};
  131. line([0,1+stop(1)/60],[4.3,4.3],'Color','k')
  132. line([0,1+stop(2)/60],[6.3,6.3],'Color','k')
  133. for session = 1:length(sessions_to_use)
  134. %get events for this session
  135. RD1 = strcmp('RD1',RAW(sessions_to_use(session)).Einfo(:,2)); %finds the index of the event
  136. RD2 = strcmp('RD2',RAW(sessions_to_use(session)).Einfo(:,2)); %finds the index of the event
  137. suc_delivery = RAW(sessions_to_use(session)).Erast{RD1,1}/60;
  138. malt_delivery = RAW(sessions_to_use(session)).Erast{RD2,1}/60;
  139. for trial = 1:length(suc_delivery)
  140. line([suc_delivery(trial),suc_delivery(trial)],[3+2*(session-1)+1.1,3+2*(session-1)+1.7],'Color',color{3-2*(session-1)})
  141. end
  142. for trial = 1:length(malt_delivery)
  143. line([malt_delivery(trial),malt_delivery(trial)],[3+2*(session-1)+0.9,3+2*(session-1)+1.5],'Color',color{4-2*(session-1)})
  144. end
  145. end
  146. % title('Two Example Sessions','FontSize',11)
  147. if perform_stats
  148. % Latency
  149. suc_interspersed = median_latencyR1(interspersed_sessions);
  150. malt_interspersed = median_latencyR2(interspersed_sessions);
  151. suc_blocked = median_latencyR1(blocked_sessions);
  152. malt_blocked = median_latencyR2(blocked_sessions);
  153. num_interspersed = length(suc_interspersed);
  154. num_blocked = length(suc_blocked);
  155. rat_interspersed = rat_name(interspersed_sessions);
  156. rat_blocked = rat_name(blocked_sessions);
  157. % t=table(cat(1,rat_interspersed,rat_blocked),categorical(cat(1,zeros(num_interspersed,1),ones(num_blocked,1))),cat(1,suc_interspersed,suc_blocked),cat(1,malt_interspersed,malt_blocked),'variablenames',{'subject','blocked','sucrose','maltodextrin'});
  158. % vars=table({'Suc'; 'Mal'},'variablenames',{'Reward'});
  159. % rm=fitrm(t,'sucrose-maltodextrin~subject+blocked','WithinDesign',vars);
  160. % BehavStats.IntblockLatency.ranovatbl=ranova(rm);
  161. % BehavStats.IntblockLatency.blocked=multcompare(rm,'blocked');
  162. % BehavStats.IntblockLatency.reward=multcompare(rm,'Reward');
  163. % BehavStats.IntblockLatency.blockedbyreward=multcompare(rm,'blocked','by','Reward');
  164. % figure;
  165. [~,BehavStats.IntblockLatency.anovatbl,stats] = anovan(cat(1,suc_interspersed,malt_interspersed,suc_blocked,malt_blocked),...
  166. {cat(1,zeros(num_interspersed*2,1),ones(num_blocked*2,1)),...
  167. cat(1,zeros(num_interspersed,1),ones(num_interspersed,1),zeros(num_blocked,1),ones(num_blocked,1)),...
  168. cat(1,rat_interspersed,rat_interspersed,rat_blocked,rat_blocked)},...
  169. 'varnames',{'Int/Block','Reward','Subject'},'model',[1 0 0;1 1 0;0 0 1],... %[1 0 0;0 1 0;1 1 0;0 0 1],...
  170. 'random',3,'display','off');
  171. [BehavStats.IntblockLatency.multcompare] = multcompare(stats,'dimension',[1 2],'display','off');
  172. BehavStats.IntblockLatency.stats=stats;
  173. % Trials completed
  174. suc_inter = fraction_completed(interspersed_sessions,1);
  175. malt_inter = fraction_completed(interspersed_sessions,2);
  176. suc_block = fraction_completed(blocked_sessions,1);
  177. malt_block = fraction_completed(blocked_sessions,2);
  178. % t=table(cat(1,rat_interspersed,rat_blocked),categorical(cat(1,zeros(num_interspersed,1),ones(num_blocked,1))),cat(1,suc_inter,suc_block),cat(1,malt_inter,malt_block),'variablenames',{'subject','blocked','sucrose','maltodextrin'});
  179. % vars=table({'Suc'; 'Mal'},'variablenames',{'Reward'});
  180. % rm=fitrm(t,'sucrose-maltodextrin~subject+blocked','WithinDesign',vars);
  181. % BehavStats.IntblockCompletion.ranovatbl=ranova(rm);
  182. % BehavStats.IntblockCompletion.blocked=multcompare(rm,'blocked');
  183. % BehavStats.IntblockCompletion.reward=multcompare(rm,'Reward');
  184. % BehavStats.IntblockCompletion.blockedbyreward=multcompare(rm,'blocked','by','Reward');
  185. % figure;
  186. [~,BehavStats.IntblockCompletion.anovatbl,stats] = anovan(cat(1,suc_inter,malt_inter,suc_block,malt_block),...
  187. {cat(1,zeros(num_interspersed*2,1),ones(num_blocked*2,1)),...
  188. cat(1,zeros(num_interspersed,1),ones(num_interspersed,1),zeros(num_blocked,1),ones(num_blocked,1)),...
  189. cat(1,rat_interspersed,rat_interspersed,rat_blocked,rat_blocked)},...
  190. 'varnames',{'Int/Block','Reward','Subject'},'model',[1 0 0;1 1 0;0 0 1],... %[1 0 0;0 1 0;1 1 0;0 0 1],...
  191. 'random',3,'display','off');
  192. [BehavStats.IntblockCompletion.multcompare] = multcompare(stats,'dimension',[1 2],'display','off');
  193. BehavStats.IntblockCompletion.stats=stats;
  194. end
  195. %lick plots
  196. global Dura Tm BSIZE %Tbin
  197. BSIZE=0.05; %what's the bin for the PSTH?
  198. Dura=[-4 15]; Tm=Dura(1):BSIZE:Dura(2);
  199. %Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
  200. %Smoothing PSTHs
  201. %smoothing filter
  202. smoothbins=25; %number of previous bins used to smooth
  203. halfnormal=makedist('HalfNormal','mu',0,'sigma',8); %std=3.98
  204. filterweights=pdf(halfnormal,[0:smoothbins]);
  205. %generate lick PSTHs
  206. for session = 1:num_sessions
  207. % Make logical index vectors to call every event type
  208. R1 = strcmp('RD1',RAW(session).Einfo(:,2)); %finds the index of the event
  209. R2 = strcmp('RD2',RAW(session).Einfo(:,2)); %finds the index of the event
  210. Licks = strcmp('Licks',RAW(session).Einfo(:,2)); %finds the index of the event
  211. EvList{1}=RAW(session).Erast{R1};
  212. EvList{2}=RAW(session).Erast{R2};
  213. for k=1:2
  214. [PSR1,N1]=MakePSR04(RAW(session).Erast(Licks),EvList{k},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  215. [PTH1,~,~]=MakePTH07(PSR1,repmat(N1,size(RAW(session).Erast(Licks),1),1),{2,0,BSIZE});%-----Fixed bin used here
  216. PTH1smooth=[];
  217. for l=1:length(Tm)
  218. PTH1smooth(1,l)=sum(PTH1(1,l-min([l-1 smoothbins]):l).*fliplr(filterweights(1:min([l smoothbins+1]))))/sum(filterweights(1:min([l smoothbins+1])));
  219. end
  220. Lick.IntBlock(k).PSTHraw(session,:)=PTH1smooth;
  221. end
  222. end
  223. %plotting
  224. Xaxis=[-2 12];
  225. Ishow=find(Tm>=Xaxis(1) & Tm<=Xaxis(2));
  226. time1=Tm(Ishow);
  227. for type=1:2
  228. if type==1 Sel=interspersed_sessions; end
  229. if type==2 Sel=blocked_sessions; end
  230. psth1=nanmean(Lick.IntBlock(1).PSTHraw(Sel,Ishow),1);
  231. sem1=nanste(Lick.IntBlock(1).PSTHraw(Sel,Ishow),1); %calculate standard error of the mean
  232. up1=psth1+sem1;
  233. down1=psth1-sem1;
  234. psthE=nanmean(Lick.IntBlock(2).PSTHraw(Sel,Ishow),1);
  235. semE=nanste(Lick.IntBlock(2).PSTHraw(Sel,Ishow),1); %calculate standard error of the mean
  236. upE=psthE+semE;
  237. downE=psthE-semE;
  238. %plotting
  239. subplot(4,4,12+type);
  240. hold on;
  241. plot(time1,psth1,'Color',color{1+(type-1)*2},'linewidth',1);
  242. plot(time1,psthE,'Color',color{2+(type-1)*2},'linewidth',1);
  243. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],color{1+(type-1)*2},'EdgeColor','none');alpha(0.5);
  244. patch([time1,time1(end:-1:1)],[upE,downE(end:-1:1)],color{2+(type-1)*2},'EdgeColor','none');alpha(0.5);
  245. plot([0 0],[-2 8],':','color','k','linewidth',0.75);
  246. axis([Xaxis(1) Xaxis(2) 0 8]);
  247. xlabel('Seconds from reward delivery');
  248. if type==1 ylabel('Mean lick rate (licks/s)'); end
  249. if type==1 title('Interspersed'); end
  250. if type==2 title('Blocked'); end
  251. legend('Suc trials','Mal trials');
  252. end
  253. %% cued sessions
  254. %subselect sessions of interest
  255. for i=1:length(RAWCued)
  256. name=char(RAWCued(i).Ninfo(1,1));
  257. sessrat(i,1)=str2num(name(2:3));
  258. sessday(i,1)=str2num(name(8:9));
  259. end
  260. %decide which sessions to include
  261. inc_sessions = (sessrat==3 | sessrat==4 | sessrat==9 | sessrat==10) & sessday>10;
  262. RAW=RAWCued(inc_sessions);
  263. %RAW=RAWCued; %all sessions
  264. % Latency
  265. % Find the indicies of all blocks and interspersed sessions
  266. num_sessions = length(RAW);
  267. num_trials = 60; %just for pre-allocating
  268. % preallocate arrays
  269. latencyNo1 = nan(num_sessions, num_trials); %non-predictive sucrose
  270. latencyNo2 = nan(num_sessions, num_trials); %non-predictive maltodextrin
  271. latencyPr1 = nan(num_sessions, num_trials); %predictive sucrose
  272. latencyPr2 = nan(num_sessions, num_trials); %predictive maltodextrin
  273. fraction_completed = zeros(num_sessions,4); %Predictive suc, pm, ns, nm
  274. rat_name = cell(num_sessions,1);
  275. for session = 1:num_sessions
  276. % Make logical index vectors to call every event type
  277. PENo1 = strcmp('PECue31',RAW(session).Einfo(:,2)); %finds the index of the event
  278. PENo2 = strcmp('PECue32',RAW(session).Einfo(:,2)); %finds the index of the event
  279. PEPr1 = strcmp('PECue1',RAW(session).Einfo(:,2)); %finds the index of the event
  280. PEPr2 = strcmp('PECue2',RAW(session).Einfo(:,2)); %finds the index of the event
  281. Cue1 = strcmp('Cue1',RAW(session).Einfo(:,2)); %finds the index of the event
  282. Cue2 = strcmp('Cue2',RAW(session).Einfo(:,2)); %finds the index of the event
  283. Cue31 = strcmp('Cue31',RAW(session).Einfo(:,2)); %finds the index of the event
  284. Cue32 = strcmp('Cue32',RAW(session).Einfo(:,2)); %finds the index of the event
  285. PECue = strcmp('PECue',RAW(session).Einfo(:,2)); %finds the index of the event
  286. % Latency Stuff
  287. No1_latencies = cell2mat(MakePSR04(RAW(session).Erast(PECue),RAW(session).Erast{Cue31},[0 20],{2,'first'}));
  288. latencyNo1(session,1:length(No1_latencies)) = No1_latencies;
  289. No2_latencies = cell2mat(MakePSR04(RAW(session).Erast(PECue),RAW(session).Erast{Cue32},[0 20],{2,'first'}));
  290. latencyNo2(session,1:length(No2_latencies)) = No2_latencies;
  291. Pr1_latencies = cell2mat(MakePSR04(RAW(session).Erast(PECue),RAW(session).Erast{Cue1},[0 20],{2,'first'}));
  292. latencyPr1(session,1:length(Pr1_latencies)) = Pr1_latencies;
  293. Pr2_latencies = cell2mat(MakePSR04(RAW(session).Erast(PECue),RAW(session).Erast{Cue2},[0 20],{2,'first'}));
  294. latencyPr2(session,1:length(Pr2_latencies)) = Pr2_latencies;
  295. % Trial Completion Stuff
  296. No1_completed = length(RAW(session).Erast{PENo1,1});
  297. No1_total = length(RAW(session).Erast{Cue31,1});
  298. No2_completed = length(RAW(session).Erast{PENo2,1});
  299. No2_total = length(RAW(session).Erast{Cue32,1});
  300. Pr1_completed = length(RAW(session).Erast{PEPr1,1});
  301. Pr1_total = length(RAW(session).Erast{Cue1,1});
  302. Pr2_completed = length(RAW(session).Erast{PEPr2,1});
  303. Pr2_total = length(RAW(session).Erast{Cue2,1});
  304. fraction_completed(session,:) = [No1_completed/No1_total,No2_completed/No2_total,Pr1_completed/Pr1_total,Pr2_completed/Pr2_total];
  305. % Rat names
  306. name=char(RAW(session).Ninfo(1,1));
  307. rat_name{session,1} = name(1:3);
  308. end
  309. % Latency stuff
  310. median_latencyNo1 = median(latencyNo1,2,'omitnan');
  311. median_latencyNo2 = median(latencyNo2,2,'omitnan');
  312. median_latencyPr1 = median(latencyPr1,2,'omitnan');
  313. median_latencyPr2 = median(latencyPr2,2,'omitnan');
  314. % Average for rat
  315. unique_rat_names = unique(rat_name);
  316. rat_latency_data = zeros(length(unique_rat_names),4);
  317. rat_completion_data = zeros(length(unique_rat_names),4);
  318. rat_latency_data_sem = zeros(length(unique_rat_names),4);
  319. rat_completion_data_sem = zeros(length(unique_rat_names),4);
  320. for rat = 1:length(unique_rat_names)
  321. rat_index = strcmp(unique_rat_names{rat}, rat_name);
  322. rat_latency_data(rat,:) = [mean(median_latencyNo1(rat_index)),... % Sucrose non-pr
  323. mean(median_latencyNo2(rat_index)),... % Malto non-pr
  324. mean(median_latencyPr1(rat_index)),... % Sucrose pr
  325. mean(median_latencyPr2(rat_index))]; % Malto pr
  326. rat_completion_data(rat,:) = [mean(fraction_completed(rat_index, 1)),... % Sucrose non-pr
  327. mean(fraction_completed(rat_index, 2)),... % Malto non-pr
  328. mean(fraction_completed(rat_index, 3)),... % Sucrose pr
  329. mean(fraction_completed(rat_index, 4))]; % Malto pr
  330. rat_latency_data_sem(rat,:) = [nanste(median_latencyNo1(rat_index),1),... % Sucrose non-pr
  331. nanste(median_latencyNo2(rat_index),1),... % Malto non-pr
  332. nanste(median_latencyPr1(rat_index),1),... % Sucrose pr
  333. nanste(median_latencyPr2(rat_index),1)]; % Malto pr
  334. rat_completion_data_sem(rat,:) = [nanste(fraction_completed(rat_index, 1),1),...
  335. nanste(fraction_completed(rat_index, 2),1),...
  336. nanste(fraction_completed(rat_index, 3),1),...
  337. nanste(fraction_completed(rat_index, 4),1)];
  338. end
  339. mean_latency_data = [mean(median_latencyNo1),... % Sucrose non-pr
  340. mean(median_latencyNo2),... % Malto non-pr
  341. mean(median_latencyPr1),... % Sucrose pr
  342. mean(median_latencyPr2);... % Malto pr
  343. nanste(median_latencyNo1,1),...
  344. nanste(median_latencyNo2,1),...
  345. nanste(median_latencyPr1,1),...
  346. nanste(median_latencyPr2,1)];
  347. mean_completion_data = [mean(fraction_completed(:, 1)),...
  348. mean(fraction_completed(:, 2)),...
  349. mean(fraction_completed(:, 3)),...
  350. mean(fraction_completed(:, 4));...
  351. nanste(fraction_completed(:, 1),1),...
  352. nanste(fraction_completed(:, 2),1),...
  353. nanste(fraction_completed(:, 3),1),...
  354. nanste(fraction_completed(:, 4),1)];
  355. % Latency subplot
  356. mean_data = {mean_latency_data, mean_completion_data};
  357. rat_data = {rat_latency_data, rat_completion_data, rat_latency_data_sem, rat_completion_data_sem};
  358. color = {Colors('sucrose'),Colors('maltodextrin'),Colors('blockrose'),Colors('blockrodextrin')};
  359. titles = {'Latency','Completion'};
  360. for plot_type = 1:2
  361. subplot(3,4,[5+(plot_type-1)*2 6+(plot_type-1)*2])
  362. hold on
  363. for rat = 1:length(rat_latency_data)
  364. xvals=[5.9 6.9]+(rat-1)*0.2/(length(unique_rat_names)-1);
  365. errorbar(xvals,rat_data{plot_type}(rat,1:2),rat_data{plot_type+2}(rat,1:2),'Color',[.7 .7 .7])
  366. errorbar(xvals+2,rat_data{plot_type}(rat,3:4),rat_data{plot_type+2}(rat,3:4),'Color',[.7 .7 .7])
  367. end
  368. for i = 1:4
  369. errorbar(i+5,mean_data{plot_type}(1,i),mean_data{plot_type}(2,i),'o','Color',color{i},'MarkerFaceColor',color{i})
  370. end
  371. if plot_type == 1
  372. % plot([6,7],[5,5],'k')
  373. % plot([8,9],[4.5,4.5],'k')
  374. % plot([6.5,8.5],[5.5,5.5],'k')
  375. % plot([6.5,6.5],[5.5,5],'k')
  376. % plot([8.5,8.5],[5.5,4.5],'k')
  377. % text(7.5, 5.7, '*','Color','k','FontSize',16)
  378. % text(2.98, 1.77, '#','Color','k') %This is to note that the two sucrose latencies are significantly different, which should be explained in the figure legend.
  379. axis([0.5 9.5 0 10]);
  380. ylabel('Port entry latency (sec)');
  381. text(0.5,11,'Interspersed');
  382. text(3,11,'Blocked');
  383. text(5.5,11,'Non-predictive');
  384. text(8,11,'Predictive');
  385. else
  386. axis([0.5 9.5 0 1]);
  387. ylabel('Fraction of trials completed')
  388. text(0.5,1.1,'Interspersed');
  389. text(3,1.1,'Blocked');
  390. text(5.5,1.1,'Non-predictive');
  391. text(8,1.1,'Predictive');
  392. end
  393. conditions = {"Suc","Mal","Suc","Mal","Suc","Mal","Suc","Mal"};
  394. set(gca,'XTick',[1:4 6:9],'XTickLabel',conditions)
  395. end
  396. subplot(3,4,1:3)
  397. session_to_use = 2;
  398. session_end = strcmp('MedEnd',RAW(session_to_use(1)).Einfo(:,2)); %finds the index of the event
  399. stop = RAW(session_to_use(1)).Erast{session_end,1};
  400. line([0,1+stop(1)/60],[2.3,2.3],'Color','k')
  401. %get events for this session
  402. Cue1RD = strcmp('Cue1RD',RAW(session_to_use).Einfo(:,2)); %finds the index of the event
  403. Cue2RD = strcmp('Cue2RD',RAW(session_to_use).Einfo(:,2)); %finds the index of the event
  404. Cue3RD1 = strcmp('Cue3RD1',RAW(session_to_use).Einfo(:,2)); %finds the index of the event
  405. Cue3RD2 = strcmp('Cue3RD2',RAW(session_to_use).Einfo(:,2)); %finds the index of the event
  406. suc_deliveryN = RAW(session_to_use).Erast{Cue3RD1,1}/60;
  407. malt_deliveryN = RAW(session_to_use).Erast{Cue3RD2,1}/60;
  408. suc_deliveryP = RAW(session_to_use).Erast{Cue1RD,1}/60;
  409. malt_deliveryP = RAW(session_to_use).Erast{Cue2RD,1}/60;
  410. for trial = 1:length(suc_deliveryN)
  411. a=line([suc_deliveryN(trial),suc_deliveryN(trial)],[2.1,2.7],'Color',color{1});
  412. end
  413. for trial = 1:length(malt_deliveryN)
  414. b=line([malt_deliveryN(trial),malt_deliveryN(trial)],[1.9,2.5],'Color',color{2});
  415. end
  416. for trial = 1:length(suc_deliveryP)
  417. c=line([suc_deliveryP(trial),suc_deliveryP(trial)],[2.1,2.7],'Color',color{3});
  418. end
  419. for trial = 1:length(malt_deliveryP)
  420. d=line([malt_deliveryP(trial),malt_deliveryP(trial)],[1.9,2.5],'Color',color{4});
  421. end
  422. text(60,6.3,'Interspersed');
  423. text(60,4.3,'Blocked');
  424. text(105,2.3,'Cued');
  425. text(0,3,'Sucrose, unpredictable','Color',color{1});
  426. text(30,3,'Sucrose, predicted by cue','Color',color{3});
  427. text(0,1.6,'Maltodextrin, unpredictable','Color',color{2});
  428. text(30,1.6,'Maltodextrin, predicted by cue','Color',color{4});
  429. XTick=[0 20 40 60 80 100];
  430. axis([0 110 0.9 7]);
  431. ax1 = gca;
  432. ax1.YAxis.Visible = 'off';
  433. xlabel('Session time (minutes)')
  434. if perform_stats
  435. % Latency
  436. % t=table(rat_name,median_latencyNo1,median_latencyNo2,median_latencyPr1,median_latencyPr2,'variablenames',{'subject','NPSuc','NPMal','PrSuc','PrMal'});
  437. % vars=table({'NPSuc';'NPMal';'PrSuc';'PrMal'},{'Non'; 'Non'; 'Pr' ;'Pr'},{'Suc'; 'Mal' ;'Suc' ;'Mal'},'variablenames',{'TrialType','Predictive','Reward'});
  438. % rm=fitrm(t,'NPSuc-PrMal~subject','WithinDesign',vars);
  439. % BehavStats.CuedLatency.ranovatbl=ranova(rm);
  440. % BehavStats.CuedLatency.predictive=multcompare(rm,'Predictive');
  441. % BehavStats.CuedLatency.reward=multcompare(rm,'Reward');
  442. % BehavStats.CuedLatency.predictivebyreward=multcompare(rm,'Predictive','by','Reward');
  443. suc_interspersed = median_latencyNo1;
  444. malt_interspersed = median_latencyNo2;
  445. suc_blocked = median_latencyPr1;
  446. malt_blocked = median_latencyPr2;
  447. [~,BehavStats.CuedLatency.anovatbl,stats] = anovan(cat(1,suc_interspersed,malt_interspersed,suc_blocked,malt_blocked),...
  448. {cat(1,zeros(num_sessions*2,1),ones(num_sessions*2,1)),...
  449. cat(1,zeros(num_sessions,1),ones(num_sessions,1),zeros(num_sessions,1),ones(num_sessions,1)),...
  450. cat(1,rat_name,rat_name,rat_name,rat_name)},...
  451. 'varnames',{'Non-pr/Pr','Reward','Subject'},'model',[1 0 0;1 1 0;0 0 1],... %[1 0 0;0 1 0;1 1 0;0 0 1],...
  452. 'random',3,'display','off');
  453. [BehavStats.CuedLatency.multcompare,BehavStats.CuedLatency.multcompare_means] = multcompare(stats,'dimension',[1],'display','off');
  454. BehavStats.CuedLatency.stats=stats;
  455. % Trials completed
  456. % t=table(rat_name,fraction_completed(:,1),fraction_completed(:,2),fraction_completed(:,3),fraction_completed(:,4),'variablenames',{'subject','NPSuc','NPMal','PrSuc','PrMal'});
  457. % vars=table({'NPSuc';'NPMal';'PrSuc';'PrMal'},{'Non'; 'Non'; 'Pr' ;'Pr'},{'Suc'; 'Mal' ;'Suc' ;'Mal'},'variablenames',{'TrialType','Predictive','Reward'});
  458. % rm=fitrm(t,'NPSuc-PrMal~subject','WithinDesign',vars);
  459. % BehavStats.CuedCompletion.ranovatbl=ranova(rm);
  460. % BehavStats.CuedCompletion.predictive=multcompare(rm,'Predictive');
  461. % BehavStats.CuedCompletion.reward=multcompare(rm,'Reward');
  462. % BehavStats.CuedCompletion.predictivebyreward=multcompare(rm,'Predictive','by','Reward');
  463. suc_inter = fraction_completed(:,1);
  464. malt_inter = fraction_completed(:,2);
  465. suc_block = fraction_completed(:,3);
  466. malt_block = fraction_completed(:,4);
  467. %
  468. % figure;
  469. [~,BehavStats.CuedCompletion.anovatbl,stats] = anovan(cat(1,suc_inter,malt_inter,suc_block,malt_block),...
  470. {cat(1,zeros(num_sessions*2,1),ones(num_sessions*2,1)),...
  471. cat(1,zeros(num_sessions,1),ones(num_sessions,1),zeros(num_sessions,1),ones(num_sessions,1)),...
  472. cat(1,rat_name,rat_name,rat_name,rat_name)},...
  473. 'varnames',{'Non-pr/Pr','Reward','Subject'},'model',[1 0 0;1 1 0;0 0 1],... %[1 0 0;0 1 0;1 1 0;0 0 1],...
  474. 'random',3,'display','off');
  475. [BehavStats.CuedCompletion.multcompare] = multcompare(stats,'dimension',[1 2],'display','off');
  476. BehavStats.CuedCompletion.stats=stats;
  477. end
  478. %lick plots
  479. %generate lick PSTHs
  480. for session = 1:num_sessions
  481. % Make logical index vectors to call every event type
  482. R1 = strcmp('Cue1RD',RAW(session).Einfo(:,2)); %finds the index of the event
  483. R2 = strcmp('Cue2RD',RAW(session).Einfo(:,2)); %finds the index of the event
  484. R31 = strcmp('Cue3RD1',RAW(session).Einfo(:,2)); %finds the index of the event
  485. R32 = strcmp('Cue3RD2',RAW(session).Einfo(:,2)); %finds the index of the event
  486. Licks = strcmp('Licks',RAW(session).Einfo(:,2)); %finds the index of the event
  487. EvList{1}=RAW(session).Erast{R1};
  488. EvList{2}=RAW(session).Erast{R2};
  489. EvList{3}=RAW(session).Erast{R31};
  490. EvList{4}=RAW(session).Erast{R32};
  491. for k=1:4
  492. [PSR1,N1]=MakePSR04(RAW(session).Erast(Licks),EvList{k},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  493. [PTH1,~,~]=MakePTH07(PSR1,repmat(N1,size(RAW(session).Erast(Licks),1),1),{2,0,BSIZE});%-----Fixed bin used here
  494. PTH1smooth=[];
  495. for l=1:length(Tm)
  496. PTH1smooth(1,l)=sum(PTH1(1,l-min([l-1 smoothbins]):l).*fliplr(filterweights(1:min([l smoothbins+1]))))/sum(filterweights(1:min([l smoothbins+1])));
  497. end
  498. Lick.Cued(k).PSTHraw(session,:)=PTH1smooth;
  499. end
  500. end
  501. %plotting
  502. for type=1:2
  503. psth1=nanmean(Lick.Cued(1+(type-1)*2).PSTHraw(:,Ishow),1);
  504. sem1=nanste(Lick.Cued(1+(type-1)*2).PSTHraw(:,Ishow),1); %calculate standard error of the mean
  505. up1=psth1+sem1;
  506. down1=psth1-sem1;
  507. psthE=nanmean(Lick.Cued(2+(type-1)*2).PSTHraw(:,Ishow),1);
  508. semE=nanste(Lick.Cued(2+(type-1)*2).PSTHraw(:,Ishow),1); %calculate standard error of the mean
  509. upE=psthE+semE;
  510. downE=psthE-semE;
  511. %plotting
  512. subplot(4,4,14+type);
  513. hold on;
  514. plot(time1,psth1,'Color',color{1+(type-1)*2},'linewidth',1);
  515. plot(time1,psthE,'Color',color{2+(type-1)*2},'linewidth',1);
  516. patch([time1,time1(end:-1:1)],[up1,down1(end:-1:1)],color{1+(type-1)*2},'EdgeColor','none');alpha(0.5);
  517. patch([time1,time1(end:-1:1)],[upE,downE(end:-1:1)],color{2+(type-1)*2},'EdgeColor','none');alpha(0.5);
  518. plot([0 0],[-2 8],':','color','k','linewidth',0.75);
  519. axis([Xaxis(1) Xaxis(2) 0 8]);
  520. xlabel('Seconds from reward delivery');
  521. if type==1 title('Cued, predictive'); end
  522. if type==2 title('Cued, non-predictive'); end
  523. %legend('Suc trials','Mal trials');
  524. end
  525. %% preferences
  526. %data
  527. x=[1 2];
  528. CPref{1}=[83.8 92.7]; %C3
  529. CPref{2}=[68.7 75.7]; %C4
  530. CPref{3}=[66.7 90.1]; %C9
  531. CPref{4}=[96.0 70.7]; %C10
  532. %CPPref{5}=[82.7 86.9]; %C2
  533. %CPPref{6}=[77.4 90.2]; %C7
  534. %not included in ephys
  535. if strcmp(plot_region,'Both')
  536. IBPref{1}=[81.7 81.3];
  537. IBPref{2}=[77 73.9];
  538. IBPref{3}=[37.4 74.6];
  539. IBPref{4}=[71 95.6];
  540. IBPref{5}=[89 83.6];
  541. IBPref{6}=[94 93];
  542. IBPref{7}=[59.6 78.8];
  543. IBPref{8}=[88.8 85.8];
  544. IBPref{9}=[71.2 89];
  545. IBPref{10}=[87 76.6];
  546. IBPref{11}=[85.2 84.1];
  547. elseif strcmp(plot_region,'VP')
  548. IBPref{1}=[59.6 78.8];
  549. IBPref{2}=[88.8 85.8];
  550. IBPref{3}=[71.2 89];
  551. IBPref{4}=[87 76.6];
  552. IBPref{5}=[85.2 84.1];
  553. elseif strcmp(plot_region,'NA')
  554. IBPref{1}=[81.7 81.3];
  555. IBPref{2}=[77 73.9];
  556. IBPref{3}=[37.4 74.6];
  557. IBPref{4}=[71 95.6];
  558. IBPref{5}=[89 83.6];
  559. IBPref{6}=[94 93];
  560. end
  561. subplot(3,5,5);
  562. hold on;
  563. %do the first 2 first in order to get the legend right
  564. plot(x,IBPref{1},'Marker','o','MarkerFaceColor','w','LineWidth',1,'Color','k');
  565. plot(x,CPref{1},'Marker','o','MarkerFaceColor','k','LineWidth',1,'Color','k');
  566. for i=2:length(IBPref)
  567. plot(x,IBPref{i},'Marker','o','MarkerFaceColor','w','LineWidth',1,'Color','k');
  568. end
  569. for i=2:length(CPref)
  570. plot(x,CPref{i},'Marker','o','MarkerFaceColor','k','LineWidth',1,'Color','k');
  571. end
  572. axis([0.7 2.2 0 100]);
  573. title('Two-bottle choice');
  574. xlabel('Initial Final');
  575. ylabel('% sucrose consumption');
  576. plot([0 3],[50 50],':','color','k','linewidth',0.75);
  577. set(gca,'xtick',[])
  578. legend('Int / block','Cued','location','southeast');
  579. end