%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%        Victor V. Matveev, Richard Bertram and Arthur Sherman (2011)
% Ca2+ Cooperativity of Exocytosis as a Measure of Ca2+ Channel Domain Overlap.
%                      Brain Research 1398: 126-138.  
%
%  This script plots Figs. 2 & 3 (depending on the value of "M")
%                  Victor Matveev, January 8, 2011
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Three measures of I_Ca cooperativity in exocytosis
% for the case of two equidistant channels
%
clear
M = 5;
n = 5 ;        % Biochemical cooperativity
nPts = 12;     % number of points
ii = [10 6 2]; % Special points where slope is shown, by index
clrs = [ [1 0 0]; [0 0 1]; [0 0.6 0]];

tstr = ['M=', num2str(M), ', r=', num2str(2^n)];
syms p q R G mica mch;
    
titleFontSize=15; labelFontSize=14; axisFontSize=14;
figure(1);

if n == 0
    R = 1-(1-p)^(M-1);
    mica = M*p*(1-p)^(M-1)/(1-(1-p)^M);
    mch = M*p/(1 - (1-p)^M);
else
    G = (p+q)^M;
    for i = 1:n
        G = p * diff(G,p);
    end
    R = subs(G,q,1-p)
    mica = p / R * diff(R,p)
    mch = subs(p*diff(G,p),q,1-p) / R
end

po   = linspace(0.01, 0.99, nPts);
Lpo  = linspace( log10(0.01), log10(0.99), nPts ); 
Apo  = 10 .^ Lpo;
LR   = log10(subs(R,Apo));
Mica = subs(mica,Apo);
P = polyfit(Lpo, LR, 1);
LP  = P(2) + P(1)*Lpo;

    subplot(1,2,1);
    hold off;
    set(gca,'fontsize',16);
    
    plot(Lpo, LR, 'ok--','markersize',7,'markerfacecolor',[0.8 0.8 0.8],'markeredgecolor',[0 0 0],'linewidth',1.5); 
    hold on;
    plot(Lpo, LP, 'k-','linewidth', 1);
    lstr = {'log P(R)'}
    lstr = [lstr, ['m_{ICa}=',sprintf('%.2g',P(1))] ];
    
    j = 1;
    for i = ii;
        dx = (Lpo(2) - Lpo(1)) * 0.7;
        xx = linspace(Lpo(i)-dx, Lpo(i)+dx, 20);
        LL = LR(i) + Mica(i) * ( xx - Lpo(i) );
        plot(xx, LL, '-', 'color', clrs(j,:), 'linewidth', 3 );
        lstr = [lstr, ['m_{ICa}=',sprintf('%.2g',Mica(i))] ];
        j = j + 1;
    end;
    legend(lstr, 'location', 'NW' );
   
    j = 1;
    for i = ii;
        line([Lpo(i) Lpo(i)], [LR(1) LR(i)], 'linewidth', 2, 'color', clrs(j,:), 'linestyle', ':');
        j = j + 1;
    end;

    set(gca,'box','off');
    xlabel('log_{10} p_o (open channel fraction)', 'fontsize', labelFontSize);
    ylabel('log_{10} P(R)', 'fontsize', labelFontSize);
    set(gca,'fontsize',axisFontSize);
    axis([Lpo(1) Lpo(end) LR(1) LR(end)]);
    set(gca,'plotBoxAspectRatioMode','manual');
    
    subplot(1,2,2);
    hold off;
    set(gca,'fontsize',16);
    set(gca,'box','off');
    plot(Apo, subs(mica,Apo),'k-','linewidth',2); hold on;
    plot(Apo, subs(mch,Apo),'--','linewidth',1,'color',[0 0 0]);
   
    lstr = {'m_{ICa}','m_{CH}'};
    j = 1;
    for i = ii;
        plot(Apo(i), Mica(i), 'o', 'markerfacecolor', clrs(j,:), 'markeredgecolor', clrs(j,:));
        j = j + 1;
    end
    
    legend(lstr,'location','best');
    
    j = 1;
    for i = ii;
        line([Apo(i) Apo(i)], [Mica(1) Mica(i)], 'linewidth', 2, 'color', clrs(j,:), 'linestyle', ':');
        line([Apo(1) Apo(i)], [Mica(i) Mica(i)], 'linewidth', 2, 'color', clrs(j,:), 'linestyle', ':');
        j = j + 1;
    end
    
    axis tight;
    set(gca,'box','off');
    xlabel('p_o (open channel fraction)', 'fontsize', labelFontSize);
    ylabel('m_{CH}, m_{ICa}', 'fontsize', labelFontSize);
    set(gca,'fontsize',axisFontSize);
    set(gca,'plotBoxAspectRatioMode','manual');
    
    
