-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaAnalyze_prepData.m
More file actions
150 lines (119 loc) · 4.66 KB
/
caAnalyze_prepData.m
File metadata and controls
150 lines (119 loc) · 4.66 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
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
%% caAnalyze_prepData
function caAnalyze_prepData(analysis_dirs, ca_downsample, csp_ID, us_ID, ca_fileID)
for n = 1:size(analysis_dirs, 1)
current_data_dir = analysis_dirs{n};
cd(current_data_dir)
% Register files in current_data_dir
behDepot_search = dir('*_analyzed*');
ca_search = dir(ca_fileID);
cue_search = dir('*cue*.csv');
ts_search = dir('*timeStamps*.csv');
frlu_search = dir('*frlu*.mat');
% Cues_search = dir('*Cues*.mat');
skip = 0;
% If BehDepot output and calcium data are successfully detected
if size(behDepot_search, 1) == 1 && size(ca_search, 1) == 1
if size(frlu_search, 1) == 1
gen_frlu = 0;
frlu_file = frlu_search.name;
else
if size(ts_search, 1) == 2
gen_frlu = 1;
ts1 = ts_search(1).name;
ts2 = ts_search(2).name;
% Sort timestamp files into miniscope and behavior
if contains(ts1, 'v4', 'IgnoreCase', true) || contains(ts1, 'ms', 'IgnoreCase', true)
ms_ts_file = ts1;
beh_ts_file = ts2;
elseif contains(ts2, 'v4', 'IgnoreCase', true) || contains(ts2, 'ms', 'IgnoreCase', true)
ms_ts_file = ts2;
beh_ts_file = ts1;
end
else
skip = 1;
end
end
else
skip = 1;
end
if skip == 1
% Skip folder if skip is triggered
disp('BehDepot output and/or calcium traces not detected for this session')
disp('Skipping folder')
% Otherwise, run main analysis script
else
% Set required files aquired through search
behDepot_dir = behDepot_search.name;
ca_file = ca_search.name;
% Parse through cue files and ID csp and/or us files (should be ca
% aligned)
if size(cue_search, 1) > 0
csp_search = dir('*csp*.csv');
us_search = dir('*us*.csv');
if size(csp_search, 1) == 1
csp_file = csp_search.name;
end
if size(us_search, 1) == 1
us_file = us_search.name;
end
end
clearvars behDepot_search ca_search cue_search csp_search us_search frlu_search ts_search
%% Load Calcium Data
% Load signals from output file
signals = load(ca_file, 'sigfn', 'dff', 'spkfn');
total_ca_frames = size(signals.sigfn, 2);
%% Load Cue Script
%% OLD VERSION: Load 'cueframes' var from .mat file
% try
% load(cue_file, 'cueframes')
% cueframes;
% clearvars cueframes
% catch
% % Generate cueframes if not already present
% datetime = input('Enter video timestamp', 's')
% calcCueFrames(cue_file, 30, datetime);
% load(cue_file, 'cueframes')
% cueframes;
% clearvars cueframes
% end
%% NEW VERSION: Load csp and/or us CSV files
if exist('csp_file')
cues.csp = load(csp_file);
end
if exist('us_file')
cues.us = load(us_file);
end
clearvars csp_file us_file
%% Make general cue vectors (currently, tones (csp) and shocks (us))
Cues = genCueStruct(cues, total_ca_frames, ca_downsample, csp_ID, us_ID);
%% Load BehaviorDEPOT output data
cd(behDepot_dir)
load('Behavior.mat')
load('Metrics.mat')
cd(current_data_dir)
%% Load and/or generate frlu
% Prompt generation of lookup table, if not found
if gen_frlu == 1
f_tbl = prepFrluData(ms_ts_file, beh_ts_file);
[frlu, frlu_cols] = createFrlu(f_tbl, ca_downsample);
% Save lookup table at same path as v4 timestamps
save('frlu.mat', 'frlu', 'frlu_cols')
else
load(frlu_file);
end
%% Generate caBehavior Struct to Hold Aligned Behavior Data
caBehavior = beh2Mini(Behavior, Metrics, frlu, total_ca_frames);
%% Make Vectors for Combined Cues / Behaviors
% Combine all cue & behavior vectors and save in Features
Features = genFeatures(Cues, caBehavior);
%% Segment tone/shock cues into learning periods (for v4PMA settings)
% Collect labels for behavior session (learning / retrieval)
Cues = getSessionLabels(Cues, total_ca_frames);
% Make vectors in Features for each session period
Features = applySession(Cues, Features);
% Save session data to log struct
save('Features.mat', 'Features')
% Loop to next session in analysis_dirs
end
end
end