-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnovember_whiskey_bravo.m
248 lines (214 loc) · 8.26 KB
/
november_whiskey_bravo.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
%% Header
% Jake Westerberg, PhD (westerberg-science)
% Netherlands Institute for Neuroscience (prev. Vanderbilt University)
% jakewesterberg@gmail.com
% Requirements
% Certain aspects of the data processing require toolboxes found in other
% github repos. Original or forked versions of all required can be found on
% Jake's github page (westerberg-science). Kilosort (2 is used here) is
% required for spike sorting. Also, this of course requires the matnwb
% toolbox.
function november_whiskey_bravo(ID, varargin)
%% Defaults - need to ammend
skip_completed = true;
this_ident = []; % used to specify specific session(s) with their ident
%% Varargin
varStrInd = find(cellfun(@ischar,varargin));
for iv = 1:length(varStrInd)
switch varargin{varStrInd(iv)}
case {'skip'}
skip_completed = varargin{varStrInd(iv)+1};
case {'this_ident'}
this_ident = varargin{varStrInd(iv)+1};
end
end
%% pathing...can change to varargin or change function defaults for own machine
pp = pipeline_paths();
% add toolboxes
addpath(genpath(pp.TBOXES));
recording_info = readmatrix( ...
ID, ...
'OutputType', 'char', ...
'UseExcel', true, ...
'Range', 1);
recording_info = array2table( ...
recording_info(2:end,:), ...
'VariableNames', recording_info(1,:));
rif = fields(recording_info);
rif = rif(1:end-3);
for ii = rif'
if iscell(recording_info.(ii{:}))
if ~isnan(cellfun(@str2double, recording_info.(ii{:})))
recording_info.(ii{:}) = cellfun(@str2double, recording_info.(ii{:}));
end
end
end
% Create default processing list
to_proc = 1:length(unique(recording_info.Identifier));
% Limit to a specific session in a specific subject (ie identifier)
if ~isempty(this_ident)
for ii = 1 : numel(this_ident)
to_proc = find(strcmp(recording_info.Identifier, this_ident{ii}), 1);
end
end
n_procd = 0;
%% Loop through sessions
for ii = to_proc
% Skip files already processed if desired
if (exist([pp.DATA_DEST '_6_NWB_DATA' filesep recording_info.Identifier{ii} '.nwb'], 'file') & skip_completed) | ...
recording_info.Preprocessor_Ignore_Flag(ii) == 1
continue;
end
% Initialize nwb file
nwb = NwbFile;
nwb.identifier = recording_info.Identifier{ii};
nwb.session_start_time = datetime(datestr(datenum(num2str(recording_info.Session(ii)), 'yyyymmdd')));
nwb.general_experimenter = recording_info.Investigator{ii};
nwb.general_institution = recording_info.Institution{ii};
nwb.general_lab = recording_info.Lab{ii};
nwb.general_session_id = recording_info.Identifier{ii};
nwb.general_experiment_description = recording_info.Experiment_Description{ii};
nwb.file_create_date = datetime('today');
num_recording_devices = numel(unique(eval(['[' recording_info.Probe_System{ii} ']' ])));
% Dealing with atypical/preprocessed datasets
if strcmp(recording_info.Raw_Data_Format{ii}, 'AI NWB')
proc_AIC(pp, nwb, recording_info, ii)
continue
elseif strcmp(recording_info.Raw_Data_Format{ii}, 'NIN TDT')
proc_NDT(pp, nwb, recording_info, ii)
continue
end
if strcmp(recording_info.Raw_Data_Path{ii}(1:2), '\\') & isempty(findDir(pp.RAW_DATA, nwb.identifier))
pull_data(pp, recording_info(ii,:));
end
for rd = 1 : num_recording_devices
if strcmp(recording_info.Raw_Data_Format{ii}, 'Blackrock NSx') || ...
strcmp(recording_info.Raw_Data_Format{ii}, 'Blackrock Elche')
if ~exist([pp.CAT_DATA nwb.identifier '_dev-' num2str(rd-1)], 'dir')
proc_CAT(pp, nwb, rd, ii, recording_info);
end
end
end
[nwb, recdev, probe] = proc_PRO(pp, nwb, recording_info, ii, num_recording_devices);
for rd = 1 : num_recording_devices
if ~exist([pp.ART_DATA nwb.identifier '_dev-' num2str(rd-1)], 'dir') & ...
recording_info.Remove_Artifact(ii) == 1
proc_ART(pp, nwb, rd, ii, recording_info, recdev, probe);
end
end
probe_ctr = 0;
for rd = 1 : num_recording_devices
% Record analog traces
nwb = proc_AIO(pp, nwb, recdev{rd}, ii, recording_info);
% Digital events
nwb = proc_DIO(pp, nwb, recdev{rd});
% Loop through probes to setup nwb tables
for jj = 1 : sum(recdev{rd}.local_probes)
% LFP AND MUA CALC
nwb = proc_CDS(pp, nwb, recdev{rd}, probe{probe_ctr+1}, recording_info.Remove_Artifact(ii));
% BIN DATA
if ~exist([pp.BIN_DATA nwb.identifier filesep ...
nwb.identifier '_probe-' num2str(probe{probe_ctr+1}.num) ...
'.bin'], 'file')
proc_BIN(pp, nwb, recdev{rd}, probe{probe_ctr+1});
end
% SPIKE SORTING
nwb = proc_SPK(pp, nwb, recdev{rd}, probe{probe_ctr+1});
probe_ctr = probe_ctr + 1;
end
end
workers = feature('numcores')*2;
n_procd = n_procd + 1;
nwbExport(nwb, [pp.NWB_DATA nwb.identifier '.nwb']);
if strcmp(recording_info.Raw_Data_Path{ii}(1:2), '\\')
fid = fopen([pp.SCRATCH '\proc_push_data.bat'], 'w');
fprintf(fid, '%s\n', ...
['robocopy ' ...
pp.NWB_DATA ...
' ' ...
recording_info.Raw_Data_Path{ii} ...
' *' ...
[nwb.identifier '.nwb'] ...
' /j /np /mt:' ...
num2str(workers)]);
fclose('all');
system([pp.SCRATCH '\proc_push_data.bat']);
delete([pp.SCRATCH '\proc_push_data.bat']);
end
n_procd = n_procd + 1;
end
disp(['SUCCESSFULLY PROCESSED ' num2str(n_procd) ' FILES.'])
end
%% Deprecated (but maybe still useful) bits of code
% Read recording session information
% url_name = sprintf('https://docs.google.com/spreadsheets/d/%s/gviz/tq?tqx=out:csv&sheet=%s', ID, 1);
% recording_info = webread(url_name, webo);
%
% Process AIBS data
% if strcmp(recording_info.Raw_Data_Format{ii}, 'AI-NWB')
%
% raw_data_dir = [pp.RAW_DATA 'dandi' filesep '000253' ...
% filesep 'sub_' recording_info.Subject{ii} ...
% filesep 'sub_' recording_info.Subject{ii} ...
% 'sess_' num2str(recording_info.Session(ii)) ...
% filesep 'sub_' recording_info.Subject{ii} ...
% '+sess_' num2str(recording_info.Session(ii)) ...
% '_ecephys.nwb'];
%
% fpath = fileparts(raw_data_dir);
%
% if ~exist(raw_data_dir, 'file')
% warning('raw allen data not detected.')
% continue
% end
%
% copyfile(raw_data_dir, [pp.NWB_DATA recording_info.Identifier{ii} '.nwb'])
% nwb = nwbRead([pp.NWB_DATA recording_info.Identifier{ii} '.nwb']);
%
% proc_AIC(pp, nwb, recording_info, ii, fpath);
%
% n_procd = n_procd + 1;
% continue
%
% end
%
% Vanderbilt grab data
% for rd = 1 : num_recording_devices
%
% % RAW DATA
% fd1 = findDir(pp.RAW_DATA, nwb.identifier);
% fd2 = findDir(pp.RAW_DATA, ['dev-' num2str(rd-1)]);
% raw_data_present = sum(ismember(fd2, fd1));
% clear fd*
%
% if ~raw_data_present
%
% if (exist([pp.SCRATCH '\proc_grab_data.bat'],'file'))
% delete([pp.SCRATCH '\proc_grab_data.bat']);
% end
%
% fd1 = findDir(pp.DATA_SOURCE, nwb.identifier);
% fd2 = findDir(pp.DATA_SOURCE, ['dev-' num2str(rd-1)]);
% raw_data_temp = fd2(ismember(fd2, fd1));
% raw_data_temp = raw_data_temp{1};
%
% [~, dir_name_temp] = fileparts(raw_data_temp);
%
% % Grab data if missing
% workers = feature('numcores');
% fid = fopen([pp.SCRATCH '\proc_grab_data.bat'], 'w');
%
% fprintf(fid, '%s\n', ...
% ['robocopy ' ...
% raw_data_temp ...
% ' ' ...
% [pp.RAW_DATA dir_name_temp] ...
% ' /e /j /mt:' ...
% num2str(workers)]);
%
% fclose('all');
% system([pp.SCRATCH '\proc_grab_data.bat']);
% delete([pp.SCRATCH '\proc_grab_data.bat']);
%
% end
% end