-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenRodentDataFile.m
More file actions
351 lines (317 loc) · 10.8 KB
/
Copy pathopenRodentDataFile.m
File metadata and controls
351 lines (317 loc) · 10.8 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
function D = opendatafile(varargin)
%SYNTAX:
% D = opendatafile(filename)
%
%The input argument 'filename' is optional; if not specified, a dialog box
%will prompt the user for a data file.
%
%The data file may be either a bhv or a nex file. That file will load
%along with the corresponding nex or bhv file (file names must be
%identical).
%
%Must call this function before calling nexgetspike or nexgetlfp.
%
%If there exists a text file named "AncillaryData.txt" in the experiment
%directory, this will be opened and the data appended to NEURO.Neuron, with
%the field names determined by the header for each column. The first
%column is expected to contain a filename, and the second is expected to
%contain a neuron label (e.g., 'sig003b'). These are used to match the
%data in the remaining columns to the appropriate record. Each item in the
%resulting array is matched to the corresponding signal in order.
%
%Created by WA, June, 2008
%Last modified 6/8/2011 -WA
D = [];
verify = 0;
BHV = [];
NEURO = struct;
NEURO.File = '';
NEURO.RecordingDurationInSeconds = [];
NEURO.Neuron = struct;
NEURO.NeuronInfo = [];
NEURO.NeuronWaveForm = [];
NEURO.LFP = struct;
NEURO.CodeTimes = [];
NEURO.CodeNumbers = [];
foundneuro = 0; %has a neurophysiology data file been found; right now, only supports nex files...
monkeywrench('ProgressBar', 0);
if ~ispref('MonkeyLogic', 'Directories'),
success = set_ml_preferences;
if ~success,
return
end
end
MLPrefs.Directories = getpref('MonkeyLogic', 'Directories');
MLPrefs.Directories.ExperimentDirectory = [fileparts(MLPrefs.Directories.ExperimentDirectory) filesep]; %eliminates extra trailing backslash
if ~ispref('MonkeyLogic', 'UserPreferences'),
success = monkeywrench_config;
if ~success, return; end
end
MLPrefs.UserPrefs = getpref('MonkeyLogic', 'UserPreferences');
readLFP = MLPrefs.UserPrefs.LoadLFP;
if readLFP > 0,
disp('... Warning: Pre-loading all LFP data into memory not currently supported ...')
readLFP = 0;
end
if isempty(varargin),
[fname pname] = uigetfile([MLPrefs.Directories.ExperimentDirectory '*.bhv; *.nex'], 'Choose data file...');
if ~fname,
return
end
NEURO.File = [pname fname];
verify = 1;
else
if length(varargin) == 1
[pname fname ext] = fileparts(varargin{1});
if isempty(pname),
pname = MLPrefs.Directories.ExperimentDirectory;
end
if isempty(ext),
ext = '.nex';
end
NEURO.File = [pname fname ext];
else
NEURO.File = varargin{1};
end
end
setpref('MonkeyLogic', 'Directories', MLPrefs.Directories);
knownfiles = {'nex'};
VarNames.BehavioralCodes = 'Strobed';
VarNames.Neurons = 'Sig';
VarNames.LFP = 'AD';
VarNames.EyeX = 'EyeX';
VarNames.EyeY = 'EyeY';
VarNames.JoyX = 'JoyX';
VarNames.JoyY = 'JoyY';
AncillaryDataFileName = 'AncillaryData.txt';
% start_trial_code = MLPrefs.UserPrefs.StartTrialCode;
% end_trial_code = MLPrefs.UserPrefs.EndTrialCode;
start_trial_code = 1;
end_trial_code = 2; %%these two lines are the only deviations from opendatafile.m
[pname fname ext] = fileparts(NEURO.File);
filetype = [];
if strcmpi(ext, '.bhv'),
bhvfile = NEURO.File;
for i = 1:length(knownfiles),
testfile = strrep(NEURO.File, '.bhv', ['.' knownfiles{i}]);
if exist(testfile, 'file'),
filetype = knownfiles{i};
NEURO.File = testfile;
break
end
end
else
bhvfile = strrep(NEURO.File, '.nex', '.bhv');
filetype = ext;
end
if exist(bhvfile, 'file'),
monkeywrench('Message', 'Reading BHV file...');
BHV = bhv_read(bhvfile);
monkeywrench('ProgressBar', 1/10);
end
if strfind(filetype, 'nex'),
foundneuro = 1;
monkeywrench('Message', 'Reading NEX file...');
[fh vh d] = nex_read(NEURO.File, readLFP);
NEURO.RecordingDurationInSeconds = (fh.End - fh.Beg)/fh.Frequency;
monkeywrench('ProgressBar', 4/10);
vn = cat(1, {vh.Name});
numvars = length(vn);
%Determine index to each variable expected in VarNames, given above
fn = fieldnames(VarNames);
n = length(fn);
monkeywrench('Message', 'Sorting data file variables...');
for i = 1:n,
v = fn{i};
flist = strfind(lower(vn), lower(VarNames.(v)));
k = zeros(1, numvars);
for ii = 1:numvars,
if ~isempty(flist{ii}) && flist{ii} == 1,
k(ii) = ii;
end
end
k = k(logical(k));
if ~isempty(k),
VarIndex.(v) = k;
else
VarIndex.(v) = [];
end
monkeywrench('ProgressBar', 0.4 + (0.2*i/n));
end
%Extract behavioral codes
monkeywrench('Message', 'Extracting behavioral codes...');
fmarker = strmatch('Marker', {vh.Name});
if ~isempty(fmarker) && isempty(VarIndex.BehavioralCodes), %behavioral codes stored as individual "markers",
nmarkers = length(fmarker);
codetimes = [];
codenumbers = [];
for i = 1:nmarkers,
varnum = fmarker(i);
markername = vh(varnum).Name;
findus = strfind(markername, '_');
if isempty(findus),
error('Unknown naming convention for behavioral codes (e.g., "%s")', markername);
end
thiscode = str2double(markername(max(findus)+1:end));
thesetimes = d{varnum};
thesecodes = thiscode*ones(size(thesetimes));
codetimes = [codetimes; thesetimes];
codenumbers = [codenumbers; thesecodes];
end
[~, indx] = sort(codetimes);
codetimes = codetimes(indx);
codenumbers = codenumbers(indx);
NEURO.CodeTimes = round(1000*codetimes);
NEURO.CodeNumbers = codenumbers;
else %behavioral codes stored as single "strobed" variable.
NEURO.CodeTimes = round(1000*d{VarIndex.BehavioralCodes});
NEURO.CodeNumbers = vh(VarIndex.BehavioralCodes).MarkerValues{1};
end
monkeywrench('ProgressBar', 7/10);
%Extract spiketimes
monkeywrench('Message', 'Extracting spike times...');
fneuron = strmatch('Lead', {vh.Name});
if ~isempty(fneuron) && isempty(VarIndex.Neurons), %neurons named according to "Lead X_XX_X'
VarIndex.Neurons = fneuron;
end
for i = 1:length(VarIndex.Neurons), %remove spaces to make valid structure fieldnames
varnum = VarIndex.Neurons(i);
varname = vh(varnum).Name;
if any(isspace(varname)),
varname(isspace(varname)) = '_';
vh(varnum).Name = varname;
end
end
lasttime = 0;
for i = 1:length(VarIndex.Neurons),
k = VarIndex.Neurons(i);
NEURO.Neuron.(vh(k).Name) = round(1000*d{k});
lasttime = max([lasttime max(NEURO.Neuron.(vh(k).Name))]);
end
monkeywrench('ProgressBar', 8/10);
monkeywrench('Message', 'Scanning LFP data...');
%Placeholder for LFPs
if VarIndex.LFP,
for i = 1:length(VarIndex.LFP),
k = VarIndex.LFP(i);
NEURO.LFP.(vh(k).Name) = d{k};
end
%need to take into account fragment start time(s)...
end
end
if ~foundneuro,
error('No associated neurophysiology data file found');
end
%Extract WaveForm data:
monkeywrench('Message', 'Extracting neuronal waveforms...');
fstr = strfind({vh.Name}, 'wf_');
f = zeros(numvars, 1);
for i = 1:length(fstr),
if ~isempty(fstr{i}),
f(i) = 1;
end
end
if any(f),
f = find(f);
for i = 1:length(f),
varnum = f(i);
signame = vh(varnum).Name;
signame = signame(4:end);
if length(signame) > 9 && strcmp(signame(end-8:end), '_template'),
signame = signame(1:end-9);
end
NEURO.NeuronWaveForm.(signame) = d{varnum};
end
end
%Extract Trials:
monkeywrench('Message', 'Determining trial boundaries...');
if ~isempty(NEURO.CodeTimes),
c9 = (NEURO.CodeNumbers == start_trial_code);
c18 = (NEURO.CodeNumbers == end_trial_code);
t9 = NEURO.CodeTimes(c9);
t18 = NEURO.CodeTimes(c18);
if t18(1) < t9(1),
error('An end-of-trial code precedes the first instance of a start-of-trial code');
elseif t9(length(t9)) > t18(length(t18)),
disp('Warning: A start-of-trial code follows the last end-of-trial code');
t9 = t9(1:length(t18));
end
count = 0;
r9 = t9;
while ~isempty(r9),
count = count + 1;
starttrial = r9(1);
NEURO.TrialTimes(count, 1) = starttrial;
r18 = t18(t18 > starttrial);
endtrial = r18(1);
NEURO.TrialDurations(count, 1) = endtrial - starttrial;
r9 = t9(t9 > endtrial);
end
NEURO.NumTrials = length(NEURO.TrialTimes);
else
NEURO.TrialTimes = [];
NEURO.TrialDurations = [];
NEURO.NumTrials = 0;
end
monkeywrench('ProgressBar', 9/10);
%Assign Neuron and LFP channels based upon sig000x and AD00 nomenclature...
neuronlabels = fieldnames(NEURO.Neuron);
numneurons = length(neuronlabels);
lfplabels = fieldnames(NEURO.LFP);
numlfp = length(lfplabels);
disp(sprintf('%s: Found %i neurons & %i LFP channels', NEURO.File, numneurons, numlfp))
neuronchannels = zeros(numneurons, 1);
k = length(VarNames.Neurons) + 1;
for i = 1:numneurons,
neuronchannels(i) = str2double(neuronlabels{i}(k:end-1));
end
NEURO.NeuronInfo.Channel = neuronchannels;
lfpchannels = zeros(numlfp, 1);
k = length(VarNames.LFP) + 1;
for i = 1:numlfp,
lfpchannels(i) = str2double(lfplabels{i}(k:end));
NEURO.LFP.(lfplabels{i}).Channel = lfpchannels(i);
end
NEURO.LFPInfo.Channel = lfpchannels;
%load & attach ancillary data
monkeywrench('Message', 'Loading & matching ancillary data...');
ADfile = [pname filesep AncillaryDataFileName];
if exist(ADfile, 'file'),
AData = loadancillarydata(ADfile);
fn = fieldnames(AData);
fn = fn(3:end); %excludes FileName and Signal fields
numfields = length(fn);
for k = 1:numfields,
NEURO.NeuronInfo.(fn{k}) = cell(numneurons, 1);
end
for i = 1:numneurons,
d = matchancillarydata(AData, fname, neuronlabels(i));
for k = 1:numfields,
NEURO.NeuronInfo.(fn{k})(i) = d.(fn{k});
end
end
end
monkeywrench('ProgressBar', 10/10);
if verify,
%cross-check trial-durations:
bhvnumtrials = length(BHV.ConditionNumber);
if bhvnumtrials ~= NEURO.NumTrials,
disp(sprintf('Trial Number Mismatch: %i trials found in NEX file and %i trials found in BHV file.', NEURO.NumTrials, bhvnumtrials))
end
numtrials = min([bhvnumtrials NEURO.NumTrials]);
tduration = zeros(numtrials, 1);
for t = 1:numtrials,
ct = BHV.CodeTimes{t};
cn = BHV.CodeNumbers{t};
cstart = min(ct(cn == start_trial_code));
cend = min(ct(cn == end_trial_code));
tduration(t) = cend - cstart;
end
durdiff = NEURO.TrialDurations(1:numtrials) - tduration;
D.TrialCrossCheck = durdiff;
end
D.NEURO = NEURO;
D.BHV = BHV;
set(0, 'userdata', D);
monkeywrench('UpdateActiveData');
monkeywrench('ProgressBar', 0);