d_SingleUnitInstrumentalAlcoholAnalysis.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. %%
  2. % Sept 26th, 2012: Added line 278. Now excludes Baseline calculations on excluded neurons
  3. % May 21st, 2012: Added the time of the max(PSTH) and the max for signigicant responses
  4. % May 15th, 2012: this version includes waveform analysis
  5. % May 9th, 2012: The baseline used to compute z-scores is matched with the prewindow matrix
  6. % May 9th, 2012: R.Erefnames added to avoid having to fetch it from the excel file
  7. % May 2nd, 2012: this version uses parametric tests (ttest/ttest2 instead of SignRank and Ranksum)
  8. % April 27th, 2012:
  9. % -handles both GoNoGo and 2Go exps
  10. % -uses the new cleaned-up ResDetectSignRank02 code
  11. % April 13th, 2012:
  12. % -uses a different way of excluding neurons (it skips the excluded neurons instead of filtering then afterwards
  13. % -R.Structure added that stores the brain region (DMS, CORE, SHELL)
  14. clear all; clc;
  15. global Dura Baseline Tm Tbase BSIZE Tbin
  16. tic
  17. path='RESULTppa.xls';
  18. load('RAWvpppaALL.mat')
  19. RAW=RAWvpppaALL;
  20. %Main settings
  21. SAVE_FLAG=1;
  22. BSIZE=0.01; %Do not change
  23. Dura=[-22 20]; Tm=Dura(1):BSIZE:Dura(2);
  24. %Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
  25. Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
  26. PStat=0.05; %for comparing pre versus post windows, or event A versus event B
  27. MinNumTrials=3;
  28. R=[];RvpppaALL.Ninfo={};NN=0;Nneurons=0;
  29. BinSize=0.02;
  30. %Smoothing
  31. Smoothing=1; %0 for raw and 1 for smoothing
  32. SmoothTYPE='lowess';
  33. SmoothSPAN=5;
  34. if Smoothing~=1, SmoothTYPE='NoSmoothing';SmoothSPAN=NaN; end
  35. % List of events to analyze and analysis windows EXTRACTED from excel file
  36. [~,Erefnames]=xlsread(path,'Windows','a3:a12'); % cell that contains the event names
  37. prewin = xlsread(path,'Windows','b3:c12');
  38. postwin = xlsread(path,'Windows','d3:e12');
  39. BLWin= xlsread(path,'Windows','f3:g12');
  40. RespWin= xlsread(path,'Windows','h3:i12');
  41. %Settings for Response detection w/ signrank
  42. WINin=[0.03,0.3]; %Onset and offset requirements in sec.
  43. %WINin=[-2 -4]; %negative values define onset requirement by the number of consecutive bins and not by duration
  44. CI=[.99,.99];
  45. %%
  46. %Finds the total number of neurons accross all sessions
  47. for i=1:length(RAW)
  48. RvpppaALL.Ninfo=cat(1,RvpppaALL.Ninfo,RAW(i).Ninfo);
  49. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  50. end
  51. load('CoordPPAall.mat');
  52. RvpppaALL.Coord=CoordPPAall;
  53. %RvpppaALL.Coord=ones(Nneurons,4); %comment this line and uncomment the 2 lines above when you have real coordinates.
  54. RvpppaALL.Structure=RvpppaALL.Coord(:,4);
  55. RvpppaALL.Ninfo=cat(2, RvpppaALL.Ninfo, mat2cell([1:Nneurons]',ones(1,Nneurons),1));
  56. RvpppaALL.Erefnames= Erefnames;
  57. RvpppaALL.Rat(1:Nneurons,1)=NaN;
  58. RvpppaALL.Session(1:Nneurons,1)=NaN;
  59. for i=1:length(RvpppaALL.Ninfo)
  60. RvpppaALL.Rat(i,1)=str2num(RvpppaALL.Ninfo{i,1}(3:5));
  61. RvpppaALL.Session(i,1)=str2num(RvpppaALL.Ninfo{i,1}(10:11));
  62. end
  63. % preallocating
  64. RvpppaALL.Param.Tm=Tm;
  65. RvpppaALL.Param.Tbin=Tbin;
  66. RvpppaALL.Param.Dura=Dura;
  67. RvpppaALL.Param.Baseline=Baseline;
  68. RvpppaALL.Param.PStat=PStat;
  69. RvpppaALL.Param.MinNumTrials=MinNumTrials;
  70. RvpppaALL.Param.path=path;
  71. RvpppaALL.Param.prewin=prewin;
  72. RvpppaALL.Param.postwin=postwin;
  73. RvpppaALL.Param.BLwin=BLWin;
  74. RvpppaALL.Param.RespWin=RespWin;
  75. RvpppaALL.Param.ResponseReq=WINin;
  76. RvpppaALL.Param.SmoothTYPE=SmoothTYPE;
  77. RvpppaALL.Param.SmoothSPAN=SmoothSPAN;
  78. for k=1:length(Erefnames)
  79. RvpppaALL.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  80. RvpppaALL.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  81. RvpppaALL.Ev(k).Meanraw(1:Nneurons,1)=NaN;
  82. RvpppaALL.Ev(k).Meanz(1:Nneurons,1)=NaN;
  83. RvpppaALL.Ev(k).BW(1:Nneurons,1)=NaN;
  84. RvpppaALL.Ev(k).ttest(1:Nneurons,1)=NaN;
  85. RvpppaALL.Ev(k).RespDir(1:Nneurons,1)=NaN;
  86. RvpppaALL.Ev(k).RFLAG(1:Nneurons,1)=NaN;
  87. RvpppaALL.Ev(k).CFLAG(1:Nneurons,1)=NaN;
  88. RvpppaALL.Ev(k).Onsets(1:Nneurons,:)=NaN(Nneurons,2);
  89. RvpppaALL.Ev(k).Offsets(1:Nneurons,:)=NaN(Nneurons,2);
  90. RvpppaALL.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
  91. RvpppaALL.Ev(k).MaxVal(1:Nneurons,1)=NaN;
  92. RvpppaALL.Ev(k).MaxTime(1:Nneurons,1)=NaN;
  93. end
  94. %% runs the main routine
  95. for i=1:length(RAW) %loops through sessions
  96. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  97. NN=NN+1; %neuron counter
  98. %if RvpppaALL.Structure(NN)~=0 %to avoid analyzing excluded neurons
  99. for k=1:length(Erefnames) %loops thorough the events
  100. EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
  101. if sum(EvInd)==0
  102. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  103. end
  104. RvpppaALL.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
  105. Tbase=prewin(k,1):BSIZE:prewin(k,2);
  106. if ~isempty(EvInd) && RvpppaALL.Ev(k).NumberTrials(NN,1)>MinNumTrials %avoid analyzing sessions where that do not have enough trials
  107. [PSR0,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{1});% makes collapsed rasters for baseline.
  108. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  109. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trail rasters. PSR1 is a cell(neurons, trials)
  110. if ~isempty(PSR0{1}) || ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  111. %optimal bin size
  112. % [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
  113. % [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
  114. %Fixed bin size
  115. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----DP used here
  116. [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
  117. %PTH1=smooth(PTH1,SmoothSPAN,SmoothTYPE)';
  118. %PTH0=smooth(PTH0,SmoothSPAN,SmoothTYPE)';
  119. %------------- Fills the RvpppaALL.Ev(k) fields --------------
  120. RvpppaALL.Ev(k).BW(NN,1)=BW1;
  121. RvpppaALL.Ev(k).PSTHraw(NN,1:length(Tm))=PTH1;
  122. RvpppaALL.Ev(k).Meanraw(NN,1)=nanmean(RvpppaALL.Ev(k).PSTHraw(NN,Tm>postwin(k,1) & Tm<postwin(k,2)),2);
  123. if sum(PTH0,2)~=0
  124. RvpppaALL.Ev(k).PSTHz(NN,1:length(Tm))=normalize(PTH1,PTH0,0);
  125. RvpppaALL.Ev(k).Meanz(NN,1)=nanmean(RvpppaALL.Ev(k).PSTHz(NN,Tm>postwin(k,1) & Tm<postwin(k,2)),2);
  126. else
  127. RvpppaALL.Ev(k).PSTHz(NN,1:length(Tm))=NaN(1,length(Tm));
  128. RvpppaALL.Ev(k).Meanz(NN,1)=NaN;
  129. end
  130. %------------------ firing (in Hz) per trial in pre/post windows ------------------
  131. %used to make the between events comparisons and Response detection in a single window----
  132. ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
  133. ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
  134. for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
  135. ev(k).pre(m)=sum(PSR2{m}<prewin(k,2) & PSR2{m}>prewin(k,1))/(prewin(k,2)-prewin(k,1));
  136. ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
  137. end
  138. %---------------------------Response detection w/ SignRank on pre/post windows---------------------------
  139. [~,RvpppaALL.Ev(k).ttest(NN,1)]=ttest(ev(k).pre, ev(k).post); %Signrank used here because it is a dependant sample test
  140. if RvpppaALL.Ev(k).ttest(NN,1)<PStat
  141. RvpppaALL.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
  142. if RvpppaALL.Ev(k).RespDir(NN,1)>0
  143. Search=find(Tm>=postwin(k,1) & Tm<=postwin(k,2));
  144. [RvpppaALL.Ev(k).MaxVal(NN,1),MaxInd]=max(RvpppaALL.Ev(k).PSTHraw(NN,Search));
  145. RvpppaALL.Ev(k).MaxTime(NN,1)=Tm(Search(1)+MaxInd-1);
  146. else
  147. Search=find(Tm>=postwin(k,1) & Tm<=postwin(k,2));
  148. [RvpppaALL.Ev(k).MaxVal(NN,1),MaxInd]=min(RvpppaALL.Ev(k).PSTHraw(NN,Search));
  149. RvpppaALL.Ev(k).MaxTime(NN,1)=Tm(Search(1)+MaxInd-1);
  150. end
  151. else RvpppaALL.Ev(k).RespDir(NN,1)=0;
  152. end
  153. %---------------------------Response detection w/ RESDETECT08 on running windows---------------------------
  154. RD=ResDetect08(PTH1,PTH0,RespWin(k,:),0,WINin, CI);
  155. RvpppaALL.Ev(k).RFLAG(NN,1)=RD.RFLAG;
  156. RvpppaALL.Ev(k).CFLAG(NN,1)=RD.CFLAG;
  157. RvpppaALL.Ev(k).Onsets(NN,:)=RD.Onsets';
  158. RvpppaALL.Ev(k).Offsets(NN,:)=RD.Offsets';
  159. RvpppaALL.Param.Paramnames=RD.Paramnames;
  160. RvpppaALL.Param.RDParam=RD.Params;
  161. end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
  162. end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
  163. end %Events
  164. %----------------------- CUES -----------------------
  165. CSPlus=strcmp('CSPlus', Erefnames);
  166. CSMinus=strcmp('CSMinus', Erefnames);
  167. if sum(CSPlus)~=0 %To avoid this analysis is Go and NoGo cues are not included in the analysis
  168. if sum(CSPlus)~=0 & sum(CSMinus)~=0
  169. RvpppaALL.CScriteria=linspace(0, max([ev(CSPlus).post(:);ev(CSMinus).post(:)]),200);%Determine the criteria range
  170. RvpppaALL.CSprecriteria=linspace(0, max([ev(CSPlus).pre(:);ev(CSMinus).pre(:)]),200);%Determine the criteria range
  171. for s=1:length(RvpppaALL.CScriteria);
  172. RvpppaALL.CSfalsepos(NN,s)=sum(ev(CSMinus).post>=RvpppaALL.CScriteria(s))/length(ev(CSMinus).post); %Determine the probability that the baseline is greater than each criteria (x)
  173. RvpppaALL.CStruepos(NN,s)=sum(ev(CSPlus).post>=RvpppaALL.CScriteria(s))/length(ev(CSPlus).post);%Determine the probability that the post-window firing firing is greater than critera(y)
  174. RvpppaALL.CSprefalsepos(NN,s)=sum(ev(CSMinus).pre>=RvpppaALL.CScriteria(s))/length(ev(CSMinus).pre); %Determine the probability that the baseline is greater than each criteria (x)
  175. RvpppaALL.CSpretruepos(NN,s)=sum(ev(CSPlus).pre>=RvpppaALL.CScriteria(s))/length(ev(CSPlus).pre);%Determine the probability that the pre-window firing firing is greater than critera(y)
  176. end
  177. RvpppaALL.CSauROC(NN,1)=trapz(RvpppaALL.CSfalsepos(NN,:),RvpppaALL.CStruepos(NN,:));%Calculate area under the curve for this neuron for this event
  178. RvpppaALL.CSpreauROC(NN,1)=trapz(RvpppaALL.CSprefalsepos(NN,:),RvpppaALL.CSpretruepos(NN,:));%Calculate area under the curve for this neuron for this event
  179. [RvpppaALL.CSStat(NN,1),~]=ranksum(ev(CSPlus).post,ev(CSMinus).post); %Ranksum test used becasue it is an independant sample test
  180. else RvpppaALL.CSStat(NN,1)=NaN;
  181. RvpppaALL.CSauROC(NN,1)=NaN;
  182. RvpppaALL.CSpreauROC(NN,1)=NaN;
  183. end
  184. end
  185. % PECSPlus=strcmp('PECSPlus', Erefnames);
  186. % PECSMinus=strcmp('PECSMinus', Erefnames);
  187. % if sum(PECSPlus)~=0 %To avoid this analysis is Go and NoGo cues are not included in the analysis
  188. % if RvpppaALL.Ev(PECSPlus).RespDir(NN,1)~=0 || RvpppaALL.Ev(PECSMinus).RespDir(NN,1)~=0
  189. % [RvpppaALL.PECueStat(NN,1),~]=ranksum(ev(PECSPlus).post,ev(PECSMinus).post); %Ranksum test used becasue it is an independant sample test
  190. % else RvpppaALL.PECueStat(NN,1)=NaN;
  191. % end
  192. % end
  193. FirstPE=strcmp('FirstPE', Erefnames);
  194. PortEntry=strcmp('PortEntry', Erefnames);
  195. if sum(FirstPE)~=0 %To avoid this analysis is Go and NoGo cues are not included in the analysis
  196. if RvpppaALL.Ev(FirstPE).RespDir(NN,1)~=0 || RvpppaALL.Ev(PortEntry).RespDir(NN,1)~=0
  197. [RvpppaALL.PERewStat(NN,1),~]=ranksum(ev(FirstPE).post,ev(PortEntry).post); %Ranksum test used becasue it is an independant sample test
  198. else RvpppaALL.PERewStat(NN,1)=NaN;
  199. end
  200. end
  201. % PECSMinus=strcmp('PECSMinus', Erefnames);
  202. % ITIPE=strcmp('ITIPE', Erefnames);
  203. % if sum(PECSMinus)~=0 %To avoid this analysis is Go and NoGo cues are not included in the analysis
  204. % if RvpppaALL.Ev(PECSMinus).RespDir(NN,1)~=0 || RvpppaALL.Ev(ITIPE).RespDir(NN,1)~=0
  205. % [RvpppaALL.PEUnrewCueStat(NN,1),~]=ranksum(ev(PECSMinus).post,ev(ITIPE).post); %Ranksum test used becasue it is an independant sample test
  206. % else RvpppaALL.PEUnrewCueStat(NN,1)=NaN;
  207. % end
  208. % end
  209. CSPluswPE=strcmp('CSPluswPE', Erefnames);
  210. CSPlusnoPE=strcmp('CSPlusnoPE', Erefnames);
  211. if sum(CSPlusnoPE)~=0 %To avoid this analysis is Go and NoGo cues are not included in the analysis
  212. if sum(CSPlusnoPE)~=0 & sum(CSPluswPE)~=0
  213. RvpppaALL.CSrespcriteria=linspace(0, max([ev(CSPlusnoPE).post(:);ev(CSPluswPE).post(:)]),200);%Determine the criteria range
  214. RvpppaALL.CSprerespcriteria=linspace(0, max([ev(CSPlusnoPE).pre(:);ev(CSPluswPE).pre(:)]),200);%Determine the criteria range
  215. for s=1:length(RvpppaALL.CSrespcriteria);
  216. RvpppaALL.CSrespfalsepos(NN,s)=sum(ev(CSPlusnoPE).post>=RvpppaALL.CSrespcriteria(s))/length(ev(CSPlusnoPE).post); %Determine the probability that the baseline is greater than each criteria (x)
  217. RvpppaALL.CSresptruepos(NN,s)=sum(ev(CSPluswPE).post>=RvpppaALL.CSrespcriteria(s))/length(ev(CSPluswPE).post);%Determine the probability that the post-window firing firing is greater than critera(y)
  218. RvpppaALL.CSprerespfalsepos(NN,s)=sum(ev(CSPlusnoPE).pre>=RvpppaALL.CSrespcriteria(s))/length(ev(CSPlusnoPE).pre); %Determine the probability that the baseline is greater than each criteria (x)
  219. RvpppaALL.CSpreresptruepos(NN,s)=sum(ev(CSPluswPE).pre>=RvpppaALL.CSrespcriteria(s))/length(ev(CSPluswPE).pre);%Determine the probability that the post-window firing firing is greater than critera(y)
  220. end
  221. RvpppaALL.CSrespauROC(NN,1)=trapz(RvpppaALL.CSrespfalsepos(NN,:),RvpppaALL.CSresptruepos(NN,:));%Calculate area under the curve for this neuron for this event
  222. RvpppaALL.CSprerespauROC(NN,1)=trapz(RvpppaALL.CSprerespfalsepos(NN,:),RvpppaALL.CSpreresptruepos(NN,:));
  223. [RvpppaALL.CSPlusResponseStat(NN,1),~]=ranksum(ev(CSPluswPE).post,ev(CSPlusnoPE).post); %Ranksum test used becasue it is an independant sample test
  224. else RvpppaALL.CSrespauROC(NN,1)=NaN;
  225. RvpppaALL.CSPlusResponseStat(NN,1)=NaN;
  226. RvpppaALL.CSprerespauROC(NN,1)=NaN;
  227. end
  228. end
  229. %
  230. CSMinuswPE=strcmp('CSMinuswPE', Erefnames);
  231. CSMinusnoPE=strcmp('CSMinusnoPE', Erefnames);
  232. % if sum(CSMinuswPE)~=0 %To avoid this analysis is Go and NoGo cues are not included in the analysis
  233. % if RvpppaALL.Ev(CSMinuswPE).RespDir(NN,1)~=0 || RvpppaALL.Ev(CSMinusnoPE).RespDir(NN,1)~=0
  234. % [RvpppaALL.CSMinusResponseStat(NN,1),~]=ranksum(ev(CSMinuswPE).post,ev(CSMinusnoPE).post); %Ranksum test used becasue it is an independant sample test
  235. % else RvpppaALL.CSMinusResponseStat(NN,1)=NaN;
  236. % end
  237. % end
  238. RvpppaALL.CSPlusRatio(NN,1)=RvpppaALL.Ev(3).NumberTrials(NN)/length(ev(CSPlus).post);
  239. RvpppaALL.CSMinusRatio(NN,1)=RvpppaALL.Ev(5).NumberTrials(NN)/length(ev(CSMinus).post);
  240. %
  241. % if NN==673
  242. % fprintf('Neuron ID # %d\n',NN);
  243. % fprintf('CS Minus Response Number ')% length(ev(CSMinuswPE).post))
  244. % fprintf('CS Minus Number ')% length(ev(CSMinus.post)))
  245. % return
  246. % else
  247. fprintf('Neuron ID # %d\n',NN);
  248. % end
  249. %elseif RvpppaALL.Structure(NN)~=0
  250. %end %exclusion: IF RvpppaALL.Structure(NN)~=0 to avoid analyzing excluded neurons
  251. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  252. end %sessions: FOR i=1:length(RAW)
  253. RvpppaALL.CSfalseposMEAN(1,:)=nanmean(RvpppaALL.CSfalsepos(RvpppaALL.Structure==10,:));
  254. RvpppaALL.CSfalseposSEM(1,:)=nanste(RvpppaALL.CSfalsepos(RvpppaALL.Structure==10,:),1);
  255. RvpppaALL.CStrueposMEAN(1,:)=nanmean(RvpppaALL.CStruepos(RvpppaALL.Structure==10,:));
  256. RvpppaALL.CStrueposSEM(1,:)=nanste(RvpppaALL.CStruepos(RvpppaALL.Structure==10,:),1);
  257. RvpppaALL.CSrespfalseposMEAN(1,:)=nanmean(RvpppaALL.CSrespfalsepos(RvpppaALL.Structure==10,:));
  258. RvpppaALL.CSrespfalseposSEM(1,:)=nanste(RvpppaALL.CSrespfalsepos(RvpppaALL.Structure==10,:),1);
  259. RvpppaALL.CSresptrueposMEAN(1,:)=nanmean(RvpppaALL.CSresptruepos(RvpppaALL.Structure==10,:));
  260. RvpppaALL.CSresptrueposSEM(1,:)=nanste(RvpppaALL.CSresptruepos(RvpppaALL.Structure==10,:),1);
  261. if SAVE_FLAG
  262. save('RvpppaALL.mat','RvpppaALL')
  263. end
  264. toc