R/circexplorer2.R

Defines functions CIRCexplorer2

Documented in CIRCexplorer2

#' @title Running CIRCexplorer2 parse command for circRNAs prediction from STAR or BWA alignment output
#' @description This function executes the circexplorer2 docker container which parses the fusion file generated by STAR or BWA and extracts circRNA coordinates
#' @param group, a string. Two options: \code{"sudo"} or \code{"docker"}, depending to which group the user belongs
#' @param scratch.folder, a string indicating the scratch folder where docker container will be mounted
#' @param data.folder, a string indicating the path of the output folder
#' @param fusion.file, a string indicating the path of the fusion file. If the used aligner was BWA, the fusion file is the resulting sam file, whereas the used aligner was STAR, the fusion file is the Chimeric.out.junction file
#' @param used.aligner, a string indicating the aligner used to generate the junctions.file. Supported aligners are STAR and BWA
#' @export 

CIRCexplorer2 <- function(group = c("sudo", "docker"), scratch.folder, data.folder, fusion.file, used.aligner = c("STAR", "BWA")) {
    # running time 1
    ptm <- proc.time() 

    scratch.folder <- normalizePath(scratch.folder)
    data.folder <- normalizePath(data.folder)
    fusion.file <- normalizePath(fusion.file)

    if (!file.exists(data.folder)) {
      cat(paste("\nIt seems that", data.folder, "file does not exist\n"))
      system("echo 3 > ExitStatusFile 2>&1")
      return(3)
    }

    #storing the position of the home folder
    home <- getwd()
    setwd(data.folder)
    #initialize status
    system("echo 0 > ExitStatusFile 2>&1")


    used.aligner <- toupper(used.aligner)

    if (!is.atomic(used.aligner) || !length(used.aligner) || !(used.aligner %in% c("STAR", "BWA"))) {
      cat(paste("\nThe parameter used.aligner has to be a scalar value. Admissible values are \"STAR\" or \"BWA\""))
      system("echo 4 > ExitStatusFile 2>&1")
      setwd(data.folder)
      return(4)
    }
    
    #check  if scratch folder exist
    if (!file.exists(scratch.folder)) {
      cat(paste("\nIt seems that", scratch.folder, "folder does not exist\n"))
      system("echo 3 > ExitStatusFile 2>&1")
      setwd(data.folder)
      return(3)
    }
    
    #check if input file exist
    if (!file.exists(fusion.file)) {
      cat(paste("\nIt seems that", fusion.file, "file does not exist\n"))
      system("echo 3 > ExitStatusFile 2>&1")
      setwd(data.folder)
      return(3)
    }
    
    output.filename <- tools::file_path_sans_ext(basename(fusion.file))

    params <- paste(
      "--cidfile", paste0(data.folder, "/dockerID"),
      "-v", paste0(fusion.file, ":/data/in/junctions"), 
      "-v", paste0(data.folder, ":/data/out"), 
      "-v", paste0(scratch.folder, ":/scratch"), 
      "-d", "docker.io/repbioinfo/circexplorer2", 
      "parse", 
      "-t", used.aligner, 
      "-b", paste0(output.filename, ".circexplorer2")
    )
    resultRun <- runDocker(group=group, params=params)
    
    if (resultRun == 0) {
      cat("CIRCexplorer2 terminated successfully.\n")
    } else {
      cat("CIRCexplorer2 terminated with errors.\n")
    }
    
    #running time 2
    ptm <- proc.time() - ptm
    dir <- dir(data.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("user run time mins ",ptm[1]/60, sep="")
      tmp.run[length(tmp.run)+1] <- paste("system run time mins ",ptm[2]/60, sep="")
      tmp.run[length(tmp.run)+1] <- paste("elapsed run time mins ",ptm[3]/60, sep="")
      writeLines(tmp.run,"run.info")
    } else {
      tmp.run <- NULL
      tmp.run[1] <- paste("run time mins ",ptm[1]/60, sep="")
      tmp.run[length(tmp.run)+1] <- paste("system run time mins ",ptm[2]/60, sep="")
      tmp.run[length(tmp.run)+1] <- paste("elapsed run time mins ",ptm[3]/60, sep="")
      
      writeLines(tmp.run,"run.info")
    }
    
    #saving log and removing docker container
    container.id <- readLines(paste(data.folder,"/dockerID", sep=""), warn = FALSE)
    system(paste0("docker logs ", substr(container.id,1,12), " &> ",data.folder,"/", "CIRCexplorer2", substr(container.id,1,12),".log"))
    system(paste("docker rm", container.id))
    # removing temporary files
    cat("\n\nRemoving the temporary file ....\n")
    system("rm -fR out.info")
    system("rm -fR dockerID")
    system(paste("cp ",paste(path.package(package="docker4seq"),"containers/containers.txt",sep="/")," ",data.folder, sep=""))
    setwd(home)
}
kendomaniac/docker4seq documentation built on April 29, 2024, 9:16 a.m.