PSTHsTH.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. %PSTHs and some initial analysis
  2. %TH stands for three outcomes (suc/mal/wat)
  3. clear all; clc;
  4. global Dura Baseline Tm Tbase BSIZE Tbin
  5. tic
  6. load('RAWTH.mat');
  7. RAW=RAWTH;
  8. %Main settings
  9. SAVE_FLAG=1;
  10. BSIZE=0.01; %Do not change
  11. Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2);
  12. %Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
  13. Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
  14. Tbase=-11:BSIZE:-1; %for fixed baseline for z-score
  15. BaseZscore=[-11 -1]; %bin for calculating z-score, new way
  16. PStat=0.05; %for comparing pre versus post windows, or event A versus event B
  17. MinNumTrials=3; %how many trials of event there need to be to conduct analysis
  18. BinSize=0.01; %in seconds
  19. %Smoothing PSTHs
  20. %smoothing filter
  21. smoothbins=25; %number of previous bins used to smooth
  22. halfnormal=makedist('HalfNormal','mu',0,'sigma',6.6); %std=3.98
  23. filterweights=pdf(halfnormal,[0:smoothbins]);
  24. %start fresh
  25. R=[];R.Ninfo={};NN=0;Nneurons=0;
  26. % List of events to analyze and analysis windows EXTRACTED from excel file
  27. spreadsheet=importdata('TH.xlsx');
  28. Erefnames=spreadsheet.textdata(3:9,1);
  29. prewin = spreadsheet.data(1:7,1:2);
  30. postwin = spreadsheet.data(1:7,3:4);
  31. %saves event names for reference later
  32. R.Erefnames=Erefnames;
  33. %Finds the total number of neurons in 2R and marks them by region/session
  34. for i=1:length(RAW)
  35. R.Ninfo=cat(1,R.Ninfo,RAW(i).Ninfo);
  36. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  37. end
  38. for i=1:Nneurons
  39. Session=string(R.Ninfo(i,1));
  40. Name=char(Session);
  41. R.Rat(i,1)=str2num(Name(3)); %an array with each neuron's rat
  42. end
  43. % preallocating
  44. R.Param.Tm=Tm;
  45. R.Param.Tbin=Tbin;
  46. R.Param.Dura=Dura;
  47. R.Param.Baseline=Baseline;
  48. R.Param.PStat=PStat;
  49. R.Param.MinNumTrials=MinNumTrials;
  50. R.Param.path=path;
  51. R.Param.prewin=prewin;
  52. R.Param.postwin=postwin;
  53. R.Param.SmoothTYPE='HalfNormal';
  54. R.Param.SmoothSPAN=std(halfnormal);
  55. for k=1:length(Erefnames)
  56. R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  57. R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  58. R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
  59. R.Ev(k).Meanz(1:Nneurons,1)=NaN;
  60. R.Ev(k).BW(1:Nneurons,1)=NaN;
  61. R.Ev(k).signrank(1:Nneurons,1)=NaN;
  62. R.Ev(k).RespDir(1:Nneurons,1)=NaN;
  63. R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
  64. end
  65. % runs the main routine
  66. for i=1:length(RAW) %loops through sessions
  67. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  68. NN=NN+1; %neuron counter
  69. %use the same baseline for z-scoring every event
  70. %baseline period is before every cue in the whole session
  71. bltrlhz=[];
  72. EvInd=strcmp('Cue',RAW(i).Einfo(:,2));
  73. [PSRbl,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},BaseZscore,{2});% makes collapsed rasters for baseline for use in normalizing
  74. for l=1:length(PSRbl)
  75. bltrlhz(l,1)=sum((PSRbl{l}<-1 & PSRbl{l}>-11)/10);
  76. end
  77. bmean=nanmean(bltrlhz);
  78. bstd=nanstd(bltrlhz);
  79. for k=1:length(Erefnames) %loops thorough the events
  80. EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
  81. if sum(EvInd)==0
  82. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  83. end
  84. R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
  85. if ~isempty(EvInd) && R.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
  86. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  87. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  88. [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
  89. if ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  90. %Optimal bin size
  91. %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
  92. %Fixed bin size
  93. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----Fixed bin used here
  94. %------------- Fills the R.Ev(k) fields --------------
  95. R.Ev(k).BW(NN,1)=BW1;
  96. PTH1smooth=[];
  97. %new smoothing
  98. for l=1:length(Tm)
  99. 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])));
  100. end
  101. R.Ev(k).PSTHraw(NN,:)=PTH1smooth;
  102. %normalize already smoothed activity
  103. for l=1:length(PTH1smooth)
  104. R.Ev(k).PSTHz(NN,l)=(PTH1smooth(l)-bmean)/bstd;
  105. end
  106. %------------------ firing (in Hz) per trial in pre/post windows ------------------
  107. %used to make the between events comparisons and Response detection in a single window----
  108. ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
  109. ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
  110. for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
  111. 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
  112. ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
  113. end
  114. R.Ev(k).Meanraw(NN,1)=nanmean(ev(k).post);
  115. R.Ev(k).Meanz(NN,1)=(nanmean(ev(k).post)-bmean)/bstd;
  116. %-------------------- signrank on event and direction----
  117. [R.Ev(k).signrank(NN,1),~]=signrank(ev(k).pre, ev(k).post); %Signrank used here because it is a dependant sample test
  118. if R.Ev(k).signrank(NN,1)<PStat
  119. R.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
  120. else R.Ev(k).RespDir(NN,1)=0;
  121. end
  122. end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
  123. end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
  124. end %Events
  125. fprintf('Neuron #%d\n',NN);
  126. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  127. end %sessions: FOR i=1:length(RAW)
  128. if SAVE_FLAG
  129. R_TH=R;
  130. save('R_TH.mat','R_TH')
  131. end
  132. toc