R/data.R

#' Import the cell data and add a couple helpful columns.
#'
#' @param name The name of the experiment to load.
#'
#' @return A data.frame with all the data.
#' @export
#'
#' @examples
readData <- function(name) {
  data <- utils::read.csv(system.file("extdata", name, package = "lineage"))
  data$G0 <- data$Cell.Count.Across.time - data$G1
  data$dead <- cumsum(data$Cell.death.across.time)

  return(data)
}

#' Import dose response data.
#'
#' @param name The name of the experiment to load.
#'
#' @return A data.frame with all the data.
#' @export
#'
#' @examples
readDoseData <- function(name) {
  data <- utils::read.delim(system.file("extdata", name, package = "lineage"))

  data <- reshape2::melt(data, id.vars = c('X'))

  datamm <- tidyr::separate(data, variable, into = c('well', 'conc'), sep = 3)

  datamm$conc <- as.double(stringr::str_replace(datamm$conc, "nM", ""))

  return(datamm)
}

#' Fit the model without a delay term.
#'
#' @param data The data.frame to use for fitting.
#'
#' @return The fit parameter set.
#' @export
#'
#' @examples
fitModel <- function(data) {

  simDF <- function(x) {
    cYY <- cycleModelSim(x, start = c(data$G0[1], data$G1[1], data$dead[1]), time = data$Time..hr.)

    return(sum((cYY[, 1] - data$G0)^2 + (cYY[, 2] - data$G1)^2 + (cYY[, 3] - data$dead)^2))
  }

  outt <- stats::optim(c(0.02, 0.07, 0.01, 0.01), simDF, lower = 1.0E-8, upper = 0.5, method = "L-BFGS-B")

  return(outt$par)
}


#' Fits a set of data with the growth model incorporating a delay.
#'
#' @param data The data.frame to use for fitting.
#' @param controlP The set of parameters that apply before the delay.
#'
#' @return
#' @export
#'
#' @examples
fitModelDelay <- function(data, controlP) {

  simDF <- function(x) {
    cYY <- cycleModelDelay(xBefore = controlP,
                           xAfter = x[1:4],
                           start = c(data$G0[1], data$G1[1], data$dead[1]),
                           t = data$Time..hr.,
                           delay = x[5])

    return(sum((cYY[, 1] - data$G0)^2 + (cYY[, 2] - data$G1)^2 + (cYY[, 3] - data$dead)^2))
  }

  outt <- stats::optim(c(0.02, 0.07, 0.01, 0.01, 20.0), simDF, lower = 1.0E-8,
                       upper = c(0.02, 0.07, 0.01, 0.01, max(data$Time..hr.) - 1),
                       method = "L-BFGS-B")

  return(outt$par)
}

#' Simulate the model and assemble the output into the data.frame given.
#'
#' @param data The data.frame to use for simulating.
#' @param par
#' @param controlP Parameters for the cells in the absence of drug. If null, drug has effect at t = 0.
#'
#' @return
#' @export
#'
#' @examples
simModel <- function(data, par, controlP = NULL) {
  if (is.null(controlP)) {
    simD <- cycleModelSim(c(par[1], par[2], par[3], par[4]), start = c(data$G0[1], data$G1[1], data$dead[1]), time = data$Time..hr.)
  } else {
    simD <- cycleModelDelay(xAfter = c(par[1], par[2], par[3], par[4]),
                            xBefore = controlP,
                            start = c(data$G0[1], data$G1[1], data$dead[1]),
                            t = data$Time..hr.,
                            delay = par[5])
  }

  data$simG0 <- simD[, 1]
  data$simG1 <- simD[, 2]
  data$simDead <- simD[, 3]

  return(data)
}
meyer-lab/cell-cycle-growth documentation built on May 13, 2019, 6:09 p.m.