Repository navigation
Expand file tree
/
Copy pathsummarizeActivityStateTransitions.m
More file actions
111 lines (100 loc) · 4.25 KB
/
Copy pathsummarizeActivityStateTransitions.m
File metadata and controls
111 lines (100 loc) · 4.25 KB
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
function T = summarizeActivityStateTransitions(rootDir, varargin)
% summarizeActivityStateTransitions Pool activity-state transition results
% from many samples into one summary table and CSV.
%
% OPTIONAL NeuroSpikeX add-on. Recursively searches rootDir for the
% 'newActivityData_*.mat' files written by analyzeActivityStateTransitions.m
% and tabulates the four transition counts for each sample and comparison.
%
% USAGE
% T = summarizeActivityStateTransitions('W:\...\MyExperiment');
%
% INPUT
% rootDir - top folder to search recursively (e.g. a condition folder that
% contains many sample folders).
%
% NAME-VALUE OPTIONS
% 'OutputFile' - CSV path (default <rootDir>/ActivityStateTransition_Summary.csv)
%
% OUTPUT
% T - table with one row per newActivityData file: sample folder, comparison,
% from/to timepoints, the four category counts, and the total classified.
p = inputParser;
p.addParameter('OutputFile', fullfile(rootDir,'ActivityStateTransition_Summary.csv'));
p.parse(varargin{:});
opt = p.Results;
fprintf('Searching for newActivityData files under:\n %s\n', rootDir);
files = dir(fullfile(rootDir, '**', 'newActivityData_*.mat'));
fprintf('Found %d file(s).\n', numel(files));
T = table();
for i = 1:numel(files)
srcFile = fullfile(files(i).folder, files(i).name);
S = load(srcFile);
fn = fieldnames(S);
hit = fn(contains(fn,'newActivityData','IgnoreCase',true));
if isempty(hit)
warning('No newActivityData variable in %s. Skipping.', srcFile);
continue;
end
A = S.(hit{1});
[fromTP, toTP, comparison] = inferComparison(A, files(i).name);
sampleFolder = inferSampleFolder(files(i).folder);
nStayedActive = countCat(A, 'stayed_active');
nBecameActive = countCat(A, 'became_active');
nBecameInactive = countCat(A, 'became_inactive');
nStayedInactive = countCat(A, 'stayed_inactive');
total = sum([nStayedActive nBecameActive nBecameInactive nStayedInactive], 'omitnan');
row = table(string(sampleFolder), string(comparison), string(fromTP), string(toTP), ...
nStayedActive, nBecameActive, nBecameInactive, nStayedInactive, total, string(srcFile), ...
'VariableNames', {'sample_folder','comparison','from_timepoint','to_timepoint', ...
'n_stayed_active','n_became_active','n_became_inactive','n_stayed_inactive', ...
'total_classified_cells','source_file'});
T = [T; row]; %#ok<AGROW>
end
if height(T) > 0
T = sortrows(T, {'sample_folder','comparison'});
writetable(T, opt.OutputFile);
fprintf('Saved summary (%d rows):\n %s\n', height(T), opt.OutputFile);
else
fprintf('No summary rows written.\n');
end
end
%% ======================= local helpers =======================
function [fromTP, toTP, comparison] = inferComparison(A, fileName)
fromTP = ''; toTP = ''; comparison = '';
if isstruct(A)
if isfield(A,'fromTag'), fromTP = char(A.fromTag); end
if isfield(A,'toTag'), toTP = char(A.toTag); end
if isfield(A,'comparison'), comparison = char(A.comparison); end
end
if isempty(fromTP) || isempty(toTP)
% Fall back to parsing "newActivityData_<from>_<to>.mat"
tok = regexp(fileName, 'newActivityData_(.+?)_(.+)\.mat$', 'tokens', 'once');
if ~isempty(tok)
fromTP = tok{1}; toTP = tok{2};
end
end
if isempty(comparison) && ~isempty(fromTP)
comparison = sprintf('%s_to_%s', fromTP, toTP);
end
end
function n = countCat(A, fieldName)
n = NaN;
if ~isstruct(A) || ~isfield(A, fieldName), return; end
F = A.(fieldName);
if isempty(F), n = 0; return; end
if isstruct(F)
if isfield(F,'numCells'), n = F.numCells; return; end
if isfield(F,'cellNum'), n = size(F.cellNum,1); return; end
end
end
function sampleFolder = inferSampleFolder(folderStr)
% If results live in an 'AdditionalAnalyses' folder, the sample root is its
% parent; otherwise use the containing folder name.
[parent, name] = fileparts(folderStr);
if strcmpi(name, 'AdditionalAnalyses')
[~, sampleFolder] = fileparts(parent);
else
sampleFolder = name;
end
end