PSTHsSucMal.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. %PSTHs and some initial analysis
  2. %this is for sessions with two rewards (2R) -- suc and mal
  3. %both interspersed and blocked sessions are here
  4. clear all; clc;
  5. global Dura Baseline Tm Tbase BSIZE Tbin
  6. tic
  7. load ('RAWintBlocks.mat');
  8. RAW=RAWblocks;
  9. %Main settings
  10. SAVE_FLAG=1;
  11. BSIZE=0.01; %Do not change
  12. Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2);
  13. %Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
  14. Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
  15. Tbase=-11:BSIZE:-1; %for fixed baseline for z-score
  16. BaseZscore=[-11 -1]; %bin for calculating z-score, new way
  17. PStat=0.05; %for comparing pre versus post windows, or event A versus event B
  18. MinNumTrials=5; %how many trials of event there need to be to conduct analysis
  19. BinSize=0.01; %in seconds
  20. %Smoothing PSTHs
  21. %smoothing filter
  22. smoothbins=25; %number of previous bins used to smooth
  23. halfnormal=makedist('HalfNormal','mu',0,'sigma',6.6); %std=3.98
  24. filterweights=pdf(halfnormal,[0:smoothbins]);
  25. %start fresh
  26. R=[];R.Ninfo={};NN=0;Nneurons=0;
  27. % List of events to analyze and analysis windows EXTRACTED from excel file
  28. spreadsheet=importdata('SucMal.xlsx');
  29. Erefnames=spreadsheet.textdata(3:14,1);
  30. prewin = spreadsheet.data(1:12,1:2);
  31. postwin = spreadsheet.data(1:12,3:4);
  32. %saves event names for reference later
  33. R.Erefnames=Erefnames;
  34. %Finds the total number of neurons in 2R and marks them by region/session
  35. for i=1:length(RAW)
  36. R.Ninfo=cat(1,R.Ninfo,RAW(i).Ninfo);
  37. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  38. end
  39. for i=1:Nneurons
  40. Session=string(R.Ninfo(i,1));
  41. Name=char(Session);
  42. R.Region(i,1)=cellstr(Name(1:2)); %an array with each neurons region
  43. R.Rat(i,1)=cellstr(Name(1:3)); %an array with each neuron's rat
  44. Blocks(i,1)=cellstr(Name(4));
  45. Blocks12(i,1)=cellstr(Name(5));
  46. end
  47. R.Blocks=strcmp('B',Blocks); %neurons from blocks sessions are marked 1, int is 0
  48. R.Blocks12=zeros(length(Blocks12),1); %start with all 0s
  49. R.Blocks12(strcmp('1',Blocks12))=1; %neurons from sessions starting with sucrose are 1
  50. R.Blocks12(strcmp('2',Blocks12))=2; %neurons from sessions starting with malt are 2
  51. % preallocating
  52. R.Param.Tm=Tm;
  53. R.Param.Tbin=Tbin;
  54. R.Param.Dura=Dura;
  55. R.Param.Baseline=Baseline;
  56. R.Param.PStat=PStat;
  57. R.Param.MinNumTrials=MinNumTrials;
  58. R.Param.prewin=prewin;
  59. R.Param.postwin=postwin;
  60. R.Param.SmoothTYPE='HalfNormal';
  61. R.Param.SmoothSPAN=std(halfnormal);
  62. for k=1:length(Erefnames)
  63. R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  64. R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
  65. R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
  66. R.Ev(k).Meanz(1:Nneurons,1)=NaN;
  67. R.Ev(k).BW(1:Nneurons,1)=NaN;
  68. R.Ev(k).signrank(1:Nneurons,1)=NaN;
  69. R.Ev(k).RespDir(1:Nneurons,1)=NaN;
  70. R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
  71. end
  72. % runs the main routine
  73. for i=1:length(RAW) %loops through sessions
  74. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  75. NN=NN+1; %neuron counter
  76. %use the same baseline for z-scoring every event
  77. %baseline period is before every cue in the whole session
  78. bltrlhz=[];
  79. EvInd=strcmp('Cue',RAW(i).Einfo(:,2));
  80. [PSRbl,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},BaseZscore,{2});% makes collapsed rasters for baseline for use in normalizing
  81. for l=1:length(PSRbl)
  82. bltrlhz(l,1)=sum((PSRbl{l}<-1 & PSRbl{l}>-11)/10);
  83. end
  84. bmean=nanmean(bltrlhz);
  85. bstd=nanstd(bltrlhz);
  86. for k=1:length(Erefnames) %loops thorough the events
  87. EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
  88. if sum(EvInd)==0
  89. fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
  90. end
  91. R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
  92. if ~isempty(EvInd) && R.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
  93. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
  94. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  95. [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
  96. if ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
  97. %Optimal bin size
  98. %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
  99. %Fixed bin size
  100. [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----Fixed bin used here
  101. %------------- Fills the R.Ev(k) fields --------------
  102. R.Ev(k).BW(NN,1)=BW1;
  103. PTH1smooth=[];
  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. %-------------------- signrank on event and direction----
  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. %----------------------- Check for difference in cue responding after sucrose and maltodextrin trials -----------------------
  133. CueP1=strcmp('CueP1', Erefnames);
  134. CueP2=strcmp('CueP2', Erefnames);
  135. [R.CueP12Stat(NN,1),~]=ranksum(ev(CueP1).post,ev(CueP2).post); %Ranksum test used becasue it is an independent sample test
  136. fprintf('Neuron #%d\n',NN);
  137. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  138. end %sessions: FOR i=1:length(RAW)
  139. if SAVE_FLAG
  140. R_blocks=R;
  141. save('R_intBlocks.mat','R_blocks')
  142. end
  143. toc