R/container_deconvolution.R

Defines functions container_deconvolution

#' @importFrom stringr str_replace_all
#' @importFrom dynwrap test_docker_installation
#' @importFrom babelwhale list_docker_images pull_container run
#' @importFrom tidyr unite
#' @importFrom dplyr %>%
#' @importFrom dplyr pull
container_deconvolution <- function(
    method,
    bulkdata,
    signature = NULL,
    scdata = NULL,
    label = NULL,
    celltypenum = NULL,
    tissue = NULL,
    reftype = NULL,
    tumortype = NULL,
    SubMethod = NULL,
    seed = NULL
){


  # 1.1 Check docker installation-----------------------------------------------
  docker_install <- dynwrap::test_docker_installation()
  if(!docker_install){
    stop("Docker has not been installed or started! Please check it!")
  }
  # 1.2 Check the installation of simpipe docker image--------------------------
  images <- babelwhale::list_docker_images() %>%
    tidyr::unite("Repository", "Tag", sep = ":", col = "Image") %>%
    dplyr::pull("Image")
  imagesname <- "jingxintao/deconvolution_pip:latest"
  if(!imagesname %in% images){
    # If not, pull jingxintao/deconvolution_pip:latest
    babelwhale::pull_container(container_id = imagesname)
  }
  # 1.3 docker container id-----------------------------------------------------
  container_id <- "jingxintao/deconvolution_pip"


  # 2.1 The input data preparation----------------------------------------------
  inputdata <- list(bulkdata = bulkdata,
                    signature = signature,
                    scdata = scdata,
                    label = label)

  # 2.2 Process file path-------------------------------------------------------

  tempfile <-  tempfile()

  temp_dir <- dir.create(tempfile, recursive = TRUE, showWarnings = FALSE)

  temp_dir <- tempfile

  #temp_dir <- tempdir()

  # 2.3 Set a path for the input data
  temp_input_path <-  stringr::str_replace_all(file.path(temp_dir, "input.rds"),
                                               pattern = "\\\\", replacement = "/")

  # 2.4Process datasets---------------------------------------------------------
  saveRDS(inputdata, file = temp_input_path)



  # 3.1 docker image working directory------------------------------------------
  wd <- "/home/admin/"
  # 3.2 local directory of the mount point--------------------------------------
  local_path <-  stringr::str_replace_all(temp_dir, pattern = "\\\\",
                                          replacement = "/")
  # 3.3 docker image directory of the mount point-------------------------------
  docker_path <- "/home/admin/docker_path"
  # 3.4 Prepare the input parameters--------------------------------------------
  input_meta <- list(method = method,
                     container_id = container_id,
                     volumes = paste0(local_path, ":", docker_path),
                     workspace = wd,
                     args = NULL,
                     command = NULL,
                     seed = seed,
                     celltypenum = celltypenum,
                     tissue = tissue,
                     reftype =  reftype,
                     tumortype = tumortype,
                     SubMethod = SubMethod)

  saveRDS(input_meta, file.path(local_path, "input_meta.rds"))

  # 4.1 Run container-----------------------------------------------------------
  output <- babelwhale::run(container_id = input_meta$container_id,
                            command = input_meta$command,
                            args = input_meta$args,
                            volumes = input_meta$volumes,
                            workspace = input_meta$workspace,
                            verbose = TRUE,
                            debug = FALSE)


  ## return output
  res <- readRDS(file = file.path(local_path, "output.rds"))

  return(res)
}
libcell/deconvBench documentation built on Sept. 24, 2022, 12:36 p.m.