b_EventAnalysis.m 33 KB

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