OptoPEAnalysis.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. clear all;
  2. %parameters
  3. window=[-5 20]; %seconds relative to reward delivery
  4. binsize=0.2; %in seconds
  5. bins=(window(2)-window(1))/binsize;
  6. xaxis=linspace(window(1)+binsize/2,window(1)+binsize/2+(bins-1)*binsize,bins);
  7. WOI=[0 10];
  8. boi=(WOI(1)-window(1))/binsize+1:(WOI(2)-window(1))/binsize;
  9. address=['C:\Users\dottenh2\Dropbox\MATLAB\David\2R_blocks\ChR2PPBehavior']; %where are the running files located
  10. %address=['/Users/David/Dropbox/MATLAB/David/2R_blocks/ChR2PPBehavior'];
  11. files=dir([address,'\\*!*']);
  12. %files=dir([address,'//*!*']);
  13. for n=1:length(files)
  14. filename=fullfile(files(n).folder,files(n).name);
  15. file=fopen(filename);
  16. L=textscan(file,'%s','Delimiter',':');
  17. fclose('all');
  18. x=str2num(files(n).name(30:31)); %x is rat #
  19. %find start of A, start of C, and end of C
  20. Jstrt=find(strcmp('J',L{1,1})); %Cue
  21. Kstrt=find(strcmp('K',L{1,1})); %Sucrose Delivery
  22. Ostrt=find(strcmp('O',L{1,1})); %Port entry times
  23. Pstrt=find(strcmp('P',L{1,1})); %Port durations
  24. Qstrt=find(strcmp('Q',L{1,1})); %Laser stimulations
  25. Xstrt=find(strcmp('X',L{1,1})); %List (marks end of laser stims)
  26. %Sucrose delivery times
  27. sucrosedel=(L{1,1}(Kstrt+2:2:Ostrt-1));
  28. sucrosedeltime=[];
  29. for i=1:length(sucrosedel)
  30. medtext=textscan(char(sucrosedel(i,1)),'%f','Delimiter',' ');
  31. sdtime=medtext{1,1}(:,1);
  32. sucrosedeltime=(cat(1,sucrosedeltime,sdtime(isnan(sdtime)==0)));
  33. end
  34. sucrosedeltime(sucrosedeltime==0)=[]; %delete 0s
  35. %Port entry times
  36. pet=(L{1,1}(Ostrt+2:2:Pstrt-1));
  37. petime=[];
  38. for i=1:length(pet)
  39. medtext=textscan(char(pet(i,1)),'%f','Delimiter',' ');
  40. portentry=medtext{1,1}(:,1);
  41. petime=(cat(1,petime,portentry(isnan(portentry)==0)));
  42. end
  43. removes=petime==0;
  44. petime(petime==0)=[]; %delete 0s
  45. %Port entry durations
  46. durationt=(L{1,1}(Pstrt+2:2:Qstrt-1));
  47. durationtime=[];
  48. for i=1:length(durationt)
  49. medtext=textscan(char(durationt(i,1)),'%f','Delimiter',' ');
  50. duration=medtext{1,1}(:,1);
  51. durationtime=(cat(1,durationtime,duration(isnan(duration)==0)));
  52. end
  53. durationtime(removes)=[]; %delete 0s
  54. %Laser stims
  55. ST=(L{1,1}(Qstrt+2:2:Xstrt-1));
  56. STtime=[];
  57. for i=1:length(ST)
  58. medtext=textscan(char(ST(i,1)),'%f','Delimiter',' ');
  59. stime=medtext{1,1}(:,1);
  60. STtime=(cat(1,STtime,stime(isnan(stime)==0)));
  61. end
  62. STtime(STtime==0)=[]; %delete 0s
  63. stim_trials = [];
  64. for trl = 1:length(STtime)
  65. stim_trials(trl,1) =max(sucrosedeltime(sucrosedeltime<STtime(trl)));
  66. end
  67. nost_trials = sucrosedeltime(ismember(sucrosedeltime,stim_trials)==0);
  68. %port occupancy matrix stim
  69. stim_matrix=[];
  70. for trl=1:length(stim_trials)
  71. for bin=1:bins
  72. reltime=window(1)+(bin-1)*binsize;
  73. time=stim_trials(trl)+reltime;
  74. %check if this time point was during a port entry
  75. if sum(petime<=time)
  76. [entry_time,index] = max(petime(petime<=time));
  77. stim_matrix(trl,bin) = time <= (entry_time + durationtime(index));
  78. else
  79. stim_matrix(trl,bin) = 0;
  80. end
  81. end
  82. end
  83. %port occupancy matrix no stim
  84. nost_matrix=[];
  85. for trl=1:length(nost_trials)
  86. for bin=1:bins
  87. reltime=window(1)+(bin-1)*binsize;
  88. time=nost_trials(trl)+reltime;
  89. %check if this time point was during a port entry
  90. if sum(petime<=time)
  91. [entry_time,index] = max(petime(petime<=time));
  92. nost_matrix(trl,bin) = time <= (entry_time + durationtime(index));
  93. else
  94. nost_matrix(trl,bin) = 0;
  95. end
  96. end
  97. end
  98. stim_PSTH(x,:) = mean(stim_matrix);
  99. nost_PSTH(x,:) = mean(nost_matrix);
  100. end
  101. %% plotting
  102. figure;
  103. yfp=[4 6 10 11 13 14 21]; %at least 30 trials, both fibers in general vicinity
  104. chr=[1 2 7 8 9 16 17 18 22 23 24]; %good placement, at least 30 trials
  105. colors{2}=[0 0.3 1];
  106. colors{1}=[0.4 0.4 0.4];
  107. groups{1,1}=yfp;
  108. groups{2,1}=chr;
  109. for group=1:2
  110. subplot(2,2,2+group);
  111. hold on;
  112. psth1 = mean(nost_PSTH(groups{group,1},:));
  113. up1 = psth1 + nanste(nost_PSTH(groups{group,1},:),1);
  114. down1 = psth1 - nanste(nost_PSTH(groups{group,1},:),1);
  115. psth2 = mean(stim_PSTH(groups{group,1},:));
  116. up2 = psth2 + nanste(stim_PSTH(groups{group,1},:),1);
  117. down2 = psth2 - nanste(stim_PSTH(groups{group,1},:),1);
  118. patch([0 0 2 2],[0 1 1 0],[0.7 0.7 1],'edgecolor','none');
  119. a=plot(xaxis,psth1,'Color',colors{1},'linewidth',1);
  120. b=plot(xaxis,psth2,'Color',colors{2},'linewidth',1);
  121. patch([xaxis,xaxis(end:-1:1)],[up1,down1(end:-1:1)],colors{1},'EdgeColor','none');alpha(0.5);
  122. patch([xaxis,xaxis(end:-1:1)],[up2,down2(end:-1:1)],colors{2},'EdgeColor','none');alpha(0.5);
  123. ylabel('Fraction of trials in port');
  124. xlabel('Seconds from reward delivery');
  125. plot([0 0],[0 1],':','color','k','linewidth',1);
  126. legend([a b],'No laser','Laser','location','northeast');
  127. if group==1 title('GFP'); end
  128. if group==2 title('ChR2'); end
  129. end
  130. %difference in time in port following 10s?
  131. boi = (xaxis>= 15) & (xaxis<= 20);
  132. nost_boi = mean(nost_PSTH(:,boi),2);
  133. stim_boi = mean(stim_PSTH(:,boi),2);
  134. p = signrank(nost_boi(chr),stim_boi(chr));
  135. %% inhibition
  136. clear all;
  137. %parameters
  138. window=[-5 20]; %seconds relative to reward delivery
  139. binsize=0.2; %in seconds
  140. bins=(window(2)-window(1))/binsize;
  141. xaxis=linspace(window(1)+binsize/2,window(1)+binsize/2+(bins-1)*binsize,bins);
  142. WOI=[0 10];
  143. boi=(WOI(1)-window(1))/binsize+1:(WOI(2)-window(1))/binsize;
  144. address=['C:\Users\dottenh2\Dropbox\MATLAB\David\2R_blocks\OptoPPBehavior']; %where are the running files located
  145. %address=['/Users/David/Dropbox/MATLAB/David/2R_blocks/OptoPPBehavior'];
  146. files=dir([address,'\\*!*']);
  147. %files=dir([address,'//*!*']);
  148. for n=1:length(files)
  149. filename=fullfile(files(n).folder,files(n).name);
  150. file=fopen(filename);
  151. L=textscan(file,'%s','Delimiter',':');
  152. fclose('all');
  153. x=str2num(files(n).name(30:31)); %x is rat #
  154. %find start of A, start of C, and end of C
  155. Jstrt=find(strcmp('J',L{1,1})); %Cue
  156. Kstrt=find(strcmp('K',L{1,1})); %Sucrose Delivery
  157. Ostrt=find(strcmp('O',L{1,1})); %Port entry times
  158. Pstrt=find(strcmp('P',L{1,1})); %Port durations
  159. Qstrt=find(strcmp('Q',L{1,1})); %Laser stimulations
  160. Xstrt=find(strcmp('X',L{1,1})); %List (marks end of laser stims)
  161. %Sucrose delivery times
  162. sucrosedel=(L{1,1}(Kstrt+2:2:Ostrt-1));
  163. sucrosedeltime=[];
  164. for i=1:length(sucrosedel)
  165. medtext=textscan(char(sucrosedel(i,1)),'%f','Delimiter',' ');
  166. sdtime=medtext{1,1}(:,1);
  167. sucrosedeltime=(cat(1,sucrosedeltime,sdtime(isnan(sdtime)==0)));
  168. end
  169. sucrosedeltime(sucrosedeltime==0)=[]; %delete 0s
  170. %Port entry times
  171. pet=(L{1,1}(Ostrt+2:2:Pstrt-1));
  172. petime=[];
  173. for i=1:length(pet)
  174. medtext=textscan(char(pet(i,1)),'%f','Delimiter',' ');
  175. portentry=medtext{1,1}(:,1);
  176. petime=(cat(1,petime,portentry(isnan(portentry)==0)));
  177. end
  178. removes=petime==0;
  179. petime(petime==0)=[]; %delete 0s
  180. %Port entry durations
  181. durationt=(L{1,1}(Pstrt+2:2:Qstrt-1));
  182. durationtime=[];
  183. for i=1:length(durationt)
  184. medtext=textscan(char(durationt(i,1)),'%f','Delimiter',' ');
  185. duration=medtext{1,1}(:,1);
  186. durationtime=(cat(1,durationtime,duration(isnan(duration)==0)));
  187. end
  188. durationtime(removes)=[]; %delete 0s
  189. %Laser stims
  190. ST=(L{1,1}(Qstrt+2:2:Xstrt-1));
  191. STtime=[];
  192. for i=1:length(ST)
  193. medtext=textscan(char(ST(i,1)),'%f','Delimiter',' ');
  194. stime=medtext{1,1}(:,1);
  195. STtime=(cat(1,STtime,stime(isnan(stime)==0)));
  196. end
  197. STtime(STtime==0)=[]; %delete 0s
  198. stim_trials = [];
  199. for trl = 1:length(STtime)
  200. stim_trials(trl,1) =max(sucrosedeltime(sucrosedeltime<STtime(trl)));
  201. end
  202. nost_trials = sucrosedeltime(ismember(sucrosedeltime,stim_trials)==0);
  203. %port occupancy matrix stim
  204. stim_matrix=[];
  205. for trl=1:length(stim_trials)
  206. for bin=1:bins
  207. reltime=window(1)+(bin-1)*binsize;
  208. time=stim_trials(trl)+reltime;
  209. %check if this time point was during a port entry
  210. if sum(petime<=time)
  211. [entry_time,index] = max(petime(petime<=time));
  212. stim_matrix(trl,bin) = time <= (entry_time + durationtime(index));
  213. else
  214. stim_matrix(trl,bin) = 0;
  215. end
  216. end
  217. end
  218. %port occupancy matrix no stim
  219. nost_matrix=[];
  220. for trl=1:length(nost_trials)
  221. for bin=1:bins
  222. reltime=window(1)+(bin-1)*binsize;
  223. time=nost_trials(trl)+reltime;
  224. %check if this time point was during a port entry
  225. if sum(petime<=time)
  226. [entry_time,index] = max(petime(petime<=time));
  227. nost_matrix(trl,bin) = time <= (entry_time + durationtime(index));
  228. else
  229. nost_matrix(trl,bin) = 0;
  230. end
  231. end
  232. end
  233. stim_PSTH(x,:) = mean(stim_matrix);
  234. nost_PSTH(x,:) = mean(nost_matrix);
  235. end
  236. %% plotting
  237. yfp=[1 2 6 9 10 15 23]; %at least 30 trials, both fibers in general vicinity
  238. %arc=[3 4 7 8 11 13 14 17 18 21 22];
  239. %arc=[3 4 11 13 18 22]; %latency effects
  240. arc=[3 4 8 11 13 18 22]; %good placement, at least 30 trials
  241. colors{2}=[0 0.6 0.4];
  242. colors{1}=[0.4 0.4 0.4];
  243. groups{1,1}=yfp;
  244. groups{2,1}=arc;
  245. for group=1:2
  246. subplot(2,2,group);
  247. hold on;
  248. psth1 = mean(nost_PSTH(groups{group,1},:));
  249. up1 = psth1 + nanste(nost_PSTH(groups{group,1},:),1);
  250. down1 = psth1 - nanste(nost_PSTH(groups{group,1},:),1);
  251. psth2 = mean(stim_PSTH(groups{group,1},:));
  252. up2 = psth2 + nanste(stim_PSTH(groups{group,1},:),1);
  253. down2 = psth2 - nanste(stim_PSTH(groups{group,1},:),1);
  254. patch([0 0 5 5],[0 1 1 0],[0.7 1 0.7],'edgecolor','none');
  255. a=plot(xaxis,psth1,'Color',colors{1},'linewidth',1);
  256. b=plot(xaxis,psth2,'Color',colors{2},'linewidth',1);
  257. patch([xaxis,xaxis(end:-1:1)],[up1,down1(end:-1:1)],colors{1},'EdgeColor','none');alpha(0.5);
  258. patch([xaxis,xaxis(end:-1:1)],[up2,down2(end:-1:1)],colors{2},'EdgeColor','none');alpha(0.5);
  259. ylabel('Fraction of trials in port');
  260. xlabel('Seconds from reward delivery');
  261. plot([0 0],[0 1],':','color','k','linewidth',1);
  262. legend([a b],'No laser','Laser','location','northeast');
  263. if group==1 title('YFP'); end
  264. if group==2 title('ArchT'); end
  265. end