Mercurial > repos > mingchen0919 > rmarkdown_bdss_client
comparison bdss_client_sra_pe_render.R @ 17:246cb7315673 draft
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_bdss_client commit 65f2ce692f3c46ccea55ee47f3c2cb2f8a2bb36f-dirty
author | mingchen0919 |
---|---|
date | Sat, 14 Oct 2017 12:12:10 -0400 |
parents | 206c59e498e7 |
children | 531c00a2acbf |
comparison
equal
deleted
inserted
replaced
16:c2150a0f9c7c | 17:246cb7315673 |
---|---|
1 ##======= Handle arguments from command line ======== | 1 ##============ Sink warnings and errors to a file ============== |
2 # setup R error handline to go to stderr | 2 ## use the sink() function to wrap all code within it. |
3 options(show.error.messages=FALSE, | 3 ##============================================================== |
4 error=function(){ | |
5 cat(geterrmessage(), file=stderr()) | |
6 quit("no", 1, F) | |
7 }) | |
8 | |
9 # we need that to not crash galaxy with an UTF8 error on German LC settings. | |
10 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | |
11 | |
12 # suppress warning | |
13 options(warn = -1) | |
14 | |
15 options(stringsAsFactors=FALSE, useFancyQuotes=FALSE) | |
16 args = commandArgs(trailingOnly=TRUE) | |
17 | |
18 suppressPackageStartupMessages({ | |
19 library(getopt) | |
20 library(tools) | |
21 }) | |
22 | |
23 #/////////////////////// SINK WARNINGS AND ERRORS TO A FILE FOR DEBUGGING /////////// | |
24 zz = file('warnings_and_errors.txt') | 4 zz = file('warnings_and_errors.txt') |
25 sink(zz) | 5 sink(zz) |
26 sink(zz, type = 'message') | 6 sink(zz, type = 'message') |
7 ##---------below is the code for rendering .Rmd templates----- | |
27 | 8 |
28 # column 1: the long flag name | 9 ##=============STEP 1: handle command line arguments========== |
29 # column 2: the short flag alias. A SINGLE character string | 10 ## |
30 # column 3: argument mask | 11 ##============================================================ |
31 # 0: no argument | 12 # column 1: the long flag name |
32 # 1: argument required | 13 # column 2: the short flag alias. A SINGLE character string |
33 # 2: argument is optional | 14 # column 3: argument mask |
34 # column 4: date type to which the flag's argument shall be cast. | 15 # 0: no argument |
35 # possible values: logical, integer, double, complex, character. | 16 # 1: argument required |
36 ##------- 1. input data --------------------- | 17 # 2: argument is optional |
37 spec_list=list() | 18 # column 4: date type to which the flag's argument shall be cast. |
38 spec_list$SRA_ACCESSION = c('sra_accession', 'i', '1', 'character') | 19 # possible values: logical, integer, double, complex, character. |
39 spec_list$FORMAT = c('format', 'f', '1', 'character') | 20 #------------------------------------------------------------- |
40 spec_list$SRA_BOOLEAN = c('sra_boolean', 'S', '1', 'character') | 21 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++ |
41 spec_list$ECHO = c('echo', 'e', '1', 'character') | 22 # 1. short flag alias should match the flag in the command section in the XML file. |
42 ##--------2. output report and outputs -------------- | 23 # 2. long flag name can be any legal R variable names |
43 spec_list$REPORT_HTML = c('report_html', 'r', '1', 'character') | 24 # 3. two names in args_list can have common string but one name should not be a part of another name. |
44 spec_list$OUTPUT_DIR = c('output_dir', 'd', '1', 'character') | 25 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems. |
45 spec_list$SINK_OUTPUT = c('sink_message', 's', '1', 'character') | 26 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
46 ##--------3. Rmd templates in the tool directory ---------- | 27 ##------- 1. input data --------------------- |
47 spec_list$FASTQ_DUMP_PE_RMD = c('fastq_dump_pe_rmd', 't', '1', 'character') | 28 args_list=list() |
48 | 29 args_list$SRA_ACCESSION = c('sra_accession', 'i', '1', 'character') |
49 spec = t(as.data.frame(spec_list)) | 30 args_list$FORMAT = c('format', 'f', '1', 'character') |
50 opt = getopt(spec) | 31 args_list$SRA_BOOLEAN = c('sra_boolean', 'S', '1', 'character') |
51 | 32 args_list$ECHO = c('echo', 'e', '1', 'character') |
52 #------ Load libraries --------- | 33 ##--------2. output report and outputs -------------- |
53 library(rmarkdown) | 34 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character') |
54 library(htmltools) | 35 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character') |
55 library(dplyr) | 36 args_list$SINK_OUTPUT = c('sink_message', 's', '1', 'character') |
56 library(RCurl) | 37 ##--------3. Rmd templates in the tool directory ---------- |
57 | 38 args_list$bdss_client_sra_se_RMD = c('bdss_client_sra_se_rmd', 't', '1', 'character') |
58 #----- 1. create the report directory ------------------------ | 39 |
59 dir.create(opt$output_dir) | 40 opt = getopt(t(as.data.frame(args_list))) |
60 | 41 |
61 #----- 2. generate Rmd files with Rmd templates -------------- | 42 ##=============STEP 2: loading libraries===================== |
62 # a. templates without placeholder variables: | 43 ## |
63 # copy templates from tool directory to the working directory. | 44 ##=========================================================== |
64 # b. templates with placeholder variables: | 45 library(rmarkdown) |
65 # substitute variables with user input values and place them in the working directory. | 46 library(htmltools) |
66 | 47 library(dplyr) |
67 #----- 01 fastq_dump_pe.Rmd ----------------------- | 48 library(RCurl) |
68 readLines(opt$fastq_dump_pe_rmd) %>% | 49 |
69 (function(x) { | 50 ##=======STEP 3: create report directory (optional)========== |
70 gsub('SRA_ACCESSION', opt$sra_accession, x) | 51 ## |
71 }) %>% | 52 ##=========================================================== |
72 (function(x) { | 53 dir.create(opt$report_dir) |
73 gsub('FORMAT', opt$format, x) | 54 |
74 }) %>% | 55 ##=STEP 4: replace placeholders in .Rmd with argument values= |
75 (function(x) { | 56 ## |
76 gsub('SRA_BOOLEAN', opt$sra_boolean, x) | 57 ##=========================================================== |
77 }) %>% | 58 #++ need to replace placeholders with args values one by one+ |
78 (function(x) { | 59 #----- 01 bdss_client_sra_se.Rmd ----------------------- |
79 gsub('ECHO', opt$echo, x) | 60 readLines(opt$bdss_client_sra_se_rmd) %>% |
80 }) %>% | 61 (function(x) { |
81 (function(x) { | 62 gsub('SRA_ACCESSION', opt$sra_accession, x) |
82 gsub('OUTPUT_DIR', opt$output_dir, x) | 63 }) %>% |
83 }) %>% | 64 (function(x) { |
84 (function(x) { | 65 gsub('FORMAT', opt$format, x) |
85 fileConn = file('fastq_dump_pe.Rmd') | 66 }) %>% |
86 writeLines(x, con=fileConn) | 67 (function(x) { |
87 close(fileConn) | 68 gsub('SRA_BOOLEAN', opt$sra_boolean, x) |
88 }) | 69 }) %>% |
89 | 70 (function(x) { |
90 #------ 3. render all Rmd files -------- | 71 gsub('ECHO', opt$echo, x) |
91 render('fastq_dump_pe.Rmd', output_file = opt$report_html) | 72 }) %>% |
73 (function(x) { | |
74 gsub('REPORT_DIR', opt$report_dir, x) | |
75 }) %>% | |
76 (function(x) { | |
77 fileConn = file('bdss_client_sra_se.Rmd') | |
78 writeLines(x, con=fileConn) | |
79 close(fileConn) | |
80 }) | |
81 | |
82 ##=============STEP 5: render .Rmd templates================= | |
83 ## | |
84 ##=========================================================== | |
85 render('bdss_client_sra_se.Rmd', output_file = opt$report_html) | |
92 | 86 |
93 | 87 |
94 #-------4. manipulate outputs ----------------------------- | 88 ##--------end of code rendering .Rmd templates---------------- |
95 | |
96 | |
97 | |
98 | |
99 | |
100 sink() | 89 sink() |
101 #/////////// END OF SINK OUTPUT /////////////////////////// | 90 ##=========== End of sinking output============================= |