-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRN_runTREXpath.m
More file actions
executable file
·211 lines (194 loc) · 6.75 KB
/
RN_runTREXpath.m
File metadata and controls
executable file
·211 lines (194 loc) · 6.75 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
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
function [out] = RN_runTREXpath(exPath,exDat,maxJobs)
out = '';
curr_dir = pwd;
%% Check Path for validity
[valid,suggested] = RN_checkExPath(exPath);
if ~valid
out = [];
msgTxt = sprintf('Extraction Path is invalid! Cancelling...\n\nSuggested Path:\n%s\nWould you like to execute this path instead?',...
strjoin(A,'\n'));
q = questdlg(msgTxt,'Yes','No','Yes');
if strcmp(q,'Yes')
exPath = suggested;
else
return;
end
end
% Figure out what exports are needed
[str1,rem] = strtok(exPath,' ');
allExports = {'spikes','LFP','time','dio','mda','phy'};
tmpIdx = strcmpi(str1,'Export');
exports = cell(1,numel(str1));
exports(tmpIdx) = strtrim(rem(tmpIdx));
exportIdx = zeros(numel(tmpIdx),1);
for i=1:numel(tmpIdx),
if tmpIdx(i),
exportIdx(i) = find(strcmpi(allExports,exports{i}));
end
end
if ~exist('exDat','var')
%% Setup day directories
selected_dirs = ListGUI([],sprintf('Select Extraction Directories\n(Animal or Day)'),'dircontent');
animal_dirs = {};
day_dirs = {};
day_names = {};
animal_names = {};
for i = 1:numel(selected_dirs),
selected_dir = selected_dirs{i};
% Check is selected_dir is animal dir or day dir
if ~strcmp(selected_dir(end),filesep)
selected_dir = [selected_dir filesep];
end
recCheck = dir([selected_dir '*.rec']);
if isempty(recCheck)
ad = selected_dir;
dns = dir(selected_dir);
dns = dns(cellfun(@(x) ~strcmp(x(1),'.'),{dns.name}));
dns = {dns.name}';
dds = cellfun(@(x) [ad x],dns,'UniformOutput',0);
n = numel(dds);
animal_dirs = [animal_dirs;repmat({ad},n,1)];
day_dirs = [day_dirs;dds];
day_names = [day_names;dns];
if ad(end)==filesep
ad = ad(1:end-1);
end
[~,an] = fileparts(ad);
animal_names = [animal_names;...
repmat({an},n,1)];
else
day_dirs = [day_dirs;selected_dir];
[ad,dns] = fileparts(selected_dir(1:end-1));
day_names = [day_names;dns];
animal_dirs = [animal_dirs;[ad filesep]];
[~,an] = fileparts(ad);
animal_names = [animal_names;an];
end
end
%% Setup extraction data strcuture
emptArr = {repmat([],numel(day_dirs),1)};
extractionDat = struct('animal_name',animal_names,'day_dir',day_dirs,...
'day_name',day_names,'prefix',emptArr,...
'config',emptArr,'rec_order',emptArr,...
'export_flags',{repmat([],numel(day_dirs),1)});
for i=1:numel(day_dirs),
dd = extractionDat(i).day_dir;
if strcmp(dd(end),filesep)
dd = dd(1:end);
end
RO = dir([dd filesep '*.rec']);
[~,idx] = sort([RO.datenum]');
RO = {RO(idx).name};
extractionDat(i).rec_order = RO;
if numel(RO)>1
extractionDat(i).prefix = RN_findCommonPrefix(RO);
else
a = dir([dd filesep '*.*']);
a = a(cellfun(@(x) ~strcmp(x(1),'.'),{a.name}));
a = {a.name};
extractionDat(i).prefix = RN_findCommonPrefix(a);
end
extractionDat(i).export_flags = repmat({''},1,numel(allExports));
end
%% Gather User Input #INTRO
[exDat,maxJobs] = RN_extractionSetupGUI(exPath,extractionDat);
if isempty(exDat)
return;
else
extractionDat = exDat;
end
else
extractionDat = exDat;
if ~exist('maxJobs','var')
maxJobs = 8;
end
end
%% Run Extraction on each day_directory #BODY
N = numel(extractionDat);
logs = cell(1,N);
for i=1:N
exd = extractionDat(i);
cd(exd.day_dir)
% Setup log
mkdir([exd.prefix '_Logs' filesep])
logFile = [exd.prefix '_Logs' filesep exd.day_name '_ExtractionLog.log'];
diary(logFile)
tic
fprintf('Running extraction for animal %s - day %s (%i/%i)',exd.animal_name,exd.day_name,i,N);
fn_mask = exd.prefix;
RO = exd.rec_order;
config = exd.config;
for j=1:numel(exPath),
pStep = exPath{j};
% Handle Export
% #EXPORT
if exportIdx(j)~=0
% Create time reset comments for exportTimes if Trodes Comments don't already exist
if strcmp(allExports{exportIdx(j)},'time')
tcfs = dir([exd.day_dir filesep '*.trodesComments']);
tcfs = {tcfs.name};
if isempty(tcfs) || numel(RO) ~= numel(tcfs)
trfs = ListSelectGUI(RO,'Select Rec Files with Time Resets',1);
for k=1:numel(RO)
if any(trfs<=k)
tmpFN = [exd.day_dir filesep strtok(RO{k},'.') '.trodesComments'];
fid = fopen(tmpFN,'w');
fprintf(fid,'time reset');
fclose(fid);
clear tmpFN
end
end
end
end
% Make Common Flag
% #EXPORT
commonFlag = '';
if ~isempty(exd.config)
commonFlag = [' -reconfig ' exd.config];
end
commonFlag = [' -rec ' strjoin(RO,' -rec ') ...
commonFlag ' -output ' fn_mask];
exType = allExports{exportIdx(j)};
% Export
disp(['Exporting ' exType '...'])
RN_exportBinary(exType,...
[commonFlag ' ' exd.export_flags{exportIdx(j)}]);
disp('Export complete.')
continue;
end
switch pStep
case 'Fix Filenames'
% Fix Filenames and Change prefix
% #FIX_FILENAMES
disp('Fixing Filenames...')
[fnMask,old_prefix] = RN_fixFilenames([],fn_mask);
% Fix rec_order if needed
if ~strcmp(fnMask,old_prefix)
RO = cellfun(@(x) strrep(x,old_prefix,fnMask),RO,...
'UniformOutput',false);
end
case 'Create Trodes Comments'
% Create Trodes Comments
% #TRODESCOMMENTS
disp('Creating Trodes Comments...')
if isrow(RO)
RO1 = RO';
else
RO1 = RO;
end
RO1 = strrep(RO1,'.rec','');
RN_createTrodesComments(RO1);
disp('Done Making Comments.')
case 'Generate Matclusts'
% Generate Matclust Files
% #MATCLUST
disp('Generating Matclust Files...')
outDir = RN_generateMatclusts(maxJobs);
% disp(['Matclust file saved to ' outDir])
end
end
cd(curr_dir);
toc
diary off
end
end