auxiliary/modelfiles/other/simulators/simulate_Influenza_OAS_simpleModel_stochastic.R

#' Influenza OAS simpleModel
#' 
#' @description Influenza Antibody Model with 2 types of B-cells/antibodies. EMM only.
#' 
#' @details An extension of Zarnitsyna et al 2016 PLoS Pathogens
#' 
#' This code was generated by the modelbuilder R package.  
#' The model is implemented as a set of stochastic equations using the adaptivetau package. 
 
#' The following R packages need to be loaded for the function to work: adpativetau 
#' 
#' @param Hf : starting value for free antigen : numeric
#' @param Hb : starting value for bound antigen : numeric
#' @param B1 : starting value for Type 1 B cells : numeric
#' @param A1 : starting value for Type 1 antibodies : numeric
#' @param B2 : starting value for Type 2 B cells : numeric
#' @param A2 : starting value for Type 2 antibodies : numeric
#' @param k1 : binding rate 1 : numeric
#' @param k2 : binding rate 2 : numeric
#' @param df : free antigen decay rate : numeric
#' @param db : bound antigen decay rate : numeric
#' @param s1 : B1 cell generation rate : numeric
#' @param p1 : saturation level 1 : numeric
#' @param g1 : antibody 1 production rate : numeric
#' @param d1 : antibody 1 decay rate : numeric
#' @param s2 : B2 cell generation rate : numeric
#' @param p2 : saturation level 2 : numeric
#' @param g2 : antibody 2 production rate : numeric
#' @param d2 : antibody 2 decay rate : numeric
#' @param tfinal : Final time of simulation : numeric
#' @param rngseed : set random number seed for reproducibility : numeric
#' @return The function returns the output as a list. 
#' The time-series from the simulation is returned as a dataframe saved as list element \code{ts}. 
#' The \code{ts} dataframe has one column per compartment/variable. The first column is time.   
#' @examples  
#' # To run the simulation with default parameters:  
#' result <- simulate_Influenza_OAS_simpleModel_stochastic() 
#' # To choose values other than the standard one, specify them like this:  
#' result <- simulate_Influenza_OAS_simpleModel_stochastic(Hf = 20000,Hb = 0,B1 = 200,A1 = 200,B2 = 20,A2 = 20) 
#' # You can display or further process the result, like this:  
#' plot(result$ts[,'time'],result$ts[,'Hf'],xlab='Time',ylab='Numbers',type='l') 
#' print(paste('Max number of Hf: ',max(result$ts[,'Hf']))) 
#' @section Warning: This function does not perform any error checking. So if you try to do something nonsensical (e.g. have negative values for parameters), the code will likely abort with an error message.
#' @section Model Author: Andreas Handel
#' @section Model creation date: 2023-05-08
#' @section Code Author: generated by the \code{modelbuilder} R package 
#' @section Code creation date: 2023-05-08
#' @export 
 
simulate_Influenza_OAS_simpleModel_stochastic <- function(Hf = 10000, Hb = 0, B1 = 100, A1 = 100, B2 = 10, A2 = 10, k1 = 0.01, k2 = 0.02, df = 0.5, db = 0.7, s1 = 1, p1 = 1000, g1 = 0.1, d1 = 0.1, s2 = 0.5, p2 = 1000, g2 = 0.1, d2 = 0.1, tfinal = 100, rngseed = 123) 
{ 
  #Block of ODE equations for adaptivetau 
  Influenza_OAS_simpleModel_fct <- function(y, parms, t) 
  {
    with(as.list(c(y,parms)),   
     { 
       #specify each rate/transition/reaction that can happen in the system 
     rates = c(k1*A1*Hf, k2*A2*Hf, d1*A1, d2*A2, db*Hb, df*Hf, g1*B1, g2*B2, s1*B1*Hf/(p1+Hf), s2*B2*Hf/(p2+Hf))
     return(rates) 
      }
	 	)   
  } # end function specifying rates used by adaptive tau 

   #specify for each reaction/rate/transition how the different variables change 
  #needs to be in exactly the same order as the rates listed in the rate function 
  transitions = list(c(Hf = -1,Hb = +1), 
 	 	 				c(A1 = -1,Hf = -1), 
 	 	 				c(A1 = -1), 
 	 	 				c(A2 = -1), 
 	 	 				c(Hb = -1), 
 	 	 				c(Hf = -1), 
 	 	 				c(A1 = +1), 
 	 	 				c(A2 = +1), 
 	 	 				c(B1 = +1), 
 	 	 				c(B2 = +1)) 
 
  ############################## 
  #Main function code block 
  ############################## 
  set.seed(rngseed) #set random number seed for reproducibility 
  #Creating named vectors 
  varvec = c(Hf = Hf, Hb = Hb, B1 = B1, A1 = A1, B2 = B2, A2 = A2) 
  parvec = c(k1 = k1, k2 = k2, df = df, db = db, s1 = s1, p1 = p1, g1 = g1, d1 = d1, s2 = s2, p2 = p2, g2 = g2, d2 = d2) 
  #Running the model 
  simout = adaptivetau::ssa.adaptivetau(init.values = varvec, transitions = transitions,
                  	 	 	 rateFunc = Influenza_OAS_simpleModel_fct, params = parvec, tf = tfinal) 
  #Setting up empty list and returning result as data frame called ts 
  result <- list() 
  result$ts <- as.data.frame(simout) 
  return(result) 
 }  
 
ahgroup/modelbuilder documentation built on April 14, 2024, 2:29 p.m.