# Please rename the file cccMaltab.ode
# Model of the cell cycle (Burns & Tannock, Smith & Martin, Mackey)
# The cell cycle is divided into 4 phases
# G0/G1: rest/growth phase - duration has exponential distribution beta(t)
#                          - loss rate delta(t)
#                          - total cell number: N(t)
# S: synthesis phase - fixed duration tS
#                    - loss rate gS(t)
#                    - total cell number S(t)
# G2: gap 2 phase - time-dependent fixed duration tP(t)
#                 - loss rate gP
#                 - total cell number P(t)
# M: mitosis - fixed duration tM
#            - loss rate gM
#            - total cell number M(t)
#
#
# The default parameters are for human tumor cells.
#
# This file is intended to be run with Matlab, but can be run as a stand-alone
# If you use the XPP-Matlab interface, setting parameters using the command ChangeXPPodeFile 
# will modify default parameter values in this file.
#
# contact: S Bernard, samubernard@gmail.com


# =======================================TREATMENT===========================================

# ------------------------------INPUT FROM THE TREATMENT-------------------------------------
par ton=24, tdur=4, duration=5, dstart=3,killing=8, period=24
# Square wave treatment of duration tdur (h)
# tr_wave(t)=heav(t)-heav(t-tdur)
# Smooth wave treatment gaussian within a window of length tdur
tr_wave(t)=(heav(t+tdur/2)-heav(t-tdur/2))*gauss(t,0,tdur/4)
# Wave type sin(2t) 12 h 
# tr_wave(t)=(heav(t)-heav(t-12))*(1-cos(4*pi/Tclock*(t)))/12
treat(t)=sum(0,duration-1)of(tr_wave(t-ton-period*i'-24*dstart))
# Flat treatment equivalent to smooth wave treatment
## treat(t)=(heav(t-24*dstart)-heav(t-24*(duration+dstart)))*1.7642/24
# Flat treatment equivalent to sin2t wave treatment
# streat(t)=(heav(t-24*dstart)-heav(t-24*(duration+dstart)))/24
# -------------------------------------------------------------------------------------------

# ======================================CELL CYCLE===========================================

# -----------------------LOSS RATE FUNCTIONS: TREATMENT DEPENDENT----------------------------
par gS0=0.01, gP0=0.01, gM0=0.01, delta0=0.004
# S PHASE DRUG
gS(t)=gS0+killing*treat(t)
gP(t)=gP0
gM(t)=gM0
delta(t)=delta0
# G0 PHASE DRUG
# delta(t)=delta0+killing*treat(t)

# -------------------------------------------------------------------------------------------

# ----------------------------------------DELAYS---------------------------------------------
par tS=30, tM=3, tP0=2, theta2=20

# TIME-DEPENDENT DELAY
tP(t)=tP0*f2(t-theta2)
# time derivative of the delay
tPp(t)=tP0*f2p(t-theta2)
tfct(t)=(1-tPp(t))

# TOTAL DELAY
tau(t)=tS+tP(t-tM)+tM
# -------------------------------------------------------------------------------------------

# ----------------------------------PROLIFERATION RATE BETA----------------------------------------
par beta0=0.04, theta=5.75, kb=0.2,k0beta=1,hbeta=3
beta(t)=beta0*f(t-theta)


# --------------------------------CELL CYCLE EQUATIONS---------------------------------------
dN/dt=-(delta(t)+beta(t))*N+2*tfct(t-tM)*sigma(t)*beta(t-tau(t))*delay(N,tau(t))
dS/dt=-gS(t)*S-sS*beta(t-tS)*delay(N,tS)+beta(t)*N
dP/dt=-gP(t)*P-tfct(t)*sP(t)*delay(sS,tP(t))*beta(t-tS-tP(t))*delay(N,tS+tP(t))\
	+sS*beta(t-tS)*delay(N,tS)
dM/dt=-gM(t)*M-tfct(t-tM)*sigma(t)*beta(t-tau(t))*delay(N,tau(t))\
	+tfct(t)*sP(t)*delay(sS,tP(t))*beta(t-tS-tP(t))*delay(N,tS+tP(t))
# -------------------------------------------------------------------------------------------

# -------------------------------SURVIVING FRACTION SIGMA------------------------------------
sS(t)=exp(-int{heav(-(t-tS-t'))*gS(t')})
sP(t)=exp(-tP(t)*gP(t))
sM(t)=exp(-tM*gM(t))
sigma(t)=delay(sS,tM+tP(t-tM))*sP(t-tM)*sM(t)
# -------------------------------------------------------------------------------------------

# ----------------------------------AUXILIARY VARIABLES--------------------------------------
# TOT: total cell number, LI: labeling index S phase fraction, G1I: G1 phase fraction, 
# MI: M phase fraction, G2I: G2 phase fraction, G2MI: G2 and M phases fraction, 
# SF: survival fraction sigma(t), TG2: G2 phase duration, TR: treatment function,
# CLOCK: circadian clock input to the cell cycle, BT: proliferation rate beta(t)
total=N+S+P+M
aux TOT=total
aux LI=S/total
aux G1I=N/total
aux MI=M/total
aux G2I=P/total
aux G2MI=(P+M)/total
aux SF=sigma(t)
aux TG2=tP(t)
aux TR=killing*treat(t)
aux CLOCK=clockout
aux BT=beta(t)

# CLOCK OUTPUT FUNCTION
clockout=f(t)
# -------------------------------------------------------------------------------------------

# ----------------------------------INITIAL CONDITIONS---------------------------------------
init N=1,S=0,P=0,M=0,sS=1
# -------------------------------------------------------------------------------------------

# ==================================CIRCADIAN CLOCK==========================================
# ------------------------------OUPUT FROM CIRCADIAN CLOCK-----------------------------------
par alpha=0.5, alpha2=0.2,phi2=14,sensty=2, Tclock=24
f(t)=1+alpha*(cos(2*pi*t/Tclock)+alpha2*cos(4*pi*(t-phi2)/Tclock))
# sensty is the clock sensitivity of tau_P relative to beta
f2(t)=1+sensty*alpha*(cos(2*pi*t/Tclock)+alpha2*cos(4*pi*(t-phi2)/Tclock))
# derivative of f2 (to use with tPp(t))
f2p(t)=-sensty*alpha*pi/Tclock*(2*sin(2*pi*t/Tclock)+alpha2*4*sin(4*pi*(t-phi2)/Tclock))
#fbeta=lgpcnp
# -------------------------------------------------------------------------------------------

# =================================GENERAL FUNCTIONS=========================================
# HILL-FUNCTION
hill(v0,k0,h,X)=v0*k0^h/(k0^h+X^h)
# MICHAELIS-MENTEN-FUNCTION
mm(v0,k0,h,X)=v0*X^h/(k0^h+X^h)
# Gaussian function
gauss(x,mu,sig)=exp(-(x-mu)^2/sig^2)
# -------------------------------------------------------------------------------------------

# ============================XPPAUT PARAMETERS AND OPTIONS==================================

@ delay=150
@ total=240,dt=0.05,maxstor=120000
@ xlo=96,xhi=240,ylo=0, yhi=1, bound=10000
@ yplot=LI, method=volterra
@ but=go:ig
@ nplot=2, yp2=TR
# @ nplot=3,yp2=CLOCK,yp3=TR
#@ njmp=40
# AUTO parameters
#@ epss=1e-7,epsu=1e-7,epsl=1e-7,parmax=15,dsmax=5,dsmin=1e-4,ntst=100

d