R/io.R

setClass(
    "File",
    slots = list(
      type = "character",
      path = "character",
      usage = "character",
      name = "character",
      optional = "logical"
    )
)

show_file <- function(object){
  cat(stringr::str_interp("FASTGenomics File\n"))
  cat(stringr::str_interp("Name: ${object@name}\n"))
  cat(stringr::str_interp("Type: ${object@type}\n"))
  cat(stringr::str_interp("Name: ${object@name}\n"))
  cat(stringr::str_interp("Optional: ${object@optional}\n"))
  cat(stringr::str_interp("Path: ${object@path}\n"))
  cat(stringr::str_interp("Usage: ${object@usage}\n"))
}


validate_input_file <- function(object){
  if (!object@optional) {
    if (!object@mapped) {
      stop(
        stringr::str_interp(
          "A non-optional file ${object@name} was not defined in input_file_mapping."
        )
      )
    }
    assertthat::is.readable(object@path)
  }
}

setClass(
  "InputFile",
  slots = list(mapped = "logical"),
  contains = "File",
  validity = validate_input_file
)

setClass("OutputFile",
         slots = list(),
         contains = "File")

#' Checl if a given InputFile exists.
#'
#' @param InputFile 
#'
#' @return TRUE if the file exists, otherwise FALSE
#' @export
#'
#' @examples
#' fg_process <- fastgenomicsR::Process()
#' file.exists(fg_process@input$some_file)
setMethod("file.exists", "InputFile",
          function(...) {(...)@mapped && callGeneric((...)@path)})


#' Reads an FG Input File as a Table
#'
#' Note: Not all Files in FASTGenomics are CSV Files! 
#' Check the file type and use a more appropiate method when necessary.
#' 
#' @param InputFile The input file to be loaded
#'
#' @return a dataframe
#' @export
#'
#' @examples
#' fg_process <- fastgenomicsR::Process()
#' read.table(fg_process@input$some_file)
setMethod("read.table", "InputFile",
          function(file) {
            if (!file.exists(file)) {
              stop(stringr::str_interp("Input file \"${file@name}\" not found."))
            }
            callGeneric(file@path, sep = ",", header = TRUE)
          })

OutputFile <- function(spec) {
  optional <- if (is.null(spec$optional))
    FALSE
  else
    spec$optional
  new(
    "OutputFile",
    type = spec$type,
    path = spec$path,
    usage = spec$usage,
    optional = optional,
    name = spec$name
  )
}

#' Write an FASTGenomics Output as table
#' 
#' Not all types of Output in FASTGenomics can be written as CSV, 
#' please check the Type carefully. 
#' You can description of available outputs on our GitHub.
#' @param file OutputFile. 
#'
#' @return NA
#' @export
#'
#' @examples
#' fg_process <- fastgenomicsR::Process()
#' df <- read.table(fg_process@input$some_file)
#' write_table(df, fg_process@ouput$some_output)
setMethod("write.table", list(file = "OutputFile"),
          function(x, file) {
            callGeneric(
              x,
              file@path,
              sep = ",",
              col.names = TRUE,
              quote = FALSE,
              row.names = FALSE
            )
          })

InputFile <- function(spec) {
  optional <- if (is.null(spec$optional))
    FALSE
  else
    spec$optional
  new(
    "InputFile",
    type = spec$type,
    path = spec$path,
    usage = spec$usage,
    optional = optional,
    mapped = !is.null(spec$file_name),
    name = spec$name
  )
}

setMethod("show", "File", show_file)
setMethod("show", "OutputFile", show_file)
setMethod("show", "InputFile", show_file)
FASTGenomics/fastgenomicsR documentation built on June 26, 2019, 12:38 p.m.