forked from lauradriscoll/pre_process_shared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectivity_measures_210630.m
257 lines (231 loc) · 12 KB
/
selectivity_measures_210630.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
%% ATK 201030 for loading from HD
% Add MI with choice info 210629
% Adds ROC AUC metrics (from Najafi et al. 2019)
% updated 210201 to also include significance testing from
% older RL selectivity measure from Harvey et al. 2012
% ATK 220625 ATK adding epoch specific select index MI
% ATK 230606 ATK edit to save choiceMI significance for each timepoint
masterPath = "/Volumes/Aaron's PPC/ppc/2P_data/";
mouse = 'LD187';
%mySessions = {'LD187_141216','LD187_141215','LD187_141214','LD187_141213','LD187_141212',...
% 'LD187_141211','LD187_141210','LD187_141209','LD187_141208','LD187_141207','LD187_141206'};
mySessions = {'LD187_141216','LD187_141215','LD187_141214','LD187_141213',};
%mySessions = {'LD187_141215','LD187_141214','LD187_141213','LD187_141212',...
% 'LD187_141211','LD187_141210','LD187_141209','LD187_141208','LD187_141207','LD187_141206'};
for i = 1:length(mySessions)
session = cell2mat(mySessions(i));
disp(session)
% Load trial aligned data
syncedDataDir = fullfile(masterPath, 'code_workspace',mouse,'syncedData');
trialAlignedData = open(fullfile(syncedDataDir,[session '.mat'])).trialAlignedData;
%{
%% Calc RL metrics ROC
disp('Calulating ROC selectivity index metrics');
lr_trials = ismember(trialAlignedData.trialType,[2,3]);
trial_types = trialAlignedData.trialType(lr_trials);
num_trials = length(trial_types);
fullTrial_Act = nanmean(trialAlignedData.CaData(:,lr_trials,14:76),3);
%
% define blocks
num_blocks = 5;
blocks_Act = NaN([size(fullTrial_Act),5]);
blocks_Act(:,:,1) = nanmean(trialAlignedData.CaData(:,lr_trials,14:26),3); % 14 is running onset + 12 frames after
blocks_Act(:,:,2) = nanmean(trialAlignedData.CaData(:,lr_trials,27:38),3); % 12 frames before cue offset (frame 39)
blocks_Act(:,:,3)= nanmean(trialAlignedData.CaData(:,lr_trials,39:51),3); % 39 is cue offset + 12 frames after
blocks_Act(:,:,4)= nanmean(trialAlignedData.CaData(:,lr_trials,52:64),3); % 12 frames before end of trial (turn a certain amt)
blocks_Act(:,:,5) = nanmean(trialAlignedData.CaData(:,lr_trials,65:76),3); % Trial end (reward given) an 12 frames after (dark, ITI)
blocks_labels = {'cueEarly_Act','cueLate_Act','delayEarly_Act','delayTurn_Act','turnITI_Act'};
% create shuffle distribution of trial types
num_shuf = 20;
trial_types_shuf = NaN(num_shuf, num_trials);
for shuf_idx = 1:num_shuf
trial_types_shuf(shuf_idx,:) = trial_types(randperm(length(trial_types)));
end
% Calculate ROC AUC metrics
num_masks = size(fullTrial_Act,1);
AUC_fullTrial = NaN(num_masks,1);
AUC_fullTrial_shuf = NaN(num_masks, num_blocks, num_shuf);
selectivity_AUC = NaN(num_masks,1);
AUC_blocks = NaN(num_masks,num_blocks);
AUC_blocks_shuf = NaN(num_masks,num_blocks,num_shuf);
selectivity_AUC_blocks = NaN(num_blocks,num_blocks);
for mask_idx = 1:size(fullTrial_Act,1)
[X,Y,T,AUC_fullTrial(mask_idx)] = perfcurve(trial_types,fullTrial_Act(mask_idx,:),2);
for blk_idx = 1:5
[X,Y,T,AUC_blocks(mask_idx,blk_idx)] = perfcurve(trial_types,blocks_Act(mask_idx,:,blk_idx),2);
end
for shuf_idx = 1:num_shuf
[X,Y,T,AUC_fullTrial_shuf(mask_idx,shuf_idx)] = perfcurve(trial_types_shuf(shuf_idx,:),fullTrial_Act(mask_idx,:),2);
for blk_idx = 1:num_blocks
[X,Y,T,AUC_blocks_shuf(mask_idx,blk_idx,shuf_idx)] = perfcurve(trial_types_shuf(shuf_idx,:),blocks_Act(mask_idx,:,blk_idx),2);
end
end
disp(['AUC for mask ' num2str(mask_idx) ' is ' num2str(AUC_fullTrial(mask_idx)) ' vs shuffle: ' num2str(nanmean(AUC_fullTrial_shuf(mask_idx,:)))]);
if prctile(AUC_fullTrial_shuf(mask_idx,:),2.5)>AUC_fullTrial(mask_idx)
selectivity_AUC(mask_idx) = 2;
elseif prctile(AUC_fullTrial_shuf(mask_idx,:),97.5)<AUC_fullTrial(mask_idx)
selectivity_AUC(mask_idx) = 3;
else
selectivity_AUC(mask_idx) = 0;
end
for blk_idx = 1:num_blocks
if prctile(AUC_blocks_shuf(mask_idx,blk_idx,:),2.5)>AUC_blocks(mask_idx,blk_idx)
selectivity_AUC_blocks(mask_idx,blk_idx) = 2;
elseif prctile(AUC_blocks_shuf(mask_idx,blk_idx,:),97.5)<AUC_blocks(mask_idx,blk_idx)
selectivity_AUC_blocks(mask_idx,blk_idx) = 3;
else
selectivity_AUC_blocks(mask_idx,blk_idx) = 0;
end
end
end
% Calculate RL metrics (Harvey 2012)
disp('Calulating RL selectivity index metrics');
num_masks = size(fullTrial_Act,1);
RL_fullTrial = NaN(num_masks,1);
RL_fullTrial_shuf = NaN(num_masks, num_blocks, num_shuf);
selectivity_RL = NaN(num_masks,1);
RL_blocks = NaN(num_masks,num_blocks);
RL_blocks_shuf = NaN(num_masks,num_blocks, num_shuf);
selectivity_RL_blocks = NaN(num_blocks,num_blocks);
for mask_idx = 1:size(fullTrial_Act,1)
R_tr_mean = nanmean(fullTrial_Act(mask_idx,trial_types==2));
L_tr_mean = nanmean(fullTrial_Act(mask_idx,trial_types==3));
RL_fullTrial(mask_idx) = (R_tr_mean - L_tr_mean)/(R_tr_mean + L_tr_mean);
for blk_idx = 1:5
R_tr_mean_blocks = nanmean(blocks_Act(mask_idx,trial_types==2,blk_idx));
L_tr_mean_blocks = nanmean(blocks_Act(mask_idx,trial_types==3,blk_idx));
RL_blocks(mask_idx,blk_idx) = (R_tr_mean_blocks - L_tr_mean_blocks)/(R_tr_mean_blocks + L_tr_mean_blocks);
end
for shuf_idx = 1:num_shuf
R_tr_mean = nanmean(fullTrial_Act(mask_idx,trial_types_shuf(shuf_idx,:)==2));
L_tr_mean = nanmean(fullTrial_Act(mask_idx,trial_types_shuf(shuf_idx,:)==3));
RL_fullTrial_shuf(mask_idx,shuf_idx) = (R_tr_mean - L_tr_mean)/(R_tr_mean + L_tr_mean);
for blk_idx = 1:num_blocks
R_tr_mean_blocks = nanmean(blocks_Act(trial_types_shuf(shuf_idx,:)==2,blk_idx));
L_tr_mean_blocks = nanmean(blocks_Act(trial_types_shuf(shuf_idx,:)==3,blk_idx));
RL_blocks_shuf(mask_idx,blk_idx) = (R_tr_mean_blocks - L_tr_mean_blocks)/(R_tr_mean_blocks + L_tr_mean_blocks);
end
end
disp(['RL for mask ' num2str(mask_idx) ' is ' num2str(RL_fullTrial(mask_idx)) ' vs shuffle: ' num2str(nanmean(RL_fullTrial_shuf(mask_idx,:)))]);
if prctile(RL_fullTrial_shuf(mask_idx,:),2.5)>RL_fullTrial(mask_idx)
selectivity_RL(mask_idx) = 2;
elseif prctile(AUC_fullTrial_shuf(mask_idx,:),97.5)<AUC_fullTrial(mask_idx)
selectivity_RL(mask_idx) = 3;
else
selectivity_RL(mask_idx) = 0;
end
for blk_idx = 1:num_blocks
if prctile(RL_blocks_shuf(mask_idx,blk_idx,:),2.5)>RL_blocks(mask_idx,blk_idx)
selectivity_RL_blocks(mask_idx,blk_idx) = 2;
elseif prctile(RL_blocks_shuf(mask_idx,blk_idx,:),97.5)<RL_blocks(mask_idx,blk_idx)
selectivity_RL_blocks(mask_idx,blk_idx) = 3;
else
selectivity_RL_blocks(mask_idx,blk_idx) = 0;
end
end
end
%}
%% Calc Mutual Information with trial type
% requires https://github.com/nmtimme/Neuroscience-Information-Theory-Toolbox
% ATK 210616
DataRaster = cell(1);
% Format trialAlignedData for info theory toolbox
trialTypes = trialAlignedData.trialType;
trialTypes(1,1) = nan; %exclude first trial (has nans in ITI)
% Only include trial types 2 and 3 (R and L)
% 1 - Ca Data
DataRaster{1} = permute(trialAlignedData.CaData(:,ismember(trialTypes,[2,3]),:),[1,3,2]);
% 2 - trial types
DataRaster{2}(1,:,:) = repmat(trialTypes(:,ismember(trialTypes, [2,3]))', [1,76])';
% Binary binning (active or not)
numBins=2;
numROIs = length(DataRaster{1});
StatesRaster{1} = int8(DataRaster{1}>0);
StatesRaster{2} = DataRaster{2};
% Uniform Binning
%{
numBins = 4; % bin into 20 uniform width bins
numROIs = length(DataRaster{1});
MethodAssign = cell(numROIs, 4);
for i = 1:numROIs
MethodAssign(i,:) = {1, i,'UniWB',{numBins}};
end
StatesRaster = data2states(DataRaster, MethodAssign);
%}
% Calculate MI with trial type
disp('Calculating MI with trial type')
choiceMI = nan(numROIs, 76);
for id = 1:numROIs
for t = 1:76
VariableIDs = {1,id,t;2,1,1};
choiceMI(id, t) = instinfo(StatesRaster, 'PairMI', VariableIDs);
end
end
[choiceMI_max, choiceMI_max_idx] = max(choiceMI, [], 2);
% R or L preferring at max?
choiceMI_pref = nan(numROIs, 1);
for id = 1:numROIs
choiceMI_pref(id) = mean(StatesRaster{1}(id,choiceMI_max_idx(id),StatesRaster{2}(1,choiceMI_max_idx(id),:)==2))...
- mean(StatesRaster{1}(id,choiceMI_max_idx(id),StatesRaster{2}(1,choiceMI_max_idx(id),:)==3));
end
% Calc significance of maxMI by shuffling trial types
disp('Calculating shuffle distributions')
numShuf = 10;
numTrials = size(StatesRaster{2},3);
choiceMI_thresh_t = nan(numROIs, 76);
choiceMI_prctile_t = nan(numROIs, 76);
choiceMI_thresh = nan(numROIs,1);
choiceMI_prctile = nan(numROIs,1);
p_thresh = 95; % 95% for significance
%p_thresh = 99; % 95% for significance
for id = 1:numROIs
choiceMI_shuf = nan(numShuf,t);
for t = 1:76
for shuf_idx = 1:numShuf
% shuffle trial types vector
StatesRasterShuf = StatesRaster;
StatesRasterShuf{2} = StatesRasterShuf{2}(:,:,randperm(numTrials));
choiceMI_shuf(shuf_idx,t) = instinfo(StatesRasterShuf,'PairMI', {1,id,t;2,1,1});
end
choiceMI_thresh_t(id, t) = prctile(choiceMI_shuf(:,t),p_thresh);
end
[choiceMI_shuf_max, choiceMI_shuf_max_idx] = max(choiceMI_shuf, [], 2);
choiceMI_thresh(id) = prctile(choiceMI_shuf_max, p_thresh);
nless = sum(choiceMI_shuf_max < choiceMI_max(id));
choiceMI_prctile(id) = nless / numShuf;
%disp(['MI for mask ' num2str(id) ' is ' num2str(choiceMI_max(id)) ' vs shuffle: ' num2str(nanmean(choiceMI_shuf_max))]);
disp(['Calc MI shuf for ROI ' num2str(id) ' Mean MI shuf: ' num2str(mean(choiceMI_thresh_t(id,:))) ]);
end
maxMI_sig = choiceMI_max > choiceMI_thresh;
choiceMI_sig_t = choiceMI > choiceMI_thresh_t;
%% Plot significant peaks as function of timepoint
frac_sig = nan(76,1);
for i = 1:76
frac_sig(i) = sum(maxMI_sig(find(choiceMI_max_idx==i)))/sum(choiceMI_max_idx==i);
num_sig(i) = sum(maxMI_sig(find(choiceMI_max_idx==i)));
end
disp([session ' ' num2str(sum(maxMI_sig>0)/numROIs) ' of ROIs are sig MI'])
%figure; plot(num_sig); xlabel('trial timepoint'); ylabel('# ROIs with sig MI with trial type');title(session);
%xline(13.5,'k');xline(26.5,'k');xline(38.5,'k--');xline(51.5,'k');xline(64.5,'k--');
figure; plot(frac_sig); xlabel('trial timepoint'); ylabel('frac of ROIs with sig MI with trial type');title(session);
xline(13.5,'k');xline(26.5,'k');xline(38.5,'k--');xline(51.5,'k');xline(64.5,'k--');
%% Plot histogram of MI values for non-sig choiceMI
figure; histogram(choiceMI(~choiceMI_sig_t));
%% Save data as .mat
output_dir = fullfile(masterPath, 'code_workspace',mouse,'selectMetrics');
%save(fullfile(output_dir,session),'AUC_fullTrial', 'AUC_blocks', 'RL_fullTrial', 'RL_blocks',...
% 'selectivity_AUC','selectivity_AUC_blocks','selectivity_RL','selectivity_RL_blocks',...
% 'choiceMI','choiceMI_max','choiceMI_max_idx','maxMI_sig','choiceMI_prctile','choiceMI_pref','-v7.3');
save(fullfile(output_dir,session),'choiceMI','choiceMI_max','choiceMI_max_idx','maxMI_sig',...
'choiceMI_prctile','choiceMI_pref','choiceMI_sig_t','-v7.3');
figure; plot(sum(selectivity_AUC_blocks == 2),'o-'); hold on;
plot(sum(selectivity_AUC_blocks == 3),'o-');
plot(sum(selectivity_AUC_blocks == 0),'o-');
legend({'left','right','non selective'});
end
%%
figure;
plot(trialAlignedData.RL_selectIdx,choiceMI_max,'.');
ylabel('choice MI peak (bits)');
xlabel('RL select Idx');
%%