GenerateCuedDataForModeling.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. %run analysis?
  2. runanalysis=1;
  3. %which sessions go in
  4. %there's a logical later that highlights included neurons
  5. IncRats=[2 3 4 7 9 10];
  6. IncDays=8:20;
  7. %get parameters
  8. trialsback=10; %how many trials back to look
  9. Baseline=[-11 -1]; %Relative to cue
  10. RDWindow=[0.75 1.95];
  11. PEWindow=[-1.5 -0.5]; %relative to RD
  12. CueWindow=[0 0.3];
  13. %%
  14. %reset
  15. NN=0;
  16. EvMeanz=0;
  17. if runanalysis==1
  18. load ('RAWCued.mat');
  19. RAW=RAWCued;
  20. CD.Ninfo={};
  21. for i=1:length(RAW) %loops through sessions
  22. if any(IncRats==RAW(i).Rat) && any(IncDays==RAW(i).Day) %only look at included sessions
  23. %store info about each neuron's channel
  24. CD.Ninfo=cat(1,CD.Ninfo,RAW(i).Ninfo);
  25. %events
  26. Cues=strmatch('Cues',RAW(i).Einfo(:,2),'exact');
  27. Cue1=strmatch('Cue1',RAW(i).Einfo(:,2),'exact');
  28. Cue2=strmatch('Cue2',RAW(i).Einfo(:,2),'exact');
  29. Cue3=strmatch('Cue3',RAW(i).Einfo(:,2),'exact');
  30. RDC=strmatch('RDC',RAW(i).Einfo(:,2),'exact');
  31. RDA=strmatch('RDA',RAW(i).Einfo(:,2),'exact');
  32. R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
  33. R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
  34. %% linear model for impact of previous rewards
  35. %reset
  36. Y=[];
  37. AllTrials=[];
  38. PrCue=[];
  39. %set up the matrix with outcome identity for each session
  40. rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
  41. rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
  42. rewards=cat(1,rewards1,rewards2);
  43. [rewards(:,1),ind]=sort(rewards(:,1));
  44. rewards(:,2)=rewards(ind,2);
  45. %put both conditions together
  46. RDCkey=cat(2,RAW(i).Erast{RDC,1}(:,1),ones(length(RAW(i).Erast{RDC,1}(:,1)),1)); %1 if predictive trial
  47. RDAkey=cat(2,RAW(i).Erast{RDA,1}(:,1),zeros(length(RAW(i).Erast{RDA,1}(:,1)),1)); %0 if not
  48. RDall=cat(1,RDCkey,RDAkey);
  49. [RDall(:,1),ind]=sort(RDall(:,1));
  50. RDall(:,2)=RDall(ind,2);
  51. AllTrials(:,1)=rewards(:,2);
  52. AllTrials(:,2)=0;
  53. X=NaN(length(RDall),trialsback+1);
  54. %create results key
  55. for k=1:length(RDall(:,1))
  56. time=RDall(k,1);
  57. entry=find(round(rewards(:,1))==round(time));
  58. for m=1:trialsback+1
  59. if entry+1-m>0
  60. X(k,m)=rewards(entry+1-m,2);
  61. end
  62. end
  63. %add a 1 to next column if predicted sucrose, column after
  64. %that if predicted maltodextrin
  65. if RDall(k,2)==1
  66. if X(k,1)==1
  67. X(k,trialsback+2)=1;
  68. X(k,trialsback+3)=0;
  69. PrCue(k,1)=1;
  70. PrCue(k,2)=0;
  71. else
  72. X(k,trialsback+2)=0;
  73. X(k,trialsback+3)=1;
  74. PrCue(k,1)=0;
  75. PrCue(k,2)=1;
  76. end
  77. else
  78. X(k,trialsback+2:trialsback+3)=0;
  79. PrCue(k,1:2)=0;
  80. end
  81. AllTrials(entry,2)=1;
  82. end
  83. %get cue for each trial
  84. cue1s=RAW(i).Erast{Cue1,1}(:,1);
  85. cue2s=RAW(i).Erast{Cue2,1}(:,1);
  86. cue3s=RAW(i).Erast{Cue3,1}(:,1);
  87. PrCueAllTrials=zeros(length(rewards),3);
  88. for j=1:length(rewards)
  89. mostrecentcue(1)=max([cue1s(cue1s<rewards(j,1))' 0]);
  90. mostrecentcue(2)=max([cue2s(cue2s<rewards(j,1))' 0]);
  91. mostrecentcue(3)=max([cue3s(cue3s<rewards(j,1))' 0]);
  92. [~,mostrecent]=max(mostrecentcue);
  93. PrCueAllTrials(j,mostrecent)=1;
  94. end
  95. for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
  96. NN=NN+1;
  97. basespk=0;
  98. %get mean baseline firing for all trials
  99. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cues},Baseline,{2});% makes trial by trial rasters for baseline
  100. for y= 1:B1n
  101. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  102. end
  103. Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
  104. Bmean=nanmean(Bhz);
  105. Bstd=nanstd(Bhz);
  106. %get trial by trial firing rate for all reward trials
  107. rewspk=[];
  108. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RDall(:,1),RDWindow,{2});% makes trial by trial rasters for event
  109. for y= 1:EVn
  110. rewspk(y,1)=sum(EVcell{1,y}>RDWindow(1));
  111. end
  112. Y=(rewspk(1:end,1)/(RDWindow(1,2)-RDWindow(1,1)));%-Bmean)/Bstd; %normalize the activity to baseline
  113. CD.RDHz{NN,1}=Y;
  114. %get trial by trial firing rate for all port entries
  115. rewspk=[];
  116. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RDall(:,1),PEWindow,{2});% makes trial by trial rasters for event
  117. for y= 1:EVn
  118. rewspk(y,1)=sum(EVcell{1,y}>PEWindow(1));
  119. end
  120. Y=(rewspk(1:end,1)/(PEWindow(1,2)-PEWindow(1,1)));%-Bmean)/Bstd; %normalize the activity to baseline
  121. CD.PEHz{NN,1}=Y;
  122. %get trial by trial firing rate for all cues
  123. CueList=[];
  124. rewspk=[];
  125. for k=1:length(RDall(:,1))
  126. CueList(k,1)=RAW(i).Erast{Cues}(max(find(RAW(i).Erast{Cues}<RDall(k,1))),1);
  127. end
  128. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
  129. for y= 1:EVn
  130. rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
  131. end
  132. Y=(rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)));%-Bmean)/Bstd; %subtract baseline to keep it linear
  133. CD.CueHz{NN,1}=Y;
  134. %save the predictors
  135. CD.Predictors{NN,1}=X;
  136. CD.AllTrials{NN,1}=AllTrials;
  137. CD.PredCue{NN,1}=PrCue;
  138. CD.PredCueAllTrials{NN,1}=PrCueAllTrials;
  139. CD.Day(NN,1)=RAW(i).Day;
  140. CD.Rat(NN,1)=RAW(i).Rat;
  141. fprintf('Neuron # %d\n',NN);
  142. end
  143. end
  144. end
  145. end
  146. %% Other data
  147. %which neurons are plotted
  148. DOI = 11:20;
  149. ROI = [3 4 9 10];
  150. CD.IncludedNeurons=ChooseNs(CD,DOI,ROI);
  151. save('ModData_Cued.mat','CD');