R/process.R

#' Represents an FastGenomics Process.
#'
setClass("Process",
  slots = list(app="App", params="list", input="list", output="list")
)


#' Initializes a FgProcess and holds all information your app needs at runtime
#'
#' If you use not default paths for app & data, modify app_dir and data_dir.
#' This will never be necessary when run in an app, but maybe for local
#' development or testing
#'
#' @param app_dir the directory where the app is located. Defaults to /app
#' @param data_dir the directory where the data is located. Defaults to /fastgenomics
#' @param input_file_mapping Where to get the mapping from inputs required
#' in the manifest to actual files on disk. If you need to customize this, only override this parameter as a last resort.
#' See \code{\link{get_input_file_mapping}} to get an idea how to inject this data.
#'
#' @export
#'
#' @return \code{\link{Process}}
#'
#' @examples
#' fg_process <- Process()
Process <- function(app_dir=get_app_dir(),
                    data_dir=get_data_dir(),
                    input_file_mapping=get_input_file_mapping(data_dir)){
    assertthat::is.readable(data_dir)
    app <- App(app_dir)

    params_file <- file.path(data_dir, "config", "parameters.json")
    assertthat::is.readable(params_file)
    params <- merge_params(app@parameters, jsonlite::read_json(params_file))

    output_dir <- file.path(data_dir, "output")
    summary_dir <- file.path(data_dir, "summary")
    output <- Map(
      purrr::compose(
            OutputFile,
            purrr::partial(to_absolute_path, dir = output_dir),
            add_name),
        app@output, names(app@output))

    output[[SUMMARY_KEY]] <- new(
      "OutputFile",
      type = "output_only",
      path = file.path(normalizePath(summary_dir), "summary.md"),
      usage = "Summary",
      optional = FALSE,
      name = "Summary"
    )

    input_dir <- file.path(data_dir, "data")
    assertthat::is.readable(input_dir)
    input <- Map(
      purrr::compose(
        InputFile,
        purrr::partial(to_absolute_path, dir = input_dir),
        purrr::partial(map_input, mapping = input_file_mapping),
        add_name
      ),
      app@input,
      names(app@input)
    )

    new(
      "Process",
      app = app,
      params = params,
      input = input,
      output = output
    )
}

setMethod("show", "Process",
          function(object){
              cat(stringr::str_interp("FASTGenomics runtime.\n"))
              show(object@app)
          })

add_name <- function(spec, name) {
    spec$name <- name
    spec
}

#' adds a new attribute $path containing the absolute path to the file described by spec
to_absolute_path <- function(spec, dir) {
    if ( R.utils::isAbsolutePath(spec$file_name) ) {
        ## short circuit if it's already a global path (works only under Linux)
        spec$path <- spec$file_name
    } else {
        spec$path <- file.path(normalizePath(dir), spec$file_name)
    }
    spec
}

#' uses mapping list (coming from input_file_mapping) to get the file name
map_input <- function(spec, mapping) {
    spec$file_name <- mapping[[spec$name]]
    spec
}


setGeneric(name="read_seurat", signature = c("process"), def = function(process) standardGeneric("read_seurat"))
setGeneric(name="write_seurat", signature = c("process", "seuratObject"), def = function(process, seuratObject) standardGeneric("write_seurat"))

#' Reads a Seurat object from the input file mapping.
#' Note that this functions works only if there is exactly a single input file mapping of type seurat present.
#'
#' @param process The FASTGenomics process. Its input file mappings will be used to read the seurat object.
#'
#' @export
#'
#' @return The Seurat object
#'
#' @examples
#' seurat_object <- read_seurat(fg_process)
setMethod("read_seurat", "Process",  def=function(process){
      return(read_the_seurat(process))
    }
)


#' Writes a Seurat object to the output file mapping.
#' Note that this functions works only if there is exactly a single output file mapping of type seurat present.
#'
#' @param process The FASTGenomics process. Its output file mappings will be used to read the seurat object.
#' @param seuratObject The seurat object.
#'
#' @export
#'
#' @return Nothing
#'
#' @examples
#' write_seurat(fg_process, seurat_object)
setMethod("write_seurat", "Process",  def=function(process, seuratObject){
      return(write_the_seurat(process, seuratObject))
    }
)
FASTGenomics/fastgenomicsR documentation built on June 26, 2019, 12:38 p.m.