A_DataProcessing_DT5.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. %%
  2. clear all; clc;
  3. global Dura Baseline Tm Tbase BSIZE Tbin DuraITI TmITI
  4. tic
  5. path='E:\Youna\Post-doc\MATLAB\Youna Matlab\DT Nex Files\RESULTdt.xls'; %excel file with windows etc
  6. %Main settings
  7. SAVE_FLAG=1;
  8. BSIZE=0.01; %Do not change
  9. Dura=[-25 20]; Tm=Dura(1):BSIZE:Dura(2);
  10. DuraITI=[-50 20]; TmITI=DuraITI(1):BSIZE:DuraITI(2);
  11. %Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
  12. PStat=0.01; %for comparing pre versus post windows, or event A versus event B
  13. MinNumTrials=10;
  14. R=[];R.Ninfo={};NN=0;
  15. Nneurons=[];
  16. Neur=0;
  17. BinSize=0.05;
  18. load('RAW_DT5.mat');
  19. RAW=RAW;
  20. %Smoothing
  21. Smoothing=1; %0 for raw and 1 for smoothing
  22. SmoothTYPE='lowess';
  23. SmoothSPAN=5;
  24. if Smoothing~=1, SmoothTYPE='NoSmoothing';SmoothSPAN=NaN; end
  25. % List of events to analyze and analysis windows EXTRACTED from excel file
  26. [~,Erefnames]=xlsread(path,'Windows','a3:a9'); % cell that contains the event names
  27. prewin = [-1 0];
  28. EventWin = xlsread(path,'Windows','l3:m9'); %to compute MeanZ in relevant window
  29. PreEventWin = xlsread(path,'Windows','d3:e9');
  30. PostEventWin = xlsread(path,'Windows','f3:g9');
  31. [~,Session] = xlsread(path,'Windows','c21:c30');
  32. %%
  33. R.Erefnames= Erefnames;
  34. R.Session= Session;
  35. SessionName = {RAW.SessionName};
  36. Ncum=0;
  37. %Finds the total number of neurons accross all sessions
  38. for i=1:length(Session) %loops through sessions
  39. Ses=strcmp(Session(i),SessionName);
  40. SesIndex=find(Ses==1);
  41. NN=0; Neur=0;
  42. R.Ses(i).Ninfo=[];
  43. for y=1:length(SesIndex)
  44. R.Ses(i).Ninfo=cat(1,R.Ses(i).Ninfo,RAW(SesIndex(y)).Ninfo);
  45. Neur=Neur+size(RAW(SesIndex(y)).Nrast,1);
  46. Nneurons(i)=Neur;
  47. end
  48. load('Coord_DT5.mat');
  49. R.Ses(i).Coord=part(i+3).Coord;
  50. R.Ses(i).Structure=part(i+3).Coord(:,4);
  51. R.Ses(i).Ninfo=cat(2, R.Ses(i).Ninfo, mat2cell([1:Nneurons(i)]',ones(1,Nneurons(i)),1));
  52. for k=1:length(Erefnames)
  53. R.Ses(i).Ev(k).PSTHraw(1:Nneurons(i),1:length(Tm))=NaN(Nneurons(i),length(Tm));
  54. R.Ses(i).Ev(k).PSTHrawBL(1:Nneurons(i),1:501)=NaN(Nneurons(i),501);
  55. R.Ses(i).Ev(k).PSTHz(1:Nneurons(i),1:length(Tm))=NaN(Nneurons(i),length(Tm));
  56. R.Ses(i).Ev(k).rawMeanz(1:Nneurons(i),1)=NaN;
  57. R.Ses(i).Ev(k).rawMeanzPre(1:Nneurons(i),1)=NaN;
  58. R.Ses(i).Ev(k).Meanraw(1:Nneurons(i),1)=NaN;
  59. R.Ses(i).Ev(k).Meanz(1:Nneurons(i),1)=NaN;
  60. R.Ses(i).Ev(k).MeanzPRE(1:Nneurons(i),1)=NaN;
  61. R.Ses(i).Ev(k).ttestPreEvent(1:Nneurons(i),1)=NaN;
  62. R.Ses(i).Ev(k).ttestPostEvent(1:Nneurons(i),1)=NaN;
  63. R.Ses(i).Ev(k).RespDirPre(1:Nneurons(i),1)=NaN;
  64. R.Ses(i).Ev(k).RespDirPost(1:Nneurons(i),1)=NaN;
  65. R.Ses(i).Ev(k).lowBL_marker(1:Nneurons(i),1)=NaN;
  66. R.Ses(i).Ev(k).NumberTrials(1:Nneurons(i),1)=NaN;
  67. end
  68. end
  69. % preallocating
  70. R.Param.Tm=Tm;
  71. R.Param.Tbin=Tbin;
  72. R.Param.Dura=Dura;
  73. R.Param.Baseline=Baseline;
  74. R.Param.PStat=PStat;
  75. R.Param.MinNumTrials=MinNumTrials;
  76. R.Param.path=path;
  77. R.Param.prewin=prewin;
  78. R.Param.postwinPreEvent=PreEventWin;
  79. R.Param.postwinPostEvent=PostEventWin;
  80. R.Param.SmoothTYPE=SmoothTYPE;
  81. R.Param.SmoothSPAN=SmoothSPAN;
  82. NS=0;
  83. TNN=0;
  84. %% runs the main routine
  85. for i=1:length(Session) %loops through sessions
  86. Ses=strcmp(Session(i),SessionName);
  87. SesIndex=find(Ses==1);
  88. NN=0;
  89. NS=NS+1;
  90. for y=1:length(SesIndex)
  91. for j= 1:size(RAW(SesIndex(y)).Nrast,1) %Number of neurons per session
  92. NN=NN+1; %neuron counter
  93. TNN=TNN+1; %neuron counter across all session
  94. if R.Ses(i).Structure(NN)~=0 %to avoid analyzing excluded neurons
  95. ev2=length(RAW(SesIndex(y)).Nrast{j})/RAW(SesIndex(y)).Nrast{j}(end);% FR computed across whole session.
  96. R.WF(TNN,:)=RAW(SesIndex(y)).waveforms(j,:);
  97. R.WFanalysis(TNN,1)=ev2; % mean FR computed across whole session.
  98. proportionISI=0;
  99. for k=3:1:size(RAW(SesIndex(y)).Nrast{j,1},1) % loop thru timestamps waveform
  100. ISIn=RAW(SesIndex(y)).Nrast{j,1}(k)-RAW(SesIndex(y)).Nrast{j,1}(k-1);
  101. ISIo=RAW(SesIndex(y)).Nrast{j,1}(k-1)-RAW(SesIndex(y)).Nrast{j,1}(k-2);
  102. calcul(k-2)=2*abs(ISIn-ISIo)/(ISIn+ISIo);
  103. if ISIo>0.5
  104. proportionISI=proportionISI+1;
  105. end
  106. end
  107. R.CV2(TNN,1)=mean(calcul);%CV2
  108. R.CV2(TNN,2)=100*proportionISI/(size(RAW(SesIndex(y)).Nrast{j,1},1)-1);% of ISI>0.5s
  109. R.SESSION(TNN,1)=NS;
  110. LeverInsertionInd=strcmp('LeverInsertion',RAW(SesIndex(y)).Einfo(:,2)); %Ref event for BL is lever insertion
  111. for k=1:length(Erefnames) %loops thorough the events
  112. EvInd=strcmp(Erefnames(k),RAW(SesIndex(y)).Einfo(:,2)); %find the event id number from RAW
  113. clear PreEventLeverIns
  114. EventTimestamp=RAW(SesIndex(y)).Erast{EvInd};
  115. LI=RAW(SesIndex(y)).Erast{LeverInsertionInd};
  116. if k<7 && ~isempty(EventTimestamp) %lever press or PE event
  117. for l=1:length(EventTimestamp) %loop thru trial
  118. PreEventLeverIns(l,1)=LI(find(LI(:,1)<EventTimestamp(l),1,'last'));
  119. end
  120. CellLevIns{1,1}=PreEventLeverIns;
  121. else
  122. CellLevIns{1,1}=RAW(SesIndex(y)).Erast{LeverInsertionInd};
  123. end
  124. if sum(EvInd)==0
  125. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  126. end
  127. R.Ses(i).Ev(k).NumberTrials(NN,1)=length(RAW(SesIndex(y)).Erast{EvInd});
  128. if ~isempty(EvInd) && R.Ses(i).Ev(k).NumberTrials(NN,1)>= MinNumTrials
  129. Tbase=prewin(1,1):BSIZE:prewin(1,2);
  130. [PSR0,N0]=MakePSR04(RAW(SesIndex(y)).Nrast(j),RAW(SesIndex(y)).Erast{LeverInsertionInd},prewin(1,:),{1});% makes collapsed rasters for baseline. BL=[-1 0] compared to LI
  131. [PSR1,N1]=MakePSR04(RAW(SesIndex(y)).Nrast(j),RAW(SesIndex(y)).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  132. [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)
  133. BLcell=MakePSR04(CellLevIns,RAW(SesIndex(y)).Erast{EvInd},[-1800 1],{2,'Last'});
  134. if ~isempty(PSR0{1}) || ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  135. %Fixed bin size
  136. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(SesIndex(y)).Nrast{j},1),1),{2,0,BinSize});%-----DP used here
  137. [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
  138. % calculate MeanZ from PSTH before smoothing
  139. if sum(PTH0,2)~=0
  140. unsmoothPSTHz(1,1:length(Tm))=normalize(PTH1,PTH0,0);
  141. R.Ses(i).Ev(k).rawMeanz(NN,1)=nanmean(unsmoothPSTHz(1,Tm>EventWin(k,1) & Tm<EventWin(k,2)),2);
  142. R.Ses(i).Ev(k).rawMeanzPre(NN,1)=nanmean(unsmoothPSTHz(1,Tm>PreEventWin(k,1) & Tm<PreEventWin(k,2)),2);
  143. else
  144. R.Ses(i).Ev(k).rawMeanz(NN,1)=NaN;
  145. R.Ses(i).Ev(k).rawMeanzPre(NN,1)=NaN;
  146. end
  147. PTH1=smooth(PTH1,SmoothSPAN,SmoothTYPE)';
  148. PTH0=smooth(PTH0,SmoothSPAN,SmoothTYPE)';
  149. %------------- Fills the R.Ses(i).Ev(k) fields --------------
  150. R.Ses(i).Ev(k).PSTHraw(NN,1:length(Tm))=PTH1;
  151. R.Ses(i).Ev(k).PSTHrawBL(NN,1:length(PTH0))=PTH0;
  152. 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);
  153. if sum(PTH0,2)~=0
  154. R.Ses(i).Ev(k).PSTHz(NN,1:length(Tm))=normalize(PTH1,PTH0,0);
  155. 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);
  156. 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);
  157. else
  158. R.Ses(i).Ev(k).PSTHz(NN,1:length(Tm))=NaN(1,length(Tm));
  159. R.Ses(i).Ev(k).Meanz(NN,1)=NaN;
  160. R.Ses(i).Ev(k).MeanzPRE(NN,1)=NaN;
  161. end
  162. %------------------ firing (in Hz) per trial in pre/post windows ------------------
  163. %used to make the between events comparisons and Response detection in a single window----
  164. ev(k).pre=NaN(size(RAW(SesIndex(y)).Erast{EvInd},1),1);
  165. ev(k).PreEvent=NaN(size(RAW(SesIndex(y)).Erast{EvInd},1),1);
  166. ev(k).PostEvent=NaN(size(RAW(SesIndex(y)).Erast{EvInd},1),1);
  167. for m=1:size(RAW(SesIndex(y)).Erast{EvInd},1) %loops through trials
  168. ev(k).pre(m)=sum(PSR2{m}<(BLcell{1,m}+prewin(1,2)) & PSR2{m}>(BLcell{1,m}+prewin(1,1)))/(prewin(1,2)-prewin(1,1));
  169. ev(k).PreEvent(m)=sum(PSR2{m}<PreEventWin(k,2) & PSR2{m}>PreEventWin(k,1))/(PreEventWin(k,2)-PreEventWin(k,1));
  170. ev(k).PostEvent(m)=sum(PSR2{m}<PostEventWin(k,2) & PSR2{m}>PostEventWin(k,1))/(PostEventWin(k,2)-PostEventWin(k,1));
  171. end
  172. %---------------------------Response detection w/ SignRank on pre/post windows---------------------------
  173. 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
  174. [~,R.Ses(i).Ev(k).ttestPreEvent(NN,1)]=signrank(ev(k).pre, ev(k).PreEvent); %Signrank used here because it is a dependant sample test
  175. if R.Ses(i).Ev(k).ttestPreEvent(NN,1)<PStat
  176. R.Ses(i).Ev(k).RespDirPre(NN,1)=sign(mean(ev(k).PreEvent)-mean(ev(k).pre));
  177. else R.Ses(i).Ev(k).RespDirPre(NN,1)=0;
  178. end
  179. [~,R.Ses(i).Ev(k).ttestPostEvent(NN,1)]=signrank(ev(k).pre, ev(k).PostEvent); %Signrank used here because it is a dependant sample test
  180. if R.Ses(i).Ev(k).ttestPostEvent(NN,1)<PStat
  181. R.Ses(i).Ev(k).RespDirPost(NN,1)=sign(mean(ev(k).PostEvent)-mean(ev(k).pre));
  182. else R.Ses(i).Ev(k).RespDirPost(NN,1)=0;
  183. end
  184. if R.Ses(i).Ev(k).RespDirPre(NN,1)>0 || R.Ses(i).Ev(k).RespDirPost(NN,1) >0
  185. Search=find(Tm>=PreEventWin(k,1) & Tm<=PostEventWin(k,2));
  186. [R.Ses(i).Ev(k).MaxVal(NN,1),MaxInd]=max(R.Ses(i).Ev(k).PSTHz(NN,Search));
  187. R.Ses(i).Ev(k).MaxTime(NN,1)=Tm(Search(1)+MaxInd-1);
  188. end
  189. else R.Ses(i).Ev(k).lowBL_marker(NN,1)=1;
  190. end
  191. end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
  192. end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
  193. end %Events
  194. fprintf('Neuron ID # %d\n',NN);
  195. elseif R.Ses(i).Structure(NN)~=0
  196. end %exclusion: IF R.Structure(NN)~=0 to avoid analyzing excluded neurons
  197. end %neurons: FOR j= 1:size(RAW(SesIndex(y)).Nrast,1)
  198. end %SesIndex
  199. end %sessions: FOR i=1:length(RAW)
  200. for k=1:size(R.Ses,2)
  201. tableTRN=[];
  202. for i=1:length(Erefnames)-1
  203. tableTRN(:,i)=R.Ses(k).Ev(i).RespDirPre(:,1);
  204. tableTRN(:,i+length(Erefnames)-1)=R.Ses(k).Ev(i).RespDirPost(:,1);
  205. end
  206. R.Ses(k).TRN(:,1)=nansum(abs(tableTRN),2);
  207. end
  208. save('R_DT5.mat','R');
  209. toc