A_eventAnalysis_ExtTrain.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. %% initial analysis of event-evoked activity - Extended training dataset - neurons pooled together
  2. % need RAW datafiles and Coord datafile with electrode placement
  3. % need to save excel sheets in the specified folder
  4. % the excel sheets say which events to make PSTHs for and what windows to
  5. % use for statistical testing
  6. %clear all; clc;
  7. clear all; clc;
  8. global Dura Baseline Tm Tbase BSIZE Tbin DuraITI TmITI
  9. tic
  10. path='C:\Users\yvandae1\Documents\MATLAB\DT Nex Files\RESULTdt.xls'; %excel file with windows etc
  11. load('RAWextendedtraining.mat')
  12. RAW=RAW;
  13. %Main settings
  14. SAVE_FLAG=1;
  15. FLAG_BL=0; %1 to use ITI for BL, 0 to use the pre-event window
  16. BSIZE=0.01; %Do not change
  17. Dura=[-25 20]; Tm=Dura(1):BSIZE:Dura(2);
  18. DuraITI=[-50 20]; TmITI=DuraITI(1):BSIZE:DuraITI(2);
  19. %Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
  20. Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
  21. PStat=0.05; %for comparing pre versus post windows, or event A versus event B
  22. MinNumTrials=10;
  23. R=[];R.Ninfo={};NN=0;Nneurons=0;
  24. BinSize=0.050;
  25. %Smoothing
  26. Smoothing=1; %0 for raw and 1 for smoothing
  27. SmoothTYPE='lowess';
  28. SmoothSPAN=5;
  29. if Smoothing~=1, SmoothTYPE='NoSmoothing';SmoothSPAN=NaN; end
  30. % List of events to analyze and analysis windows EXTRACTED from excel file
  31. [~,Erefnames]=xlsread(path,'Windows','a3:a15'); % cell that contains the event names
  32. prewin = xlsread(path,'Windows','b3:c15');
  33. PreEventWin = xlsread(path,'Windows','d3:e15');
  34. PostEventWin = xlsread(path,'Windows','f3:g15');
  35. EventWin = xlsread(path,'Windows','l3:m15');
  36. BLWin= xlsread(path,'Windows','h3:i15');
  37. RespWin= xlsread(path,'Windows','j3:k15');
  38. %Settings for Response detection w/ signrank
  39. WINin=[0.03,0.1]; %Onset and offset requirements in sec.
  40. %WINin=[-2 -4]; %negative values define onset requirement by the number of consecutive bins and not by duration
  41. CI=[.95,.95];
  42. %%
  43. %Finds the total number of neurons accross all sessions
  44. for i=1:length(RAW)
  45. R.Ninfo=cat(1,R.Ninfo,RAW(i).Ninfo);
  46. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  47. end
  48. load('Coord_extendedtraining.mat');
  49. R.Coord=Coord;
  50. %R.Coord=ones(Nneurons,4); %comment this line and uncomment the 2 lines above when you have real coordinates.
  51. R.Structure=R.Coord(:,4);
  52. R.Ninfo=cat(2, R.Ninfo, mat2cell([1:Nneurons]',ones(1,Nneurons),1));
  53. R.Sinfo=[];
  54. for i=1:Nneurons
  55. fname=char(R.Ninfo(i,1));
  56. R.Sinfo=cat(1,R.Sinfo,cellstr(fname(11:end-4)));
  57. end
  58. R.Erefnames= Erefnames;
  59. % preallocating
  60. R.Param.Tm=Tm;
  61. R.Param.Tbin=Tbin;
  62. R.Param.Dura=Dura;
  63. R.Param.Baseline=Baseline;
  64. R.Param.PStat=PStat;
  65. R.Param.MinNumTrials=MinNumTrials;
  66. R.Param.path=path;
  67. R.Param.prewin=prewin;
  68. R.Param.postwinPreEvent=PreEventWin;
  69. R.Param.postwinPostEvent=PostEventWin;
  70. R.Param.BLwin=BLWin;
  71. R.Param.RespWin=RespWin;
  72. R.Param.ResponseReq=WINin;
  73. R.Param.SmoothTYPE=SmoothTYPE;
  74. R.Param.SmoothSPAN=SmoothSPAN;
  75. R.TRNevent(1:Nneurons,1:length(Erefnames))=NaN;
  76. for k=1:length(Erefnames)
  77. R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  78. R.Ev(k).PSTHrawBL(1:Nneurons,1:length(Tbase))=NaN(Nneurons,length(Tbase));
  79. R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  80. R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
  81. R.Ev(k).rawMeanz(1:Nneurons,1)=NaN;
  82. R.Ev(k).rawMeanzPre(1:Nneurons,1)=NaN;
  83. R.Ev(k).Meanz(1:Nneurons,1)=NaN;
  84. R.Ev(k).MeanzPRE(1:Nneurons,1)=NaN;
  85. R.Ev(k).ttestPreEvent(1:Nneurons,1)=NaN;
  86. R.Ev(k).ttestPostEvent(1:Nneurons,1)=NaN;
  87. R.Ev(k).ttestEvent(1:Nneurons,1)=NaN;
  88. R.Ev(k).RespDirPre(1:Nneurons,1)=NaN;
  89. R.Ev(k).RespDirPost(1:Nneurons,1)=NaN;
  90. R.Ev(k).RespDirEvent(1:Nneurons,1)=NaN;
  91. R.Ev(k).ActDirPre(1:Nneurons,1)=NaN;
  92. R.Ev(k).ActDirPost(1:Nneurons,1)=NaN;
  93. R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
  94. R.Ev(k).MaxVal(1:Nneurons,1)=NaN;
  95. R.Ev(k).MaxTime(1:Nneurons,1)=NaN;
  96. end
  97. %% runs the main routine
  98. for i=1:length(RAW) %loops through sessions
  99. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  100. NN=NN+1; %neuron counter
  101. if R.Structure(NN)~=0 %to avoid analyzing excluded neurons
  102. ev2=NaN(size(RAW(i).Eint{1,3},1),2);
  103. [PSR1iti,N1iti]=MakePSR0int(RAW(i).Nrast(j),RAW(i).Eint{1,1},RAW(i).Eint{1,2},{2},{1});% makes trial by trial rasters for baseline based on ITI
  104. [PSR1trial,N1trial]=MakePSR0int(RAW(i).Nrast(j),RAW(i).Eint{1,3},RAW(i).Eint{1,4},{2},{2});% makes trial by trial rasters for each trial based on time interval
  105. for l=1:length(RAW(i).Eint{1,3}) %loops through trials
  106. ev2(l,1)=sum(PSR1iti{l}<RAW(i).Eint{1,2}(l) & PSR1iti{l}>RAW(i).Eint{1,1}(l))/(RAW(i).Eint{1,2}(l)-RAW(i).Eint{1,1}(l)-20);
  107. ev2(l,2)=sum(PSR1trial{l}<RAW(i).Eint{1,4}(l) & PSR1trial{l}>RAW(i).Eint{1,3}(l))/(RAW(i).Eint{1,4}(l)-RAW(i).Eint{1,3}(l));
  108. end
  109. R.WF(NN,:)=RAW(i).waveforms(j,:);
  110. R.WFanalysis(NN,1)=mean(ev2(:,1)); % mean FR during Baseline in the middle of each ITI (first and last 10s excluded)
  111. proportionISI=0;
  112. for k=3:1:size(RAW(i).Nrast{j,1},1) % loop thru timestamps waveform
  113. ISIn=RAW(i).Nrast{j,1}(k)-RAW(i).Nrast{j,1}(k-1);
  114. ISIo=RAW(i).Nrast{j,1}(k-1)-RAW(i).Nrast{j,1}(k-2);
  115. calcul(k-2)=2*abs(ISIn-ISIo)/(ISIn+ISIo);
  116. if ISIo>2
  117. proportionISI=proportionISI+1;
  118. end
  119. end
  120. R.CV2(NN,1)=mean(calcul);
  121. R.CV2(NN,2)=100*proportionISI/(size(RAW(i).Nrast{j,1},1)-1);% of ISI>2s
  122. for k=1:length(Erefnames) %loops thorough the events
  123. EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
  124. if sum(EvInd)==0
  125. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  126. end
  127. R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
  128. Tbase=prewin(k,1):BSIZE:prewin(k,2);
  129. [PSR0,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{1});% makes collapsed rasters for baseline.
  130. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  131. % if strcmp(Erefnames(k),'BaselineITI')
  132. % [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},DuraITI,{2});% makes trial by trail rasters for BL based on ITI. PSR1 is a cell(neurons, trials)
  133. % else
  134. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trail rasters. PSR1 is a cell(neurons, trials)
  135. if ~isempty(PSR0{1}) || ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  136. %Fixed bin size
  137. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----DP used here
  138. [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
  139. % calculate MeanZ from PSTH before smoothing
  140. if sum(PTH0,2)~=0
  141. unsmoothPSTHz(1,1:length(Tm))=normalize(PTH1,PTH0,0);
  142. R.Ev(k).rawMeanz(NN,1)=nanmean(unsmoothPSTHz(1,Tm>EventWin(k,1) & Tm<EventWin(k,2)),2);
  143. R.Ev(k).rawMeanzPre(NN,1)=nanmean(unsmoothPSTHz(1,Tm>PreEventWin(k,1) & Tm<PreEventWin(k,2)),2);
  144. else
  145. R.Ev(k).rawMeanz(NN,1)=NaN;
  146. R.Ev(k).rawMeanzPre(NN,1)=NaN;
  147. end
  148. PTH1=smooth(PTH1,SmoothSPAN,SmoothTYPE)';
  149. PTH0=smooth(PTH0,SmoothSPAN,SmoothTYPE)';
  150. %------------- Fills the R.Ev(k) fields --------------
  151. %R.Ev(k).BW(NN,1)=BW1;
  152. R.Ev(k).PSTHraw(NN,1:length(Tm))=PTH1;
  153. R.Ev(k).PSTHrawBL(NN,1:length(Tbase))=PTH0;
  154. R.Ev(k).Meanraw(NN,1)=nanmean(R.Ev(k).PSTHraw(NN,Tm>PostEventWin(k,1) & Tm<PostEventWin(k,2)),2);
  155. if sum(PTH0,2)~=0
  156. R.Ev(k).PSTHz(NN,1:length(Tm))=normalize(PTH1,PTH0,0);
  157. R.Ev(k).Meanz(NN,1)=nanmean(R.Ev(k).PSTHz(NN,Tm>EventWin(k,1) & Tm<EventWin(k,2)),2);
  158. R.Ev(k).MeanzPRE(NN,1)=nanmean(R.Ev(k).PSTHz(NN,Tm>PreEventWin(k,1) & Tm<PreEventWin(k,2)),2);
  159. else
  160. R.Ev(k).PSTHz(NN,1:length(Tm))=NaN(1,length(Tm));
  161. R.Ev(k).Meanz(NN,1)=NaN;
  162. R.Ev(k).MeanzPRE(NN,1)=NaN;
  163. end
  164. %------------------ firing (in Hz) per trial in pre/post windows ------------------
  165. %used to make the between events comparisons and Response detection in a single window----
  166. ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
  167. ev(k).PreEvent=NaN(size(RAW(i).Erast{EvInd},1),1);
  168. ev(k).PostEvent=NaN(size(RAW(i).Erast{EvInd},1),1);
  169. ev(k).Event=NaN(size(RAW(i).Erast{EvInd},1),1);
  170. for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
  171. ev(k).pre(m)=sum(PSR2{m}<prewin(k,2) & PSR2{m}>prewin(k,1))/(prewin(k,2)-prewin(k,1));
  172. ev(k).PreEvent(m)=sum(PSR2{m}<PreEventWin(k,2) & PSR2{m}>PreEventWin(k,1))/(PreEventWin(k,2)-PreEventWin(k,1));
  173. ev(k).PostEvent(m)=sum(PSR2{m}<PostEventWin(k,2) & PSR2{m}>PostEventWin(k,1))/(PostEventWin(k,2)-PostEventWin(k,1));
  174. ev(k).Event(m)=sum(PSR2{m}<EventWin(k,2) & PSR2{m}>EventWin(k,1))/(EventWin(k,2)-EventWin(k,1));
  175. end
  176. %---------------------------Response detection w/ SignRank on pre/post windows---------------------------
  177. if ~isempty(EvInd) && R.Ev(k).NumberTrials(NN,1)>= MinNumTrials && nanmean(ev(k).pre(:,1),1)>0.5 %avoid analyzing sessions that do not have enough trials / exclude low firing neurons
  178. [~,R.Ev(k).ttestPreEvent(NN,1)]=ttest(ev(k).pre, ev(k).PreEvent); %Signrank used here because it is a dependant sample test
  179. if R.Ev(k).ttestPreEvent(NN,1)<PStat
  180. R.Ev(k).RespDirPre(NN,1)=sign(mean(ev(k).PreEvent)-mean(ev(k).pre));
  181. else R.Ev(k).RespDirPre(NN,1)=0;
  182. end
  183. [~,R.Ev(k).ttestPostEvent(NN,1)]=ttest(ev(k).pre, ev(k).PostEvent); %Signrank used here because it is a dependant sample test
  184. if R.Ev(k).ttestPostEvent(NN,1)<PStat
  185. R.Ev(k).RespDirPost(NN,1)=sign(mean(ev(k).PostEvent)-mean(ev(k).pre));
  186. else R.Ev(k).RespDirPost(NN,1)=0;
  187. end
  188. [~,R.Ev(k).ttestEvent(NN,1)]=ttest(ev(k).pre, ev(k).Event); %Signrank used here because it is a dependant sample test
  189. if R.Ev(k).ttestEvent(NN,1)<PStat
  190. R.Ev(k).RespDirEvent(NN,1)=sign(mean(ev(k).Event)-mean(ev(k).pre));
  191. else R.Ev(k).RespDirEvent(NN,1)=0;
  192. end
  193. if R.Ev(k).RespDirPre(NN,1)>0 || R.Ev(k).RespDirPost(NN,1) >0
  194. Search=find(Tm>=PreEventWin(k,1) & Tm<=PostEventWin(k,2));
  195. [R.Ev(k).MaxVal(NN,1),MaxInd]=max(R.Ev(k).PSTHraw(NN,Search));
  196. R.Ev(k).MaxTime(NN,1)=Tm(Search(1)+MaxInd-1);
  197. elseif R.Ev(k).RespDirPre(NN,1)<0 || R.Ev(k).RespDirPost(NN,1) <0
  198. Search=find(Tm>=PreEventWin(k,1) & Tm<=PostEventWin(k,2));
  199. [R.Ev(k).MaxVal(NN,1),MaxInd]=min(R.Ev(k).PSTHraw(NN,Search));
  200. R.Ev(k).MaxTime(NN,1)=Tm(Search(1)+MaxInd-1);
  201. end
  202. TRNeventPOST(NN,k)=R.Ev(k).RespDirPost(NN);
  203. TRNeventPRE(NN,k)=R.Ev(k).RespDirPre(NN);
  204. %---------------------------Response detection w/ RESDETECT08 on running windows---------------------------
  205. end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
  206. end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN OR low firing during baseline
  207. end %Events
  208. %%----------------------- STAT -----------------------
  209. fprintf('Neuron ID # %d\n',NN);
  210. elseif R.Structure(NN)~=0
  211. end %exclusion: IF R.Structure(NN)~=0 to avoid analyzing excluded neurons
  212. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  213. end %sessions: FOR i=1:length(RAW)
  214. TRNevent=[TRNeventPRE(:,1:6) TRNeventPOST(:,1:5) TRNeventPOST(:,7)];
  215. R.TRN(:,1)=sum(abs(TRNevent(NN,:)),2,'omitnan'); % if >0 : Task related responsive neurons (include excitation and inhibition)
  216. R.TRN(:,2)=sum(TRNevent(NN,:),2,'omitnan'); % if >0 : TRN excited. if < 0: TRN inhibited. may loose ambivalent neurons (both excited and inhib)
  217. if SAVE_FLAG
  218. save('Rextendedtraining.mat','R')
  219. end
  220. toc