function [t,cell,conc,G] = HepatRegenGrowth(initialFraction)
% Author:       Daniel Cook
% Date:         10/08/2015
% Copyright:    Creative Commons Share Alike
% To run, use:  [t,cell,conc,G] = HepatRegenGrowth(0.3);
% Modified from "A Model of Liver Regeneration" (2009) by Furchtgott et al.

% Citation: Cook, D.; Ogunnaike, B.A.; Vadigepalli, R. 
% Systems Analysis of Non-Parenchymal Cell Modulation of Liver Repair
% across Multiple Regeneration Modes, BMC Systems Biology, 2015

% Set species: 1=rats, 2=mice, 3=humans
setSpecies = 1;

% Set disease (for use only with rats)
% 1= no disease
% 2= non-alcoholic steatohepatitis (NASH)
% 3= alcoholic steatohepatitis (ASH)
% 4= cirrhosis
% 5= toxin-induced diabetes
setDisease = 1;

% Set alternate hypotheses (for use only with humans)
% Hypothesis 5 is the default human parameter set
% 1= altered stress response
% 2= altered ECM remodeling and storage of GFs
% 3= altered transition times
% 4= longer cell cycle, higher apoptosis rate, et al.
% 5= lower metabolic demand
setHypotheses = 5;

% Plot results: 1=yes, 2=no
shouldPlot = 1;

% Define model parameters
k = zeros(41,1);
% Table 1: Molecular parameters
k(1) = initialFraction; % Fraction of liver mass remaining (N)
k(2) = 16.8; % Metabolic load (M)
k(3) = 1.5; % TNF production
k(4) = 0.9; % TNF mRNA degrad
k(5) = 2e4; % JAK activation rate (V_JAK)
k(6) = 1e4; % Km JAK
k(7) = 0.4; % JAK degrad
k(8) = 2; % Conc. of monomeric STAT3
k(9) = 7.5e2; % STAT3 activation rate
k(10) = 0.4; % Km STAT3
k(11) = 0.1; % STAT3 degrad
k(12) = 2.4e4; % SOCS activation rate
k(13) = 7e-4; % Km SOCS3
k(14) = 0.4; % SOCS3 degrad.
k(15) = 1.5e-2; % SOCS3 inhibition constant
k(16) = 2.5e2; % IE response gene expression rate
k(17) = 18; % Km for IE genes
k(18) = 5; % IE gene degrad
k(19) = 7; % ECM degrad by MMPs
k(20) = 33; % ECM degrad

% Table 2: Cellular parameters
k(21) = 0.113; % GF production
k(22) = 0.23; % GF degrad
k(23) = 6e-2; % k_up - GF uptake/production rate by ECM
k(24) = 7e-3; % k_Q
k(25) = 4.4e-3; % k_P
k(26) = 5.4e-2; % k_R
k(27) = 2e-2; % k_prol - specifies length of mitotic cycle
k(28) = 0.1; % k_req - requiescence rate
k(29) = 8; % theta_req (for sigma_req)
k(30) = 3; % beta_req (for sigma_req)
k(31) = 1e-1; %1e-2; % k_ap - apoptosis rate
k(32) = 9e-3; % theta_ap
k(33) = 4.5e-3; % beta_ap

% Growth rate and metabolic load for rats
if setSpecies == 1
    k(2) = 20.8217; % Metabolic demand
    k(41) = 3.4742e-4; % Growth rate
end

% Growth rate and metabolic load for mice
if setSpecies == 2
    k(2) = 23.0294; % Metabolic demand
    k(41) = 9.6607e-4; % Growth rate
    k(32) = k(32)*21.3671/k(2); % Scaled apoptosis rate
end

% Growth rate and metabolic load for humans
if setSpecies == 3
    k(2) = 5.8507; % For fitting data from Pomfret
    k(41) = 6.5675e-4; % For fitting data from Pomfret
    k(32) = k(32)*20.8217/k(2); % Scale the apoptosis response
end

% Set parameters for fructose-induced steatosis (NASH)
if setDisease == 2
    k(2) = 23.1645; 
    k(41) = 0.0025;
    k(3) = 0.3095;
    k(4) = 2.3633;
    k(19) = 1.7312;
    k(20) = 9.5395;
    k(21) = 0.0793;
    k(22) = 0.1679;
    k(23) = 0.0075;
end

% Set parameters for Alcohol-induced steatosis (ASH)
if setDisease == 3
    k(2) = 14.4017;
    k(41) = .0014;
    k(3) = 0.79;
    k(4) = 0.2073;
    k(19) = 10.5646;
    k(20) = 77.7923;
    k(21) = 0.0002;
    k(22) = 0.1196;
    k(23) = 0.0071;
end

% Parameters for cirrhosis-impaired regeneration (rat)
if setDisease == 4
    k(2) = 18.0454; 
    k(41) = 4.358e-06;
    k(3) = 2.1565;
    k(4) = 1.043;
    k(19) = 0;
    k(20) = 83.5649;
    k(21) = 0.069;
    k(22) = 0.2456;
    k(23) = 0.0027;
end

% Set parameters for Diabetes-impaired regeneration
if setDisease == 5
    k(2) = 16.4539; 
    k(41) = 8.92e-4;
    k(3) =  1.5892;
    k(4) =  0.3699;
    k(19) = 6.4571;
    k(20) = 1.8271e-6;
    k(21) = 1.7519e-7;
    k(22) = 0.5044;
    k(23) = 0.1218;
end

% Hypothesis testing (humans)
% Hyp1: humans have a higher stress response than rats
if setHypotheses == 1
    k(2) = 20.8217; % Metabolic demand
    k(41) = 3.4742e-4; % Growth rate
    k(32) = 9e-3; % theta_ap
    k(3) = 0.1435; % TNF production
    k(4) = 0.4942; % TNF mRNA degrad
    k(5) = 1.3637e3; % JAK activation rate (V_JAK)
    k(6) = 7.5654e3; % Km JAK
    k(7) = 0.0398; % JAK degrad
    k(8) = 2.031; % Conc. of monomeric STAT3
    k(9) = 1.1091e3; % STAT3 activation rate
    k(10) = 0.5178; % Km STAT3
    k(11) = 0.035; % STAT3 degrad
    k(12) = 6.3042e3; % SOCS activation rate
    k(13) = 6.8196e-4; % Km SOCS3
    k(14) = 0.1682; % SOCS3 degrad.
    k(15) = 0.0569; % SOCS3 inhibition constant
    k(16) = 18.6019; % IE response gene expression rate
    k(17) = 88.1259; % Km for IE genes
    k(18) = 1.1482; % IE gene degrad
end
% Hyp2: humans store a greater quantitiy of GFs in ECM & Potentially
% altered ECM synthesis
if setHypotheses == 2
    k(2) = 20.8217; % Metabolic demand
    k(41) = 3.4742e-4; % Growth rate
    k(32) = 9e-3; % theta_ap
    k(19) = 4.9548; % ECM degrad by MMPs
    k(20) = 56.2986; % ECM degrad
    k(21) = 3.288e-7; % GF production
    k(22) = 2.1389e-7; % GF degrad
    k(23) = 0.1008; % k_up - GF uptake/production rate by ECM
end
% Hyp3: humans have a higher transition time
if setHypotheses == 3
    k(2) = 20.8217; % Metabolic demand
    k(41) = 3.4742e-4; % Growth rate
    k(32) = 9e-3; % theta_ap
    k(24) = 1.4e-3; % k_Q
    k(25) = 1.5e-3; % k_P
    k(26) = 70.9e-3; % k_R
end
% Hyp4: longer cell cycle, higher apoptosis rate, et al.
if setHypotheses == 4
    k(2) = 20.8217; % Metabolic demand
    k(41) = 3.4742e-4; % Growth rate
    k(32) = 9e-3; % theta_ap
    k(28) = 0.00417; %k_req
    k(27) = 0.00833; %k_prol
    k(31) = 0.00417; %k_apop
    k(24) = 1.1e-3; %k_q
    k(25) = 2.6e-3; %k_p
    k(26) = 135e-3; %k_r
end

% Table 3: Steady-State parameters
k(34) = -k(3)*k(2) + k(5)/(1+k(6)) + k(4); %-22.3; % TNF production
k(35) = -k(5)/(1+k(6)) + k(7); %-1.6; % JAK production
k(36) = -k(9)*k(8)^2/(k(8)^2+k(10)*(1+1/k(15))) + ...
    k(16)/(1+k(17)) + k(12)/(1+k(13)) + k(11); %2.39e4; % STAT3 production
k(37) = -k(12)/(1+k(13)) + k(14); %2.4e4; % SOCS production
k(38) = -k(16)/(1+k(17)) + k(18); %-8.16; % IE gene production
k(39) = k(19) + k(20); %40; % ECM production
k(40) = -k(21)*k(2) + k(23) + k(22); %-1.6; % GF production

% Set initial hepatocyte states, cell = (Q,P,R)
% and concentrations, conc = [IE,GF,ECM,TNF,JAK,STAT3,SOCS3]
cell = [k(1),0,0];
conc = ones(1,7);
G = 1;
x0 = [cell,conc,G]';

tStart = 0;
if setSpecies == 3
    tEnd = 400*24; % for humans
else tEnd = 300; % for rats/mice
end

% Call ODE solver
[t,x] = ode15s(@(t,x)odefun(t,x,k,x0), [tStart:.1:tEnd], x0);

% Organize results
cell = x(:,1:3);
% Remove negative values not used in calculations
cell(cell(:,2) < 0,2) = 0;
cell(cell(:,3) < 0,3) = 0;
% End of removing negative values
conc = x(:,4:10);
G = x(:,11);

% Plot results here
if shouldPlot == 1
    
    Stylez = 'k-'; % How to plot results
    Stylez2 = '-';
    if setSpecies == 3
        xAxisText = 'Time post-PHx (days)';
        t = t/24;
    else xAxisText = 'Time post-PHx (hrs)';
    end
    
    figure(1); hold on;
    plot(t,cell(:,2),Stylez2,t,cell(:,3),Stylez2,t,cell(:,1)+cell(:,2)+cell(:,3),Stylez,'linewidth',3)
    set(gca,'FontSize',12)
    xlabel(xAxisText,'FontSize',14);ylabel('Fraction of Liver','FontSize',14);
    ylim([0 1.10000001])
    title('Regeneration Profile','FontSize',18);
    legend('Primed','Replicating','Total','Location','Best');

    figure(2)
    plot(t,(conc),'linewidth',3)
    set(gca,'FontSize',12)
    xlabel(xAxisText,'FontSize',14);ylabel('Fold Change','FontSize',14);
    title('Molecular Levels','FontSize',18);
    legend('IE','GF','ECM','TNF','JAK','STAT3','SOCS3','location','best')

    figure(3);hold on;
    plot(t,cell(:,1)+G.*(cell(:,2)+cell(:,3)),Stylez,'linewidth',3)
    set(gca,'FontSize',12)
    xlabel(xAxisText,'FontSize',14);ylabel('Fraction of Liver','FontSize',14);
    ylim([0 1.10000001])
    title('Regeneration Profile','FontSize',18);

    brdu = cell(:,3)./(sum(cell')');
    brduMax = find(brdu >= max(brdu),1);
    disp('Maximum Brdu Incorporation is at')
    disp(t(brduMax))

    figure(4);hold on;
    plot(t,brdu,Stylez,'linewidth',3)
    set(gca,'FontSize',12)
    xlabel(xAxisText,'FontSize',14);ylabel('Fraction of Cells Replicating','FontSize',14);
    title('Replicating Hepatocytes','FontSize',18);
end %Ends plotting if statement
end

%% ODE Function to solve
function dxdt = odefun(t,x,k,x0)
% Set variable names
G = x(11);
Q=max(0,x(1)); P=max(0,x(2)); R=max(0,x(3)); N=Q+G*(P+R);
IE=x(4); GF=max(1,x(5)); ECM=x(6); TNF=x(7); JAK=x(8); STAT=x(9); SOCS=x(10);
IE0=x0(4); GF0=x0(5); ECM0=x0(6); TNF0=x0(7); JAK0=x0(8); STAT0=x0(9); SOCS0=x0(10);

% Calculate sigmas
sr = 0.5*(1+tanh((k(29)-GF)/k(30))); % sigma_req
sa = 0.5*(1+tanh((k(32)-((N)/k(2)))/k(33))); % sigma_ap

% Calculate reaction rates
r1 = k(24)*(IE-IE0)*Q; % Priming of Quiescent cells
r2 = k(26)*ECM*R; % Replicating cell returning to Quiescence
r3 = k(28)*sr*P; % Requiescence of Primed cells
r4 = k(25)*(GF-GF0)*P; % Primed cells begining Replication
r5 = k(27)*R; % Doubling of Replicating cells
ra1 = k(31)*sa*Q; % Apoptosis of Quiescent cells
ra2 = k(31)*sa*P; % Apoptosis of Primed cells
ra3 = k(31)*sa*R; % Apoptosis of Replicating cells

r6 = k(3)*k(2)/(N); % TNF production by stimulus
r7 = k(5)*TNF/(TNF+k(6)); % TNF production of JAK
r8 = k(4)*TNF; % TNF degradation
r9 = k(7)*JAK; % JAK degredation
r10 = k(9)*JAK*k(8)^2/(k(8)^2+k(10)*(1+SOCS/k(15))); % STAT3 production by JAK
r11 = k(16)*STAT/(STAT+k(17)); % IE production by STAT3
r12 = k(12)*STAT/(STAT+k(13)); % SOCS3 production by STAT3
r13 = k(11)*STAT; % STAT3 degredation
r14 = k(14)*SOCS; % SOCS3 degredation
r15 = k(18)*IE; % IE degredation
r16 = k(19)*TNF*ECM; % ECM degredation by TNF activated MMPs
r17 = k(20)*ECM; % ECM degredation
r18 = k(21)*k(2)/N; % GF production by stimulus
r19 = k(23)*GF*ECM; % GF uptake/production by ECM
r20 = k(22)*GF; % GF degredation

% Set differential equations
dxdt(1) = -r1 + r2 + r3 - ra1; % Q Phase
dxdt(2) = r1 - r4 - r3 - ra2; % P Phase
dxdt(3) = r4 - r2 + r5 - ra3; % R Phase

dxdt(4) = r11 - r15 + k(38); % IE genes
dxdt(5) = r18 - r19 - r20 + k(40); % GF
dxdt(6) = -r16 - r17 + k(39); % ECM

dxdt(7) = r6 - r7 - r8 + k(34); % TNF
dxdt(8) = r7 - r9 + k(35); % JAK
dxdt(9) = r10 - r11 - r12 - r13 + k(36); % STAT3
dxdt(10) = r12 - r14 + k(37); % SOCS3

dxdt(11) = (k(2)/(N))*k(41) - k(2)*k(41); % Cell Mass (M)

dxdt = dxdt';
end