-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackRock2KiloSortFormat.m
73 lines (57 loc) · 2.48 KB
/
blackRock2KiloSortFormat.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
function blackRock2KiloSortFormat()
% A function which processes the typical output of blackrock files which
% contains the raw traces of data (x.ns5, if sampled @ 30 kHz), and
% produces a file in a similarly structured directory tree which can be read by kilosort (x.bin)
% Path to Blackrock file reading tools
blackrockDir = 'C:\OneDrive\Lab\ESIN_Ephys_Files\Analysis\phyzzyML\dependencies\NPMK';
addpath(genpath(blackrockDir))
% Directory of Interest with x.ns5 files
dataDir = 'D:\EphysData\Data'; % The structure which is searched for
dataDirOut = 'E:\EphysDataBin'; % The structure which replaces the line above in the data file paths
% Identify files to be converted
files2ProcStruct = dir(fullfile(dataDir, '**', '*.ns5'));
files2Proc = fullfile({files2ProcStruct.folder}, {files2ProcStruct.name})';
% Generate the full path for the output
fileCoreName = extractBefore({files2ProcStruct.name}, '.ns5');
fileOutPaths = fullfile({files2ProcStruct.folder}', fileCoreName');
fileOutName = strcat(extractBefore({files2ProcStruct.name}, '.ns5'), '.bin')';
dataOutDirs = cellfun(@(x) strrep(x, dataDir, dataDirOut), fileOutPaths, 'UniformOutput', false); % Swap to E drive for output
fileOutName = fullfile(dataOutDirs, fileOutName);
% Don't worry about recordings prior to 2020, since those were single
% channel
keepInd = contains(files2Proc, {'2020', '2021'});
fileOutName = fileOutName(keepInd);
files2Proc = files2Proc(keepInd);
% Identify channels per recording to actually take
largerChSet = contains(files2Proc, 'Mo');
% for every file...
for file_i = 1:length(files2Proc)
% If the file has already been processed, skip
if exist(fileOutName{file_i}, 'file')
delete(fileOutName{file_i})
end
% Open the file
nsxStruct = openNSx(files2Proc{file_i}, 'uV');
electrodeNum = [nsxStruct.ElectrodesInfo.ElectrodeID];
if largerChSet(file_i)
dataInd = electrodeNum <= 128;
else
dataInd = electrodeNum <= 96;
end
% Extract the relevant data
dat = nsxStruct.Data(dataInd, :);
% Make sure the folders exist.
outDir = fileparts(fileOutName{file_i});
if ~exist(outDir, 'dir')
mkdir(outDir)
end
% write it to a binary appropriate for kiloSort (code pulled from kiloSort github page)
datI = int16(dat);
fid = fopen(fileOutName{file_i}, 'w');
fwrite(fid, datI, 'int16');
fclose(fid);
% Generate the channel map appropriate for the file, and save it in the
% same folder as the .bin file.
generateChanMap(sum(dataInd), fileOutName{file_i});
end
end