GenerateintBlocksDataForModeling.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. load ('RAWintBlocks.mat');
  2. RAW=RAWblocks;
  3. runanalysis=1;
  4. %get parameters
  5. trialsback=10; %how many trials back to look
  6. Baseline=[-11 -1]; %For normalizing activity
  7. RDWindow=[0.75 1.95];
  8. PEWindow=[-1.5 -0.5]; %relative to RD
  9. CueWindow=[0 0.75];
  10. %reset
  11. NN=0;
  12. EvMeanz=0;
  13. Ninfo=[];
  14. Nneurons=0;
  15. %Finds the total number of neurons in 2R and marks them by region/session
  16. for i=1:length(RAW)
  17. Ninfo=cat(1,Ninfo,RAW(i).Ninfo);
  18. Nneurons=Nneurons+size(RAW(i).Nrast,1);
  19. end
  20. for i=1:Nneurons
  21. Session=string(Ninfo(i,1));
  22. Name=char(Session);
  23. Region(i,1)=cellstr(Name(1:2)); %an array with each neurons region
  24. Rat(i,1)=cellstr(Name(1:3)); %an array with each neuron's rat
  25. Blocks(i,1)=cellstr(Name(4));
  26. Blocks12(i,1)=cellstr(Name(5));
  27. end
  28. CS.Blocks=strcmp('B',Blocks); %neurons from blocks sessions are marked 1, int is 0
  29. CS.Blocks12=zeros(length(Blocks12),1); %start with all 0s
  30. CS.Blocks12(strcmp('1',Blocks12))=1; %neurons from sessions starting with sucrose are 1
  31. CS.Blocks12(strcmp('2',Blocks12))=2; %neurons from sessions starting with malt are 2
  32. if runanalysis==1
  33. for i=1:length(RAW) %loops through sessions
  34. %ADD IN STUFF
  35. %events
  36. EV1=strmatch('RD1P2',RAW(i).Einfo(:,2),'exact');
  37. EV2=strmatch('RD1P1',RAW(i).Einfo(:,2),'exact');
  38. EV3=strmatch('RD2P2',RAW(i).Einfo(:,2),'exact');
  39. EV4=strmatch('RD2P1',RAW(i).Einfo(:,2),'exact');
  40. RD=strmatch('RD',RAW(i).Einfo(:,2),'exact');
  41. R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
  42. R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
  43. R3=strmatch('R3withanylick',RAW(i).Einfo(:,2),'exact');
  44. Cue=strmatch('Cue',RAW(i).Einfo(:,2),'exact');
  45. %% linear model for impact of previous rewards
  46. %reset
  47. X=NaN(length(RAW(i).Erast{RD,1}(:,1)),trialsback+1);
  48. Y=[];
  49. AllTrials=[];
  50. %set up the matrix with outcome identity for each session
  51. rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
  52. rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
  53. rewards=cat(1,rewards1,rewards2);
  54. [rewards(:,1),ind]=sort(rewards(:,1));
  55. rewards(:,2)=rewards(ind,2);
  56. AllTrials(:,1)=rewards(:,2);
  57. AllTrials(:,2)=0;
  58. for k=1:length(RAW(i).Erast{RD,1}(:,1))
  59. time=RAW(i).Erast{RD,1}(k,1);
  60. entry=find(round(rewards(:,1))==round(time));
  61. for m=1:trialsback+1
  62. if entry+1-m>0
  63. X(k,m)=rewards(entry+1-m,2);
  64. end
  65. end
  66. AllTrials(entry,2)=1;
  67. end
  68. for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
  69. NN=NN+1;
  70. rewspk=0;
  71. basespk=0;
  72. %get mean baseline firing for all reward trials
  73. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
  74. for y= 1:B1n
  75. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  76. end
  77. Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
  78. Bmean=nanmean(Bhz);
  79. Bstd=nanstd(Bhz);
  80. %get trial by trial firing rate for all reward trials
  81. Window=RDWindow;
  82. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
  83. for y= 1:EVn
  84. rewspk(y,1)=sum(EVcell{1,y}>Window(1));
  85. end
  86. Y=(rewspk(1:end,1)/(Window(1,2)-Window(1,1)));%-Bmean)/Bstd; %subtract baseline firing
  87. CS.RDHz{NN,1}=Y;
  88. %get trial by trial firing rate for all PE trials based on fixed window
  89. rewspk=0;
  90. Window=PEWindow;
  91. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
  92. for y= 1:EVn
  93. rewspk(y,1)=sum(EVcell{1,y}>Window(1));
  94. end
  95. Y=(rewspk(1:end,1)/(Window(1,2)-Window(1,1)));%-Bmean)/Bstd; %z-score
  96. CS.PEHz{NN,1}=Y;
  97. %get trial by trial firing rate for all cue trials based on fixed window
  98. CueList=[];
  99. for k=1:length(RAW(i).Erast{RD})
  100. CueList(k,1)=RAW(i).Erast{Cue}(max(find(RAW(i).Erast{Cue}<RAW(i).Erast{RD}(k,1))),1);
  101. end
  102. Window=CueWindow;
  103. rewspk=0;
  104. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
  105. for y= 1:EVn
  106. rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
  107. end
  108. Y=(rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)));%-Bmean)/Bstd; %subtract baseline to keep it linear
  109. CS.CueHz{NN,1}=Y;
  110. %All Cues
  111. AllCues = RAW(i).Erast{Cue};
  112. rewspk=0;
  113. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),AllCues,CueWindow,{2});% makes trial by trial rasters for event
  114. for y= 1:EVn
  115. rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
  116. end
  117. Y=(rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)));%-Bmean)/Bstd; %subtract baseline to keep it linear
  118. CS.CueHzAll{NN,1}=Y;
  119. CS.Predictors{NN,1}=X;
  120. CS.AllTrials{NN,1}=AllTrials;
  121. fprintf('Neuron # %d\n',NN);
  122. end
  123. end
  124. end
  125. %% Other data
  126. CS.Rat=Rat;
  127. CS.Region=Region;
  128. save('ModData_intBlocks.mat','CS');