123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- close all;
- clear all;
- clc;
- %% Define search path.
- parentDir = fileparts(fileparts(mfilename('fullpath')));
- saveFolder = [parentDir filesep 'data'];
- load([saveFolder filesep 'Fig2-Tm9-processedTableWithBackgroundOnOff'], 'ProcessedTable');
- %%
- figDir = [parentDir filesep 'figures' filesep 'Fig2' filesep];
- if ~exist(figDir, 'dir')
- mkdir(figDir);
- end
- %%
- % Stimuli of interest.
- stimSetCellStr = getStimSetCellStr(2);
- %%
- StimTable = ProcessedTable;
- StimSubTablesCell = arrayfun(@(x) getStimSubsetTable(ProcessedTable, ...
- stimSetCellStr, x), ...
- 1: numel(stimSetCellStr), 'uni', 0);
- %% Plot tuning curves.
- StimTableCell = StimSubTablesCell(2: end);
- % This extracts the unaligned tuning curves (TCs).
- % Retrieve table data in arrays.
- [tcArrayCell, responseIndArrayCell, rSquareArrayCell, fitParamsCell, ...
- tcExtCellArray, tcArgExtCellArray] = cellfun(@getTuningCurveArrayFromTable, ...
- StimTableCell, 'uni', 0);
- % Align to fit
- posCell = cellfun(@(x) arrayfun(@(y) y.b1, x, 'uni', 0), fitParamsCell, 'uni', 1);
- widthCell = cellfun(@(x) arrayfun(@(y) 2 * (2 * sqrt(log(2))) * y.c1, x, 'uni', 0), ...
- fitParamsCell, 'uni', 1); % The 2 factor is because of bar separation.
- lagsCell = cellfun(@(x, y) round(x - size(y, 2) / 2), posCell, tcArrayCell, 'uni', 0);
- barRFs = cellfun(@(x, y) alignRFsToAbsMax(x, 0, y'), tcArrayCell, lagsCell, 'uni', 0);
- validIndsCell = cellfun(@(x, y) x(:) > 0.2 & y(:) > 0.5, ...
- rSquareArrayCell, responseIndArrayCell, 'uni', 0)';
- %% Blank epoch was already deleted, now delete vertical bars on edges of screen.
- barRFs{2}(:, end-1: end) = [];
- barRFs{4}(:, end-1: end) = [];
- barRFs{2}(:, 1: 2) = [];
- barRFs{4}(:, 1: 2) = [];
- %%
- offX = barRFs{1}(validIndsCell{1}, :);
- offY = barRFs{2}(validIndsCell{2}, :);
- onX = barRFs{3}(validIndsCell{3}, :);
- onY = barRFs{4}(validIndsCell{4}, :);
- % normalize to peak and smooth and normalize again.
- maxNorm = @(x, dim) x ./ max(abs(x), [], dim);
- getRow = @(x, nRow) x(nRow, :);
- normRFs = cellfun(@(x) cell2mat(arrayfun(@(y) smooth(getRow(maxNorm(x,2), y))', 1: size(x, 1), 'uni', 0)'), ...
- {offX, offY, onX, onY}, 'uni', 0);
- normRFs = cellfun(@(x) maxNorm(x,2), normRFs, 'uni', 0);
- %%
- % Plot lasagna plots, separate figures for off and on, and reapeated panel
- % is to have a colorbar that does not distort existing axis of interest.
- hFig = createPrintFig(15 * [ 1 2/3]);
- hSubAx = plotLasagnaTracesFromCell(gcf, {offX, offY, offY}, brewermap(6, 'Paired'));
- colorbar
- % arrayfun(@(x) set(get(x, 'YAxis'), 'Visible', 'on'), hSubAx(1, :))
- arrayfun(@prettifyAxes, hSubAx)
- arrayfun(@offsetAxes, hSubAx)
- % set(hSubAx, 'Visible', 'off')
- figFileName = ['RFcurves-OFF-XY-lasagna'];
- print(hFig, [figDir figFileName '.pdf'], '-dpdf')
- %%
- colorbar
- hFig = createPrintFig(15 * [ 1 2/3]);
- hSubAx = plotLasagnaTracesFromCell(gcf, {onX, onY, onY}, brewermap(6, 'Paired'));
- colorbar
- % arrayfun(@(x) set(get(x, 'YAxis'), 'Visible', 'on'), hSubAx(1, :))
- arrayfun(@prettifyAxes, hSubAx)
- arrayfun(@offsetAxes, hSubAx)
- % set(hSubAx, 'Visible', 'off')
- figFileName = ['RFcurves-ON-XY-lasagna'];
- print(hFig, [figDir figFileName '.pdf'], '-dpdf')
- %% For nat comms editorial policies source data
- dataFileName = [figDir filesep 'Fig2.xls'];
- DataTable = createRFsTable(offX, 'OFF_horizontal_position_deg');
- writetable(DataTable, dataFileName, 'Sheet', 'Fig2b')
- DataTable = createRFsTable(offY, 'OFF_vertical_position_deg');
- writetable(DataTable, dataFileName, 'Sheet', 'Fig2b', 'Range', 'A28:IV100')
- DataTable = createRFsTable(onX, 'ON_horizontal_position_deg');
- writetable(DataTable, dataFileName, 'Sheet', 'Fig2b', 'Range', 'A56:IV100')
- DataTable = createRFsTable(onY, 'ON_vertical_position_deg');
- writetable(DataTable, dataFileName, 'Sheet', 'Fig2b', 'Range', 'A84:IV100')
- %% This produces panel 2
- validWidthCell = getCellArrayFromIndexThreshold(widthCell, validIndsCell', 0);
- hFig = createPrintFig(15 * [ 1/3 1/3]);
- hWidthAx = beanPlot(validWidthCell, {'xOFF' 'yOFF' 'xON' 'yON'}, getONOFFXYColors, ...
- 1, gca, [0-eps 100], 'vertical', 0, 1, 0);
- setFontForThesis(hWidthAx, gcf);
- arrayfun(@prettifyAxes, hWidthAx)
- arrayfun(@offsetAxes, hWidthAx)
- figFileName = ['RF-FWHM-bean'];
- print(hFig, [figDir figFileName '.pdf'], '-dpdf')
- %% Write data for fig2c
- xlswrite(dataFileName, {'FHWM-OFF-bars-horizontal'}, 'Fig2c', 'A1')
- xlswrite(dataFileName, validWidthCell{1}, 'Fig2c', 'B1')
- xlswrite(dataFileName, {'FHWM-OFF-bars-vertical'}, 'Fig2c', 'A2')
- xlswrite(dataFileName, validWidthCell{2}, 'Fig2c', 'B2')
- xlswrite(dataFileName, {'FHWM-ON-bars-horizontal'}, 'Fig2c', 'A3')
- xlswrite(dataFileName, validWidthCell{3}, 'Fig2c', 'B3')
- xlswrite(dataFileName, {'FHWM-ON-bars-vertical'}, 'Fig2c', 'A4')
- xlswrite(dataFileName, validWidthCell{4}, 'Fig2c', 'B4')
- %% Range and mean of ON RF fwhm
- min([validWidthCell{3}(:); validWidthCell{4}(:)])
- max([validWidthCell{3}(:); validWidthCell{4}(:)])
- mean([validWidthCell{3}(:); validWidthCell{4}(:)])
- std([validWidthCell{3}(:); validWidthCell{4}(:)])
- %%
- threshold = 0.5;
- rSquareThreshold = 0.2;
- qualityMeasure = 'responseQuality';
- widthCell = plotParamsThresholdedDistribution(StimTableCell, threshold, qualityMeasure, figDir, rSquareThreshold);
- %% Do stats for panel 2c
- % Not really normal lly distributed
- cellfun(@lillietest, widthCell);
- % Test offX vs onX and offY vs onY
- [pX, meanXDiff, ~] = permutationTest(widthCell{1}, widthCell{3}, 100000);
- [pY, meanYDiff, ~] = permutationTest(widthCell{2}, widthCell{4}, 100000);
- %% Nested permutations
- flyIndsCell = cellfun(@(y) repelem(y.flyInd, cellfun(@numel, y.responseIndex)), ...
- StimTableCell, 'uni', 0);
- for iStim = 1: size(StimTableCell, 2)
- flyIndsCellArray{iStim} = mat2cell(flyIndsCell{iStim}, ...
- cellfun(@numel, StimTableCell{iStim}.responseIndex));
-
- inputCellArray = flyIndsCellArray{iStim};
- StimTable = StimTableCell{iStim};
-
- nonParamTunings = getRoisFeaturePerChannel(cellfun(@(x) x', StimTable.nonParamTuning, 'uni', 0));
- domainCellArray = arrayfun(@(x) x.domain, nonParamTunings, 'uni', 0);
- domainSize = cellfun(@numel, domainCellArray);
- outputCellArray{iStim} = cell2mat(inputCellArray(domainSize == mode(domainSize)));
- end
- correctedFlyInds = outputCellArray;
- %%
- goodFlyIndsCell = getCellArrayFromIndexThreshold(correctedFlyInds, validIndsCell', 0);
- nFlies = cellfun(@(x) numel(unique(x)), goodFlyIndsCell);
- %% Store the fly indices
- xlswrite(dataFileName, {'FlyID-OFF-bars-horizontal'}, 'Fig2c', 'A6')
- xlswrite(dataFileName, goodFlyIndsCell{1}', 'Fig2c', 'B6')
- xlswrite(dataFileName, {'FlyID-OFF-bars-vertical'}, 'Fig2c', 'A7')
- xlswrite(dataFileName, goodFlyIndsCell{2}', 'Fig2c', 'B7')
- xlswrite(dataFileName, {'FlyID-ON-bars-horizontal'}, 'Fig2c', 'A8')
- xlswrite(dataFileName, goodFlyIndsCell{3}', 'Fig2c', 'B8')
- xlswrite(dataFileName, {'FlyID-ON-bars-vertical'}, 'Fig2c', 'A9')
- xlswrite(dataFileName, goodFlyIndsCell{4}', 'Fig2c', 'B9')
- %% Still do the nested permutation
- nROIsPerFlyCell = cellfun(@(x) accumarray(x(:), 1), goodFlyIndsCell, 'uni', 0);
- % Delete flies with no rois to analyze.
- for iStim = 1: numel(nROIsPerFlyCell)
- nROIsPerFlyCell{iStim}(nROIsPerFlyCell{iStim} == 0) = [];
- end% generate continuos numbering.
- reorderedFlyIndsCell = cellfun(@(x) repelem(1: numel(x), x), nROIsPerFlyCell, 'uni', 0);
- %%
- % samples are cells, but we will permute all cells of a fly together.
- % Test only within orientation differences
- sample1 = 1: numel(nROIsPerFlyCell{1});
- sample2 = numel(sample1) + (1: numel(nROIsPerFlyCell{3}));
- permutations = 10000;
- sidedness = 'both';
- plotresult = 1;
- [obsDiffX, pValX] = nestetPermutationTest(sample1, sample2, ...
- widthCell([1, 3]), ...
- reorderedFlyIndsCell([1, 3]), ...
- permutations, sidedness, plotresult);
- sample1 = 1: numel(nROIsPerFlyCell{2});
- sample2 = numel(sample1) + (1: numel(nROIsPerFlyCell{4}));
- [obsDiffY, pValY] = nestetPermutationTest(sample1, sample2, ...
- widthCell([2, 4]), ...
- reorderedFlyIndsCell([2, 4]), ...
- permutations, sidedness, plotresult);
-
- %% Plot raw traces for supplementary figure.
- hFig = createPrintFig(15 * [1 1]);
- drawRandomSubset = 1;
- for iStim = 1: size(StimTableCell, 2)
-
- inputCellArray = StimTableCell{iStim}.paddedEpochStacks;
- StimTable = StimTableCell{iStim};
- % To remove flies with different number of epochs than the majority.
- nonParamTunings = getRoisFeaturePerChannel(cellfun(@(x) x', StimTable.nonParamTuning, 'uni', 0));
-
- domainCellArray = arrayfun(@(x) x.domain, nonParamTunings, 'uni', 0);
- domainSize = cellfun(@numel, domainCellArray);
- outputCellArray{iStim} = inputCellArray(domainSize == mode(domainSize));
- allRFTraces = cell2mat(cellfun(@(x) cat(1, x{:})', ...
- outputCellArray{iStim}, 'uni', 0));
- size(allRFTraces)
- rng('default')
- % imagesc(allRFTraces(validIndsCell{1}, :))
- hTracesAx(iStim) = subplot(2, 2, iStim);
- cla
- plotInds = find(validIndsCell{iStim});
- if drawRandomSubset
- plotInds = plotInds(randperm(10));
- end
- barResponseTraces{iStim} = allRFTraces(plotInds, :);
- timePoints{iStim} = (1: numel(allRFTraces(plotInds(1), :))) / 10; % Data interpolated at 10Hz
- plot(timePoints{iStim}, bsxfun(@plus, allRFTraces(plotInds, :)', ...
- 0.9 * (1: numel(plotInds))), 'LineWidth', 1, 'Color', 0.25 * [1 1 1]);
- hold on;
- axis tight
- hLines = arrayfun(@(x) refline(0, x), 0.9 * (1: numel(plotInds)));
- [hLines.Color] = deal([1 1 1] * 0.5);
- [hLines.LineWidth] = deal(0.5);
- timePointsPerEpoch = size(allRFTraces, 2) / mode(domainSize);
- hLines = arrayfun(@(x) line([x, x], ylim), ...
- timePointsPerEpoch / 10 * (1/3: 1: mode(domainSize)) + 1/3);
- [hLines.Color] = deal([1 1 1] * 0.75);
- [hLines.LineWidth] = deal(0.5);
- [hLines.LineStyle] = deal(':');
- % Send the reference lines to the back.
- chH = get(gca,'Children');
- set(gca,'Children',flipud(chH));
- end
- xlabel(hTracesAx(3), 'Time (s)')
- xlabel(hTracesAx(4), 'Time (s)')
- ylabel(hTracesAx(1), '\DeltaF/F_0')
- ylabel(hTracesAx(3), '\DeltaF/F_0')
- setFontForThesis(hTracesAx, gcf);
- arrayfun(@prettifyAxes, hTracesAx)
- arrayfun(@offsetAxes, hTracesAx)
- figFileName = ['Fig2-suppl-meanTraces-sortedPositions'];
- print(hFig, [figDir figFileName '.pdf'], '-dpdf')
- %% Export data for Supplementary Fig 2
- dataFileName = [figDir filesep 'Fig2.xls'];
- sheetName = 'SupFig2';
- stimNameStrCell = {'Tm9_OFF_horizontal_bar_time_seconds', 'Tm9_OFF_vertical_bar_time_seconds', ...
- 'Tm9_ON_horizontal_bar_time_seconds', 'Tm9_ON_vertical_bar_time_seconds'};
- startRow = 1;
- for iStim = 1: 4
- DataTable = createRFsTable(barResponseTraces{iStim}, stimNameStrCell{iStim});
- DataTable(:, 1).(stimNameStrCell{iStim}) = timePoints{iStim}(:);
- writetable(DataTable, dataFileName, 'Sheet', sheetName, 'Range', ['A' num2str(startRow)]);
- startRow = startRow + size(DataTable, 1) + 2;
- end
- %% Now pick a value an compare stimuli. And do some statistics.
- plotBootstrapCoeffOfVariationTest(widthCell, figDir, threshold, rSquareThreshold)
- %% Variance plot
- plotBarVariance(widthCell, figDir, threshold, rSquareThreshold)
|