clear
addpath(genpath('../../cellLib/Scripts/'))
%% Build new components from the .csv
csvFile=[pwd filesep 'Vars_874.csv'];
libPath="C:/Users/wai484/Documents/RecruitmentSynchronization_SMCs/cellLib";
libPath2="C:/Users/wai484/Documents/RecruitmentSynchronization_SMCs/SinglePASMC/Components";
%% Reuse existing cellml components
cellmlFiles=[libPath+ "/Protocols/X_clamp_protocol.cellml",libPath+ "/Components/time.cellml",libPath+ "/Components/E_Nernst.cellml",libPath+ "/Components/E_Nernst.cellml",...
    libPath2+ "/JVOCCi.cellml",libPath2+ "/JNCXi.cellml",libPath2+ "/JSERCAi.cellml",libPath2+ "/JCICRi.cellml",...
    libPath2+ "/JPMCAi.cellml",libPath2+ "/Jleaki.cellml",libPath2+ "/Casr.cellml",libPath2+ "/Vm.cellml"];
whichComp={["X_clamp_protocol"],["time_s"],["E_K"],["E_Cl"],["JVOCCi"],["JNCXi"],["JSERCAi"],["JCICRi"],["JPMCAi"],["Jleaki"],["Casr"],["Vm"]};
% Specify the inputs of the existing components, otherwise the variable would
% be categorized as parameters if pub:in
labelRead={};
nm=1;
labelRead(nm).comp="E_K";
labelRead(nm).vars=["Ko"];
nm=nm+1;
labelRead(nm).comp="E_Cl";
labelRead(nm).vars=["Clo"];
nm=nm+1;
labelRead(nm).comp="JVOCCi";
labelRead(nm).vars=["v_i"];
nm=nm+1;
labelRead(nm).comp="JNCXi";
labelRead(nm).vars=["v_i","c_i"];
nm=nm+1;
labelRead(nm).comp="JSERCAi";
labelRead(nm).vars=["c_i"];
nm=nm+1;
labelRead(nm).comp="JCICRi";
labelRead(nm).vars=["s_i","c_i"];
nm=nm+1;
labelRead(nm).comp="JPMCAi";
labelRead(nm).vars=["v_i","c_i"];
nm=nm+1;
labelRead(nm).comp="Jleaki";
labelRead(nm).vars=["s_i"];
nm=nm+1;
labelRead(nm).comp="JCli";
labelRead(nm).vars=["v_i"];
nm=nm+1;
labelRead(nm).comp="Casr";
labelRead(nm).vars=["J_SERCAi","J_CICRi","J_leaki","t"];
nm=nm+1;
labelRead(nm).comp="Vm";
labelRead(nm).vars=["J_NKAi","J_Cli","J_VOCCi","J_NCXi","J_Ki","t"];

%% Encapsuate new components
encap={};
m=1;
encap(m).name="JKi";
encap(m).chd.name=["E_K",];
encap(m).chd.def=["E_K"];
% Label some vars for mapping
label={};
nm=1;
label(nm).comp="E_K";
label(nm).vars=["E_K","v_K";];
encap(m).chd.label=label;
m=m+1;
encap(m).name="JCli";
encap(m).chd.name=["E_Cl",];
encap(m).chd.def=["E_Cl"];
label={};
nm=1;
label(nm).comp="E_Cl";
label(nm).vars=["E_Cl","v_Cl";];
encap(m).chd.label=label;
m=m+1;
encap(m).name="SMC";
encap(m).chd.name=["JVOCCi","JNCXi","JSERCAi","JCICRi","JPMCAi","Jleaki","JNKAi","JCli","JKi","JPLCdeltai","Jdegradi","JIP3i","IP3","Casr","Cai","Vm"];
encap(m).chd.def=["JVOCCi","JNCXi","JSERCAi","JCICRi","JPMCAi","Jleaki","JNKAi","JCli","JKi","JPLCdeltai","Jdegradi","JIP3i","IP3","Casr","Cai","Vm"];
encap(m).chd.label=[];
%% Add equations to components
Eqs={};
m=1;
Eqs(m).comp="JNKAi";
Eqs(m).listEqs=["J_NKAi = F_NKA;"];
m=m+1;
Eqs(m).comp="JKi";
Eqs(m).listEqs=["J_Ki=G_Ki*w_i*(v_i-v_K);",...
    "ode(w_i,t)=lambda*(K_activationi-w_i);",...
    "K_activationi = sqr(c_i+c_w)/(sqr(c_i+c_w)+beta*exp(-(v_i-v_Ca3)/R_K));"];
m=m+1;
Eqs(m).comp="IP3";
Eqs(m).listEqs=["ode(I_i, t) = J_PLCagonisti+J_PLC_delta_i-J_degradi;"];
m=m+1;
Eqs(m).comp="Cai";
Eqs(m).listEqs=["ode(c_i, t) = J_IP3i+J_VOCCi-J_NCXi-J_SERCAi+J_CICRi-J_PMCAi+J_leaki;"];
m=m+1;
Eqs(m).comp="JPLCdeltai";
Eqs(m).listEqs=["J_PLC_delta_i=E*(sqr(c_i)/(sqr(K_Ca)+sqr(c_i)));"];
m=m+1;
Eqs(m).comp="Jdegradi";
Eqs(m).listEqs=["J_degradi=k*I_i;"];
m=m+1;
Eqs(m).comp="JIP3i";
Eqs(m).listEqs=["J_IP3i=F_IP3*(sqr(I_i)/(sqr(K_r)+sqr(I_i)));"];
m=m+1;
Eqs(m).comp="JCli";
Eqs(m).listEqs=["J_Cli=G_Cl*(v_i-v_Cl);"];
m=m+1;
Eqs(m).comp="clamp_para";
Eqs(m).listEqs=["KCl=X;","Clo=147{mM}+KCl;"];
%%
[comp,idx]=buildComponents(csvFile,cellmlFiles,whichComp,labelRead,encap,Eqs);
save("comp.mat","comp","idx")