R/wrapperDeseq2.R

Defines functions wrapperDeseq2

Documented in wrapperDeseq2

#' @title A wrapper function for deseq2 for two groups only
#' @description This function runs deseq2 on a table genrated with sample2experiment having the covariates added in the names of the columns, separated by the names with underscore.
#' @param group, a character string. Two options: \code{"sudo"} or \code{"docker"}, depending to which group the user belongs
#' @param output.folder, a character string indicating where the tables generated by samples2experiment are located and were results will be placed
#' @param experiment.table, a character string indicating the counts table generated with sample3experiment with addition of covariates
#' @param log2fc, log2fc threshold for differetially expressed genes
#' @param fdr, fdr threshold
#' @param ref.covar, covariate to be used as reference
#' @param type, character with three options: gene, isoform, mirna. if gene is used two files are generated for geneset enrichment, the filtered Gene symbols and the background that contains all gene simbols.
#' @param batch, logical FALSE, TRUE
#' @author Raffaele Calogero
#'
#' @return Returns a full table of differentially expressed genes (prefix DEfull), a filtered table of differentially expressed genes (prefix DEfiltered) and the normalized counts table (prefix normalized)

#' @examples
#'\dontrun{
#'     system("wget 130.192.119.59/public/test.analysis.zip")
#'     unzip("test.analysis.zip")
#'     setwd("test.analysis")
#'     library(docker4seq)
#'     wrapperDeseq2(output.folder=getwd(), group="docker", experiment.table="_counts.txt", log2fc=1,
#'     fdr=0.1, ref.covar="Cov.1", type="gene", batch=FALSE)
#'
#' }
#' @export
wrapperDeseq2 <- function(output.folder, group=c("sudo","docker"), experiment.table, log2fc=1, fdr=0.1, ref.covar="0", type=c("gene","isoform","mirna"), batch=FALSE){

  home <- getwd()
  output.folder <- normalizePath(output.folder)
  setwd(output.folder)

  #initialize status
  system("echo 0 > ExitStatusFile 2>&1")

  #running time 1
  ptm <- proc.time()
  #running time 1
  test <- dockerTest()
  if(!test){
    cat("\nERROR: Docker seems not to be installed in your system\n")
    system("echo 10 > ExitStatusFile 2>&1")
    setwd(home)
    return(10)
  }

  #removing the path from filename
  filename.tmp <- unlist(strsplit(experiment.table,'/'))
  experiment.table <-  filename.tmp[length(filename.tmp)]


  params <- paste("--cidfile ",output.folder, "/dockerID -v ",output.folder,":/data/scratch -d docker.io/repbioinfo/r332.2017.01 Rscript /bin/.wrapperDeseq2.R ", experiment.table, " ", log2fc, " ", fdr, " ", ref.covar, " ", type, " ", batch, sep="")
  resultRun <- runDocker(group=group, params=params)

  if(resultRun==0){
    cat("\nDESeq2 analysis is finished\n")
  }

  home <- getwd()
  setwd(output.folder)

  dir.out <- dir()
  if(type=="gene"){
    de.out0 <- dir.out[grep("DEfiltered", dir.out)]
    de.out1 <- sub("DEfiltered", "DEfiltered_gene", de.out0)
    system(paste("mv ", de.out0, " ", de.out1, sep=""))
    system("mv genes4david.txt genes2GO.txt")
    system("mv bkg4david.txt bkg2GO.txt")
  }else if(type=="isoform"){
    de.out0 <- dir.out[grep("DEfiltered", dir.out)]
    de.out1 <- sub("DEfiltered", "DEfiltered_isoform", de.out0)
    system(paste("mv ", de.out0, " ", de.out1, sep=""))
  }else if(type=="mirna"){
    de.out0 <- dir.out[grep("DEfiltered", dir.out)]
    de.out1 <- sub("DEfiltered", "DEfiltered_mirna", de.out0)
    system(paste("mv ", de.out0, " ", de.out1, sep=""))
  }


  #running time 2
  ptm <- proc.time() - ptm
  dir <- dir(output.folder)
  dir <- dir[grep("run.info",dir)]
  if(length(dir)>0){
    con <- file("run.info", "r")
    tmp.run <- readLines(con)
    close(con)
    tmp.run[length(tmp.run)+1] <- paste("DESeq2 user run time mins ",ptm[1]/60, sep="")
    tmp.run[length(tmp.run)+1] <- paste("DESeq2 system run time mins ",ptm[2]/60, sep="")
    tmp.run[length(tmp.run)+1] <- paste("DESeq2 elapsed run time mins ",ptm[3]/60, sep="")
    writeLines(tmp.run,"run.info")
  }else{
    tmp.run <- NULL
    tmp.run[1] <- paste("DESeq2 user run time mins ",ptm[1]/60, sep="")
    tmp.run[length(tmp.run)+1] <- paste("DESeq2 system run time mins ",ptm[2]/60, sep="")
    tmp.run[length(tmp.run)+1] <- paste("DESeq2 elapsed run time mins ",ptm[3]/60, sep="")

    writeLines(tmp.run,"run.info")
  }

  #saving log and removing docker container
  container.id <- readLines(paste(output.folder,"/dockerID", sep=""), warn = FALSE)
#  system(paste("docker logs ", container.id, " >& ", substr(container.id,1,12),".log", sep=""))
  system(paste("docker logs ", container.id, " >& ","wrapperDeseq2_",substr(container.id,1,12),".log", sep=""))
 # system(paste("docker rm ", container.id, sep=""))
  system("rm -fR anno.info")
  system("rm -fR dockerID")
  system(paste("cp ",paste(path.package(package="docker4seq"),"containers/containers.txt",sep="/")," ",output.folder, sep=""))

  setwd(home)
}
kendomaniac/docker4seq documentation built on Oct. 16, 2023, 2:30 a.m.