b_EventAnalysis.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. %% initial analysis of event-evoked activity
  2. %need to save excel sheets in the specified folder
  3. %the excel sheets say which events to make PSTHs for and what windows to
  4. %use for statistical testing
  5. %clear all; clc;
  6. global Dura Baseline Tm Tbase BSIZE Tbin
  7. tic
  8. if ~exist('RAW'), load ('RAW.mat'); end
  9. %Main settings
  10. SAVE_FLAG=1;
  11. BSIZE=0.01; %Do not change
  12. Dura=[-5 12]; Tm=Dura(1):BSIZE:Dura(2);
  13. %Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
  14. Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
  15. PStat=0.05; %for comparing pre versus post windows, or event A versus event B
  16. MinNumTrials=5; %how many trials of event there need to be to conduct analysis
  17. BinSize=0.05;
  18. %Smoothing
  19. Smoothing=1; %0 for raw and 1 for smoothing
  20. SmoothTYPE='lowess';
  21. SmoothSPAN=5;
  22. if Smoothing~=1, SmoothTYPE='NoSmoothing';SmoothSPAN=NaN; end
  23. %for bins analysis
  24. BinBase=[-22 -12]; %For normalizing activity
  25. BinDura=[0 0.6]; %size of bin
  26. bins = 66; %number of bins
  27. binint = 0.1; %spacing of bins
  28. binstart = -2; %start time of first bin relative to event
  29. %% Standard sucrose and maltodextrin sessions
  30. %start fresh
  31. R_2R=[];R_2R.Ninfo={};NN=0;Nneurons=0;
  32. % List of events to analyze and analysis windows EXTRACTED from excel file
  33. path='C:\Users\dottenh2\Documents\MATLAB\David\2Rewards Nex Files\2R_paper.xls';
  34. [~,Erefnames]=xlsread(path,'Windows','a3:a15'); % cell that contains the event names
  35. prewin = xlsread(path,'Windows','b3:c15');
  36. postwin = xlsread(path,'Windows','d3:e15');
  37. %saves event names for reference later
  38. R_2R.Erefnames=Erefnames;
  39. %Finds the total number of neurons in 2R and marks them by region/session
  40. for i=1:length(RAW)
  41. if strcmp('NA',RAW(i).Type(1:2)) | strcmp('VP',RAW(i).Type(1:2))
  42. R_2R.Ninfo=cat(1,R_2R.Ninfo,RAW(i).Ninfo);
  43. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  44. end
  45. end
  46. for i=1:Nneurons
  47. Session=string(R_2R.Ninfo(i,1));
  48. Name=char(Session);
  49. R_2R.Ninfo(i,3)=cellstr(Name(1:2));
  50. R_2R.Ninfo(i,4)=cellstr(Name(1:3));
  51. end
  52. % preallocating
  53. R_2R.Param.Tm=Tm;
  54. R_2R.Param.Tbin=Tbin;
  55. R_2R.Param.Dura=Dura;
  56. R_2R.Param.Baseline=Baseline;
  57. R_2R.Param.PStat=PStat;
  58. R_2R.Param.MinNumTrials=MinNumTrials;
  59. R_2R.Param.path=path;
  60. R_2R.Param.prewin=prewin;
  61. R_2R.Param.postwin=postwin;
  62. R_2R.Param.SmoothTYPE=SmoothTYPE;
  63. R_2R.Param.SmoothSPAN=SmoothSPAN;
  64. R_2R.Param.BinBase=BinBase;
  65. R_2R.Param.BinDura=BinDura;
  66. R_2R.Param.bins = bins;
  67. R_2R.Param.binint = binint;
  68. R_2R.Param.binstart = binstart;
  69. for k=1:length(Erefnames)
  70. R_2R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  71. R_2R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  72. R_2R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
  73. R_2R.Ev(k).Meanz(1:Nneurons,1)=NaN;
  74. R_2R.Ev(k).BW(1:Nneurons,1)=NaN;
  75. R_2R.Ev(k).signrank(1:Nneurons,1)=NaN;
  76. R_2R.Ev(k).RespDir(1:Nneurons,1)=NaN;
  77. R_2R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
  78. end
  79. % runs the main routine
  80. for i=1:length(RAW) %loops through sessions
  81. if strcmp('NA',RAW(i).Type(1:2)) | strcmp('VP',RAW(i).Type(1:2))
  82. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  83. NN=NN+1; %neuron counter
  84. for k=1:length(Erefnames) %loops thorough the events
  85. EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
  86. if sum(EvInd)==0
  87. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  88. end
  89. R_2R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
  90. Tbase=prewin(k,1):BSIZE:prewin(k,2);
  91. if ~isempty(EvInd) && R_2R.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
  92. [PSR0,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{1});% makes collapsed rasters for baseline for use in normalizing
  93. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  94. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  95. [PSR3,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{2});% makes trial by trial rasters for baseline for use in response detection
  96. if ~isempty(PSR0{1}) || ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  97. %Optimal bin size
  98. %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
  99. %[PTH0,~,~]=MakePTH07(PSR0,repmat(N0, size(RAW(i).Nrast{j},1),1),{1,0,BW1});%BW1 reinjected here to make sure PTH0 & PTH1 have the same BW
  100. %Fixed bin size
  101. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----Fixed bin used here
  102. [PTH0,~,~]=MakePTH07(PSR0,repmat(N0, size(RAW(i).Nrast{j},1),1),{1,0,BinSize});%BW1 reinjected here to make sure PTH0 & PTH1 have the same BW
  103. PTH1=smooth(PTH1,SmoothSPAN,SmoothTYPE)';
  104. PTH0=smooth(PTH0,SmoothSPAN,SmoothTYPE)';
  105. %------------- Fills the R_2R.Ev(k) fields --------------
  106. R_2R.Ev(k).BW(NN,1)=BW1;
  107. R_2R.Ev(k).PSTHraw(NN,1:length(Tm))=PTH1;
  108. R_2R.Ev(k).Meanraw(NN,1)=nanmean(R_2R.Ev(k).PSTHraw(NN,Tm>postwin(k,1) & Tm<postwin(k,2)),2);
  109. if sum(PTH0,2)~=0
  110. R_2R.Ev(k).PSTHz(NN,1:length(Tm))=normalize(PTH1,PTH0,0);
  111. R_2R.Ev(k).Meanz(NN,1)=nanmean(R_2R.Ev(k).PSTHz(NN,Tm>postwin(k,1) & Tm<postwin(k,2)),2);
  112. else
  113. R_2R.Ev(k).PSTHz(NN,1:length(Tm))=NaN(1,length(Tm));
  114. R_2R.Ev(k).Meanz(NN,1)=NaN;
  115. end
  116. %------------------ firing (in Hz) per trial in pre/post windows ------------------
  117. %used to make the between events comparisons and Response detection in a single window----
  118. ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
  119. ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
  120. for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
  121. ev(k).pre(m)=sum(PSR3{m}<prewin(k,2) & PSR3{m}>prewin(k,1))/(prewin(k,2)-prewin(k,1)); %CHANGED FROM PSR2 to PSR0 here 10/24/17
  122. ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
  123. end
  124. %-------------------- signrank on event and direction----
  125. [R_2R.Ev(k).signrank(NN,1),~]=signrank(ev(k).pre, ev(k).post); %Signrank used here because it is a dependant sample test
  126. if R_2R.Ev(k).signrank(NN,1)<PStat
  127. R_2R.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
  128. else R_2R.Ev(k).RespDir(NN,1)=0;
  129. end
  130. end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
  131. end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
  132. end %Events
  133. %----------------------- Check for difference in cue responding after sucrose and maltodextrin trials -----------------------
  134. CueP1=strcmp('CueP1', Erefnames);
  135. CueP2=strcmp('CueP2', Erefnames);
  136. [R_2R.CueP12Stat(NN,1),~]=ranksum(ev(CueP1).post,ev(CueP2).post); %Ranksum test used becasue it is an independent sample test
  137. %----------------------- Check for difference in PE responding after sucrose and maltodextrin trials -----------------------
  138. PEP1=strcmp('PEP1', Erefnames);
  139. PEP2=strcmp('PEP2', Erefnames);
  140. [R_2R.PEP12Stat(NN,1),~]=ranksum(ev(PEP1).post,ev(PEP1).post); %Ranksum test used becasue it is an independent sample test
  141. %----------------------- Check for reward modulation across all the bins surrounding reward delivery-----------------------
  142. BHz=0;
  143. Trial=0;
  144. Reward=0;
  145. EV1=strmatch('RD1',RAW(i).Einfo(:,2),'exact');
  146. EV2=strmatch('RD2',RAW(i).Einfo(:,2),'exact');
  147. %get mean baseline firing for all reward trials
  148. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV1},BinBase,{2});% makes trial by trial rasters for baseline
  149. for y= 1:B1n
  150. BHz(1,y)=sum(Bcell1{1,y}>BinBase(1))/(BinBase(1,2)-BinBase(1,1));
  151. Reward(1,y)=1;
  152. end
  153. [Bcell2,B2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV2},BinBase,{2});% makes trial by trial rasters for baseline
  154. for z= 1:B2n
  155. BHz(1,z+B1n)=sum(Bcell2{1,z}>BinBase(1))/(BinBase(1,2)-BinBase(1,1));
  156. Reward(1,z+B1n)=2;
  157. end
  158. for k=1:bins %go through each bin for each neuron
  159. EVHz=0;
  160. %get trial by trial firing rate for sucrose trials
  161. [EV1cell,EV1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV1},[(BinDura(1)+(binstart - binint)+k*binint) (BinDura(2)+(binstart - binint)+k*binint)],{2});% makes trial by trial rasters for event
  162. for y= 1:EV1n
  163. EVHz(1,y)=sum(EV1cell{1,y}>binstart)/(BinDura(1,2)-BinDura(1,1));
  164. end
  165. %get trial by trial firing rate for maltodextrin trials
  166. [EV2cell,EV2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV2},[(BinDura(1)+(binstart - binint)+k*binint) (BinDura(2)+(binstart - binint)+k*binint)],{2});% makes trial by trial rasters for event
  167. for z= 1:EV2n
  168. EVHz(1,z+EV1n)=sum(EV2cell{1,z}>binstart)/(BinDura(1,2)-BinDura(1,1));
  169. end
  170. %run ANOVA on the effects of pre vs post, cued or not,
  171. %kind of reward, and the interactions
  172. [~,Bintbl,~]=anovan([BHz EVHz],{cat(2,zeros(1,length(Reward)),ones(1,length(Reward))),[Reward Reward]},'varnames',{'Pre vs Post','Reward'},'model','full','display','off');
  173. R_2R.BinStatRwrd{k,1}(NN).IntSig=cell2mat(Bintbl(4,7));
  174. %get mean firing rates
  175. R_2R.BinStatRwrd{k,1}(NN).R1Mean=(nanmean(EVHz(Reward==1))-nanmean(BHz(Reward==1)))/std(BHz(Reward==1));
  176. R_2R.BinStatRwrd{k,1}(NN).R2Mean=(nanmean(EVHz(Reward==2))-nanmean(BHz(Reward==2)))/std(BHz(Reward==2));
  177. %signranks for checking if inhibitory or excitatory
  178. %sucrose trials
  179. signranksig=signrank(BHz(Reward==1), EVHz(Reward==1)); %Signrank used here because it is a dependant sample test
  180. if signranksig<PStat
  181. R_2R.BinStatRwrd{k,1}(NN).SucRespDir=sign(nanmean(EVHz(Reward==1))-mean(BHz(Reward==1)));
  182. else
  183. R_2R.BinStatRwrd{k,1}(NN).SucRespDir=0;
  184. end
  185. %maltodextrin trials
  186. signranksig=signrank(BHz(Reward==2), EVHz(Reward==2)); %Signrank used here because it is a dependant sample test
  187. if signranksig<PStat
  188. R_2R.BinStatRwrd{k,1}(NN).MalRespDir=sign(nanmean(EVHz(Reward==2))-mean(BHz(Reward==2)));
  189. else
  190. R_2R.BinStatRwrd{k,1}(NN).MalRespDir=0;
  191. end
  192. end %bins for bins analysis
  193. fprintf('2R Neuron #%d\n',NN);
  194. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  195. end %if it's the right kind of session
  196. end %sessions: FOR i=1:length(RAW)
  197. if SAVE_FLAG
  198. save('R_2R.mat','R_2R')
  199. end
  200. toc
  201. %% water and maltodextrin sessions
  202. %start fresh
  203. R_W=[];R_W.Ninfo={};NN=0;Nneurons=0;
  204. % List of events to analyze and analysis windows EXTRACTED from excel file
  205. path='C:\Users\dottenh2\Documents\MATLAB\David\2Rewards Nex Files\2R_paper.xls';
  206. [~,Erefnames]=xlsread(path,'Windows','a3:a15'); % cell that contains the event names
  207. prewin = xlsread(path,'Windows','b3:c15');
  208. postwin = xlsread(path,'Windows','d3:e15');
  209. %saves event names for reference later
  210. R_W.Erefnames=Erefnames;
  211. %Finds the total number of neurons in 2R and marks them by region/session
  212. for i=1:length(RAW)
  213. if strcmp('WA',RAW(i).Type(1:2))
  214. R_W.Ninfo=cat(1,R_W.Ninfo,RAW(i).Ninfo);
  215. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  216. end
  217. end
  218. for i=1:Nneurons
  219. Session=string(R_W.Ninfo(i,1));
  220. Name=char(Session);
  221. R_W.Ninfo(i,3)=cellstr(Name(1:2));
  222. R_W.Ninfo(i,4)=cellstr(Name(1:3));
  223. end
  224. % preallocating
  225. R_W.Param.Tm=Tm;
  226. R_W.Param.Tbin=Tbin;
  227. R_W.Param.Dura=Dura;
  228. R_W.Param.Baseline=Baseline;
  229. R_W.Param.PStat=PStat;
  230. R_W.Param.MinNumTrials=MinNumTrials;
  231. R_W.Param.path=path;
  232. R_W.Param.prewin=prewin;
  233. R_W.Param.postwin=postwin;
  234. R_W.Param.SmoothTYPE=SmoothTYPE;
  235. R_W.Param.SmoothSPAN=SmoothSPAN;
  236. R_W.Param.BinBase=BinBase;
  237. R_W.Param.BinDura=BinDura;
  238. R_W.Param.bins = bins;
  239. R_W.Param.binint = binint;
  240. R_W.Param.binstart = binstart;
  241. for k=1:length(Erefnames)
  242. R_W.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  243. R_W.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  244. R_W.Ev(k).Meanraw(1:Nneurons,1)=NaN;
  245. R_W.Ev(k).Meanz(1:Nneurons,1)=NaN;
  246. R_W.Ev(k).BW(1:Nneurons,1)=NaN;
  247. R_W.Ev(k).signrank(1:Nneurons,1)=NaN;
  248. R_W.Ev(k).RespDir(1:Nneurons,1)=NaN;
  249. R_W.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
  250. end
  251. % runs the main routine
  252. for i=1:length(RAW) %loops through sessions
  253. if strcmp('WA',RAW(i).Type(1:2))
  254. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  255. NN=NN+1; %neuron counter
  256. for k=1:length(Erefnames) %loops thorough the events
  257. EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
  258. if sum(EvInd)==0
  259. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  260. end
  261. R_W.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
  262. Tbase=prewin(k,1):BSIZE:prewin(k,2);
  263. if ~isempty(EvInd) && R_W.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
  264. [PSR0,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{1});% makes collapsed rasters for baseline for use in normalizing
  265. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  266. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  267. [PSR3,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{2});% makes trial by trial rasters for baseline for use in response detection
  268. if ~isempty(PSR0{1}) || ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  269. %optimal bin size
  270. %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
  271. %[PTH0,~,~]=MakePTH07(PSR0,repmat(N0, size(RAW(i).Nrast{j},1),1),{1,0,BW1});%BW1 reinjected here to make sure PTH0 & PTH1 have the same BW
  272. %Fixed bin size
  273. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----fixed bin size
  274. [PTH0,~,~]=MakePTH07(PSR0,repmat(N0, size(RAW(i).Nrast{j},1),1),{1,0,BinSize});%BW1 reinjected here to make sure PTH0 & PTH1 have the same BW
  275. PTH1=smooth(PTH1,SmoothSPAN,SmoothTYPE)';
  276. PTH0=smooth(PTH0,SmoothSPAN,SmoothTYPE)';
  277. %------------- Fills the R_W.Ev(k) fields --------------
  278. R_W.Ev(k).BW(NN,1)=BW1;
  279. R_W.Ev(k).PSTHraw(NN,1:length(Tm))=PTH1;
  280. R_W.Ev(k).Meanraw(NN,1)=nanmean(R_W.Ev(k).PSTHraw(NN,Tm>postwin(k,1) & Tm<postwin(k,2)),2);
  281. if sum(PTH0,2)~=0
  282. R_W.Ev(k).PSTHz(NN,1:length(Tm))=normalize(PTH1,PTH0,0);
  283. R_W.Ev(k).Meanz(NN,1)=nanmean(R_W.Ev(k).PSTHz(NN,Tm>postwin(k,1) & Tm<postwin(k,2)),2);
  284. else
  285. R_W.Ev(k).PSTHz(NN,1:length(Tm))=NaN(1,length(Tm));
  286. R_W.Ev(k).Meanz(NN,1)=NaN;
  287. end
  288. %------------------ firing (in Hz) per trial in pre/post windows ------------------
  289. %used to make the between events comparisons and Response detection in a single window----
  290. ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
  291. ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
  292. for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
  293. ev(k).pre(m)=sum(PSR3{m}<prewin(k,2) & PSR3{m}>prewin(k,1))/(prewin(k,2)-prewin(k,1)); %CHANGED FROM PSR2 to PSR0 here 10/24/17
  294. ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
  295. end
  296. %-------------------- signrank on event and direction----
  297. [R_W.Ev(k).signrank(NN,1),~]=signrank(ev(k).pre, ev(k).post); %Signrank used here because it is a dependant sample test
  298. if R_W.Ev(k).signrank(NN,1)<PStat
  299. R_W.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
  300. else R_W.Ev(k).RespDir(NN,1)=0;
  301. end
  302. end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
  303. end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
  304. end %Events
  305. %----------------------- Check for reward modulation across all the bins surrounding reward delivery-----------------------
  306. BHz=0;
  307. Trial=0;
  308. Reward=0;
  309. EV1=strmatch('RD1',RAW(i).Einfo(:,2),'exact');
  310. EV2=strmatch('RD2',RAW(i).Einfo(:,2),'exact');
  311. %get mean baseline firing for all reward trials
  312. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV1},BinBase,{2});% makes trial by trial rasters for baseline
  313. for y= 1:B1n
  314. BHz(1,y)=sum(Bcell1{1,y}>BinBase(1))/(BinBase(1,2)-BinBase(1,1));
  315. Reward(1,y)=1;
  316. end
  317. [Bcell2,B2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV2},BinBase,{2});% makes trial by trial rasters for baseline
  318. for z= 1:B2n
  319. BHz(1,z+B1n)=sum(Bcell2{1,z}>BinBase(1))/(BinBase(1,2)-BinBase(1,1));
  320. Reward(1,z+B1n)=2;
  321. end
  322. for k=1:bins %go through each bin for each neuron
  323. EVHz=0;
  324. %get trial by trial firing rate for water trials
  325. [EV1cell,EV1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV1},[(BinDura(1)+(binstart - binint)+k*binint) (BinDura(2)+(binstart - binint)+k*binint)],{2});% makes trial by trial rasters for event
  326. for y= 1:EV1n
  327. EVHz(1,y)=sum(EV1cell{1,y}>binstart)/(BinDura(1,2)-BinDura(1,1));
  328. end
  329. %get trial by trial firing rate for maltodextrin trials
  330. [EV2cell,EV2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV2},[(BinDura(1)+(binstart - binint)+k*binint) (BinDura(2)+(binstart - binint)+k*binint)],{2});% makes trial by trial rasters for event
  331. for z= 1:EV2n
  332. EVHz(1,z+EV1n)=sum(EV2cell{1,z}>binstart)/(BinDura(1,2)-BinDura(1,1));
  333. end
  334. %run ANOVA on the effects of pre vs post, cued or not,
  335. %kind of reward, and the interactions
  336. [~,Bintbl,~]=anovan([BHz EVHz],{cat(2,zeros(1,length(Reward)),ones(1,length(Reward))),[Reward Reward]},'varnames',{'Pre vs Post','Reward'},'model','full','display','off');
  337. R_W.BinStatRwrd{k,1}(NN).IntSig=cell2mat(Bintbl(4,7));
  338. %get mean firing rates
  339. R_W.BinStatRwrd{k,1}(NN).R1Mean=(nanmean(EVHz(Reward==1))-nanmean(BHz(Reward==1)))/std(BHz(Reward==1));
  340. R_W.BinStatRwrd{k,1}(NN).R2Mean=(nanmean(EVHz(Reward==2))-nanmean(BHz(Reward==2)))/std(BHz(Reward==2));
  341. %signranks for checking if inhibitory or excitatory
  342. %water trials
  343. signranksig=signrank(BHz(Reward==1), EVHz(Reward==1)); %Signrank used here because it is a dependant sample test
  344. if signranksig<PStat
  345. R_W.BinStatRwrd{k,1}(NN).WatRespDir=sign(nanmean(EVHz(Reward==1))-mean(BHz(Reward==1)));
  346. else
  347. R_W.BinStatRwrd{k,1}(NN).WatRespDir=0;
  348. end
  349. %maltodextrin trials
  350. signranksig=signrank(BHz(Reward==2), EVHz(Reward==2)); %Signrank used here because it is a dependant sample test
  351. if signranksig<PStat
  352. R_W.BinStatRwrd{k,1}(NN).MalRespDir=sign(nanmean(EVHz(Reward==2))-mean(BHz(Reward==2)));
  353. else
  354. R_W.BinStatRwrd{k,1}(NN).MalRespDir=0;
  355. end
  356. end %bins for bins analysis
  357. fprintf('Water neuron #%d\n',NN);
  358. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  359. end %if right kind of session
  360. end %sessions: FOR i=1:length(RAW)
  361. if SAVE_FLAG
  362. save('R_W.mat','R_W')
  363. end
  364. toc
  365. %% All three rewards sessions
  366. %make this lower because there are only 3 of some current/previous reward combinations
  367. MinNumTrials=3; %how many trials of event there need to be to conduct analysis
  368. %start fresh
  369. R_3R=[];R_3R.Ninfo={};NN=0;Nneurons=0;
  370. % List of events to analyze and analysis windows EXTRACTED from excel file
  371. path='C:\Users\dottenh2\Documents\MATLAB\David\2Rewards Nex Files\3R_paper.xls';
  372. [~,Erefnames]=xlsread(path,'Windows','a3:a23'); % cell that contains the event names
  373. prewin = xlsread(path,'Windows','b3:c23');
  374. postwin = xlsread(path,'Windows','d3:e23');
  375. %saves event names for reference later
  376. R_3R.Erefnames=Erefnames;
  377. %Finds the total number of neurons in 2R and marks them by region/session
  378. for i=1:length(RAW)
  379. if strcmp('TH',RAW(i).Type(1:2))
  380. R_3R.Ninfo=cat(1,R_3R.Ninfo,RAW(i).Ninfo);
  381. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  382. end
  383. end
  384. for i=1:Nneurons
  385. Session=string(R_3R.Ninfo(i,1));
  386. Name=char(Session);
  387. R_3R.Ninfo(i,3)=cellstr(Name(1:2));
  388. R_3R.Ninfo(i,4)=cellstr(Name(1:3));
  389. end
  390. % preallocating
  391. R_3R.Param.Tm=Tm;
  392. R_3R.Param.Tbin=Tbin;
  393. R_3R.Param.Dura=Dura;
  394. R_3R.Param.Baseline=Baseline;
  395. R_3R.Param.PStat=PStat;
  396. R_3R.Param.MinNumTrials=MinNumTrials;
  397. R_3R.Param.path=path;
  398. R_3R.Param.prewin=prewin;
  399. R_3R.Param.postwin=postwin;
  400. R_3R.Param.SmoothTYPE=SmoothTYPE;
  401. R_3R.Param.SmoothSPAN=SmoothSPAN;
  402. R_3R.Param.BinBase=BinBase;
  403. R_3R.Param.BinDura=BinDura;
  404. R_3R.Param.bins = bins;
  405. R_3R.Param.binint = binint;
  406. R_3R.Param.binstart = binstart;
  407. for k=1:length(Erefnames)
  408. R_3R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  409. R_3R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  410. R_3R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
  411. R_3R.Ev(k).Meanz(1:Nneurons,1)=NaN;
  412. R_3R.Ev(k).BW(1:Nneurons,1)=NaN;
  413. R_3R.Ev(k).signrank(1:Nneurons,1)=NaN;
  414. R_3R.Ev(k).RespDir(1:Nneurons,1)=NaN;
  415. R_3R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
  416. end
  417. % runs the main routine
  418. for i=1:length(RAW) %loops through sessions
  419. if strcmp('TH',RAW(i).Type(1:2))
  420. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  421. NN=NN+1; %neuron counter
  422. for k=1:length(Erefnames) %loops thorough the events
  423. EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
  424. if sum(EvInd)==0
  425. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  426. end
  427. R_3R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
  428. Tbase=prewin(k,1):BSIZE:prewin(k,2);
  429. if ~isempty(EvInd) && R_3R.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
  430. [PSR0,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{1});% makes collapsed rasters for baseline for use in normalizing
  431. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  432. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  433. [PSR3,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{2});% makes trial by trial rasters for baseline for use in response detection
  434. if ~isempty(PSR0{1}) || ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  435. %optimal bin size
  436. %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
  437. %[PTH0,~,~]=MakePTH07(PSR0,repmat(N0, size(RAW(i).Nrast{j},1),1),{1,0,BW1});%BW1 reinjected here to make sure PTH0 & PTH1 have the same BW
  438. %Fixed bin size
  439. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----Fixed bin used here
  440. [PTH0,~,~]=MakePTH07(PSR0,repmat(N0, size(RAW(i).Nrast{j},1),1),{1,0,BinSize});%BW1 reinjected here to make sure PTH0 & PTH1 have the same BW
  441. PTH1=smooth(PTH1,SmoothSPAN,SmoothTYPE)';
  442. PTH0=smooth(PTH0,SmoothSPAN,SmoothTYPE)';
  443. %------------- Fills the R_3R.Ev(k) fields --------------
  444. R_3R.Ev(k).BW(NN,1)=BW1;
  445. R_3R.Ev(k).PSTHraw(NN,1:length(Tm))=PTH1;
  446. R_3R.Ev(k).Meanraw(NN,1)=nanmean(R_3R.Ev(k).PSTHraw(NN,Tm>postwin(k,1) & Tm<postwin(k,2)),2);
  447. if sum(PTH0,2)~=0
  448. R_3R.Ev(k).PSTHz(NN,1:length(Tm))=normalize(PTH1,PTH0,0);
  449. R_3R.Ev(k).Meanz(NN,1)=nanmean(R_3R.Ev(k).PSTHz(NN,Tm>postwin(k,1) & Tm<postwin(k,2)),2);
  450. else
  451. R_3R.Ev(k).PSTHz(NN,1:length(Tm))=NaN(1,length(Tm));
  452. R_3R.Ev(k).Meanz(NN,1)=NaN;
  453. end
  454. %------------------ firing (in Hz) per trial in pre/post windows ------------------
  455. %used to make the between events comparisons and Response detection in a single window----
  456. ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
  457. ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
  458. for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
  459. ev(k).pre(m)=sum(PSR3{m}<prewin(k,2) & PSR3{m}>prewin(k,1))/(prewin(k,2)-prewin(k,1)); %CHANGED FROM PSR2 to PSR0 here 10/24/17
  460. ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
  461. end
  462. %-------------------- signrank on event and direction----
  463. [R_3R.Ev(k).signrank(NN,1),~]=signrank(ev(k).pre, ev(k).post); %Signrank used here because it is a dependant sample test
  464. if R_3R.Ev(k).signrank(NN,1)<PStat
  465. R_3R.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
  466. else R_3R.Ev(k).RespDir(NN,1)=0;
  467. end
  468. end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
  469. end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
  470. end %Events
  471. %----------------------- Check for reward modulation across all the bins surrounding reward delivery-----------------------
  472. EV1=strmatch('RD1',RAW(i).Einfo(:,2),'exact');
  473. EV2=strmatch('RD2',RAW(i).Einfo(:,2),'exact');
  474. EV3=strmatch('RD3',RAW(i).Einfo(:,2),'exact');
  475. Bcell1=0;
  476. Bcell2=0;
  477. Bcell3=0;
  478. BHz=0;
  479. Trial=0;
  480. Reward=0;
  481. %get mean baseline firing for all reward trials
  482. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV1},BinBase,{2});% makes trial by trial rasters for baseline
  483. for y= 1:B1n
  484. BHz(1,y)=sum(Bcell1{1,y}>BinBase(1))/(BinBase(1,2)-BinBase(1,1));
  485. Trial(1,y)=y;
  486. Reward(1,y)=1;
  487. end
  488. [Bcell2,B2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV2},BinBase,{2});% makes trial by trial rasters for baseline
  489. for z= 1:B2n
  490. BHz(1,z+B1n)=sum(Bcell2{1,z}>BinBase(1))/(BinBase(1,2)-BinBase(1,1));
  491. Trial(1,z+B1n)=z+B1n;
  492. Reward(1,z+B1n)=2;
  493. end
  494. [Bcell3,B3n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV3},BinBase,{2});% makes trial by trial rasters for baseline
  495. for x= 1:B3n
  496. BHz(1,x+B2n+B1n)=sum(Bcell3{1,x}>BinBase(1))/(BinBase(1,2)-BinBase(1,1));
  497. Trial(1,x+B2n+B1n)=x+B2n+B1n;
  498. Reward(1,x+B2n+B1n)=3;
  499. end
  500. for k=1:bins %go through each bin for each neuron
  501. EVHz=0;
  502. EV1cell=0;
  503. EV2cell=0;
  504. EV3Cell=0;
  505. %get trial by trial firing rate for water trials
  506. [EV1cell,EV1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV1},[(BinDura(1)+(binstart - binint)+k*binint) (BinDura(2)+(binstart - binint)+k*binint)],{2});% makes trial by trial rasters for event
  507. for y= 1:EV1n
  508. EVHz(1,y)=sum(EV1cell{1,y}>binstart)/(BinDura(1,2)-BinDura(1,1));
  509. end
  510. %get trial by trial firing rate for maltodextrin trials
  511. [EV2cell,EV2n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV2},[(BinDura(1)+(binstart - binint)+k*binint) (BinDura(2)+(binstart - binint)+k*binint)],{2});% makes trial by trial rasters for event
  512. for z= 1:EV2n
  513. EVHz(1,z+EV1n)=sum(EV2cell{1,z}>binstart)/(BinDura(1,2)-BinDura(1,1));
  514. end
  515. %get trial by trial firing rate for water trials
  516. [EV3cell,EV3n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EV3},[(BinDura(1)+(binstart - binint)+k*binint) (BinDura(2)+(binstart - binint)+k*binint)],{2});% makes trial by trial rasters for event
  517. for x= 1:EV3n
  518. EVHz(1,x+EV1n+EV2n)=sum(EV3cell{1,x}>binstart)/(BinDura(1,2)-BinDura(1,1));
  519. end
  520. [~,Bintbl,~]=anovan([BHz EVHz],{[Reward Reward],cat(2,zeros(1,length(Trial)),ones(1,length(Trial)))},'varnames',{'Reward','Pre vs Post'},'model','full','display','off');
  521. R_3R.BinStatRwrd{k,1}(NN).IntSig=cell2mat(Bintbl(4,7));
  522. %get mean firing rates
  523. R_3R.BinStatRwrd{k,1}(NN).R1Mean=(nanmean(EVHz(Reward==1))-nanmean(BHz(Reward==1)))/std(BHz(Reward==1));
  524. R_3R.BinStatRwrd{k,1}(NN).R2Mean=(nanmean(EVHz(Reward==2))-nanmean(BHz(Reward==2)))/std(BHz(Reward==2));
  525. R_3R.BinStatRwrd{k,1}(NN).R3Mean=(nanmean(EVHz(Reward==3))-nanmean(BHz(Reward==3)))/std(BHz(Reward==3));
  526. %signranks for checking if inhibitory or excitatory
  527. %sucrose trials
  528. signranksig=signrank(BHz(Reward==1), EVHz(Reward==1)); %Signrank used here because it is a dependant sample test
  529. if signranksig<PStat
  530. R_3R.BinStatRwrd{k,1}(NN).SucRespDir=sign(nanmean(EVHz(Reward==1))-mean(BHz(Reward==1)));
  531. else
  532. R_3R.BinStatRwrd{k,1}(NN).SucRespDir=0;
  533. end
  534. %maltodextrin trials
  535. signranksig=signrank(BHz(Reward==2), EVHz(Reward==2)); %Signrank used here because it is a dependant sample test
  536. if signranksig<PStat
  537. R_3R.BinStatRwrd{k,1}(NN).MalRespDir=sign(nanmean(EVHz(Reward==2))-mean(BHz(Reward==2)));
  538. else
  539. R_3R.BinStatRwrd{k,1}(NN).MalRespDir=0;
  540. end
  541. %water trials
  542. signranksig=signrank(BHz(Reward==3), EVHz(Reward==3)); %Signrank used here because it is a dependant sample test
  543. if signranksig<PStat
  544. R_3R.BinStatRwrd{k,1}(NN).WatRespDir=sign(nanmean(EVHz(Reward==3))-mean(BHz(Reward==3)));
  545. else
  546. R_3R.BinStatRwrd{k,1}(NN).WatRespDir=0;
  547. end
  548. end %bins for bins analysis
  549. fprintf('3R neuron #%d\n',NN);
  550. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  551. end %if right kind of session
  552. end %sessions: FOR i=1:length(RAW)
  553. if SAVE_FLAG
  554. save('R_3R.mat','R_3R')
  555. end
  556. toc