DESCRIPTION:
Full TMDD model for FcRn mediated recycling


[LONGITUDINAL]
input = {kelc, kele, kepc, kup, kout, kon, koff, konA, koffA, kr, ktp, kpt, Lc0, R0, Fc0}

EQUATION:
odeType = stiff

kin = kout*R0        ; Synthesis of receptor (nM/day)

;Initial Conditions
Lc_0 = Lc0
R_0 = R0

;Ordinary Differential Equations
ddt_Lc   = -(kelc+kup+kpt)*Lc-kon*R*Lc+koff*Pc+kr*FcLe+ktp*Lt
ddt_Lt   = kpt*Lc-ktp*Lt
ddt_R    = kin-kout*R-kon*R*Lc+koff*Pc
ddt_Pc   = kon*R*Lc-(koff+kepc+kup+kpt)*Pc+ktp*Pt+kr*FcPe
ddt_Pt   = kpt*Pc-ktp*Pt
ddt_Le   = kup*Lc-kele*Le-konA*Fc0*Le+koffA*FcLe
ddt_FcLe = konA*Fc0*Le-(koffA+kr)*FcLe
ddt_Pe   = kup*Pc-kele*Pe-konA*Fc0*Pe+koffA*FcPe
ddt_FcPe = konA*Fc0*Pe-(koffA+kr)*FcPe


ui <- shinyUI(fluidPage(
navbarPage("TMDD - Model 7",
tabPanel("Plot",
fluidRow(
column(2,
tabsetPanel(type='tabs',
tabPanel('param1',br(),
sliderInput("kelc", label="kelc",value=0.024,min=0.012,max=0.048,step=0.0024),
sliderInput("kele", label="kele",value=0.024,min=0.012,max=0.048,step=0.0024),
sliderInput("kepc", label="kepc",value=0.201,min=0.1005,max=0.402,step=0.0201),
sliderInput("kepe", label="kepe",value=0.201,min=0.1005,max=0.402,step=0.0201),
br()
),
tabPanel('param2',br(),
sliderInput("kout", label="kout",value=0.823,min=0.4115,max=1.646,step=0.0823),
sliderInput("koff", label="koff",value=0.9,min=0.45,max=1.8,step=0.09),
sliderInput("kon", label="kon",value=0.592,min=0.296,max=1.184,step=0.0592),
sliderInput("koffA", label="koffA",value=2,min=1,max=4,step=0.2),
br()
),
tabPanel('param3',br(),
sliderInput("konA", label="konA",value=4.83,min=2.415,max=9.66,step=0.483),
sliderInput("ktp", label="ktp",value=0.725,min=0.3625,max=1.45,step=0.0725),
sliderInput("kpt", label="kpt",value=0.902,min=0.451,max=1.804,step=0.0902),
sliderInput("kup", label="kup",value=0.36,min=0.18,max=0.72,step=0.036),
br()
),
tabPanel('param4',br(),
sliderInput("kr", label="kr",value=0.4,min=0.2,max=0.8,step=0.04),
sliderInput("Lc0", label="Lc0",value=200,min=100,max=400,step=20),
sliderInput("R0", label="R0",value=0.03,min=0.015,max=0.06,step=0.003),
sliderInput("Fc0", label="Fc0",value=0.05,min=0.025,max=0.1,step=0.005),
br()
),
br()
)

),
column(2,
br(),
checkboxGroupInput("out1", label="plot 1", choices=c('Lc','Le'), selected=c('Lc','Le')),

selectInput("x1", label="v.s.",c('time','Lc','Le','Pc','Pe','FcLe','FcPe')),
hr(),
checkboxGroupInput("out2", label="plot 2", choices=c('Pc','Pe'), selected=c('Pc','Pe')),

selectInput("x2", label="v.s.",c('time','Lc','Le','Pc','Pe','FcLe','FcPe')),
hr(),
checkboxGroupInput("out3", label="plot 3", choices=c('FcLe','FcPe'), selected=c('FcLe','FcPe')),

selectInput("x3", label="v.s.",c('time','Lc','Le','Pc','Pe','FcLe','FcPe')),
hr()
),
column(8,br(),            
       plotOutput("plot"),
       br(),
       br(),
       br(),
       br(),
       br(),
       fluidRow(
         column(1),
         column(1,checkboxInput("boxref", label="ref.")),
         column(3,actionButton("butref", label = "Reset")),
         column(7,radioButtons("ilog", "scale", c("linear" = FALSE,"log" = TRUE), selected=TRUE, inline=TRUE))
       )
)
)
),
tabPanel("Model", pre(includeText("model.txt"))),
tabPanel("ui.R", pre(includeText("ui.R"))),
tabPanel("server.R", pre(includeText("server.R")))
)
))
library("mlxR")
library("reshape")
library("gridExtra")
source("shinymlxTools.R")

out1 <- list(name=c('Lc','Le'), time=seq(0,360,by=1))
out2 <- list(name=c('Pc','Pe'), time=seq(0,360,by=1))
out3 <- list(name=c('FcLe','FcPe'), time=seq(0,360,by=1))
f  <- list(out1, out2, out3)
nf <- length(f)
info <- info_res(f)

server <- function(input, output) {
  ref <- reactive({
    input$butref
    p <- list(name  = c('kelc', 'kele', 'kepc', 'kepe', 'kout', 'koff', 'kon', 'koffA', 'konA', 'ktp', 'kpt', 'kup', 'kr', 'Lc0', 'R0', 'Fc0'),
              value = isolate(c(input$kelc, input$kele, input$kepc, input$kepe, input$kout, input$koff, input$kon, input$koffA, input$konA, input$ktp, input$kpt, input$kup, input$kr, input$Lc0, input$R0, input$Fc0)))

    r <- simulx( model     = 'model.txt',
                 parameter = p,
                 output    = f)

    ref <- merge_res(r,f)
    return(ref)
  })
  
  res <- reactive({
    p <- list(name  = c('kelc', 'kele', 'kepc', 'kepe', 'kout', 'koff', 'kon', 'koffA', 'konA', 'ktp', 'kpt', 'kup', 'kr', 'Lc0', 'R0', 'Fc0'),
              value = c(input$kelc, input$kele, input$kepc, input$kepe, input$kout, input$koff, input$kon, input$koffA, input$konA, input$ktp, input$kpt, input$kup, input$kr, input$Lc0, input$R0, input$Fc0))
                     
    r <- simulx( model     = 'model.txt',
                 parameter = p,
                 output    = f)
                     
    res <- merge_res(r,f)
    return(res)
  })  
  
  output$plot <- renderPlot({
    res=res()
   ref=ref()    
    gr.txt <- "grid.arrange("
    for (j in (1:length(f))){
      xj <- "time"
      fj <- f[[j]]
      name.fj <- fj$name

   eval(parse(text=paste0("inputyj=input$out",j)))  
  i.plot=FALSE
  if (!is.null(inputyj)){
    ij <- which(name.fj %in% inputyj)
    if (length(ij>0)){
      eval(parse(text=paste0("inputxj=input$x",j)))
      if (!is.null(inputxj))
        xj <- inputxj
    }
    i.plot=TRUE
  }
  else if (is.null(inputyj) & length(f)==1){
    ij=1
    i.plot=TRUE
  }
  if (i.plot){
    pl <- ggplotmlx()
    nfj <- length(name.fj)
    for (k in (1:nfj)){
      if (k %in% ij){
           if (input$boxref==TRUE){
      pj <- paste0('pl <- pl + geom_path(data=ref, aes(x=',xj,',y=',name.fj[k],'),colour="grey",size=0.75)')
      eval(parse(text=pj))
    }
   pj <- paste0('pl <- pl + geom_path(data=res, aes(x=',xj,',y=',name.fj[k],',colour="',info[[j]]$colour[k],'"),size=0.75)')
    eval(parse(text=pj))
      }  
    }
    pl <- pl + scale_colour_manual(values=info[[j]]$values, labels=info[[j]]$labels)
    print(pl)
    if (length(ij)>1){
      if (!is.null(input$legend) && input$legend==FALSE)
        pl <- pl + theme(legend.position="none")
      else
        pl <- pl + guides(colour=guide_legend(title=NULL)) + theme(legend.position=c(.9, .8))
      pl <- pl + ylab("")
    }else{
      pl <- pl + theme(legend.position="none")
    }
  
      if (input$ilog==TRUE)
       pl=pl + scale_y_log10()
    eval(parse(text=paste0("pl",j," <- pl")))
    gr.txt <- paste0(gr.txt,"pl",j,",")
  }
    }
    gr.txt <- paste0(gr.txt,"ncol=1)")
    eval(parse(text=gr.txt))
  }, height = 500)
}