-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess_NPX2ConcatenateDat.m
135 lines (105 loc) · 3.84 KB
/
Process_NPX2ConcatenateDat.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
function Process_NPX2ConcatenateDat(fbasename,varargin)
% Processes raw data from SpikeGLX, renames files and folders, concatenates
% dat files (if multiple). Must be called from the folder where all
% recording folders are stored (indexed by _g1,2, and assumes only t0)
%
% USAGE
%
% Process_NPX2ConcatenateDat(filebasename,<optional>mergename)
%
% filebasename a cell array of filebasenames (with or without '.dat' extenstion)
% mergename final concatenated file and folder name (if omitted,
% mergename will be the name of the current folder.
% Copyright (C) 2021 Adrien Peyrache
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%% Parameters
if isempty(varargin)
[~,mergename,~] = fileparts(pwd);
else
mergename = varargin{1};
if length(varargin)>1
destDir = varargin{2};
else
destDir = pwd;
end
end
fprintf('Processing %s...\n',mergename);
%%Create destination folder
if ~exist(destDir,'dir')
error(['destination folder ' destDir ' does not exist'])
end
destDir = fullfile(destDir,mergename);
if ~exist(destDir,'dir')
mkdir(destDir)
end
%% Check folders, compute duration
folders = dir([fbasename '_g*']);
recName = [];
for ii=1:length(folders)
if folders(ii).isdir
recName = [recName;{folders(ii).name}];
end
end
fprintf('Creating Epoch_TS.csv\n',mergename);
nRec = length(recName);
epochs = zeros(nRec, 2);
start = 0;
for ii=1:nRec
fname = [recName{ii} '_t0.imec0.ap.bin'];
if exist(fullfile(recName{ii},fname), 'file')
meta = NPX_ReadMeta(fname,recName{ii});
recDuration = str2num(meta.fileTimeSecs);
else
error(['Bin file ' fname ' does not exist in ' dataDir]);
end
epochs(ii,1) = start;
start = start + recDuration;
epochs(ii,2) = start;
end
dlmwrite(fullfile(destDir,'Epoch_TS.csv'),epochs,'precision','%.6f');
%% Concatenate Data for Kilosort (or others)
% Concatenating NPX bin files
fprintf('Concatening NPX bin files\n',mergename);
if nRec>1
cmdline = 'cat ';
for ii=1:nRec-1
cmdline = [cmdline fullfile(recName{ii}, [recName{ii} '_t0.imec0.ap.bin '])];
end
cmdline = [cmdline fullfile(recName{ii+1},[recName{ii+1} '_t0.imec0.ap.bin']) ' > ' fullfile(destDir,[mergename '.dat'])];
else
cmdline = ['mv ' fullfile(recName{ii},[recName{ii} '_t0.imec0.ap.bin ']) fullfile(destDir,[mergename '.dat'])];
end
fprintf('Launch command %s\n\n',cmdline)
system(cmdline);
%% Concatenating NIDQ bin files
fprintf('Concatening NIDQ bin files\n',mergename);
if nRec>1
cmdline = 'cat ';
for ii=1:nRec-1
cmdline = [cmdline fullfile(recName{ii}, [recName{ii} '_t0.nidq.bin '])];
end
cmdline = [cmdline fullfile(recName{ii+1},[recName{ii+1} '_t0.nidq.bin']) ' > ' fullfile(destDir,[mergename '.nidq.dat'])];
else
cmdline = ['mv ' fullfile(recName{ii},[recName{ii} '_t0.nidq.bin ']) fullfile(destDir,[mergename '.nidq.dat'])];
end
fprintf('Launch command %s\n\n',cmdline)
system(cmdline);
%% Copy files to new final directory
%mkdir(mergename);
%% Moving xml and dat file
%movefile([mergename '.bin'],mergename,'f')
%copyfile([recList{1} '.meta'],[mergename '.meta'],'f')
%movefile([mergename '.meta'],mergename,'f')
% %% Moving position csv file if any exists and renaming it for python (starting at 0)
% nRec = length(recList);
% for ii=1:nRec
% fname = [recList{ii} '.csv'];
% if exist(fname, 'file')
% targetFile = fullfile(pwd, mergename, [mergename '_' num2str(ii-1) '.csv']);
% movefile(fname, targetFile);
% end
% end