diff fastqc_report_render.R @ 14:2efa46ce2c4c draft

upgrade fastqc_report
author mingchen0919
date Wed, 18 Oct 2017 22:06:39 -0400
parents 22cd2369354b
children d1d20f341632
line wrap: on
line diff
--- a/fastqc_report_render.R	Mon Oct 16 21:33:31 2017 -0400
+++ b/fastqc_report_render.R	Wed Oct 18 22:06:39 2017 -0400
@@ -1,87 +1,88 @@
-##======= 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)
+library(getopt)
+library(rmarkdown)
+library(htmltools)
+library(plyr)
+library(dplyr)
+library(stringr)
+library(highcharter)
+library(DT)
+library(reshape2)
+library(plotly)
+library(formattable)
 
-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()
-spec_list$READS = c('reads', 'r', '1', 'character')
-spec_list$ECHO = c('echo', 'e', '1', 'character')
-spec_list$FASTQC_TPL = c('fastqc_tpl', 'p', 1, 'character')
-spec_list$REPORT = c('report', 'o', '1', 'character')
-spec_list$REPORT_OUTPUT_DIR = c('report_output_dir', 'd', '1', 'character')
+##============ Sink warnings and errors to a file ==============
+## use the sink() function to wrap all code within it.
+##==============================================================
+zz = file('warnings_and_errors.txt')
+sink(zz)
+sink(zz, type = 'message')
+  ##---------below is the code for rendering .Rmd templates-----
+  
+  ##=============STEP 1: handle command line arguments==========
+  ##
+  ##============================================================
+  # 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.
+  #-------------------------------------------------------------
+  #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++
+  # 1. short flag alias should match the flag in the command section in the XML file.
+  # 2. long flag name can be any legal R variable names
+  # 3. two names in args_list can have common string but one name should not be a part of another name.
+  #    for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems.
+  #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  args_list=list()
+  ##------- 1. input data ---------------------
+  args_list$ECHO = c('echo', 'e', '1', 'character')
+  args_list$READS = c('reads', 'r', '1', 'character')
+  ##--------2. output report and outputs --------------
+  args_list$REPORT_HTML = c('report_html', 'r', '1', 'character')
+  args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character')
+  args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character')
+  ##--------3. .Rmd templates in the tool directory ----------
+  args_list$FASTQC_REPORT_RMD = c('fastqc_report_rmd', 't', '1', 'character')
+  ##-----------------------------------------------------------
+  opt = getopt(t(as.data.frame(args_list)))
 
 
-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 ==========
-
+  
+  ##=======STEP 2: create report directory (optional)==========
+  ##
+  ##===========================================================
+  dir.create(opt$report_dir)
+  
+  ##=STEP 3: replace placeholders in .Rmd with argument values=
+  ##
+  ##===========================================================
+  #++ need to replace placeholders with args values one by one+
+  readLines(opt$fastqc_report_rmd) %>%
+    (function(x) {
+      gsub('ECHO', opt$echo, x)
+    }) %>%
+    (function(x) {
+      gsub('READS', opt$reads, x)
+    }) %>%
+    (function(x) {
+      gsub('REPORT_DIR', opt$output_dir, x)
+    }) %>%
+    (function(x) {
+      fileConn = file('fastqc_report.Rmd')
+      writeLines(x, con=fileConn)
+      close(fileConn)
+    })
+  
 
-mgsub = function(pattern, replacement, x) {
-  if(length(pattern) != length(replacement) ) {
-    stop("pattern and replacement have to be the same in length")
-  }
-  
-  result = x
-  
-  for(i in 1:length(pattern)) {
-    result = try( gsub(pattern[i], replacement[i], x = result) )
-  }
-  
-  result
-}
+  ##=============STEP 4: render .Rmd templates=================
+  ##
+  ##===========================================================
+  render('fastqc_report.Rmd', output_file = opt$report_html)
 
 
-##====== replace variables in tpl file ======
-p = c('READS', 
-      'ECHO',
-      'FASTQC_TPL',
-      'REPORT_OUTPUT_DIR',
-      'REPORT')
-r = c(opt$reads,
-      opt$echo,
-      opt$fastqc_tpl,
-      opt$report_output_dir,
-      opt$report)
-
-fastqc_report_tpl = mgsub(p, r, readLines(opt$fastqc_tpl))
-
-##====== write replaced text into Rmd file ===
-fileConn = file('fastqc_report.Rmd')
-writeLines(fastqc_report_tpl, con=fileConn)
-close(fileConn)
-
-##====== render Rmd files ====================
-rmarkdown::render('fastqc_report.Rmd')
-file.copy('fastqc_report.html', opt$report, recursive=TRUE)
-paste0('cp -r ./* ', opt$report_output_dir) %>%
-  system()
-
+  ##--------end of code rendering .Rmd templates----------------
+sink()
+##=========== End of sinking output=============================
\ No newline at end of file