Skip to content

Commit

Permalink
interneuron classification for full cohort
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyasterjones committed Jun 23, 2023
1 parent 81a353e commit c39e06e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 65 deletions.
83 changes: 52 additions & 31 deletions ProcessCellMetrics.m
Original file line number Diff line number Diff line change
Expand Up @@ -778,32 +778,32 @@
if any(contains(parameters.metrics,{'monoSynaptic_connections','all'})) && ~any(contains(parameters.excludeMetrics,{'monoSynaptic_connections'}))
spkExclu = setSpkExclu('monoSynaptic_connections',parameters);
dispLog('MonoSynaptic connections',basename)
if ~exist(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'file')
% if ~exist(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'file')
mono_res = ce_MonoSynConvClick(spikes{spkExclu},'includeInhibitoryConnections',parameters.includeInhibitoryConnections,'sr',sr);
if parameters.manualAdjustMonoSyn
dispLog('Loading MonoSynaptic GUI for manual adjustment',basename)
mono_res = gui_MonoSyn(mono_res);
end
save(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'mono_res','-v7.3','-nocompression');
else
disp(' Loading previous detected MonoSynaptic connections')
load(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'mono_res');
if parameters.includeInhibitoryConnections && (~isfield(mono_res,'sig_con_inhibitory') || (isfield(mono_res,'sig_con_inhibitory') && isempty(mono_res.sig_con_inhibitory_all)))
disp(' Detecting MonoSynaptic inhibitory connections')
mono_res_old = mono_res;
mono_res = ce_MonoSynConvClick(spikes{spkExclu},'includeInhibitoryConnections',parameters.includeInhibitoryConnections,'sr',sr);
mono_res.sig_con_excitatory = mono_res_old.sig_con;
mono_res.sig_con = mono_res_old.sig_con;
if parameters.manualAdjustMonoSyn
dispLog('Loading MonoSynaptic GUI for manual adjustment',basename)
mono_res = gui_MonoSyn(mono_res);
end
save(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'mono_res','-v7.3','-nocompression');
elseif parameters.forceReload == true && parameters.manualAdjustMonoSyn
mono_res = gui_MonoSyn(mono_res);
save(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'mono_res','-v7.3','-nocompression');
end
end
% save(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'mono_res','-v7.3','-nocompression');
% else
% disp(' Loading previous detected MonoSynaptic connections')
% load(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'mono_res');
% if parameters.includeInhibitoryConnections && (~isfield(mono_res,'sig_con_inhibitory') || (isfield(mono_res,'sig_con_inhibitory') && isempty(mono_res.sig_con_inhibitory_all)))
% disp(' Detecting MonoSynaptic inhibitory connections')
% mono_res_old = mono_res;
% mono_res = ce_MonoSynConvClick(spikes{spkExclu},'includeInhibitoryConnections',parameters.includeInhibitoryConnections,'sr',sr);
% mono_res.sig_con_excitatory = mono_res_old.sig_con;
% mono_res.sig_con = mono_res_old.sig_con;
% if parameters.manualAdjustMonoSyn
% dispLog('Loading MonoSynaptic GUI for manual adjustment',basename)
% mono_res = gui_MonoSyn(mono_res);
% end
% save(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'mono_res','-v7.3','-nocompression');
% elseif parameters.forceReload == true && parameters.manualAdjustMonoSyn
% mono_res = gui_MonoSyn(mono_res);
% save(fullfile(basepath,[basename,'.mono_res',erase(parameters.saveAs,'cell_metrics'),'.cellinfo.mat']),'mono_res','-v7.3','-nocompression');
% end
% end

field2remove = {'putativeConnections'};
test = isfield(cell_metrics,field2remove);
Expand Down Expand Up @@ -838,16 +838,25 @@
disp(' Determining transmission probabilities')
ccg2 = mono_res.ccgR;
ccg2(isnan(ccg2)) = 0;
total_time = mono_res.ccgR_bins(end) - mono_res.ccgR_bins(1);

% Excitatory connections
for i = 1:size(cell_metrics.putativeConnections.excitatory,1)
[trans,prob,prob_uncor,pred] = ce_GetTransProb(ccg2(:,cell_metrics.putativeConnections.excitatory(i,1),cell_metrics.putativeConnections.excitatory(i,2)), spikes{spkExclu}.total(cell_metrics.putativeConnections.excitatory(i,1)), mono_res.binSize, 0.020);
[trans,~,~] = ce_GetTransProb(ccg2(:,cell_metrics.putativeConnections.excitatory(i,1),...
cell_metrics.putativeConnections.excitatory(i,2)),...
spikes{spkExclu}.total(cell_metrics.putativeConnections.excitatory(i,1)),...
spikes{spkExclu}.total(cell_metrics.putativeConnections.excitatory(i,2))/total_time,...
mono_res.binSize, 0.020);
cell_metrics.putativeConnections.excitatoryTransProb(i) = trans;
end

% Inhibitory connections
for i = 1:size(cell_metrics.putativeConnections.inhibitory,1)
[trans,prob,prob_uncor,pred] = ce_GetTransProb(ccg2(:,cell_metrics.putativeConnections.inhibitory(i,1),cell_metrics.putativeConnections.inhibitory(i,2)), spikes{spkExclu}.total(cell_metrics.putativeConnections.inhibitory(i,1)), mono_res.binSize, 0.020);
[trans,~,~] = ce_GetTransProb(ccg2(:,cell_metrics.putativeConnections.inhibitory(i,1),...
cell_metrics.putativeConnections.inhibitory(i,2)),...
spikes{spkExclu}.total(cell_metrics.putativeConnections.inhibitory(i,1)),...
spikes{spkExclu}.total(cell_metrics.putativeConnections.inhibitory(i,2))/total_time,...
mono_res.binSize, 0.020);
cell_metrics.putativeConnections.inhibitoryTransProb(i) = trans;
end

Expand All @@ -858,18 +867,30 @@
ccg2(isnan(ccg2)) = 0;

for i = 1:size(cell_metrics.putativeConnections.excitatory,1)
exc_idx = cell_metrics.putativeConnections.excitatory(i,1);
st_bin_idx = spikes{spkExclu}.times{exc_idx}>mono_res.ccgR_bins(b) & spikes{spkExclu}.times{exc_idx}<=mono_res.ccgR_bins(b+1);
[trans,~,~,~] = ce_GetTransProb(ccg2(:,exc_idx,cell_metrics.putativeConnections.excitatory(i,2)),...
length(spikes{spkExclu}.times{exc_idx}(st_bin_idx)), mono_res.binSize, 0.020);
ref_idx = cell_metrics.putativeConnections.excitatory(i,1);
target_idx = cell_metrics.putativeConnections.excitatory(i,2);
ref_st_idx = spikes{spkExclu}.times{ref_idx}>mono_res.ccgR_bins(b) & spikes{spkExclu}.times{ref_idx}<=mono_res.ccgR_bins(b+1);
target_st_idx = spikes{spkExclu}.times{target_idx}>mono_res.ccgR_bins(b) & spikes{spkExclu}.times{target_idx}<=mono_res.ccgR_bins(b+1);
[trans,trans_fr_corr,trans_prior_corr] = ce_GetTransProb(ccg2(:,ref_idx,target_idx),...
length(spikes{spkExclu}.times{ref_idx}(ref_st_idx)),...
length(spikes{spkExclu}.times{target_idx}(target_st_idx))/10,...
mono_res.binSize, 0.020);
cell_metrics.putativeConnections.excitatoryTransProb_binned(b,i) = trans;
cell_metrics.putativeConnections.excitatoryTransProbFRCorr_binned(b,i) = trans_fr_corr;
cell_metrics.putativeConnections.excitatoryTransProbPriorCorr_binned(b,i) = trans_prior_corr;
end
for i = 1:size(cell_metrics.putativeConnections.inhibitory,1)
inh_idx = cell_metrics.putativeConnections.inhibitory(i,1);
st_bin_idx = spikes{spkExclu}.times{inh_idx}>mono_res.ccgR_bins(b) & spikes{spkExclu}.times{inh_idx}<=mono_res.ccgR_bins(b+1);
[trans,~,~,~] = ce_GetTransProb(ccg2(:,inh_idx,cell_metrics.putativeConnections.inhibitory(i,2)),...
length(spikes{spkExclu}.times{inh_idx}(st_bin_idx)), mono_res.binSize, 0.020);
ref_idx = cell_metrics.putativeConnections.inhibitory(i,1);
target_idx = cell_metrics.putativeConnections.inhibitory(i,2);
ref_st_idx = spikes{spkExclu}.times{ref_idx}>mono_res.ccgR_bins(b) & spikes{spkExclu}.times{ref_idx}<=mono_res.ccgR_bins(b+1);
target_st_idx = spikes{spkExclu}.times{target_idx}>mono_res.ccgR_bins(b) & spikes{spkExclu}.times{target_idx}<=mono_res.ccgR_bins(b+1);
[trans,trans_fr_corr,trans_prior_corr] = ce_GetTransProb(ccg2(:,ref_idx,target_idx),...
length(spikes{spkExclu}.times{ref_idx}(ref_st_idx)),...
length(spikes{spkExclu}.times{target_idx}(target_st_idx))/10,...
mono_res.binSize, 0.020);
cell_metrics.putativeConnections.inhibitoryTransProb_binned(b,i) = trans;
cell_metrics.putativeConnections.inhibitoryTransProbFRCorr_binned(b,i) = trans_fr_corr;
cell_metrics.putativeConnections.inhibitoryTransProbPriorCorr_binned(b,i) = trans_prior_corr;
end
end
else
Expand Down
2 changes: 1 addition & 1 deletion calc_CellMetrics/sessionTemplate.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
% You can have multiple sets of spike sorted data. The first set is loaded by default
if ~isfield(session,'spikeSorting')
% Looks for a Kilosort output folder (generated by the KiloSortWrapper)
kiloSortFolder = dir('Kilosort_*');
kiloSortFolder = dir('imec*_ks');
% Extract only those that are directories.
kiloSortFolder = kiloSortFolder(kiloSortFolder.isdir);
if ~isempty(kiloSortFolder)
Expand Down
92 changes: 59 additions & 33 deletions run_CellExplorer.m
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
%% set session
animal = 'BaggySweatpants';
session_name = '20220214_BaggySweatpants_DY01';
basepath = ['Z:\WT_Sequences\2022_winter\Preprocessed_Data\Spikes\g1\',animal,'\Ecephys\',...
session_name,'\catgt_',session_name,'_g1\',session_name,'_g1_imec0'];
cd(basepath)
addpath(genpath("C:\Users\Niflheim\Documents\GitHub\External\CellExplorer"))

%% calculate
tic
% % set params
session = sessionTemplate(basepath);
session.extracellular.fileName = [session_name,'_g1_tcat.imec0.ap.bin'];
session.extracellular.srLfp = 2500;
% session = gui_session(session);
% validateSessionStruct(session);
% set session
sessions = readtable('D:\WT_Sequences\all_sessions.csv');
start_gate = 1;
probe = 0;
for s = 1:height(sessions)
task = string(sessions{s,'Task'});
rec_error = string(sessions{s,'Recording_Error'});
if strcmp(task, 'X Maze') && strcmp(rec_error, 'FALSE')

% generate metrics file
cell_metrics = ProcessCellMetrics('session', session,...
'metrics',{'monoSynaptic_connections'},...
'includeInhibitoryConnections',true,...
'manualAdjustMonoSyn',false,...
'getWaveformsFromDat',false,...
'showWaveforms',false,...
'sessionSummaryFigure',false,...
'showGUI',false);
toc
% generate the path to the directory containing the ap.bin file
base_dir = string(sessions{s,'Base_Directory'});
% base_dir = strrep(base_dir, 'Z', 'D'); %while running off local drive
base_dir = strrep(base_dir, '/', '\');
ecephys_path = strcat(base_dir, '\Preprocessed_Data\Spikes\g1');

rec_file_stem = split(string(sessions{s,'File'}),'/');
rec_file_stem = rec_file_stem(2);
rec_file_stem = convertStringsToChars(rec_file_stem);
rec_file_stem = rec_file_stem(1:end-3);
rec_file_path = sprintf('%s\\%s\\Ecephys\\%s\\catgt_%s_g%d\\%s_g%d_imec%d',...
ecephys_path, string(sessions{s,'Animal'}),...
rec_file_stem, rec_file_stem, start_gate,...
rec_file_stem, start_gate, probe);
cd(rec_file_path)

% set params
tic
session = sessionTemplate(rec_file_path);
% pass the ap.bin file name (not the path)
session.extracellular.fileName = sprintf('%s_g%d_tcat.imec%d.ap.bin', rec_file_stem, start_gate, probe);

% generate metrics file
% session.extracellular.srLfp = 2500; %only necessary if running LFP metrics

% %% view
% cell_metrics = CellExplorer('metrics',cell_metrics);
%
% %% view from file
% animal = 'BaggySweatpants';
% session_name = '20220214_BaggySweatpants_DY01';
% basepath = ['Z:\WT_Sequences\2022_winter\Preprocessed_Data\Spikes\g1\',animal,'\Ecephys\',...
% session_name,'\catgt_',session_name,'_g1\',session_name,'_g1_imec0'];
% cd(basepath)
% monosynaptic connections only (fast)
% cell_metrics = ProcessCellMetrics('session', session,...
% 'metrics',{'monoSynaptic_connections'},...
% 'includeInhibitoryConnections',true,...
% 'manualAdjustMonoSyn',false,...
% 'getWaveformsFromDat',false,...
% 'showWaveforms',false,...
% 'sessionSummaryFigure',false,...
% 'showGUI',false);
% all interneuron metrics (slow_
cell_metrics = ProcessCellMetrics('session', session,...
'metrics',{'monoSynaptic_connections','waveform_metrics','acg_metrics'},...
'includeInhibitoryConnections',true,...
'manualAdjustMonoSyn',false,...
'showWaveforms',false,...
'sessionSummaryFigure',false,...
'showGUI',false, ...
'keepCellClassification',false);
% display GUI
% cell_metrics = CellExplorer('metrics',cell_metrics);
toc
end
end

%% view from file
% cd(rec_file_path)
% session = loadSession;
% cell_metrics = loadCellMetrics;
% cell_metrics = CellExplorer('metrics',cell_metrics);

0 comments on commit c39e06e

Please sign in to comment.