processDFFInitVars.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. function [ newNeurons, fluorescenceData, classifications, binaryPullTimes,pulls,options] = processDFFInitVars(dir, pullFrames, fr, autoClassifyNeurons, pTA)
  2. % This function initializes variables needed for processDFFPipeline.m
  3. % Detailed explanation goes here
  4. %% Find the json file and Load in variables for dff extraction from same directory.
  5. if isempty(dir)
  6. disp('Pick Centroid json file');
  7. [foldername, dir] = uigetfile('.json', 'Pick Centroid json file');
  8. jsonFilePath = fullfile(dir,foldername); % Set the file name using this variable
  9. else
  10. jsonFilePath = fullfile(dir, 'centroids.json');
  11. end
  12. if(exist(fullfile(dir, 'Fdf.mat'),'file'))
  13. load(fullfile(dir, 'Fdf.mat'))
  14. end
  15. if(exist(fullfile(dir, 'Cd.mat'),'file'))
  16. load(fullfile(dir, 'Cd.mat'))
  17. end
  18. if(exist(fullfile(dir, 'Sp.mat'),'file'))
  19. load(fullfile(dir, 'Sp.mat'))
  20. end
  21. %% Create a new struct with neuron data (nid, dff, Cd, and Sp) concatenated by time (optional)
  22. Fdf_concat = [];
  23. Sp_concat = [];
  24. Cd_concat = [];
  25. for i = 1:length(F_df)
  26. if(exist('F_df','var'))
  27. Fdf_concat = horzcat(Fdf_concat, cell2mat(F_df(i)));
  28. end
  29. if(exist('Sp','var'))
  30. Sp_concat = horzcat(Sp_concat, cell2mat(Sp(i)));
  31. end
  32. if(exist('Cd','var'))
  33. Cd_concat = horzcat(Cd_concat, cell2mat(Cd(i)));
  34. end
  35. end
  36. fluorescenceData = struct('Fdf',Fdf_concat,'Cd',Cd_concat,'Sp',Sp_concat);
  37. newNeurons = struct('nid',[],'dff',[],'Cd',[],'Sp',[]);
  38. neurons = jsonread(jsonFilePath);
  39. numNeurons = length(neurons.jmesh);
  40. for i = 1:numNeurons
  41. newNeurons(i).nid = i;
  42. %newNeurons(i).dff = Fdf_concat(i,:)';
  43. newNeurons(i).Sp = Sp_concat(i,:)';
  44. newNeurons(i).Cd = Cd_concat(i,:)';
  45. end
  46. %% Initialize Variables
  47. numFrames = length(Cd_concat);
  48. xpoints = 1:numFrames;
  49. if isempty(pTA)
  50. pTA = 1; % Default Value - frames before and after pull to average
  51. end
  52. if isempty(fr)
  53. fr = 3; %% If no framerate set, just use frame numbers.
  54. end
  55. options = struct('numFrames',numFrames,'numNeurons',numNeurons,'pTA',pTA,'xpoints',xpoints,'framerate',fr);
  56. %% Pull Time Data
  57. binaryPullTimes = zeros(1,numFrames);
  58. for i = 1:2:length(pullFrames)
  59. binaryPullTimes(pullFrames(i) - pTA :pullFrames(i+1) + pTA) = 1;
  60. end
  61. pulls= struct('pullNum',[],'pullFrames',[],'average',[]);
  62. pullNum = 1;
  63. for i = 1:2:length(pullFrames)
  64. thisPull = Cd_concat(:,pullFrames(i) - pTA : pullFrames(i+1) + pTA);
  65. meanPull = mean(thisPull,1);
  66. pulls(pullNum).pullNum = pullNum;
  67. pulls(pullNum).pullFrames = [pullFrames(i) pullFrames(i+1)];
  68. pulls(pullNum).average = meanPull;
  69. pullNum = pullNum + 1;
  70. end
  71. %% Initialize Data Frame for Classifying Cells as Active or Quiescent Active
  72. % data(3).im(2).roi_trace_thresh(10,:) % Third Animal on second days 10th roi
  73. % Data Struct - first input
  74. data=struct('im',[]);
  75. data.im = struct('roi_trace_thresh',Sp_concat,'roi_trace_df',Sp_concat);
  76. % Analysis Struct - second input
  77. % analysis(3).lever(2).lever_move_frames(:,1) % Third Animal on the Second
  78. % day - binarized movement frames
  79. analysis = struct('lever',[]);
  80. analysis.lever = struct('lever_move_frames',[]);
  81. analysis(1).lever(1).lever_move_frames = binaryPullTimes';
  82. [classified_rois, classified_p] = AP_classify_movement_cells_continuous(data,analysis); % Seems to work pretty well
  83. %% Neuron Classification Variables
  84. if isempty(autoClassifyNeurons)
  85. autoClassifyNeurons = true;
  86. end
  87. if autoClassifyNeurons
  88. active = find(classified_rois.movement);
  89. quiesc = find(classified_rois.quiescent);
  90. indisc = find(classified_rois.unclassified_active);
  91. else % Have a csv file with the data for
  92. disp('Pick neuron classification file');
  93. nCfile = uigetfile(fullfile(dir,'*.csv'),'Pick neuron classification file');
  94. neuronClass = csvread(fullfile(dir,nCfile));
  95. active = find(neuronClass==1);
  96. quiesc = find(neuronClass==2);
  97. indisc = find(neuronClass==3);
  98. end
  99. classifications = struct('classified_rois',classified_rois,'classified_p',classified_p,'active',active,'quiescent',quiesc,'indisc',indisc);
  100. end