Skip to content

Commit

Permalink
fixing open ephys pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
petersenpeter committed Aug 30, 2024
1 parent bf25350 commit 93c78ca
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
6 changes: 3 additions & 3 deletions calc_CellMetrics/concatenate_binary_files.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ function concatenate_binary_files(inputFiles,outputFile)
% Creating the command

if ispc
command1 = ['copy /b ', strjoin(inputFiles,'+'),' ', outputFile];
command1 = ['copy /b "', strjoin(inputFiles,'"+"'),'" ', outputFile];
status = system(command1);
elseif ismac
command1 = ['! cat ', strjoin(inputFiles,' '), ' > ', outputFile];
command1 = ['! cat "', strjoin(inputFiles,'" "'), '" > ', outputFile];
status = system(command1);
elseif isunix
command1 = ['! cat ', strjoin(inputFiles,' '), ' > ', outputFile];
command1 = ['! cat "', strjoin(inputFiles,'" "'), '" > ', outputFile];
status = system(command1);
end

Expand Down
2 changes: 1 addition & 1 deletion calc_CellMetrics/loadOpenEphysSettingsFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
channelmapping = [];
for i = 1:session.extracellular.nChannels
if isfield(openEphys_metadata.continuous(1).channels{i},'channel_metadata')
channelmapping(i) = openEphys_metadata.continuous(1).channels{i}.channel_metadata.value;
channelmapping(i) = openEphys_metadata.continuous(1).channels{i}.channel_metadata.value+1;
end
end

Expand Down
94 changes: 55 additions & 39 deletions calc_CellMetrics/preprocessOpenEphysData.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@

% 1. Detects epochs (name and duration from experiments and recording folders)
k = 1;
subFolder_Experiments = checkfolder(basepath, 'experiment');
for i = 1:numel(subFolder_Experiments)
subFolder_Recordings = checkfolder(subFolder_Experiments{i}, 'recording');
for j = 1:numel(subFolder_Recordings)
session.epochs{k}.name = fullfile(subFolder_Experiments{i},subFolder_Recordings{j});
session.epochs{k}.startTime = 0;
session.epochs{k}.stopTime = 0;
session.epochs{k}.notes = fullfile(subFolder_Experiments{i},subFolder_Recordings{j});
k = k + 1;
subFolder_RecordNodes = checkfolder(basepath, 'Record Node');
for m = 1:numel(subFolder_RecordNodes)
subFolder_Experiments = checkfolder(fullfile(basepath,subFolder_RecordNodes{m}), 'experiment');
for i = 1:numel(subFolder_Experiments)
subFolder_Recordings = checkfolder(fullfile(basepath,subFolder_RecordNodes{m},subFolder_Experiments{i}), 'recording');
for j = 1:numel(subFolder_Recordings)
session.epochs{k}.name = fullfile(subFolder_RecordNodes{m},subFolder_Experiments{i},subFolder_Recordings{j});
session.epochs{k}.startTime = 0;
session.epochs{k}.stopTime = 0;
session.epochs{k}.notes = fullfile(subFolder_RecordNodes{m},subFolder_Experiments{i},subFolder_Recordings{j});
k = k + 1;
end
end
end

Expand All @@ -47,40 +50,21 @@
session = loadOpenEphysSettingsFile(file1,session);



% 3. Epoch durations
inputFiles = {};
startTime = 0;
stopTime = 0;
nSamples = 0;
for i = 1:numel(session.epochs)
session.epochs{i}.startTime = startTime;
if exist(fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.0','continuous.bin'),'file')
inputFiles{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.0','continuous.bin');

elseif exist(fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.0','continuous.dat'),'file')
inputFiles{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.0','continuous.dat');

else
error(['Epoch duration could not be estimated as raw data file does not exist: ', inputFiles{i}]);
end
temp = dir(inputFiles{i});
duration = temp.bytes/session.extracellular.sr/session.extracellular.nChannels/2;
stopTime = startTime+duration;
session.epochs{i}.stopTime = stopTime;
startTime = stopTime;
nSamples = nSamples + temp.bytes/session.extracellular.nChannels/2;
end
session.extracellular.nSamples = nSamples;
session.general.duration = nSamples/session.extracellular.sr;
[session,inputFiles] = calculateEpochDurations(session,basepath);

% Shows session GUI if requested by user
if parameters.showGUI
session = gui_session(session);

% Calculates epoch durations and inputFiles again in case the number of epochs has changed
[session,inputFiles] = calculateEpochDurations(session,basepath);
end

% 4. Saving session struct
saveStruct(session);
if parameters.saveMat
saveStruct(session);
end


% 5. Merge dat files to single binary .dat file in basepath
Expand All @@ -90,14 +74,15 @@
outputFile = fullfile(basepath,[session.general.name,'.dat']);
binaryMergeWrapper(inputFiles, outputFile)


% 6. Merge lfp files
inputFiles_lfp = {};
for i = 1:numel(session.epochs)
if exist(fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.1','continuous.bin'),'file')
inputFiles_lfp{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.1','continuous.bin');
else
elseif exist(fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.1','continuous.dat'),'file')
inputFiles_lfp{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.1','continuous.dat');
else
inputFiles_lfp{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.ProbeA-LFP','continuous.dat');
end
end
outputFile_lfp = fullfile(basepath,[session.general.name,'.lfp']);
Expand All @@ -122,8 +107,6 @@
end




%% Local functions
function binaryMergeWrapper(inputFiles, outputFile)
% binaryMergeWrapper(inputFiles, outputFile)
Expand Down Expand Up @@ -215,4 +198,37 @@ function binaryMergeWrapper(inputFiles, outputFile)
individualSizes(f) = d.bytes;
end
totalSize = sum(individualSizes);
end

function [session,inputFiles] = calculateEpochDurations(session,basepath)
inputFiles = {};
startTime = 0;
stopTime = 0;
nSamples = 0;
for i = 1:numel(session.epochs)
session.epochs{i}.startTime = startTime;
if exist(fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.0','continuous.bin'),'file')
inputFiles{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.0','continuous.bin');

elseif exist(fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.0','continuous.dat'),'file')
inputFiles{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.0','continuous.dat');

elseif exist(fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.ProbeA','continuous.dat'),'file')
inputFiles{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.ProbeA','continuous.dat');

elseif exist(fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.ProbeA-AP','continuous.dat'),'file')
inputFiles{i} = fullfile(basepath,session.epochs{i}.name,'continuous','Neuropix-PXI-100.ProbeA-AP','continuous.dat');

else
error(['Epoch duration could not be estimated as raw data file does not exist: ', inputFiles{i}]);
end
temp = dir(inputFiles{i});
duration = temp.bytes/session.extracellular.sr/session.extracellular.nChannels/2;
stopTime = startTime+duration;
session.epochs{i}.stopTime = stopTime;
startTime = stopTime;
nSamples = nSamples + temp.bytes/session.extracellular.nChannels/2;
end
session.extracellular.nSamples = nSamples;
session.general.duration = nSamples/session.extracellular.sr;
end

0 comments on commit 93c78ca

Please sign in to comment.