R/run_bowtie2.R

Defines functions run_bowtie2

Documented in run_bowtie2

#' Run the Bowtie2
#'
#' @description Runs the Bowtie2 tool
#'
#' @import parallel
#'
#' @param mate1 List of the paths to files containing to the forward reads
#' @param mate2 List of the paths to files containing to the reverse reads
#' @param index Path to the reference genome index
#' @param sample.name List of the sample names
#' @param out.dir Name of the directory from the Bowtie2 output
#' @param threads Number of threads for stringtie to use, default set to 10
#' @param end2end Select presets for end to end alignments
#' @param sensitive Select the very sensitive preset
#' @param parallel Run in parallel, default set to FALSE
#' @param cores Number of cores/threads to use for parallel processing, default set to 4
#' @param bowtie2 Path to the Bowtie2 program
#' @param version Returns the version number
#'
#' @return A list with the Bowtie2 commands
#'
#' @examples
#' \dontrun{

#' path <- "/software/bowtie_v2-2.3.5.1/bowtie2"
#'
#' bowtie2.version <- run_bowtie2(bowtie2 = path,
#'                                version = TRUE)
#' bowtie2.version[1]
#'
#' out <- "bowtie2_alignments"
#' trimmed_reads_dir <- "trimmed_reads"
#'
#' mate1 <- list.files(path = trimmed_reads_dir,
#'                     pattern = "*_R1_001.fastq$",
#'                     full.names = TRUE)
#' mate2 <- list.files(path = trimmed_reads_dir,
#'                    pattern = "*_R2_001.fastq$",
#'                    full.names = TRUE)
#' sample.names <- unlist(lapply(strsplit(list.files(path = trimmed_reads_dir,
#'                                                   pattern = "*_R1_001.fastq$",
#'                                                   full.names = FALSE),"_"), `[[`, 1))
#' index <- "/export/jessie3/gmh5n/BactoCap/JoHalliday/Project1543/MLSTReference/MLST"
#'
#' bowtie2.cmds <- run_bowtie2(mate1 = mate1,
#'                             mate2 = mate2,
#'                             index = index,
#'                             out.dir = out,
#'                             sample.name = sample.names,
#'                             bowtie2 = path,
#'                             version = FALSE)
#' bowtie2.cmds
#' }
#'
#' @export
#'

run_bowtie2 <- function(mate1 = mate1,
                    mate2 = NULL,
                    index = NULL,
                    sample.name = NULL,
                    out.dir = NULL,
                    threads = 10,
                    end2end = FALSE,
                    sensitive = FALSE,
                    parallel = FALSE,
                    cores = 4,
                    bowtie2 = NULL,
                    version = FALSE){
  # Check bowtie2 program can be found
  sprintf("type -P %s &>//dev//null && echo 'Found' || echo 'Not Found'", bowtie2)

  # Version
  if (isTRUE(version)){
    bowtie2.run <- sprintf('%s --version',
                       bowtie2)
    result <- system(bowtie2.run, intern = TRUE)
    return(result)
  }

  # Create the sample directories for the per sample bowtie2 results
  lapply(paste(out.dir,sample.name, sep = "/"), function(cmd) dir.create(cmd, showWarnings = FALSE, recursive = TRUE))

  # Set the additional arguments
  args <- ""
  # End to End Alignment
  if (isTRUE(end2end)){
    args <- paste(args,"--end-to-end",sep = " ")
    }
  # Sensitive
  if (isTRUE(sensitive)){
    args <- paste(args,"--very-sensitive",sep = " ")
  }

  # Set the names for the alignment and logfiles
  log.files <- paste(out.dir,sample.name,paste(sample.name,"log",sep = "."),sep = "/")
  sam.files <- paste(out.dir,sample.name,paste(sample.name,"sam",sep = "."),sep = "/")

  # Paired end
  if(!is.null(mate2)){
    bowtie2.run <- sprintf('%s %s -x %s -S %s -1 %s -2 %s > %s 2>&1',
                           bowtie2,args,index,sam.files,mate1,mate2,log.files)
  }else{
    bowtie2.run <- sprintf('%s %s -x %s -S %s -U %s > %s 2>&1',
                           bowtie2,args,index,sam.files,mate1,log.files)
  }

  if (isTRUE(parallel)){
    cluster <- makeCluster(cores)
    parLapply(cluster, bowtie2.run, function (cmd)  system(cmd))
    stopCluster(cluster)
  }else{
    lapply(bowtie2.run, function (cmd)  system(cmd))
  }

  return(bowtie2.run)
}
GrahamHamilton/pipelineTools documentation built on March 26, 2021, 5:40 p.m.