PSTHsCued.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. %PSTHs and some simple analysis
  2. clear all; clc;
  3. global Dura Baseline Tm Tbase BSIZE Tbin
  4. tic
  5. if ~exist('RAWCued'), load ('RAWCued.mat'); end
  6. RAW=RAWCued;
  7. %Main settings
  8. SAVE_FLAG=1;
  9. BSIZE=0.01; %Do not change
  10. Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2);
  11. %Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
  12. Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
  13. Tbase=-11:BSIZE:-1; %for fixed baseline for z-score
  14. PStat=0.05; %for comparing pre versus post windows, or event A versus event B
  15. MinNumTrials=5;
  16. R=[];R.Ninfo={};NN=0;Nneurons=0;
  17. %for fixed bin size
  18. BinSize=0.01; %in seconds
  19. %which sessions included
  20. IncRats=[3 4 9 10];
  21. IncDays=11:20;
  22. %Smoothing PSTHs
  23. %smoothing filter
  24. smoothbins=25; %number of previous bins used to smooth
  25. halfnormal=makedist('HalfNormal','mu',0,'sigma',6.6); %std=3.98
  26. filterweights=pdf(halfnormal,[0:smoothbins]);
  27. %old smoothing
  28. % Smoothing=1; %0 for raw and 1 for smoothing
  29. % SmoothTYPE='lowess';
  30. % SmoothSPAN=7;
  31. % if Smoothing~=1, SmoothTYPE='NoSmoothing';SmoothSPAN=NaN; end
  32. % List of events to analyze and analysis windows EXTRACTED from excel file
  33. spreadsheet=importdata('Cued.xlsx');
  34. Erefnames=spreadsheet.textdata(3:11,1);
  35. prewin = spreadsheet.data(1:9,1:2);
  36. postwin = spreadsheet.data(1:9,3:4);
  37. %%
  38. %Finds the total number of neurons accross all sessions
  39. for i=1:length(RAW)
  40. if any(IncRats==RAW(i).Rat) && any(IncDays==RAW(i).Day) %only look at included sessions
  41. R.Ninfo=cat(1,R.Ninfo,RAW(i).Ninfo);
  42. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  43. end
  44. end
  45. R.Ninfo=cat(2, R.Ninfo, mat2cell([1:Nneurons]',ones(1,Nneurons),1));
  46. R.Erefnames= Erefnames;
  47. % preallocating
  48. R.Param.Tm=Tm;
  49. R.Param.Tbin=Tbin;
  50. R.Param.Dura=Dura;
  51. R.Param.Baseline=Baseline;
  52. R.Param.PStat=PStat;
  53. R.Param.MinNumTrials=MinNumTrials;
  54. R.Param.path=path;
  55. R.Param.prewin=prewin;
  56. R.Param.postwin=postwin;
  57. R.Param.SmoothTYPE='HalfNormal';
  58. R.Param.SmoothSPAN=std(halfnormal);
  59. for k=1:length(Erefnames)
  60. R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  61. R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  62. R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
  63. R.Ev(k).Meanz(1:Nneurons,1)=NaN;
  64. R.Ev(k).BW(1:Nneurons,1)=NaN;
  65. R.Ev(k).signrank(1:Nneurons,1)=NaN;
  66. R.Ev(k).RespDir(1:Nneurons,1)=NaN;
  67. R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
  68. end
  69. %% runs the main routine
  70. figure;
  71. for i=1:length(RAW) %loops through sessions
  72. if any(IncRats==RAW(i).Rat) && any(IncDays==RAW(i).Day) %only look at included sessions
  73. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  74. NN=NN+1; %neuron counter
  75. R.Day(NN,1)=RAW(i).Day;
  76. R.Rat(NN,1)=RAW(i).Rat;
  77. %use the same baseline for z-scoring every event
  78. %baseline period is before every cue in the whole session
  79. bltrlhz=[];
  80. EvInd=strcmp('Cues',RAW(i).Einfo(:,2));
  81. [PSRbl,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},[-11 -1],{2});% makes collapsed rasters for baseline for use in normalizing
  82. for l=1:length(PSRbl)
  83. bltrlhz(l,1)=sum((PSRbl{l}<-1 & PSRbl{l}>-11)/10);
  84. end
  85. bmean=nanmean(bltrlhz(2:2:end)); %2:2:end is to match the number of trials in int/blocks sessions
  86. bstd=nanstd(bltrlhz(2:2:end));
  87. for k=1:length(Erefnames) %loops thorough the events
  88. EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
  89. if sum(EvInd)==0
  90. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  91. end
  92. R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
  93. if ~isempty(EvInd) && R.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
  94. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  95. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  96. [PSR3,~]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{2});% makes trial by trial rasters for baseline for use in response detection
  97. if ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  98. %optimal bin size
  99. %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
  100. %fixed bin size
  101. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%fix bin used here
  102. %------------- Fills the R.Ev(k) fields --------------
  103. R.Ev(k).BW(NN,1)=BW1;
  104. %new smoothing
  105. for l=1:length(Tm)
  106. PTH1smooth(1,l)=sum(PTH1(1,l-min([l-1 smoothbins]):l).*fliplr(filterweights(1:min([l smoothbins+1]))))/sum(filterweights(1:min([l smoothbins+1])));
  107. end
  108. R.Ev(k).PSTHraw(NN,:)=PTH1smooth;
  109. %normalize already smoothed activity
  110. for l=1:length(PTH1smooth)
  111. R.Ev(k).PSTHz(NN,l)=(PTH1smooth(l)-bmean)/bstd;
  112. end
  113. %------------------ firing (in Hz) per trial in pre/post windows ------------------
  114. %used to make the between events comparisons and Response detection in a single window----
  115. ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
  116. ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
  117. for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
  118. 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
  119. ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
  120. end
  121. R.Ev(k).Meanraw(NN,1)=nanmean(ev(k).post);
  122. R.Ev(k).Meanz(NN,1)=(nanmean(ev(k).post)-bmean)/bstd;
  123. %---------------------------Response detection w/ SignRank on pre/post windows---------------------------
  124. R.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.Ev(k).signrank(NN,1)<PStat
  126. R.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
  127. else R.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. fprintf('Neuron ID # %d\n',NN);
  133. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  134. end
  135. end %sessions: FOR i=1:length(RAW)
  136. R_cued=R;
  137. if SAVE_FLAG
  138. save('R_cued_all.mat','R_cued')
  139. end
  140. toc