-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLastStep_PreProcessing_OE_DB.m
More file actions
executable file
·571 lines (455 loc) · 27.8 KB
/
Copy pathLastStep_PreProcessing_OE_DB.m
File metadata and controls
executable file
·571 lines (455 loc) · 27.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
%% File by file preparation for concatenation
clear all
load('ExpeInfo.mat')
%% Add the correct sampling rate
out_ind = regexp(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{1}, 'continuous');
oebin = fileread([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{1}(1:out_ind-1) '/structure.oebin']);
[~, sr_id] = regexp(oebin,'"sample_rate": ');
ExpeInfo.PreProcessingInfo.SR = str2double(oebin(sr_id(1)+1:sr_id(1)+5));
save('ExpeInfo.mat')
BaseFileName = ['M' num2str(ExpeInfo.nmouse) '_' ExpeInfo.date '_' ExpeInfo.SessionType];
FinalFolder = cd;
% Change one informational field of ExpeInfo
if sum(cell2mat(ExpeInfo.PreProcessingInfo.IntanRecorded)) > 0
ExpeInfo.PreProcessingInfo.TypeOfSystem = 'OpenEphys';
end
%% Preallocate arrays
TimeBeginRec_Allfiles = nan(length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys), 3);
TimeEndRec_Allfiles = nan(length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys), 3);
TTLInfo_sess = cell(length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys), 1);
switch ExpeInfo.PreProcessingInfo.IsThereEphys
case 'Yes'
%% ephys
for f = 1:length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys)
if ExpeInfo.PreProcessingInfo.IntanRecorded{f} == 0
%% go to folder
cd(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f})
disp(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f})
%% Get the true time of the beginning and the end of the folders
%Sanity check : the foldername should finish with the time int TimeBeginRec
out_ind = regexp(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}, 'continuous');
StartFile = dir([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}(1:out_ind-1) '*.oebin']);
if isempty(StartFile)
error('structure.oebin was not found. It should be present at the same level as "continuous" folder');
end
ind_start=strfind(StartFile.date,':');
TimeBeginRec_Allfiles(f,1:3)=[str2num(StartFile.date(ind_start(1)-2:ind_start(1)-1)),...
str2num(StartFile.date(ind_start(1)+1:ind_start(2)-1)),str2num(StartFile.date(ind_start(2)+1:end))];
StopFile=dir('*.dat');
ind_stop=strfind(StopFile.date,':');
TimeEndRec_Allfiles(f,1:3)=[str2num(StopFile.date(ind_stop(1)-2:ind_stop(1)-1)),...
str2num(StopFile.date(ind_stop(1)+1:ind_stop(2)-1)),str2num(StopFile.date(ind_stop(2)+1:end))];
disp(['File starts at ' num2str(TimeBeginRec_Allfiles(f,1:3)) ' and ends at ' num2str(TimeEndRec_Allfiles(f,1:3))])
%% Make TTLInfo - modification SB 12/02/2024
% TTLInfo_sess{f} = MakeData_TTLInfo_OpenEphys([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}(1:out_ind-1) ...
% 'events/Rhythm_FPGA-100.0_TTL_1.mat'],ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}(1:out_ind-1),...
% ExpeInfo);
TTLInfo_sess{f} = MakeData_TTLInfo_OpenEphys(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}(1:out_ind-1),...
ExpeInfo);
TTLInfo = TTLInfo_sess{f};
if not(exist([FinalFolder filesep 'behavResources.mat']))
save([FinalFolder filesep 'behavResources.mat'],'TTLInfo')
else
save([FinalFolder filesep 'behavResources.mat'],'TTLInfo','-append')
end
cd(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f})
%% here is the place where we check if the ephys is the right length comapred to the behav resources - to do
%% copy ephys files that are ref subtracted and merged
if ExpeInfo.PreProcessingInfo.RefDone{f}
disp('file is merged and ref subtracted - copying ...')
% Create the xml
WriteExpeInfoToXml(ExpeInfo)
% copy the .dat and the .xml
copyfile(fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}, 'continuous.dat'),...
fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f},...
[BaseFileName '-' sprintf('%02d',f) '.dat']));
movefile(fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f},...
[BaseFileName '-' sprintf('%02d',f) '.dat']), FinalFolder);
copyfile(fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}, 'amplifier.xml'),...
fullfile(FinalFolder, [BaseFileName '-' sprintf('%02d',f) '.xml']));
delete([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f} filesep 'amplifier.xml']);
elseif ExpeInfo.PreProcessingInfo.RefDone{f}==0
disp('file is merged - subtracting ref ...')
% Create the xml
WriteExpeInfoToXml(ExpeInfo)
% subtract the reference
RefChannel = ExpeInfo.ChannelToAnalyse.Ref;
% do subtraction on all the wideband channels
ChanToSub = [0 : ExpeInfo.PreProcessingInfo.NumWideband-1];
ChanToSub(ChanToSub==RefChannel) = [];
% special case for breathing
if isfield(ExpeInfo.ChannelToAnalyse,'Respi')
ChanToSub(ChanToSub==ExpeInfo.ChannelToAnalyse.Respi) = [];
end
ChanToSaveWithoutChange = [0 :ExpeInfo.PreProcessingInfo.TotalChannels-1];
ChanToSaveWithoutChange(ChanToSub+1) = [];
% Do the subtraction
RefSubtraction_multi('continuous.dat',ExpeInfo.PreProcessingInfo.TotalChannels,1,...
['M' num2str(ExpeInfo.nmouse)],ChanToSub,RefChannel,ChanToSaveWithoutChange,'frequency',ExpeInfo.PreProcessingInfo.SR);
disp(['file is merged and ref subtracted - copying ...'])
% copy the file
movefile(fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f},...
['continuous_M' num2str(ExpeInfo.nmouse) '.dat']),...
fullfile(FinalFolder, [BaseFileName '-' sprintf('%02d',f) '.dat']));
copyfile([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f} filesep 'amplifier.xml'],...
fullfile(FinalFolder, [BaseFileName '-' sprintf('%02d',f) '.xml']));
delete([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f} filesep 'amplifier.xml']);
end
%%
elseif ExpeInfo.PreProcessingInfo.IntanRecorded{f} == 1
% go to folder
cd(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f})
disp(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f})
%% Get the true time of the beginning and the end of the folders
StopFile=dir('*supply.dat');
ind=strfind(StopFile.date,':');
TimeEndRec_Allfiles(f,1:3)=[str2num(StopFile.date(ind(1)-2:ind(1)-1)),...
str2num(StopFile.date(ind(1)+1:ind(2)-1)),str2num(StopFile.date(ind(2)+1:end))];
%Sanity check : the foldername should finish with the time int TimeBeginRec
FolderNameTime = (ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}(end-5:end));
TimeBeginRec_Allfiles(f,1:3)=[str2num(FolderNameTime(1:2)),...
str2num(FolderNameTime(3:4)),str2num(FolderNameTime(5:6))];
disp(['File starts at ' num2str(TimeBeginRec_Allfiles(f,1:3)) ' and ends at ' num2str(TimeEndRec_Allfiles(f,1:3))])
%% Make TTLInfo
TTLInfo_sess{f} = GetTTLTimesInd_Intan(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f},...
ExpeInfo);
if not(exist('behavResources.mat'))
save('behavResources.mat','TTLInfo')
else
save('behavResources.mat','TTLInfo','-append')
end
%% copy ephys files that are ref subtracted and merged
if ExpeInfo.PreProcessingInfo.RefDone{f} && ExpeInfo.PreProcessingInfo.MergeDone{f}==0
disp(['file is ref subtracted - merging...'])
% Create the xml
WriteExpeInfoToXml(ExpeInfo)
% Merge
movefile(['amplifier.dat'], 'amplifier-wideband.dat');
if ExpeInfo.PreProcessingInfo.NumAccelero>0
movefile('auxiliary.dat', 'amplifier-accelero.dat');
end
system('ndm_mergedat amplifier')
disp('file is merged and ref subtracted - copying ...')
% copy the file
copyfile(fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}, 'amplifier.dat'),...
fullfile(FinalFolder, [BaseFileName '-' sprintf('%02d',f) '.dat']));
copyfile(fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}, 'amplifier.xml'),...
fullfile(FinalFolder, [BaseFileName '-' sprintf('%02d',f) '.xml']));
elseif ExpeInfo.PreProcessingInfo.RefDone{f}==0 && ExpeInfo.PreProcessingInfo.MergeDone{f}==0
disp('file is unpreprocessed - subtracting ref...')
% Create the xml
WriteExpeInfoToXml(ExpeInfo)
% subtract the reference
RefChannel = ExpeInfo.ChannelToAnalyse.Ref;
% do subtraction on all the wideband channels
ChanToSub = [0 : ExpeInfo.PreProcessingInfo.NumWideband-1];
ChanToSub(ChanToSub==RefChannel) = [];
% special case for breathing
if isfield(ExpeInfo.ChannelToAnalyse,'Respi')
ChanToSub(ChanToSub==ExpeInfo.ChannelToAnalyse.Respi) = [];
end
ChanToSaveWithoutChange = 0 :ExpeInfo.PreProcessingInfo.NumWideband-1;
ChanToSaveWithoutChange(ChanToSub+1) = [];
% Do the subtraction
RefSubtraction_multi('continuous.dat',ExpeInfo.PreProcessingInfo.TotalChannels,1,...
['M' num2str(ExpeInfo.nmouse)],ChanToSub,RefChannel,ChanToSaveWithoutChange,'frequency',ExpeInfo.PreProcessingInfo.SR);
disp('file is ref subtracted - merging...')
% Merge
movefile(['amplifier_M' num2str(ExpeInfo.nmouse) '.dat'], 'amplifier-wideband.dat');
if ExpeInfo.PreProcessingInfo.NumAccelero>0
movefile('auxiliary.dat', 'amplifier-accelero.dat');
end
system('ndm_mergedat amplifier');
disp('file is merged and ref subtracted - copying ...')
% copy the file
copyfile(fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}, 'amplifier.dat'),...
fullfile(FinalFolder, [BaseFileName '-' sprintf('%02d',f) '.dat']));
copyfile(fullfile(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys{f}, 'amplifier.xml'),...
fullfile(FinalFolder, [BaseFileName '-' sprintf('%02d',f) '.xml']));
end
end
cd(FinalFolder)
end
cd(FinalFolder)
%% save the times
TimeEndRec = TimeEndRec_Allfiles(end,:);
TimeBeginRec = TimeBeginRec_Allfiles(1,:);
save([FinalFolder filesep 'TimeRec.mat'],'TimeEndRec','TimeBeginRec','TimeEndRec_Allfiles','TimeBeginRec_Allfiles')
%% Create session-wide .xml
copyfile([FinalFolder filesep BaseFileName '-' sprintf('%02d',f) '.xml'],...
[FinalFolder filesep BaseFileName '.xml']);
%% concatenate all the files
%updated by AG on 25/11/2025 to be windows compatible
if length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys) > 1
if ispc
% 1) Get ndm_dir
if contains(FinalFolder, 'Arsenii')
ndm_dir = 'D:\Arsenii\GitHub\ndm_scripts\ndmanager-plugins\scripts';
else
if ~exist('ndm_dir', 'var') || ~isfolder(ndm_dir)
ndm_dir = uigetdir('', 'Select the folder containing ndm scripts (ndm_concatenate, ndm_lfp, etc.)');
if ndm_dir == 0
error('You must select a valid ndm_scripts directory!');
end
end
end
% 2) Convert FinalFolder to WSL path (this is where the .dat/.xml live)
drive_data = lower(FinalFolder(1)); % e.g. 'z'
wsl_data_dir = strrep(FinalFolder, '\', '/'); % Z:\... -> Z:/...
wsl_data_dir = regexprep(wsl_data_dir, '^[A-Za-z]:', ['/mnt/' drive_data]); % -> /mnt/z/...
% 3) Build command: cd into data dir, call ndm_concatenate with ONLY BaseFileName
cmd = sprintf(['wsl bash -lc "cd ''%s'' ' ...
'&& ndm_concatenate ''%s''"'], ...
wsl_data_dir, BaseFileName);
disp(['Executing: ' cmd])
system(cmd);
cd(FinalFolder)
else
% Linux case unchanged
system(['ndm_concatenate ' BaseFileName])
end
else
movefile(fullfile(FinalFolder, [BaseFileName '-' sprintf('%02d',f) '.dat']), ...
fullfile(FinalFolder, [BaseFileName '.dat']));
end
%% make lfp
%updated by AG on 25/07/2025 to be windows compatible
if ispc
if contains(FinalFolder, 'Arsenii')
ndm_dir = 'D:\Arsenii\GitHub\ndm_scripts\ndmanager-plugins\scripts';
else
if ~exist('ndm_dir', 'var') || ~isfolder(ndm_dir)
ndm_dir = uigetdir('', 'Select the folder containing ndm_lfp and related scripts. Usually ..\scripts');
if ndm_dir == 0
error('You must select a valid ndm_scripts directory!');
end
end
end
drive_ndm = lower(ndm_dir(1));
wsl_ndm_dir = strrep(ndm_dir, '\', '/');
wsl_ndm_dir = regexprep(wsl_ndm_dir, '^[A-Za-z]:', ['/mnt/' drive_ndm]);
drive_data = lower(FinalFolder(1));
wsl_data_dir = strrep(FinalFolder, '\', '/');
wsl_data_dir = regexprep(wsl_data_dir, '^[A-Za-z]:', ['/mnt/' drive_data]);
cmd = sprintf(['wsl bash -c "cd ''%s'' ' ...
'&& export PATH=\\"$PATH:$(pwd)\\" ' ...
'&& ./ndm_lfp ''%s/%s''"'], ...
wsl_ndm_dir, wsl_data_dir, BaseFileName);
disp(['Executing: ' cmd])
cd('C:\')
system(cmd);
cd(FinalFolder)
else
system(['ndm_lfp ' BaseFileName]);
end
%% create lfp and channels to analyse folders
InfoLFP = ExpeInfo.InfoLFP;
mkdir('LFPData')
save('LFPData/InfoLFP.mat','InfoLFP')
mkdir('ChannelsToAnalyse');
if isfield(ExpeInfo,'ChannelToAnalyse')
AllStructures = fieldnames(ExpeInfo.ChannelToAnalyse);
for stru=1:length(AllStructures)
channel = ExpeInfo.ChannelToAnalyse.(AllStructures{stru});
save(['ChannelsToAnalyse/',AllStructures{stru},'.mat'],'channel');
end
end
%% run the makeData scripts
global DATA
SetCurrentSession([BaseFileName '.xml'])
SessLength = MakeData_LFP_PluggedOnly(FinalFolder,ExpeInfo);
% This is SUPER dirty!! DO it again Sophie!!
if length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Ephys)==1
%evt
evt.time(1)=0;
evt.time(2)=SessLength*1e4;
evt.description{1}='beginning of';
evt.description{2}='end of';
CreateEvent(evt, [FinalFolder filesep BaseFileName], 'cat')
movefile( [FinalFolder filesep BaseFileName '.evt.cat'],[FinalFolder filesep BaseFileName '.cat.evt'])
end
SetCurrentSession('same');
tpsCatEvt = MakeData_CatEvents(FinalFolder);
MakeData_RealTime(FinalFolder)
if ExpeInfo.PreProcessingInfo.NumAccelero>0
MakeData_Accelero(FinalFolder)
end
%% Make final TTLInfo
% Duration files
beg_end = cell2mat(tpsCatEvt);
st = beg_end(1:2:end);
en = beg_end(2:2:end);
duration = nan(length(st),1);
for i=1:length(st)
duration(i) = en(i)-st(i);
end
duration = duration*1e4;
% Concatenation of single session TTLs
if isfield(TTLInfo_sess{1},'StopSession')
if isempty(TTLInfo_sess{1}.StopSession) % add by BM on 01/05/2022 when we don't have TTL
hasStimEpoch = all(cellfun(@(s) isfield(s,'StimEpoch'), TTLInfo_sess)); % add by AG 25/11/2025 to check for StimEpoch in concat case
if length(TTLInfo_sess) == 1
TTLInfo = TTLInfo_sess{1};
if hasStimEpoch
try % updated by AG on 25/07/2025 to avoid error
StimEpoch = TTLInfo_sess{1}.StimEpoch;
catch
disp('no StimEpoch found')
end
else
disp('no StimEpoch field in TTLInfo_sess{1}')
end
else
TTLInfo.StartSession = TTLInfo_sess{1}.StartSession;
TTLInfo.StopSession = TTLInfo_sess{end}.StopSession;
if hasStimEpoch
% StimEpoch
Stim = cell(length(TTLInfo_sess),1);
for ittl = 1:length(TTLInfo_sess)
Stim{ittl} = Start(TTLInfo_sess{ittl}.StimEpoch);
end
for ittl=1:(length(TTLInfo_sess)-1)
Stim{ittl+1} = Stim{ittl+1} + (sum(duration(1:ittl)));
end
StimTime = Stim{1};
for ittl = 2:length(TTLInfo_sess)
StimTime = [StimTime; Stim{ittl}];
end
TTLInfo.StimEpoch = intervalSet(StimTime, StimTime);
StimEpoch = TTLInfo.StimEpoch;
clear Stim Stimtime
else
disp('no StimEpoch field in some TTLInfo_sess entries, skipping StimEpoch concatenation')
end
end
save('behavResources.mat','TTLInfo','-append')
if exist('StimEpoch')>0
save('behavResources.mat','StimEpoch','-append')
end
end
end
%% if there are spikes
if ExpeInfo.PreProcessingInfo.DoSpikes
% Do subtraction if necessary
if sum(ExpeInfo.SpikeGroupInfo.UseForElecSub)>0
% get the electrodes to make the subtraction on
ElecPerformSub = [];
for sp = 1:length(ExpeInfo.SpikeGroupInfo.UseForElecSub)
if ExpeInfo.SpikeGroupInfo.UseForElecSub(sp) ==1
ElecPerformSub = [ElecPerformSub,str2num(ExpeInfo.SpikeGroupInfo.ChanNames{sp})];
end
end
% Old version
% Get the electrodes which will eb averaged to make the subtraction
%RefElec = str2num((ExpeInfo.SpikeGroupInfo.ElecsToSub));
% Preform the subtraction
% AllChans = [0:ExpeInfo.PreProcessingInfo.TotalChannels-1];
% AllChans(ElecPerformSub+1) = [];
% RefSubtraction_multi_AverageChans([BaseFileName '.dat'],ExpeInfo.PreProcessingInfo.TotalChannels,1,'SpikeRef',ElecPerformSub,RefElec,AllChans);
% New version with different reference channels
LeftOverChannels = 0:ExpeInfo.PreProcessingInfo.TotalChannels-1;
ToDel = [];
numtet = 1;
for tet = 1:length(ExpeInfo.SpikeGroupInfo.ChanNames)
if ~isempty(ExpeInfo.SpikeGroupInfo.ElecsToSub{tet})
ChannelsToPerfSub{numtet} = str2num(ExpeInfo.SpikeGroupInfo.ChanNames{tet});
ChannelsToAverageForSub{numtet} = str2num(ExpeInfo.SpikeGroupInfo.ElecsToSub{tet});
ToDel = [ToDel, str2num(ExpeInfo.SpikeGroupInfo.ChanNames{tet})];
numtet = numtet +1;
end
end
LeftOverChannels(ismember(LeftOverChannels,ToDel)) = [];
RefSubtraction_GroupByGroup([BaseFileName '.dat'],ExpeInfo.PreProcessingInfo.TotalChannels,1,'SpikeRef', ...
ChannelsToPerfSub, ChannelsToAverageForSub, LeftOverChannels);
system(['mv ' BaseFileName '_original.dat ' BaseFileName '.dat'])
copyfile([BaseFileName '.xml'], [BaseFileName '_SpikeRef.xml'])
% Do the preprcessing steps
system(['ndm_hipass ' BaseFileName '_SpikeRef'])
system(['ndm_extractspikes ' BaseFileName '_SpikeRef'])
if strcmp(ExpeInfo.PreProcessingInfo.CleanSpikes, 'Yes')
disp('I will clean bad spikes...');
if exist('UnfilteredChans', 'dir') ~= 7 || isempty(dir([FinalFolder filesep 'UnfilteredChans/*.mat']))
MakeData_Detection(FinalFolder, ExpeInfo.ChannelToAnalyse.Ref);
end
% Get spikes without artefacts
files = dir([FinalFolder filesep 'UnfilteredChans/*.mat']);
CleanEpoch = DefineCleanSpikesEpochs([files(1).folder filesep files(1).name], FinalFolder,...
ExpeInfo.PreProcessingInfo.StimDur);
% Remove spikes
RemoveArtefactualSpikes(FinalFolder, CleanEpoch, [BaseFileName '_SpikeRef']);
end
system(['ndm_pca ' BaseFileName '_SpikeRef'])
else %% Here we don't need to do re-referencing
system(['ndm_hipass ' BaseFileName])
system(['ndm_extractspikes ' BaseFileName])
if strcmp(ExpeInfo.PreProcessingInfo.CleanSpikes, 'Yes')
disp('I will clean bad spikes...');
if exist('UnfilteredChans', 'dir') ~= 7 || isempty(dir([FinalFolder filesep 'UnfilteredChans/*.mat']))
MakeData_Detection(FinalFolder, ExpeInfo.ChannelToAnalyse.Ref);
end
% Get spikes without artefacts
files = dir([FinalFolder filesep 'UnfilteredChans/*.mat']);
CleanEpoch = DefineCleanSpikesEpochs([files(1).folder filesep files(1).name], FinalFolder,...
ExpeInfo.PreProcessingInfo.StimDur);
% Remove spikes
RemoveArtefactualSpikes(FinalFolder, CleanEpoch, BaseFileName);
end
system(['ndm_pca ' BaseFileName])
end
end
case 'No'
disp('No Ephys');
end
%% behaviour (Camera)
% First sort out times if they don't yet exist because there is no ephys
switch ExpeInfo.PreProcessingInfo.IsThereBehav
case 'Yes'
switch ExpeInfo.PreProcessingInfo.IsThereEphys
case 'No'
tps = 0;
tpsCatEvt = 0;
for f = 1:length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Behav)
load([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Behav{f} filesep 'behavResources.mat'] ,'PosMat')
tps = tps + max(PosMat(:,1));
tpsCatEvt = [tpsCatEvt, [tps;tps]']; % in seconds
end
tpsCatEvt(end) = [];
tpsCatEvt = num2cell(tpsCatEvt);
try
save([FinalFolder 'behavResources'],'tpsCatEvt','-append')
catch
disp('Creating behavResources.mat')
save([FinalFolder 'behavResources'],'tpsCatEvt')
end
case 'Yes'
disp('')
end
case 'No'
disp('')
end
switch ExpeInfo.PreProcessingInfo.IsThereBehav
case 'Yes'
if ~strcmpi(ExpeInfo.CameraType, 'None')
if length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Behav)==1
copyfile([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Behav{1} filesep 'behavResources.mat'],...
[FinalFolder filesep 'behavResources-temp.mat']);
TempVar = load([FinalFolder filesep 'behavResources-temp.mat']);
TempVar = CorrectTimeBackToEphys(TempVar, TTLInfo_sess{1});
TempVar.SessionEpoch = intervalSet(TempVar.PosMat(1,1)*1e4, TempVar.PosMat(end,1)*1e4);
save([FinalFolder filesep 'behavResources.mat'],'-struct','TempVar','-append')
delete([FinalFolder filesep 'behavResources-temp.mat'])
else
for f = 1:length(ExpeInfo.PreProcessingInfo.FolderForConcatenation_Behav)
copyfile([ExpeInfo.PreProcessingInfo.FolderForConcatenation_Behav{f} filesep 'behavResources.mat'],...
[FinalFolder filesep 'behavResources-' sprintf('%02d',f) '.mat']);
filelist{f} = [FinalFolder filesep 'behavResources-' sprintf('%02d',f) '.mat'];
end
IntanRecorded = ExpeInfo.PreProcessingInfo.IntanRecorded;
BehavResourcesConcatenation(filelist, ExpeInfo.PreProcessingInfo.FolderSessionName, tpsCatEvt, FinalFolder, 'StartInfo', ...
TTLInfo_sess, 'IntanRecorded', IntanRecorded)
end
end
case 'No'
end
disp('All done, you''re good! Jesus loves you. And your momma too')
disp('All hail the Holy Octopus')