R/salmonAnnotation.R

Defines functions salmonAnnotation

Documented in salmonAnnotation

#' @title A function to annotate salmon output
#' @description This function executes a docker that convert the transcripts count file generated by Salmon quasi-alignment in the same format of isoforms.result, geness.result of  RSEM and add the ENSEMBL GTF based annotation
#' @param group, a character string. Two options: sudo or docker, depending to which group the user belongs
#' @param fastq.folder, a character string indicating the folder where input data are located and where output will be written, it should contain quant.sf file generated by salmonCounts
#' @param index.folder, a character string indicating the folder where Salmon transcriptome index was created with salmonIndex.
#' @author Raffaele Calogero, raffaele.calogero [at] unito [dot] it, Bioinformatics and Genomics unit, University of Torino Italy
#'
#' @examples
#' \dontrun{
#' system("wget http://130.192.119.59/public/test_R1.fastq.gz")
#' system("wget http://130.192.119.59/public/test_R2.fastq.gz")
#' library(docker4seq)
#' #running salmonCounts
#' library(docker4seq)
#' wrapperSalmon(group="docker", scratch.folder="/data/scratch/",
#'               fastq.folder=getwd(), index.folder="/data/genomes/hg38salmon",
#'               threads=8, seq.type="pe", adapter5="AGATCGGAAGAGCACACGTCTGAACTCCAGTCA",
#'               adapter3="AGATCGGAAGAGCGTCGTGTAGGGAAAGAGTGT", min.length=40, strandness="none")

#' #converting in a format identical to rsem isoform.results
#' salmonAnnotation(group="docker", fastq.folder=getwd(),
#'                  index.folder="/data/genomes/hg38salmon")
#'
#' }
#'
#' @export
salmonAnnotation <- function(group=c("sudo","docker"), fastq.folder, index.folder){
  home <- getwd()
  #running time 1
  ptm <- proc.time()
  #setting the data.folder as working folder
  if (!file.exists(fastq.folder)){
    cat(paste("\nIt seems that the ",fastq.folder, " folder does not exist\n"))
    return(2)
  }
  setwd(fastq.folder)
  
  #initialize status
  system("echo 0 > ExitStatusFile 2>&1")
  
  params <- paste("--cidfile ",fastq.folder,"/dockerID -v ",fastq.folder,":/data/scratch -v ",index.folder,":/index -d docker.io/repbioinfo/r340.2017.01 Rscript /bin/annotate.salmon.R", sep="")
  resultRun <- runDocker(group=group, params=params)


  #waiting for the end of the container work
  if(resultRun==0){
    cat("\nSalmon output annotation is finished\n")

    #saving log and removing docker container
    container.id <- readLines(paste(fastq.folder,"/dockerID", sep=""), warn = FALSE)
    system(paste("docker logs ", substr(container.id,1,12), " &> ","salmonTranscriptsAnnotation_",substr(container.id,1,12),".log", sep=""))
    system(paste("docker rm ", container.id, sep=""))
    #removing temporary folder
    cat("\n\nRemoving the temporary file ....\n")
    system("rm -fR dockerID")
    system("rm  -fR tempFolderID")
#    system(paste("cp ",paste(path.package(package="docker4seq"),"containers/containers.txt",sep="/")," ",fastq.folder, sep=""))
  }

#gene level conversion

   params <- paste("--cidfile ",fastq.folder,"/dockerID -v ",fastq.folder,":/data/scratch -v ",index.folder,":/index -d docker.io/repbioinfo/r340.2017.01 Rscript /bin/iso2gene.R", sep="")
   resultRun <- runDocker(group=group, params=params)
 

  #waiting for the end of the container work
  if(resultRun==0){
    cat("\nSalmon output gene level conversion is finished\n")

    #saving log and removing docker container
    container.id <- readLines(paste(fastq.folder,"/dockerID", sep=""), warn = FALSE)
    system(paste("docker logs ", substr(container.id,1,12), " &> ","salmonGeneConversion_",substr(container.id,1,12),".log", sep=""))
    system(paste("docker rm ", container.id, sep=""))
    #removing temporary folder
    cat("\n\nRemoving the temporary file ....\n")
    system("rm -fR dockerID")
    system("rm  -fR tempFolderID")
    #    system(paste("cp ",paste(path.package(package="docker4seq"),"containers/containers.txt",sep="/")," ",fastq.folder, sep=""))
  }

  #ensembl gtf annotation at gene level
    params <- paste("--cidfile ",fastq.folder,"/dockerID -v ",fastq.folder,":/data/scratch -v ",index.folder,":/index -d docker.io/repbioinfo/r340.2017.01 Rscript /bin/salmon.annoByGtf.R", sep="")
    resultRun <- runDocker(group=group, params=params)
  

  #waiting for the end of the container work
  if(resultRun==0){
    cat("\nSalmon output ensembl gtf annotation at gene level is finished\n")

    #saving log and removing docker container
    container.id <- readLines(paste(fastq.folder,"/dockerID", sep=""), warn = FALSE)
    system(paste("docker logs ", substr(container.id,1,12), " &> ","salmonGeneAnnotation_",substr(container.id,1,12),".log", sep=""))
    system(paste("docker rm ", container.id, sep=""))
    #removing temporary folder
    cat("\n\nRemoving the temporary file ....\n")
    system("rm -fR dockerID")
    system("rm  -fR tempFolderID")
    system(paste("cp ",paste(path.package(package="docker4seq"),"containers/containers.txt",sep="/")," ",fastq.folder, sep=""))
  }


  #running time 2
  ptm <- proc.time() - ptm
  dir <- dir(fastq.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")
  }


  setwd(home)



}
kendomaniac/docker4seq documentation built on April 29, 2024, 9:16 a.m.