%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Weber AM, Wong FK, Tufford AR, Schlichter LC, Matveev V, Stanley EF
%        N-type Ca2+ channels carry the largest current: 
%    implications for nanodomains and transmitter release.
%
%              Nature Neuroscience 13: 1348-1350.
%
%      This script plots Figs. S7 & S9 using data produced
%              by CalC script GenerateData.par
%
%               Victor Matveev, August 8, 2012
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

T     = 0.5;   % Time after channel opening
kappa = 50;    % Buffering capacity

kdArray = [5 10 100];   % Array of release site affinities
NKD = length(kdArray);

Darray = [0 0.05];
ND = length(Darray);

figure(7);
XM = 50; PWR = 5;
prefix={'N', 'L'};

hill = @(x,K,n) x.^n ./ (x + K).^n;

str = 'ABCD';
cls = {'b', 'r', 'k', 'c', 'm', 'g', 'y'};
lst = {'-', '-.', ':'};
ii  = 7;

for V = [65 40];
    
    figure(ii); ii = ii + 2;
    hold off;
    ind = 1;

    for ikd = 1 : NKD
        Aff = kdArray(ikd);
        subplot(NKD, 1, ind);
        lgnd = {};

    for j = 1 : ND;
        D = Darray(j);
        for i = 1 : length(prefix)
            fname = ['1Dr_V', num2str(V), '_Ca_', prefix{i}, '_kappa_', num2str(kappa), '_BD_', num2str(D), '_300x300.dat'];
            A     = process(fname);
            I     = find(A(1,1,:) >= T);
            it    = I(1)-1;
            lgnd  = [ lgnd, ['I_{Ca,', prefix{i}, '} D_B=', num2str(1000*D), '\mum^2/s'] ];
            plot(A(2,:,it)*1000, hill(A(3,:,it), Aff, PWR), [cls{j}, lst{i}], 'linewidth', 2 ); 
            hold on; 
        end
    end

    axis([0 XM 0 1]);
    plot([0 XM], [0.5 0.5], 'k-', 'linewidth', 1);
    grid on;

    if ind == 3
        legend(lgnd);
        xlabel('Distance (nm)');
    end;

    set(gca, 'XTick', [0:10:XM]);
    ylabel(['[Ca^{2+}]^', num2str(PWR), '/ ([Ca^{2+}] + K)^', num2str(PWR) ]);
    tstr = sprintf('K_D=%d \\muM', Aff);
    title(tstr, 'fontsize', 12);
    set(gca,'box','off');

    ind = ind+1;
    end;
end
