R/simulation.R

#' sufficiently simulate a cell splitting tree
#'
#' @param given_starting_gland a gland object
#' @param paths a list of paths consisting of vectors of "left" and "right".
#' @inheritParams constant_stage
#'
#' @return a list of gland objects
#' @export
#'
#' @examples
#' gland <- gland_create(sample(c(0, 1), 10, TRUE), n_iter = 4)
#' paths <- list(c("left", "left", "right", "right"),
#'               c("left", "left", "left", "right"),
#'               c("left", "left", "left", "left"))
#'
#' path_sim(gland, paths)
path_sim <- function(given_starting_gland, paths, n = 100) {
  seqs <- directions_to_seqs(paths)
  suff_paths <- seqs_to_paths(seqs)

  seqs$S[["sim"]] <- growth_stage(given_starting_gland,
                                  length(seqs$S$seq),
                                  seqs$S$seq)

  path_place <- 1

  current_path <- suff_paths[[path_place]]
  place <- length(current_path)

  repeat {
    if (is.null(seqs[[current_path[place - 1]]][["sim"]])) {
      while (is.null(seqs[[current_path[place - 1]]][["sim"]])) {
        place <- place - 1
      }
    }

    sides <- seqs[[current_path[place]]][["seq"]]
    seqs[[current_path[place]]][["sim"]] <-
      growth_stage(seqs[[current_path[place - 1]]][["sim"]],
                   length(sides),
                   sides)

    if (place == length(current_path)) {
      if (path_place == length(suff_paths)) break
      path_place <- path_place + 1

      current_path <- suff_paths[[path_place]]
      place <- length(current_path)
    } else {
      place <- place + 1
    }
  }

  lapply(suff_paths, function(x) {
    out <- seqs[[x[length(x)]]][["sim"]]
    explosion_stage(out, n)
  })
}
EmilHvitfeldt/cell documentation built on May 5, 2019, 7:03 p.m.