Skip to content

Commit

Permalink
Merge pull request #1 from donaldsonlab/addBORISparser
Browse files Browse the repository at this point in the history
Add boris parser
  • Loading branch information
anmc9115 authored Sep 16, 2020
2 parents 9e8edea + 9d136f3 commit c7644d7
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 31 deletions.
43 changes: 43 additions & 0 deletions BORIS_format.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%% Script to take BORIS .csv output and format it for fiber fluorescence analysis with behavior_driver script
% Written 9/16/20 by Lisa Hiura
% Editted 9/16/20 by Anna McTigue

%% Imports Data
% % Import behavior Data
prompt = 'Enter the BORIS output file name \n';
behavior_file_name = input(prompt, 's');
behaviorData = readtable(behavior_file_name);

%drop the first 10 lines because they are extraneous
behaviorData(1:10,:) = [];

%drop columns for media file, total length, FPS, subject, behavioral
%category, and comment
behaviorData(:,[2:5,7:8]) = [];

%Assign meaningful column names
behaviorData.Properties.VariableNames = {'Time_in_Video' 'Behavior' 'Status'};

%drop rows that have 'STOP' as Status to treat all behaviors as point
%events
behaviorData(ismember(behaviorData.Status,'STOP'),:)=[];

%transform time in sec to msec
behaviorData.Time_in_Video = behaviorData.Time_in_Video ./ 1000;

%move the Status column over 1 to the right (should now be column 4)
behaviorData(:,4) = [ behaviorData(:,3)];

%prompt time offset to align behavior data with fluorescence data
prompt = 'Enter the timestamp file name \n';
timestamp_file_name = input(prompt, 's');
timestamps = readtable(timestamp_file_name);
time_offset = timestamps{1,1};

%fix column names
behaviorData.Properties.VariableNames = {'Time_in_Video' 'Behavior' 'corrected_time' 'Status'};

%add offset to the behavioral timestamps
%behaviorData.Var9 = behaviorData.ObservationId+time_offset;
behaviorData.corrected_time = behaviorData.Time_in_Video+time_offset;

5 changes: 2 additions & 3 deletions Zscore_wdriver.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
%% Plots zscores for each behavior occurance (Asks for channel)
% UPDATE THIS EACH NEW VERSION (date of update):
zversion = 'v.8/22/20';
% RENAME OLD FILE WITH DATE (for collie)
% Keep current file name as: Zscore_wdriver.m
zversion = 'v1.1';


%% Determines indices of behaviorT, 2sec before, & 5sec after in fTimeChannel

Expand Down
81 changes: 59 additions & 22 deletions behavior_driver.m
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
%% Creates Z-score plot for all behaviors
% updated 7/10/20 by Anna
% updated 9/10/20 by Anna

%% To Hard code files/behaviors use this
% % Behavior file:
% behavior_file_name = '2020-06-10T15_09_22.csv';
% behavior_file_name = '1fiberbehaviortest.csv';
% behaviorData = readtable(behavior_file_name);
%
% % Fluoresence file:
% f_file_name = 'FiberPhoSig2020-06-10T15_09_21.csv';
% f_file_name = '1fibersignaltest.csv';
% fData = readtable(f_file_name);

%
% % Behaviors:
% behaviors_input = 'Nose to Nose, Side by Side';
% behaviors_input = 'Autogroom';
% behaviors = regexp(behaviors_input,', ','split');
%
% % Animal number:
% animal_num = 1234;

%% Imports Data
% Import behavior Data
prompt = 'Enter the behavior data file name \n';
behavior_file_name = input(prompt, 's');
behaviorData = readtable(behavior_file_name);
if ~(exist('behaviorData','var'))
prompt = 'Enter the behavior data file name \n';
behavior_file_name = input(prompt, 's');
behaviorData = readtable(behavior_file_name);
end

% Import fluoresence Data
prompt = 'Enter the fluoresence data file name \n';
Expand All @@ -43,24 +48,56 @@
%% Runs behavior_data_twofiber script and z-score script on each behavior
for i = 1:length(behaviors)

%% Runs behavior two-fiber plots
%% Runs behavior plots
behavior_name = behaviors(i);
behavior_twofiber_wdriver;

%% Runs Z-score plots
% Asks what channel(s) to analyze
list = {'fGreenLisosbestic','fGreenLred','fGreenLgreen','fGreenRisosbestic',...
'fGreenRred','fGreenRgreen','fRedLisosbestic','fRedLred','fRedLgreen',...
'fRedRisosbestic','fRedRred','fRedRgreen'};
[zscore_indx,tf] = listdlg('PromptString',{'Choose one or more channels to' ...
' analyze for ' + string(behavior_name)}...

% prompts user to select one or two fiber script
list = {'One fiber','Two fiber'};
[fiber_indx,tf1] = listdlg('PromptString',{'How many fibers did you use?'}...
,'ListString',list);
channels = {fGreenLisosbestic,fGreenLred,fGreenLgreen,fGreenRisosbestic,...
fGreenRred,fGreenRgreen,fRedLisosbestic,fRedLred,fRedLgreen,...
fRedRisosbestic,fRedRred,fRedRgreen};
if tf == 0 % catch no selection error
if tf1 == 0 % catch no selection error
fprintf('**Please make a selection from the channels list to continue** /n');
break
end
if fiber_indx == 1
behavior_onefiber_wdriver;
end
if fiber_indx == 2
behavior_twofiber_wdriver;
end


%% Runs Z-score plots
% Asks what channel(s) to analyze
if fiber_indx == 1
list = {'fGreenisosbestic','fGreenred','fGreengreen','fRedisosbestic',...
'fRedred','fRedgreen'};
[zscore_indx,tf] = listdlg('PromptString',{'Choose one or more channels to' ...
' analyze for ' + string(behavior_name)}...
,'ListString',list);
channels = {fGreenisosbestic,fGreenred,fGreengreen,fRedisosbestic,...
fRedred,fRedgreen};
if tf == 0 % catch no selection error
fprintf('**Please make a selection from the channels list to continue** /n');
break
end
end

if fiber_indx == 2
list = {'fGreenLisosbestic','fGreenLred','fGreenLgreen','fGreenRisosbestic',...
'fGreenRred','fGreenRgreen','fRedLisosbestic','fRedLred','fRedLgreen',...
'fRedRisosbestic','fRedRred','fRedRgreen'};
[zscore_indx,tf] = listdlg('PromptString',{'Choose one or more channels to' ...
' analyze for ' + string(behavior_name)}...
,'ListString',list);
channels = {fGreenLisosbestic,fGreenLred,fGreenLgreen,fGreenRisosbestic,...
fGreenRred,fGreenRgreen,fRedLisosbestic,fRedLred,fRedLgreen,...
fRedRisosbestic,fRedRred,fRedRgreen};
if tf == 0 % catch no selection error
fprintf('**Please make a selection from the channels list to continue** /n');
break
end
end

% Creates z-score plot for each selected channel
for i = 1:length(zscore_indx)
Expand Down
196 changes: 196 additions & 0 deletions behavior_onefiber_wdriver.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
%% Script to analze behavior fluorescence data (one fiber)
% written by Katie Gallagher Edited by Kathleen Murphy & Anna McTigue

% UPDATE THIS EACH NEW VERSION (date of update):
onefiberversion = 'v1.1';

format long
%% Read Tables

fTime = table2array(fData(:,1)); %time column in fluorescent data

fRed = table2array(fData(:,3)); %3rd column (red)
fGreen = table2array(fData(:,4)); %4th column (green)

bTime = table2array(behaviorData(:,3)); % time of behavior
behavior = table2array(behaviorData(:,2)); % actual behavior
bTime = double(bTime);

%% De-interleave

% determining which row index is red channel
offset1 = fRed(1:3:end);
offset2 = fRed(2:3:end);
offset3 = fRed(3:3:end);
meanoffsets = [mean(offset1), mean(offset2), mean(offset3)];
redIdx = find(meanoffsets == max(meanoffsets(:)));

% assigning correct rows to colors
fGreenisosbestic = fGreen(redIdx+2:3:end); %iso
fGreenred = fGreen(redIdx:3:end); %red
fGreengreen = fGreen(redIdx+1:3:end);%green

fRedisosbestic = fRed(redIdx+2:3:end);
fRedred = fRed(redIdx:3:end);
fRedgreen = fRed(redIdx+1:3:end);

fTimeIsosbestic = fTime(redIdx+2:3:end);
fTimeRed = fTime(redIdx:3:end);
fTimeGreen = fTime(redIdx+1:3:end);

%% Minimize behavior data
%pull out index and time for every time behavior occurs
%initialize matrices to hold indices and corresponding times
behaviorIdx = {}; %will be populated with behavior indices
behaviorT = {};
j=1;
for i = 1:length(behavior)
if string(behavior(i)) == behavior_name
behaviorIdx{j} = i;
behaviorT{j} = bTime(i);
j=j+1;
end
end

behaviorT = cell2mat(behaviorT);
behaviorIdx = cell2mat(behaviorIdx);

% if time between behaviors sis greater than threshold seconds then count as
% one bout
Thres=200;
startbout = {}; %start of first bout is the first time it licks
startboutIdx = {}; %index of start bout
finbout = {};
finboutIdx = {}; %index of end bout
% i=1;
% startbout{i}=behaviorT(i);
% startboutIdx{i} = behaviorIdx(i);
% j=1;
% % seeing if diff in time btwn licks is great enough to consider it as
% % about5
% while i < length(behaviorT)
% if abs(behaviorT(i+1)-behaviorT(i))>=Thres
% endTime=behaviorT(i);
% endTimeIdx = behaviorIdx(i);
% startTime=behaviorT(i+1);
% startTimeIdx = behaviorIdx(i+1);
% finbout{j} = endTime;
% finboutIdx{j} = endTimeIdx;
% startbout{j+1} = startTime;
% startboutIdx{j+1} = startTimeIdx;
%
% j=j+1;
% end
% i=i+1;
% end
% finbout{j} = behaviorT(end);
% finboutIdx{j}=behaviorIdx(end);
% finbout{length(startbout)} = behaviorT(length(behaviorT));
% finbout = cell2mat(finbout);
% startbout = cell2mat(startbout);
% finboutIdx = cell2mat(finboutIdx);
% startboutIdx = cell2mat(startboutIdx);
% %seeing if number of licks within each bout is great enough to consider it
% %as a bout
% % threshold on how many licks constitute a bout
% numLicksThres = 0; %must have 10 licks in order to be considered a bout
% i=1;
% j=1;
% while i < length(startbout)
% if (finboutIdx(i) - startboutIdx(i)) < numLicksThres
% finbout(i) = [];
% startbout(i) = [];
% j=j+1;
% end
% i=i+1;
% end

%% Plot behavior times on raw data plot as tick marks

%Green Plot
gplot_title = string(behavior_name) + ' Green Plot ' + ' Animal no. '+ ...
animal_num + ' ' + onefiberversion;
figure('Name', gplot_title)
subplot(3,1,1)
plot(fTimeIsosbestic,fGreenisosbestic)
xlabel('Time (ms)')
ylabel('Isosbestic')
for i = 1:length(behaviorT)
xline(behaviorT(i),'k')
end
for i = 1:length(startbout)
xline(startbout(i),'g');
end
for i = 1:length(finbout)
xline(finbout(i),'r');
end
subplot(3,1,2)
plot(fTimeRed,fGreenred)
xlabel('Time (ms)')
ylabel('Red')
for i = 1:length(behaviorT)
xline(behaviorT(i),'k')
end
for i = 1:length(startbout)
xline(startbout(i),'g');
end
for i = 1:length(finbout)
xline(finbout(i),'r');
end
subplot(3,1,3)
plot(fTimeGreen,fGreengreen)
xlabel('Time (ms)')
ylabel('Green')
for i = 1:length(behaviorT)
xline(behaviorT(i),'k')
end
for i = 1:length(startbout)
xline(startbout(i),'g');
end
for i = 1:length(finbout)
xline(finbout(i),'r');
end

%Red PLot
rplot_title = string(behavior_name) + ' Red Plot' + ' Animal no. '+ ...
animal_num + ' ' + onefiberversion;
figure('Name', rplot_title)
subplot(3,1,1)
plot(fTimeIsosbestic,fRedisosbestic)
xlabel('Time (ms)')
ylabel('Isosbestic')
for i = 1:length(behaviorT)
xline(behaviorT(i),'k')
end
for i = 1:length(startbout)
xline(startbout(i),'g');
end
for i = 1:length(finbout)
xline(finbout(i),'r');
end
subplot(3,1,2)
plot(fTimeRed,fRedred)
xlabel('Time (ms)')
ylabel('Red')
for i = 1:length(behaviorT)
xline(behaviorT(i),'k')
end
for i = 1:length(startbout)
xline(startbout(i),'g');
end
for i = 1:length(finbout)
xline(finbout(i),'r');
end
subplot(3,1,3)
plot(fTimeGreen,fRedgreen)
xlabel('Time (ms)')
ylabel('Green')
for i = 1:length(behaviorT)
xline(behaviorT(i),'k')
end
for i = 1:length(startbout)
xline(startbout(i),'g');
end
for i = 1:length(finbout)
xline(finbout(i),'r');
end
10 changes: 4 additions & 6 deletions behavior_twofiber_wdriver.m
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
%% Script to analze behavior fluorescence data
%% Script to analze behavior fluorescence data (two fiber)
% written by Katie Gallagher Edited by Kathleen Murphy & Anna McTigue

% UPDATE THIS EACH NEW VERSION (date of update):
twofiberversion = 'v.8/22/2020';
% RENAME OLD FILE WITH DATE (for collie)
% Keep current file name as: behavior_twofiber_wdriver.m
twofiberversion = 'v1.1';

format long
%% Read Tables

fTime = table2array(fData(:,1)); %time column in fluorescent data

fRedL = table2array(fData(:,3)); %3rd column in fluorescent data = red left
fGreenL = table2array(fData(:,4)); %4th = green right
fGreenL = table2array(fData(:,4)); %4th = green left
fRedR = table2array(fData(:,5)); % red right
fGreenR = table2array(fData(:,6)); % green left
fGreenR = table2array(fData(:,6)); % green right

bTime = table2array(behaviorData(:,3)); % time of behavior
behavior = table2array(behaviorData(:,2)); % actual behavior
Expand Down

0 comments on commit c7644d7

Please sign in to comment.