% 18 April: make it fit 6 state CC model

clear;

%% Set directories
main_dir = pwd;
data_dir = [main_dir '\data' filesep];
output_dir = [main_dir '\output' filesep];
storage_dir = [main_dir '\storage' filesep];


%% Define constants and volumes ( [=] pL)
R = 8.314; % unit J/mol/K
T = 310;
F = 96485;
W_i = 38;
W_e = 5.182;
N_A = 6.022e23;
x_tcc_channel = 50000/N_A*1e15; % channel density: unit fmol. Using Pan value for LCC

%% Load stoichiometric matrices  

N_f = [1	0	0	0	0	0	0	0; % includes GHK for Cai and Cao
0	0	0	0	0	0	0	0;
0	1	0	0	0	1	0	0;
0	0	0	1	0	0	1	0;
0	0	0	0	0	0	0	1;
0	0	1	0	0	0	0	0;
0	0	0	0	1	0	0	0;
0	0	0	0	0	0	0	0;
];

N_r = [0	0	0	0	0	0	0	0;
1	0	0	0	0	0	0	0;
0	0	0	0	0	0	0	0;
0	1	0	0	0	0	0	0;
0	0	0	1	0	0	0	0;
0	0	0	0	0	1	0	0;
0	0	1	0	0	0	1	0;
0	0	0	0	1	0	0	1;
];

N_fT = transpose(N_f);
N_rT = transpose(N_r);

N = N_r - N_f;
% N_T = N_rT - N_fT;

num_cols = size(N,2); % number of reactions
I = eye(num_cols); 

M = [I N_fT; I N_rT];
M_rref = rref(M);

% GHK permeability for K in Ks channel (see PSO_GHK_fitting_curve.m)
load([storage_dir 'tcc_G_GHK.mat']);
P_CaTCC = G_GHK/F * 1e12; % Unit pL/s

%% Set up the vectors
load([storage_dir 'TCC_b_parameters.mat']);
params_b = params_vec;

load([storage_dir 'TCC_g_parameters.mat']);
params_g = params_vec;

z_bf = params_b(2);
z_br = params_b(4);
z_gf = params_g(2);
z_gr = params_g(4);
zf = [z_bf, z_gf];
zr = [z_br, z_gr];

% unit s^-1
alpha_b = params_b(1); % unit s^-1
beta_b = params_b(3); % unit s^-1

alpha_g = params_g(1); % unit s^-1
beta_g = params_g(3); % unit s^-1

kf_Ca = [P_CaTCC/x_tcc_channel; ... % R_GHK
    2*alpha_b; ... % Rb_00
    2*alpha_b; ... % Rb_01
    alpha_b; ... % Rb_10
    alpha_b; ... % Rb_11
    alpha_g; ... % Rg_00
    alpha_g; ... % Rg_10
    alpha_g];    % Rg_20

kr_Ca = [P_CaTCC/x_tcc_channel; ... % R_GHK
    beta_b; ... % Rb_00
    beta_b; ... % Rb_01
    2*beta_b; ... % Rb_10
    2*beta_b; ... % Rb_11
    beta_g; ... % Rg_00
    beta_g; ... % Rg_10
    beta_g];    % Rg_20

% Overall
kf = kf_Ca;
kr = kr_Ca;

k = [kf;kr];
W = [ones(size(N,2),1);W_i;W_e;ones(6,1)];

lambdaW = exp(pinv(M)*log(k));
lambda = lambdaW./W;
kappa = lambda(1:size(N,2));   % [=] mol/s if corresponding v [=] mol/s
K = lambda(size(N,2)+1:end);   % [=] 1/mol if corresponding q [=] mol

save([output_dir 'K_kappa_TCC.mat'],'kappa','K');

%% Checks
N_rref = rref(N);
R_zdvat = null(N,'r');

K_eq = kf./kr;
zero_est = R_zdvat'*K_eq;

k_est = exp(M*log(lambdaW));
diff = sum(abs((k-k_est)./k));

kdiff = [k,k_est];

if diff > 1
    warning('lol DIFF is greater than 1, nublet.');
end

%% display text for cellml
rname = {'TCC','b00','b01','b10','b11','g00','g10','g20'};
Kname = {'Cai','Cae','S00','S10','S20','S01','S11','S21'};
zname = {'b', 'g'};
for ik = 1:length(kappa)
    fprintf('var kappa_%s: fmol_per_sec {init: %g, pub: out};\n', rname{ik}, kappa(ik));
end
for ik = 1:length(K)
    fprintf('var K_%s: per_fmol {init: %g, pub: out};\n', Kname{ik}, K(ik));
end
for ik = 1:length(zf)
    fprintf('var z_%sf: dimensionless {init: %g, pub: out};\n', zname{ik}, zf(ik));
end
for ik = 1:length(zr)
    fprintf('var z_%sr: dimensionless {init: %g, pub: out};\n', zname{ik}, zr(ik));
end
if true
    % component import parameters
for ik = 1:length(kappa)
    fprintf('var kappa_%s: fmol_per_sec {pub: in};\n', rname{ik});
end
for ik = 1:length(K)
    fprintf('var K_%s: per_fmol {pub: in};\n', Kname{ik});
end
    for ik = 1:length(zf)
        fprintf('var z_%sf: dimensionless {pub: in};\n', zname{ik});
    end
    for ik = 1:length(zr)
        fprintf('var z_%sr: dimensionless {pub: in};\n', zname{ik});
    end
    
    % mapping
    for ik = 1:length(kappa)
        fprintf('vars kappa_%s and kappa_%s;\n', rname{ik}, rname{ik});
    end
    for ik = 1:length(K)
        fprintf('vars K_%s and K_%s;\n', Kname{ik}, Kname{ik});
    end    
    for ik = 1:length(zf)
        fprintf('vars z_%sf and z_%sf;\n', zname{ik}, zname{ik});
    end
    for ik = 1:length(zr)
        fprintf('vars z_%sr and z_%sr;\n', zname{ik}, zname{ik});
    end
end