-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIm2P_Analysis_PC2Variability.m
269 lines (242 loc) · 11 KB
/
Im2P_Analysis_PC2Variability.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
datafile = 'Z:\Dudi\Imaging\2Photon\Diego_setup\Alldata_Playback.mat';
IsRedo_mOneROIData = 1;
nOnlyFirstSet = 1;
%ROIname = 'Ring';
ROIname = 'PC2Cell';
%%find sessions with pC2
if ~exist('mOneROIData','var') || IsRedo_mOneROIData
load(datafile)
mOneROIData = cell2table(cell(1,10),'VariableNames',...
{'StimName', 'Fly', 'Folder', 'StimNumber','GreenSignal_FinROI','GreenSignal_DFFinROI','GreenSignal_MaxDFF',...
'GreenSignal_SumDFF','MaxDFFAllStim','MaxSumDFFAllStim'});
nIndex = 1;
nFly = 1;
for nLine = 1:size(mAllData,1)
if ~strcmp(mAllData.ROIname(nLine),ROIname) || ~strcmp(mAllData.Gender(nLine),'F') ||...
~(mAllData.Intensity{nLine}(1)==4) || ...
~isempty(strfind(mAllData.Folder{nLine},'Context')) || (mAllData.StimNumber{nLine}>35 && nOnlyFirstSet)
continue
end %only female, ROIname, first 35 trials, intensity = 4
if nIndex > 1 && ~strcmp(mAllData.Folder{nLine,1},mOneROIData.Folder{nIndex-1,1}), nFly = nFly +1; end
mOneROIData.StimName{nIndex,1} = mAllData.StimName{nLine};
mOneROIData.Fly{nIndex,1} = nFly;
mOneROIData.Folder{nIndex,1} = mAllData.Folder{nLine};
mOneROIData.StimNumber{nIndex,1} = mAllData.StimNumber{nLine};
mOneROIData.GreenSignal_FinROI{nIndex,1} = mAllData.GreenSignal_FinROI{nLine};
mOneROIData.GreenSignal_DFFinROI{nIndex,1} = mAllData.GreenSignal_DFFinROI{nLine};
mOneROIData.GreenSignal_MaxDFF{nIndex,1} = mAllData.GreenSignal_MaxDFF{nLine};
mOneROIData.GreenSignal_SumDFF{nIndex,1} = mAllData.GreenSignal_SumDFF{nLine};
mOneROIData.MaxDFFAllStim{nIndex,1} = mAllData.MaxDFFAllStim{nLine};
mOneROIData.MaxSumDFFAllStim{nIndex,1} = mAllData.MaxSumDFFAllStim{nLine};
nIndex = nIndex + 1;
end
end
%%List the responses from high to low
AllStim = unique(mOneROIData.StimName);
AllResponses = struct('Norm_DFF',[],'Norm_SumDFF',[]);
for StimNum = 1:size(AllStim,1)
AllResponses(StimNum).Norm_DFF = [];
AllResponses(StimNum).Norm_SumDFF = [];
end
for nLine = 1:size(mOneROIData,1)
StimName = mOneROIData.StimName{nLine,1};
StimNum = find(strcmp(AllStim,StimName));%StimNum is the index to the stimulus in AllStim, so later I1,I2 are the indexes of stimuli in AllStim
AllResponses(StimNum).Norm_DFF =...
[AllResponses(StimNum).Norm_DFF mOneROIData.GreenSignal_MaxDFF{nLine}/mOneROIData.MaxDFFAllStim{nLine}];
AllResponses(StimNum).Norm_SumDFF =...
[AllResponses(StimNum).Norm_SumDFF mOneROIData.GreenSignal_SumDFF{nLine}/mOneROIData.MaxSumDFFAllStim{nLine}];
end
vMaxDFF = zeros(1,size(AllStim,1)); vMaxSumDFF = zeros(1,size(AllStim,1));
for StimNum = 1:size(AllStim,1)
vMaxDFF(StimNum) = mean(AllResponses(StimNum).Norm_DFF);
vMaxSumDFF(StimNum) = mean(AllResponses(StimNum).Norm_SumDFF);
end
[S1,I1] = sort(vMaxDFF,'descend');
[S2,I2] = sort(vMaxSumDFF,'descend');
%%
figure(1),clf; figure(2),clf
for nFlyNumber = 1:mOneROIData.Fly{end}
AllResponses = struct('Norm_DFF',[],'Norm_SumDFF',[]);
for StimNum = 1:size(AllStim,1)
AllResponses(StimNum).Norm_DFF = [];
AllResponses(StimNum).Norm_SumDFF = [];
end
for nLine = 1:size(mOneROIData,1)
if ~(mOneROIData.Fly{nLine,1} == nFlyNumber), continue,end
StimName = mOneROIData.StimName{nLine,1};
StimNum = find(strcmp(AllStim,StimName));
AllResponses(StimNum).Norm_DFF =...
[AllResponses(StimNum).Norm_DFF mOneROIData.GreenSignal_MaxDFF{nLine}/mOneROIData.MaxDFFAllStim{nLine}];
AllResponses(StimNum).Norm_SumDFF =...
[AllResponses(StimNum).Norm_SumDFF mOneROIData.GreenSignal_SumDFF{nLine}/mOneROIData.MaxSumDFFAllStim{nLine}];
end
figure(1)
subplot(3,4,nFlyNumber),hold off
for ii = 1:length(I1)
StimNum = I1(ii);
for jj = 1:size(AllResponses(StimNum).Norm_DFF,2)
plot(ii,AllResponses(StimNum).Norm_DFF(jj),'ko'),hold on
end
M = mean(AllResponses(StimNum).Norm_DFF);
S = std(AllResponses(StimNum).Norm_DFF);
%plot([ii ii],[M-S M+S],'r-','LineWidth',3), hold on
end
title(['Fly ',num2str(nFlyNumber)])
ylabel('Norm peak \deltaF/F')
ylim([0 1])
figure(2)
subplot(3,4,nFlyNumber),hold off
for ii = 1:length(I2)
StimNum = I2(ii);
for jj = 1:size(AllResponses(StimNum).Norm_SumDFF,2)
plot(ii,AllResponses(StimNum).Norm_SumDFF(jj),'ko'),hold on
end
M = mean(AllResponses(StimNum).Norm_SumDFF);
S = std(AllResponses(StimNum).Norm_SumDFF);
%plot([ii ii],[M-S M+S],'r-','LineWidth',3), hold on
end
title(['Fly ',num2str(nFlyNumber)])
ylabel('Norm sum \deltaF/F')
ylim([0 1])
end
%all flies together
%Selected = I1([2 8 10 14 18 26 27 28]);
Selected = I1([2 12 22 28]);
%Selected = I1;
figure(3),clf
AllResponses = struct('Norm_DFF',[],'Norm_SumDFF',[]);
for StimNum = 1:size(AllStim,1)
AllResponses(StimNum).Norm_DFF = [];
AllResponses(StimNum).Norm_SumDFF = [];
end
for nLine = 1:size(mOneROIData,1)
StimName = mOneROIData.StimName{nLine,1};
StimNum = find(strcmp(AllStim,StimName));
AllResponses(StimNum).Norm_DFF =...
[AllResponses(StimNum).Norm_DFF mOneROIData.GreenSignal_MaxDFF{nLine}/mOneROIData.MaxDFFAllStim{nLine}];
AllResponses(StimNum).Norm_SumDFF =...
[AllResponses(StimNum).Norm_SumDFF mOneROIData.GreenSignal_SumDFF{nLine}/mOneROIData.MaxSumDFFAllStim{nLine}];
end
figure(3), subplot(121),hold off
for ii = 1:length(Selected)
StimNum = Selected(ii);
for jj = 1:size(AllResponses(StimNum).Norm_DFF,2)
plot(ii,AllResponses(StimNum).Norm_DFF(jj),'ko'),hold on
end
M = mean(AllResponses(StimNum).Norm_DFF);
S = std(AllResponses(StimNum).Norm_DFF)/sqrt(length(AllResponses(StimNum).Norm_DFF));
h = plot(ii,M,'rd','MarkerSize',12);
h.MarkerFaceColor = [1 0 0];
plot([ii ii],[M-S M+S],'r-','LineWidth',3), hold on
end
title(['Variability between cells, ','Peak \deltaF/F'])
ylabel('Peak \deltaF/F')
ylim([0 1])
figure(3), subplot(122),hold off
for ii = 1:length(Selected)
StimNum = Selected(ii);
for jj = 1:size(AllResponses(StimNum).Norm_SumDFF,2)
plot(ii,AllResponses(StimNum).Norm_SumDFF(jj),'ko'),hold on
end
M = mean(AllResponses(StimNum).Norm_SumDFF);
S = std(AllResponses(StimNum).Norm_SumDFF)/sqrt(length(AllResponses(StimNum).Norm_SumDFF));
h = plot(ii,M,'rd','MarkerSize',12);
h.MarkerFaceColor = [1 0 0];
plot([ii ii],[M-S M+S],'r-','LineWidth',3), hold on
end
title(['Variability between cells, ','Sum \deltaF/F'])
ylabel('Sum \deltaF/F')
ylim([0 1])
%% variability between cells - R^2 between normalized traces of pairs of cells in the same fly
if ~strcmp(ROIname,'PC2Cell'), return, end
vFlies = unique(cell2mat(mOneROIData.Fly(:)));
mTraces = struct('Folder',[],'StimName',[],'mResponseAllCells',[],'mCorr',[],'vCorr',[],'vNormPeakResponse',[]);
for nFly = vFlies'
mOneFly = mOneROIData(cell2mat(mOneROIData.Fly(:))==nFly,:);
vAllStim = cell2mat(mOneFly.StimNumber(:));
vStimNumber = unique(vAllStim);
if mod(length(cell2mat(mOneFly.StimNumber(:))),length(vStimNumber))~=0
disp('Number of stimuli is not the same for each cell - must be some mistake.')
return
end
nCells = length(cell2mat(mOneFly.StimNumber(:)))/length(vStimNumber);
sFlyTraces = zeros(nCells,nIndex);
for nStim = vStimNumber'
nSameStimDifferentCells = find(vAllStim == nStim);
if ~(length(nSameStimDifferentCells) == nCells),disp('Number of repeats doesnt match number of cells'),return,end
for nCell = 1:nCells
nLine = nSameStimDifferentCells(nCell);
mTraces(nFly,nStim).Folder = mOneFly.Folder{nLine};
mTraces(nFly,nStim).StimName = mOneFly.StimName{nLine};
mTraces(nFly,nStim).mResponseAllCells(nCell,1:length(mOneFly.GreenSignal_DFFinROI{nLine})) =...
mOneFly.GreenSignal_DFFinROI{nLine}/mOneFly.MaxDFFAllStim{nLine};%Response is normalized to the max response over all stim for a given cell
mTraces(nFly,nStim).vNormPeakResponse(nCell) =...
mOneFly.GreenSignal_MaxDFF{nLine}/mOneFly.MaxDFFAllStim{nLine};
end
end
end
% add correlation between pairs of cells
for nFly = 1:size(mTraces,1)
for nSelectedStim = 1:size(AllStim,1)
sSelectedStim = AllStim{nSelectedStim};
for nStim = 1:size(mTraces,2)
if strcmp(mTraces(nFly,nStim).StimName,sSelectedStim),break,end
end
MCORR = corrcoef(mTraces(nFly,nStim).mResponseAllCells');
mTraces(nFly,nStim).mCorr = MCORR;
V = zeros(1,sum(1:size(MCORR,1)-1));
nIndex = 1;
for nLine = 1:size(MCORR,1)-1
V(nIndex:nIndex+size(MCORR,2)-nLine - 1) = MCORR(nLine,nLine+1:size(MCORR,2));
nIndex = nIndex + size(MCORR,2)-nLine;
end
mTraces(nFly,nStim).vCorr = V;%simply putting all the correlations between pairs in one row
%now, for the selected stim - make example figures, including
%correlation. Than sum correlations
%ToMala: example movie - different cells. Example trajectories (same
%movie) and statistics (for the selected stimuli).
end
end
mCorrOneStim = struct('corr',[]);
for nSelectedStim = I1 %order from largest to smallest peak response
nIndex = 1;
sSelectedStim = AllStim{nSelectedStim};
for nFly = 1:size(mTraces,1)
for nStim = 1:size(mTraces,2)
if strcmp(mTraces(nFly,nStim).StimName,sSelectedStim),break,end
end
%temporary exclude a fly with a lot of motion (in the future need to remove this recording alltogether)
if strcmp(mTraces(nFly,nStim).Folder,'Z:\Dudi\Imaging\2Photon\Diego_setup\DSX_2P_Playback\20161221\20161221_105'), continue,end%motion
if strcmp(mTraces(nFly,nStim).Folder,'Z:\Dudi\Imaging\2Photon\Diego_setup\DSX_2P_Playback\20170110\20170110_204'), continue,end%motion
%If ANY cell has a clear response - look at the correlation between the cells
if max(max(mTraces(nFly,nStim).mResponseAllCells,[],2)) < 0.5, continue, end
mCorrOneStim(nSelectedStim).corr(nIndex:nIndex+length(mTraces(nFly,nStim).vCorr)-1) = mTraces(nFly,nStim).vCorr;
%temp
if mean(mTraces(nFly,nStim).vCorr) < 0.85
disp([num2str(mean(mTraces(nFly,nStim).vCorr)) ,',Folder: ',mTraces(nFly,nStim).Folder])
disp(sSelectedStim)
end
%temp
nIndex = nIndex + length(mTraces(nFly,nStim).vCorr);
end
end
%plot
figure(4),clf
nLastIndex = 8;
for nIndex = 1:nLastIndex
nSelectedStim = I1(nIndex);
L = length(mCorrOneStim(nSelectedStim).corr);
x = nIndex + 0.1*rand(1,L);
y = mCorrOneStim(nSelectedStim).corr.^2;
plot(x,y,'.','MarkerSize',10), hold on
ylim([0 1.05]), xlim([0.5 nLastIndex+0.5])
title('Pairwise correlation of Calcium response in pC2 cells')
ylabel('R^{2}'), xlabel('Stimulus number [ordered by mean response amplitude]')
box off
h = gca;
h.XTick = 1:nLastIndex;
h.YTick = 0:0.2:1;
h.FontSize = 16;
hf = gcf;
hf.Color = 'white';
end