Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanharvey1 committed Oct 31, 2024
1 parent d8acfaf commit f5a05ef
Showing 1 changed file with 86 additions and 85 deletions.
171 changes: 86 additions & 85 deletions preProcessing/preprocessSession.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function preprocessSession(varargin)
function preprocessSession(varargin)

% preprocessSession(varargin)

Expand All @@ -19,16 +19,16 @@ function preprocessSession(varargin)
% supports Intan Buzsaki Edition)
% digitalChannels List of digital channels with pulses to be detected (it
% supports Intan Buzsaki Edition)
% forceSum Force make folder summary (overwrite, if necessary).
% forceSum Force make folder summary (overwrite, if necessary).
% Default false
% cleanArtifacts Remove artifacts from dat file. By default, if there is
% cleanArtifacts Remove artifacts from dat file. By default, if there is
% analogEv in folder, is true
% stateScore Run automatic brain state detection with SleepScoreMaster.
% stateScore Run automatic brain state detection with SleepScoreMaster.
% Default true
% spikeSort Run automatic spike sorting using Kilosort. Default true
% sortFiles Sort subsessions with the date and timestamp in the end
% sortFiles Sort subsessions with the date and timestamp in the end
% of the folder name (ignore alphabetical order)
% removeNoise Remove first (noise) ICA component from .dat file before
% removeNoise Remove first (noise) ICA component from .dat file before
% spike sorting
% cleanRez Run automatic noise detection of the Kilosort results
% (these will be pre-labelled as noise in phy). Default true
Expand All @@ -40,7 +40,7 @@ function preprocessSession(varargin)
% path_to_dlc_bat_file path to your dlc bat file to analyze your videos
% (see neurocode\behavior\dlc for example files)
% nKilosortRuns Number of desired Kilosort runs (default = 1). The
% function will break down the shanks into "nKilosortRuns"
% function will break down the shanks into "nKilosortRuns"
% groups for each run
% [sortFiles] Concatenate .dat files based on intan time or
% alphabetically (default = true, based on time).
Expand All @@ -56,7 +56,7 @@ function preprocessSession(varargin)
% SEE ALSO
%
% Copyright (C) 2020-2023 by AntonioFR, 2021 Azahara Oliva,
% 2022-2023 Lindsay Karaba, 2022-2024 Heath Larson,
% 2022-2023 Lindsay Karaba, 2022-2024 Heath Larson,
% 2022-2024 Ryan Harvey, 2022-2024 Ralitsa Todorova
%
% This program is free software; you can redistribute it and/or modify
Expand All @@ -70,28 +70,28 @@ function preprocessSession(varargin)
%% Set up parameters and parse inputs

p = inputParser;
addParameter(p,'basepath',pwd,@isfolder); % by default, current folder
addParameter(p,'fillMissingDatFiles',false,@islogical);
addParameter(p,'fillTypes',[],@iscellstr);
addParameter(p,'analogInputs',false,@islogical);
addParameter(p,'analogChannels',[],@isnumeric);
addParameter(p,'digitalInputs',false,@islogical);
addParameter(p,'digitalChannels',[],@isnumeric);
addParameter(p,'getAcceleration',false,@islogical);
addParameter(p,'cleanArtifacts',false,@islogical);
addParameter(p,'stateScore',true,@islogical);
addParameter(p,'spikeSort',true,@islogical);
addParameter(p,'cleanRez',true,@islogical);
addParameter(p,'getPos',false,@islogical);
addParameter(p,'removeNoise',false,@islogical); % denoising method removing the first PCA component
addParameter(p,'runSummary',false,@islogical);
addParameter(p,'SSD_path','D:\KiloSort',@ischar) % Path to SSD disk. Make it empty to disable SSD
addParameter(p,'path_to_dlc_bat_file','',@isfile)
addParameter(p,'nKilosortRuns',1,@isnumeric);
addParameter(p,'sortFiles',true,@islogical);
addParameter(p, 'basepath', pwd, @isfolder); % by default, current folder
addParameter(p, 'fillMissingDatFiles', false, @islogical);
addParameter(p, 'fillTypes', [], @iscellstr);
addParameter(p, 'analogInputs', false, @islogical);
addParameter(p, 'analogChannels', [], @isnumeric);
addParameter(p, 'digitalInputs', false, @islogical);
addParameter(p, 'digitalChannels', [], @isnumeric);
addParameter(p, 'getAcceleration', false, @islogical);
addParameter(p, 'cleanArtifacts', false, @islogical);
addParameter(p, 'stateScore', true, @islogical);
addParameter(p, 'spikeSort', true, @islogical);
addParameter(p, 'cleanRez', true, @islogical);
addParameter(p, 'getPos', false, @islogical);
addParameter(p, 'removeNoise', false, @islogical); % denoising method removing the first PCA component
addParameter(p, 'runSummary', false, @islogical);
addParameter(p, 'SSD_path', 'D:\KiloSort', @ischar) % Path to SSD disk. Make it empty to disable SSD
addParameter(p, 'path_to_dlc_bat_file', '', @isfile)
addParameter(p, 'nKilosortRuns', 1, @isnumeric);
addParameter(p, 'sortFiles', true, @islogical);

% addParameter(p,'pullData',[],@isdir); To do...
parse(p,varargin{:});
parse(p, varargin{:});

basepath = p.Results.basepath;
fillMissingDatFiles = p.Results.fillMissingDatFiles;
Expand All @@ -114,101 +114,102 @@ function preprocessSession(varargin)
sortFiles = p.Results.sortFiles;


if ~exist(basepath,'dir')
if ~exist(basepath, 'dir')
error('path provided does not exist')
end
cd(basepath)

%% Pull meta data

% Get session names
if strcmp(basepath(end),filesep)
if strcmp(basepath(end), filesep)
basepath = basepath(1:end-1);
end
[~,basename] = fileparts(basepath);
[~, basename] = fileparts(basepath);

% Get xml file in order
xmlFile = checkFile('fileType','.xml','searchSubdirs',true);
xmlFile = checkFile('fileType', '.xml', 'searchSubdirs', true);
xmlFile = xmlFile(1);
if ~(strcmp(xmlFile.folder,basepath)&&strcmp(xmlFile.name(1:end-4),basename))
copyfile([xmlFile.folder,filesep,xmlFile.name],[basepath,filesep,basename,'.xml'])
if ~(strcmp(xmlFile.folder, basepath) && strcmp(xmlFile.name(1:end-4), basename))
copyfile([xmlFile.folder, filesep, xmlFile.name], [basepath, filesep, basename, '.xml'])
end

% Check info.rhd
% (assumes this will be the same across subsessions)
rhdFile = checkFile('fileType','.rhd','searchSubdirs',true);
rhdFile = checkFile('fileType', '.rhd', 'searchSubdirs', true);
rhdFile = rhdFile(1);
if ~(strcmp(rhdFile.folder,basepath)&&strcmp(rhdFile.name(1:end-4),basename))
copyfile([rhdFile.folder,filesep,rhdFile.name],[basepath,filesep,basename,'.rhd'])
if ~(strcmp(rhdFile.folder, basepath) && strcmp(rhdFile.name(1:end-4), basename))
copyfile([rhdFile.folder, filesep, rhdFile.name], [basepath, filesep, basename, '.rhd'])
end

%% Make SessionInfo
% Manually ID bad channels at this point. automating it would be good
session = sessionTemplate(basepath,'showGUI',false);
save(fullfile(basepath,[basename, '.session.mat']),'session');
session = sessionTemplate(basepath, 'showGUI', false);
save(fullfile(basepath, [basename, '.session.mat']), 'session');

%% Fill missing dat files of zeros
if fillMissingDatFiles
if isempty(fillTypes)
fillTypes = {'analogin';'digitalin';'auxiliary';'time';'supply'};
fillTypes = {'analogin'; 'digitalin'; 'auxiliary'; 'time'; 'supply'};
end
for ii = 1:length(fillTypes)
fillMissingDats('basepath',basepath,'fileType',fillTypes{ii});
fillMissingDats('basepath', basepath, 'fileType', fillTypes{ii});
end
end

%% Concatenate sessions
disp('Concatenate session folders...');
concatenateDats(basepath,sortFiles);
concatenateDats(basepath, sortFiles);

%% run again to add epochs from basename.MergePoints.m
session = sessionTemplate(basepath,'showGUI',false);
save(fullfile(basepath,[basename, '.session.mat']),'session');
session = sessionTemplate(basepath, 'showGUI', false);
save(fullfile(basepath, [basename, '.session.mat']), 'session');

%% Process additional inputs - CHECK FOR OUR LAB

% Analog inputs
% check the two different fucntions for delaing with analog inputs and proably rename them
if analogInputs
if ~isempty(analogChannels)
analogInp = computeAnalogInputs('analogCh',analogChannels,'saveMat',true);
if ~isempty(analogChannels)
analogInp = computeAnalogInputs('analogCh', analogChannels, 'saveMat', true);
else
analogInp = computeAnalogInputs('analogCh',[],'saveMat',true);
analogInp = computeAnalogInputs('analogCh', [], 'saveMat', true);
end

% analog pulses ...
[pulses] = getAnalogPulses('samplingRate',session.extracellular.sr);
[pulses] = getAnalogPulses('samplingRate', session.extracellular.sr);
end

% Digital inputs
if digitalInputs
if ~isempty(digitalChannels)
% need to change to only include specified channels
digitalInp = getDigitalIn('all','fs',session.extracellular.sr,'digUse',digitalChannels);
digitalInp = getDigitalIn('all', 'fs', session.extracellular.sr, 'digUse', digitalChannels);
else
digitalInp = getDigitalIn('all','fs',session.extracellular.sr);
digitalInp = getDigitalIn('all', 'fs', session.extracellular.sr);
end
end

% Auxilary input
if getAcceleration
accel = computeIntanAccel('saveMat',true);
accel = computeIntanAccel('saveMat', true);
end

%% Make LFP
try
try
LFPfromDat(basepath,'outFs',1250,'useGPU',false);
LFPfromDat(basepath, 'outFs', 1250, 'useGPU', false);
catch
if (exist([basepath '\' basename '.lfp'])~=0)
fclose([basepath '\' basename '.lfp']); %if the above run failed after starting the file
delete([basepath '\' basename '.lfp']);
if (exist([basepath, '\', basename, '.lfp']) ~= 0)
fclose([basepath, '\', basename, '.lfp']); %if the above run failed after starting the file
delete([basepath, '\', basename, '.lfp']);
end
LFPfromDat(basepath,'outFs',1250,'useGPU',false);
LFPfromDat(basepath, 'outFs', 1250, 'useGPU', false);
end
catch
try
warning('LFPfromDat failed, trying ResampleBinary')
ResampleBinary([basepath '\' basename '.dat'],[basepath '\' basename '.lfp'],session.extracellular.nChannels,1,16);
ResampleBinary([basepath, '\', basename, '.dat'], [basepath, '\', basename, '.lfp'], session.extracellular.nChannels, 1, 16);
catch
warning('LFP file could not be generated, moving on');
end
Expand All @@ -221,18 +222,18 @@ function preprocessSession(varargin)
%% Clean data - CHECK FOR OUR LAB
% Remove stimulation artifacts
if cleanArtifacts && analogInputs
[pulses] = getAnalogPulses(analogInp,'analogCh',analogChannels);
[pulses] = getAnalogPulses(analogInp, 'analogCh', analogChannels);
cleanPulses(pulses.ints{1}(:));
end

%% Get brain states
% an automatic way of flaging bad channels is needed
if stateScore
try
if exist('pulses','var')
SleepScoreMaster(basepath,'noPrompts',true,'ignoretime',pulses.intsPeriods,'rejectChannels',session.channelTags.Bad.channels); % try to sleep score
if exist('pulses', 'var')
SleepScoreMaster(basepath, 'noPrompts', true, 'ignoretime', pulses.intsPeriods, 'rejectChannels', session.channelTags.Bad.channels); % try to sleep score
else
SleepScoreMaster(basepath,'noPrompts',true,'rejectChannels',session.channelTags.Bad.channels); % takes lfp in base 0
SleepScoreMaster(basepath, 'noPrompts', true, 'rejectChannels', session.channelTags.Bad.channels); % takes lfp in base 0
end
catch
warning('Problem with SleepScore scoring... unable to calculate');
Expand All @@ -242,44 +243,44 @@ function preprocessSession(varargin)

% remove noise from data for cleaner spike sorting
if removeNoise
try EMGFromLFP = getStruct(basepath,'EMGFromLFP');
try EMGFromLFP = getStruct(basepath, 'EMGFromLFP');
catch
EMGFromLFP = getEMGFromLFP(basepath,'noPrompts',true,'saveMat',true);
EMGFromLFP = getEMGFromLFP(basepath, 'noPrompts', true, 'saveMat', true);
end

baseline = EMGFromLFP.timestamps(FindInterval(EMGFromLFP.data>quantile(EMGFromLFP.data,0.99))); % select the period of top 1% EMG activity as the denoising baseline
DenoiseDat(fullfile(basepath,[basename '.dat']),session,'baseline',baseline);
baseline = EMGFromLFP.timestamps(FindInterval(EMGFromLFP.data > quantile(EMGFromLFP.data, 0.99))); % select the period of top 1% EMG activity as the denoising baseline
DenoiseDat(fullfile(basepath, [basename, '.dat']), session, 'baseline', baseline);
end


%% Kilosort concatenated sessions - Needs to be changed to probes, not shanks HLR 01/05/2023
if spikeSort
if nKilosortRuns>1 % if more than one Kilosort cycle desired, break the shanks down into the desired number of kilosort runs
if nKilosortRuns > 1 % if more than one Kilosort cycle desired, break the shanks down into the desired number of kilosort runs
shanks = session.extracellular.spikeGroups.channels;
kilosortGroup = ceil(((1:length(shanks))/nKilosortRuns));
for i=1:nKilosortRuns
channels = cat(2,shanks{kilosortGroup==i});
kilosortGroup = ceil(((1:length(shanks)) / nKilosortRuns));
for i = 1:nKilosortRuns
channels = cat(2, shanks{kilosortGroup == i});
excludeChannels = [];
excludeChannels = find(~ismember((1:session.extracellular.nChannels),channels));
excludeChannels = cat(2,excludeChannels,session.channelTags.Bad.channels);
excludeChannels = find(~ismember((1:session.extracellular.nChannels), channels));
excludeChannels = cat(2, excludeChannels, session.channelTags.Bad.channels);
excludeChannels = unique(excludeChannels);
if (length(excludeChannels)==session.extracellular.nChannels)
warning(['Run number ' num2str(i) ' excluded, moving on']);
if (length(excludeChannels) == session.extracellular.nChannels)
warning(['Run number ', num2str(i), ' excluded, moving on']);
else
kilosortFolder = KiloSortWrapper('SSD_path',SSD_path,'rejectchannels',excludeChannels);
kilosortFolder = KiloSortWrapper('SSD_path', SSD_path, 'rejectchannels', excludeChannels);
if cleanRez
load(fullfile(kilosortFolder,'rez.mat'),'rez');
CleanRez(rez,'savepath',kilosortFolder);
load(fullfile(kilosortFolder, 'rez.mat'), 'rez');
CleanRez(rez, 'savepath', kilosortFolder);
end
end
end
else

%% single sort
kilosortFolder = KiloSortWrapper('SSD_path',SSD_path,...
'rejectchannels',session.channelTags.Bad.channels); % 'NT',20*1024 for long sessions when RAM is overloaded
kilosortFolder = KiloSortWrapper('SSD_path', SSD_path, ...
'rejectchannels', session.channelTags.Bad.channels); % 'NT',20*1024 for long sessions when RAM is overloaded
if cleanRez
load(fullfile(kilosortFolder,'rez.mat'),'rez');
CleanRez(rez,'savepath',kilosortFolder);
load(fullfile(kilosortFolder, 'rez.mat'), 'rez');
CleanRez(rez, 'savepath', kilosortFolder);
end
% PhyAutoClustering(kilosortFolder);
end
Expand All @@ -288,11 +289,11 @@ function preprocessSession(varargin)
%% Get tracking positions
if getPos
% check for pre existing deeplab cut
if exist(path_to_dlc_bat_file,'file')
system(path_to_dlc_bat_file,'-echo')
if exist(path_to_dlc_bat_file, 'file')
system(path_to_dlc_bat_file, '-echo')
end
% put tracking into standard format
general_behavior_file('basepath',basepath)
general_behavior_file('basepath', basepath)
end

%% Summary - NOT WELL IMPLEMENTED YET
Expand Down

0 comments on commit f5a05ef

Please sign in to comment.