view fastqc_site_render.R @ 3:54a93db1a101 draft

planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit 9285c2b8ad41a486dde2a87600a6b8267841c8b5-dirty
author mingchen0919
date Tue, 08 Aug 2017 11:29:56 -0400
parents 58f3c3128fdd
children
line wrap: on
line source

##======= Handle arguments from command line ========
# setup R error handline to go to stderr
options(show.error.messages=FALSE,
        error=function(){
          cat(geterrmessage(), file=stderr())
          quit("no", 1, F)
        })

# we need that to not crash galaxy with an UTF8 error on German LC settings.
loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")

# suppress warning
options(warn = -1)

options(stringsAsFactors=FALSE, useFancyQuotes=FALSE)
args = commandArgs(trailingOnly=TRUE)

suppressPackageStartupMessages({
  library(getopt)
  library(tools)
})

# column 1: the long flag name
# column 2: the short flag alias. A SINGLE character string
# column 3: argument mask
#           0: no argument
#           1: argument required
#           2: argument is optional
# column 4: date type to which the flag's argument shall be cast.
#           possible values: logical, integer, double, complex, character.
spec_list=list()

##------- 1. input data ---------------------
spec_list$READS = c('reads', 'r', '1', 'character')
spec_list$ECHO = c('echo', 'e', '1', 'character')

##--------2. output report and report site directory --------------
spec_list$FASTQC_SITE = c('fastqc_site', 'o', '1', 'character')
spec_list$FASTQC_SITE_DIR = c('fastqc_site_dir', 'd', '1', 'character')

##--------3. Rmd templates sitting in the tool directory ----------

    ## _site.yml and index.Rmd files
    spec_list$SITE_YML = c('site_yml', 's', 1, 'character')
    spec_list$INDEX_Rmd = c('index_rmd', 'i', 1, 'character')
    
    ## other Rmd body template files
    spec_list$x01 = c('x01_evaluation_overview', 'p', '1', 'character')
    spec_list$x02 = c('x02_fastqc_original_reports', 'a', '1', 'character')
    spec_list$x1 = c('x1_per_base_quality_scores', 'b', '1', 'character')
    spec_list$x2 = c('x2_per_base_N_content', 'c', '1', 'character')
    spec_list$x3 = c('x3_per_sequence_quality_scores', 'f', '1', 'character')
    spec_list$x4 = c('x4_per_sequence_GC_content', 'g', '1', 'character')
    spec_list$x5 = c('x5_per_base_sequence_content', 'h', '1', 'character')

##------------------------------------------------------------------

spec = t(as.data.frame(spec_list))
opt = getopt(spec)
# arguments are accessed by long flag name (the first column in the spec matrix)
#                        NOT by element name in the spec_list
# example: opt$help, opt$expression_file
##====== End of arguments handling ==========

#------ Load libraries ---------
library(rmarkdown)
library(plyr)
library(stringr)
library(dplyr)
library(highcharter)
library(DT)
library(reshape2)
library(plotly)
library(formattable)
library(htmltools)


#----- 1. create the report directory ------------------------
paste0('mkdir -p ', opt$fastqc_site_dir) %>%
  system()

#----- 2. generate Rmd files with Rmd templates --------------
#   a. templates without placeholder variables:
#         copy templates from tool directory to the working directory.
#   b. templates with placeholder variables:
#         substitute variables with user input values and place them in the working directory.


    #----- Copy index.Rmd and _site.yml files to job working direcotry -----
    file.copy(opt$index_rmd, 'index.Rmd', recursive=TRUE)
    file.copy(opt$site_yml, '_site.yml', recursive=TRUE)
    #---------------------------------------------------------
    
    #----- 01_evaluation_overview.Rmd -----------------------
    readLines(opt$x01_evaluation_overview) %>%
      (function(x) {
        gsub('ECHO', opt$echo, x)
      }) %>%
      (function(x) {
        gsub('READS', opt$reads, x)
      }) %>%
      (function(x) {
        gsub('REPORT_OUTPUT_DIR', opt$fastqc_site_dir, x)
      }) %>%
      (function(x) {
        fileConn = file('01_evaluation_overview.Rmd')
        writeLines(x, con=fileConn)
        close(fileConn)
      })
    
    #----- 1_per_base_quality_scores.Rmd --------------------
    readLines(opt$x1_per_base_quality_scores) %>%
      (function(x) {
        gsub('ECHO', opt$echo, x)
      }) %>%
      (function(x) {
        fileConn = file('1_per_base_quality_scores.Rmd')
        writeLines(x, con=fileConn)
        close(fileConn)
      })
    
    #----- 2_per_base_N_content.Rmd -------------------------
    readLines(opt$x2_per_base_N_content) %>%
      (function(x) {
        gsub('ECHO', opt$echo, x)
      }) %>%
      (function(x) {
        fileConn = file('2_per_base_N_content.Rmd')
        writeLines(x, con=fileConn)
        close(fileConn)
      })
    
    #----- 3_per_sequence_quality_scores.Rmd ----------------
    readLines(opt$x3_per_sequence_quality_scores) %>%
      (function(x) {
        gsub('ECHO', opt$echo, x)
      }) %>%
      (function(x) {
        fileConn = file('3_per_sequence_quality_scores.Rmd')
        writeLines(x, con=fileConn)
        close(fileConn)
      })
    
    
    #----- 4_per_sequence_GC_content.Rmd --------------------
    readLines(opt$x4_per_sequence_GC_content) %>%
      (function(x) {
        gsub('ECHO', opt$echo, x)
      }) %>%
      (function(x) {
        fileConn = file('4_per_sequence_GC_content.Rmd')
        writeLines(x, con=fileConn)
        close(fileConn)
      })
    
    
    #----- 5_per_base_sequence_content.Rmd ------------------
    readLines(opt$x5_per_base_sequence_content) %>%
      (function(x) {
        gsub('ECHO', opt$echo, x)
      }) %>%
      (function(x) {
        fileConn = file('5_per_base_sequence_content.Rmd')
        writeLines(x, con=fileConn)
        close(fileConn)
      })

    #----- 02_fastqc_original_reports.Rmd -------------------
    readLines(opt$x02_fastqc_original_reports) %>%
      (function(x) {
        gsub('ECHO', opt$echo, x)
      }) %>%
      (function(x) {
        gsub('REPORT_OUTPUT_DIR', opt$fastqc_site_dir, x)
      }) %>%
      (function(x) {
        fileConn = file('02_fastqc_original_reports.Rmd')
        writeLines(x, con=fileConn)
        close(fileConn)
      })



#------ 3. render all Rmd files with render_site() --------
render_site()    


#-------4. manipulate outputs -----------------------------
#   a. copy index.html to the report output path
#   b. copy all files in 'my_site' to the report output directory
file.copy('my_site/index.html', opt$fastqc_site, recursive=TRUE)
paste0('cp -r my_site/* ', opt$fastqc_site_dir) %>%
  system()