From 85c3e99d768470ed8e3185b90aad6a2914d10330 Mon Sep 17 00:00:00 2001 From: anmc9115 Date: Thu, 10 Sep 2020 22:43:58 -0600 Subject: [PATCH 1/3] add one fiber script --- Zscore_wdriver.m | 5 +- behavior_driver.m | 131 +++++++++++++++--------- behavior_onefiber_wdriver.m | 196 ++++++++++++++++++++++++++++++++++++ behavior_twofiber_wdriver.m | 10 +- 4 files changed, 285 insertions(+), 57 deletions(-) create mode 100644 behavior_onefiber_wdriver.m diff --git a/Zscore_wdriver.m b/Zscore_wdriver.m index 51a6caf..d2c67ca 100644 --- a/Zscore_wdriver.m +++ b/Zscore_wdriver.m @@ -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 diff --git a/behavior_driver.m b/behavior_driver.m index 1bcacbb..21b173e 100644 --- a/behavior_driver.m +++ b/behavior_driver.m @@ -1,67 +1,102 @@ %% 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'; -% behaviorData = readtable(behavior_file_name); -% -% % Fluoresence file: -% f_file_name = 'FiberPhoSig2020-06-10T15_09_21.csv'; -% fData = readtable(f_file_name); +% Behavior file: +behavior_file_name = '1fiberbehaviortest.csv'; +behaviorData = readtable(behavior_file_name); -% % Behaviors: -% behaviors_input = 'Nose to Nose, Side by Side'; -% behaviors = regexp(behaviors_input,', ','split'); +% Fluoresence file: +f_file_name = '1fibersignaltest.csv'; +fData = readtable(f_file_name); -%% Imports Data -% Import behavior Data -prompt = 'Enter the behavior data file name \n'; -behavior_file_name = input(prompt, 's'); -behaviorData = readtable(behavior_file_name); +% Behaviors: +behaviors_input = 'Autogroom'; +behaviors = regexp(behaviors_input,', ','split'); -% Import fluoresence Data -prompt = 'Enter the fluoresence data file name \n'; -f_file_name = input(prompt, 's'); -fData = readtable(f_file_name); +% Animal number: +animal_num = 1234; -% Import behaviors to analyze -unique_behaviors = unique(table2array(behaviorData(:,2))); -[behavior_indx,ok] = listdlg('Promptstring',{'Pick the behaviors to be ' ... - 'analyzed'}, 'Liststring', unique_behaviors); -if ok == 0 % catch no selection error - fprintf('**Please make a selection from the behaviors list to continue** \n'); -end -behaviors = []; -% puts selected behaviors into behaviors array -for i = 1:(length(behavior_indx)) - behaviors = [behaviors, unique_behaviors(behavior_indx(i))]; -end -% Asks for animal number -animal_num = input('What is the animal number? \n','s'); +% %% Imports Data +% % Import behavior Data +% prompt = 'Enter the behavior data file name \n'; +% behavior_file_name = input(prompt, 's'); +% behaviorData = readtable(behavior_file_name); +% +% % Import fluoresence Data +% prompt = 'Enter the fluoresence data file name \n'; +% f_file_name = input(prompt, 's'); +% fData = readtable(f_file_name); +% +% % Import behaviors to analyze +% unique_behaviors = unique(table2array(behaviorData(:,2))); +% [behavior_indx,ok] = listdlg('Promptstring',{'Pick the behaviors to be ' ... +% 'analyzed'}, 'Liststring', unique_behaviors); +% if ok == 0 % catch no selection error +% fprintf('**Please make a selection from the behaviors list to continue** \n'); +% end +% behaviors = []; +% % puts selected behaviors into behaviors array +% for i = 1:(length(behavior_indx)) +% behaviors = [behaviors, unique_behaviors(behavior_indx(i))]; +% end +% % Asks for animal number +% animal_num = input('What is the animal number? \n','s'); %% 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) % Saves channel array in channel and channel name in channel_name diff --git a/behavior_onefiber_wdriver.m b/behavior_onefiber_wdriver.m new file mode 100644 index 0000000..78d5a7d --- /dev/null +++ b/behavior_onefiber_wdriver.m @@ -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 diff --git a/behavior_twofiber_wdriver.m b/behavior_twofiber_wdriver.m index b4321c1..2d62276 100644 --- a/behavior_twofiber_wdriver.m +++ b/behavior_twofiber_wdriver.m @@ -1,10 +1,8 @@ -%% 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 @@ -12,9 +10,9 @@ 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 From a31f85f039d004b0c299b85cfef380114dabe63e Mon Sep 17 00:00:00 2001 From: anmc9115 Date: Wed, 16 Sep 2020 16:07:14 -0600 Subject: [PATCH 2/3] add boris parser --- BORIS_format.m | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 BORIS_format.m diff --git a/BORIS_format.m b/BORIS_format.m new file mode 100644 index 0000000..1c8899f --- /dev/null +++ b/BORIS_format.m @@ -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; + From 9d136f37f3e61665e3bb6918e71fea7a5fa75b14 Mon Sep 17 00:00:00 2001 From: anmc9115 Date: Wed, 16 Sep 2020 16:08:32 -0600 Subject: [PATCH 3/3] compatible with boris parser --- behavior_driver.m | 76 ++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/behavior_driver.m b/behavior_driver.m index 21b173e..6aed92f 100644 --- a/behavior_driver.m +++ b/behavior_driver.m @@ -2,46 +2,48 @@ % updated 9/10/20 by Anna %% To Hard code files/behaviors use this -% Behavior file: -behavior_file_name = '1fiberbehaviortest.csv'; -behaviorData = readtable(behavior_file_name); - -% Fluoresence file: -f_file_name = '1fibersignaltest.csv'; -fData = readtable(f_file_name); - -% Behaviors: -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'); +% % Behavior file: +% behavior_file_name = '1fiberbehaviortest.csv'; % behaviorData = readtable(behavior_file_name); % -% % Import fluoresence Data -% prompt = 'Enter the fluoresence data file name \n'; -% f_file_name = input(prompt, 's'); -% fData = readtable(f_file_name); +% % Fluoresence file: +% f_file_name = '1fibersignaltest.csv'; +% fData = readtable(f_file_name); +% +% % Behaviors: +% behaviors_input = 'Autogroom'; +% behaviors = regexp(behaviors_input,', ','split'); % -% % Import behaviors to analyze -% unique_behaviors = unique(table2array(behaviorData(:,2))); -% [behavior_indx,ok] = listdlg('Promptstring',{'Pick the behaviors to be ' ... -% 'analyzed'}, 'Liststring', unique_behaviors); -% if ok == 0 % catch no selection error -% fprintf('**Please make a selection from the behaviors list to continue** \n'); -% end -% behaviors = []; -% % puts selected behaviors into behaviors array -% for i = 1:(length(behavior_indx)) -% behaviors = [behaviors, unique_behaviors(behavior_indx(i))]; -% end -% % Asks for animal number -% animal_num = input('What is the animal number? \n','s'); +% % Animal number: +% animal_num = 1234; + +%% Imports Data +% Import behavior Data +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'; +f_file_name = input(prompt, 's'); +fData = readtable(f_file_name); + +% Import behaviors to analyze +unique_behaviors = unique(table2array(behaviorData(:,2))); +[behavior_indx,ok] = listdlg('Promptstring',{'Pick the behaviors to be ' ... + 'analyzed'}, 'Liststring', unique_behaviors); +if ok == 0 % catch no selection error + fprintf('**Please make a selection from the behaviors list to continue** \n'); +end +behaviors = []; +% puts selected behaviors into behaviors array +for i = 1:(length(behavior_indx)) + behaviors = [behaviors, unique_behaviors(behavior_indx(i))]; +end +% Asks for animal number +animal_num = input('What is the animal number? \n','s'); %% Runs behavior_data_twofiber script and z-score script on each behavior for i = 1:length(behaviors)