MakeTmNeuronsRFs_Fig4.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. close all;
  2. clear all;
  3. clc;
  4. %% Define search path.
  5. parentDir = fileparts(fileparts(mfilename('fullpath')));
  6. saveFolder = [parentDir filesep 'data'];
  7. load([saveFolder filesep 'Fig4-Tm1Tm2Tm4-processedTableWithBackground'], 'ProcessedTable');
  8. %%
  9. figDir = [parentDir filesep 'figures' filesep 'Fig4' filesep];
  10. if ~exist(figDir, 'dir'); mkdir(figDir); end
  11. %%
  12. % Stimuli of interest.
  13. stimSetCellStr = getStimSetCellStr(2);
  14. %% Split table into cell types.
  15. tokens = arrayfun(@(x) regexp(x, '.*Tm(\d).*', 'tokens'), ProcessedTable.timeSeriesPath);
  16. cellTypeNumArray = cellfun(@(x) str2num(x{1}{1}), tokens);
  17. cellTypeNum = unique(cellTypeNumArray);
  18. for iNeuron = 1: numel(cellTypeNum)
  19. StimTableNeuronCell{iNeuron} = ProcessedTable(cellTypeNumArray == cellTypeNum(iNeuron), :);
  20. end
  21. %%
  22. stimInd = 1;
  23. nStims = numel(stimSetCellStr);
  24. for iNeuron = 1: numel(cellTypeNum)
  25. for jStim = 1: nStims
  26. StimTableNeuronStim{iNeuron, jStim} = getStimSubsetTable(StimTableNeuronCell{iNeuron}, stimSetCellStr, jStim);
  27. end
  28. end
  29. for iFly = 1: numel(unique(ProcessedTable.flyInd))
  30. flyIndsCell = ProcessedTable.flyInd == iFly;
  31. flyStims = ProcessedTable(flyIndsCell, :).stimParamFileName;
  32. % Find if stim is within the chosen subset.
  33. flyStimInds{iFly} = (cellfun(@(x) find(strcmp(x, stimSetCellStr)), flyStims, 'uni', 0));
  34. % If it contains stimuli, check that it includes the 5 chosen.
  35. if ~isempty(flyStimInds{iFly})
  36. setDiff{iFly} = setdiff(1: 5, [flyStimInds{iFly}{:}]);
  37. else
  38. setDiff{iFly} = -1;
  39. end
  40. % For flies with all chosen stimuli order the table accordingly.
  41. if isempty(setDiff{iFly})
  42. sortFlyIndsTemp = find(flyIndsCell);
  43. [~, sortInd] = sort([flyStimInds{iFly}{:}]);
  44. sortFlyInds{iFly} = sortFlyIndsTemp(sortInd);
  45. end
  46. end
  47. StimOrderedTable = ProcessedTable(vec([sortFlyInds{:}]), :);
  48. %% Split table into cell types.
  49. tokens = arrayfun(@(x) regexp(x, '.*Tm(\d).*', 'tokens'), StimOrderedTable.timeSeriesPath);
  50. cellTypeNumArray = cellfun(@(x) str2num(x{1}{1}), tokens);
  51. cellTypeNum = unique(cellTypeNumArray);
  52. for iNeuron = 1: numel(cellTypeNum)
  53. StimTableNeuronCell{iNeuron} = StimOrderedTable(cellTypeNumArray == cellTypeNum(iNeuron), :);
  54. end
  55. %%
  56. nStims = numel(stimSetCellStr);
  57. for iNeuron = 1: numel(cellTypeNum)
  58. for jStim = 1: nStims
  59. StimTableNeuronStim{iNeuron, jStim} = getStimSubsetTable(StimTableNeuronCell{iNeuron}, stimSetCellStr, jStim);
  60. end
  61. end
  62. %% Now can use this table cell array into the other functions.
  63. StimTable = ProcessedTable;
  64. StimSubTablesCell = arrayfun(@(x) getStimSubsetTable(ProcessedTable, ...
  65. stimSetCellStr, x), ...
  66. 1: numel(stimSetCellStr), 'uni', 0);
  67. %% Hand stitched data for Tmneurons and Tm9 saved.
  68. load([saveFolder filesep 'Fig4-TmNeurons-allNeuronsNeuronStimTableOnOff'], 'tmAll');
  69. StimTableNeuronStim = tmAll;
  70. %% Plot tuning curves. Do not discard the on.
  71. alignToOff = true;
  72. alignToAll = false;
  73. respQualityThreshold = 0.5;
  74. rSquareThreshold = 0.2;
  75. padWithNaNs = 0;
  76. qualityMeasure = 'responseQuality';
  77. %%
  78. cellTypeStr = {'Tm1', 'Tm2', 'Tm4', 'Tm9'};
  79. % plotRFsAlignedWithFitGenotypes(StimTableNeuronStim(:, 2: end), 0.0, 0.5, figDir, [cellTypeStr{:}]);
  80. plotRFsAlignedWithFitGenotypes(StimTableNeuronStim(:, 2: end), 0.2, 0.5, figDir, [cellTypeStr{:}]);
  81. % plotRFsAlignedWithFitGenotypes(StimTableNeuronStim(:, 2: end), -20.0, 0, figDir, [cellTypeStr{:}]);
  82. %% Export excel data figure 4b-i
  83. receptiveFields = plotRFsAlignedWithFitGenotypes(StimTableNeuronStim(:, 2: end), 0.2, 0.5, figDir, [cellTypeStr{:}]);
  84. %% Write data per cell type
  85. dataFileName = [figDir filesep 'Fig4.xls'];
  86. sheetNamesCell = {'bf', 'cg', 'dh', 'ei'};
  87. stimNameStrCell = {'_OFF_horizontal_position_deg', '_OFF_vertical_position_deg', ...
  88. '_ON_horizontal_position_deg', '_ON_vertical_position_deg'};
  89. for iCellType = 1: numel(cellTypeStr)
  90. startRow = 1;
  91. sheetName = ['Fig4' sheetNamesCell{iCellType}];
  92. for iStim = 1: 4
  93. parameterLabel = [cellTypeStr{iCellType} stimNameStrCell{iStim}];
  94. DataTable = createRFsTable(receptiveFields{iCellType, iStim}, parameterLabel);
  95. writetable(DataTable, dataFileName, 'Sheet', sheetName, 'Range', ['A' num2str(startRow)]);
  96. startRow = startRow + size(DataTable, 1) + 2;
  97. end
  98. end
  99. %% Export data from Supplementary Figure 4
  100. receptiveFields = plotRFsAlignedWithFitGenotypes(StimTableNeuronStim(:, 2: end), -20, 0, figDir, [cellTypeStr{:}]);
  101. %%
  102. dataFileName = [figDir filesep 'Fig4.xls'];
  103. sheetNamesCell = {'ae', 'bf', 'cg', 'dh'};
  104. stimNameStrCell = {'_OFF_horizontal_position_deg', '_OFF_vertical_position_deg', ...
  105. '_ON_horizontal_position_deg', '_ON_vertical_position_deg'};
  106. for iCellType = 1: numel(cellTypeStr)
  107. startRow = 1;
  108. sheetName = ['SuppFig4' sheetNamesCell{iCellType}];
  109. for iStim = 1: 4
  110. parameterLabel = [cellTypeStr{iCellType} stimNameStrCell{iStim}];
  111. DataTable = createRFsTable(receptiveFields{iCellType, iStim}, parameterLabel);
  112. writetable(DataTable, dataFileName, 'Sheet', sheetName, 'Range', ['A' num2str(startRow)]);
  113. startRow = startRow + size(DataTable, 1) + 2;
  114. end
  115. end
  116. %% Inline from plotRFsAlignedWithFitGenotypes
  117. respIndThresh = respQualityThreshold;
  118. rSqThresh = rSquareThreshold;
  119. clear flyIndsCell
  120. for iNeuron = 1: size(StimTableNeuronStim(:, 2: end), 2)
  121. StimTableCell = StimTableNeuronStim(iNeuron, 2: end);
  122. [~, responseIndArrayCell, rSquareArrayCell, fitParamsCell, ...
  123. flyIndsCell{iNeuron}, ~, ~] = getStimArraysForSameRois(StimTableCell);
  124. % Align to fit
  125. widthCell{iNeuron} = cellfun(@(x) arrayfun(@(y) 2 * (2 * sqrt(log(2))) * y.c1, x, 'uni', 0), ...
  126. fitParamsCell, 'uni', 1); % The 2 factor is because of bar separation.
  127. validIndsCell{iNeuron} = cellfun(@(x, y) x(:) > rSqThresh & y(:) > respIndThresh, ...
  128. rSquareArrayCell, responseIndArrayCell, 'uni', 0)';
  129. end
  130. %%
  131. validOffInds = cellfun(@(x) x{1} & x{2}, validIndsCell, 'uni', 0);
  132. validOnInds = cellfun(@(x) x{3} & x{4}, validIndsCell, 'uni', 0);
  133. %%
  134. widthCell = cellfun(@(x) cat(1, x{:}), widthCell, 'uni', 0);
  135. %%
  136. xOFFWidth = cellfun(@(x, y) x(1, y), widthCell, validOffInds, 'uni', 0);
  137. yOFFWidth = cellfun(@(x, y) x(2, y), widthCell, validOffInds, 'uni', 0);
  138. xONWidth = cellfun(@(x, y) x(3, y), widthCell, validOnInds, 'uni', 0);
  139. yONWidth = cellfun(@(x, y) x(4, y), widthCell, validOnInds, 'uni', 0);
  140. allWidthsCell = {xOFFWidth, yOFFWidth, xONWidth, yONWidth};
  141. %%
  142. for iW = 1: numel(allWidthsCell)
  143. allWidthsCell{iW}(cellfun(@isempty, allWidthsCell{iW}, 'uni', 1)) = {pi};
  144. end
  145. %%
  146. hFig = createPrintFig(15 * [ 1 1/4]);
  147. colors = brewermap(4, 'Set1');
  148. colors = colors([1, 2, 4, 3], :);
  149. for iAx = 1: numel(allWidthsCell)
  150. hSubAx(iAx) = subplot(1, 4, iAx);
  151. hWidthAx = beanPlot(allWidthsCell{iAx}, cellTypeStr, colors, ...
  152. 1, gca, [0-eps 100], 'vertical', 0, 1, 0);
  153. end
  154. linkaxes(hSubAx, 'y');
  155. setFontForThesis(hWidthAx, gcf);
  156. arrayfun(@prettifyAxes, hWidthAx)
  157. arrayfun(@offsetAxes, hWidthAx)
  158. figFileName = ['RF-FWHM-bean'];
  159. print(hFig, [figDir figFileName '.pdf'], '-dpdf')
  160. %% Export data from figure 4 j-m
  161. %%
  162. dataFileName = [figDir filesep 'Fig4.xls'];
  163. stimNameStrCell = {'_OFF_bars_horizontal_deg', '_OFF_bars_vertical_deg', ...
  164. '_ON_bars_horizontal_deg', '_ON_bars_vertical_deg'};
  165. startRow = 1;
  166. sheetName = ['Fig4j-m'];
  167. for iCellType = 1: numel(cellTypeStr)
  168. for iStim = 1: 4
  169. parameterLabel = [cellTypeStr{iCellType} '_FWHM' stimNameStrCell{iStim}];
  170. xlswrite(dataFileName, {parameterLabel}, sheetName, ['A' num2str(startRow)])
  171. if allWidthsCell{iStim}{iCellType} == pi % pi was placeholder for no data
  172. xlswrite(dataFileName, {''}, sheetName, ['B' num2str(startRow)])
  173. else
  174. xlswrite(dataFileName, allWidthsCell{iStim}{iCellType}, sheetName, ['B' num2str(startRow)])
  175. end
  176. startRow = startRow + 1;
  177. end
  178. startRow = startRow + 1;
  179. end
  180. %%
  181. meanONWidth = cellfun(@mean, cellfun(@(x, y) (x + y) / 2, xONWidth, yONWidth, 'uni', 0));