A_eventAnalysis_EarlyTrain.m 14 KB

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