% GJLFP, eLife 2026. Richa Sirmaur and Rishikesh Narayanan.
% Figure 3: All currents- chem syn vs gap junctions

%%
%  This is chemcal synapse, same for data from gap junctions
models = {'default','nona','noleak','nonaleak'};
colors = lines(numel(models));

nBins = 5; % 5 bins for intracellular recordings; 0-300 um distance from soma
binCenters = linspace(0,300,nBins);

currents = {'ina','ica','hd','ik','pas','cap'};

for m = 1:numel(models) % loop over all models/ do one by one
    modelName = models{m};

    basePath = fullfile('/chemsyn/synchro/Intra', modelName, 'Basal');
    basalV = load(fullfile(basePath, 'basalV.txt'));
    allCurrents = cell(numel(currents),1);
    for c = 1:numel(currents)

        currPath = fullfile(basePath, 'othercurrent', 'Basal', currents{c});
        data = cell(nBins,1);
        for b = 1:nBins
            fname = sprintf('bin%d.txt', b);
            data{b} = load(fullfile(currPath, fname));
        end
        allCurrents{c} = data;
    end

    figure(100 + m); hold on;
    title(['Peak currents: ' currents{c} ' - ' modelName]);

    for c = 1:numel(currents)

        meanVals = zeros(nBins,1);
        semVals  = zeros(nBins,1);

        for b = 1:nBins
            vals = allCurrents{c}{b};

            meanVals(b) = mean(vals);
            semVals(b)  = std(vals) / sqrt(numel(vals));
        end

        errorbar(binCenters, meanVals, semVals, '-o', ...
            'Color', colors(c,:), ...
            'LineWidth', 2, ...
            'MarkerFaceColor', colors(c,:));

    end

    xlabel('Radial distance (\mum)');
    ylabel('Current'); %uAcm-2/nAcm-2/mAcm-2 based on different currents
    legend(currents);
    box off;

    figure(200 + m); hold on;
    plot(binCenters,basalV, '-o', ...
        'Color', [0 0 0], ...
        'LineWidth', 2);
    title(['Membrane potential - ' modelName]);
    xlabel('Radial distance (\mum)');
    ylabel('Voltage (mV)'); 
    box off;

end