comparison mirdeep2_render.R @ 0:963905bcb754 draft

planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_mirdeep2 commit 29e8b40899c71ca12fd07b2bb530b0ee65037588-dirty
author mingchen0919
date Tue, 08 Aug 2017 13:14:41 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:963905bcb754
1 ##======= Handle arguments from command line ========
2 # setup R error handline to go to stderr
3 options(show.error.messages=FALSE,
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 # column 1: the long flag name
24 # column 2: the short flag alias. A SINGLE character string
25 # column 3: argument mask
26 # 0: no argument
27 # 1: argument required
28 # 2: argument is optional
29 # column 4: date type to which the flag's argument shall be cast.
30 # possible values: logical, integer, double, complex, character.
31 spec_list=list()
32
33 ##------- 1. input data ---------------------
34 spec_list$ECHO = c('echo', 'e', '1', 'character')
35 spec_list$COLLASPED_READS = c('collapsed_reads', 'a', '1', 'character')
36 spec_list$REFERENCE_GENOME = c('reference_genome', 'b', '1', 'character')
37 spec_list$READS_MAPPING = c('reads_mapping', 'c', '1', 'character')
38
39 ##--------2. output report and report site directory --------------
40 spec_list$OUTPUT_HTML = c('mirdeep2_html', 'o', '1', 'character')
41 spec_list$OUTPUT_DIR = c('mirdeep2_output_dir', 'd', '1', 'character')
42 spec_list$CSV_RESULT = c('csv_result', 'r', '1', 'character')
43 spec_list$HTML_RESULT = c('html_result', 't', '1', 'character')
44 spec_list$REPORT_LOG = c('report_log', 'u', '1', 'character')
45
46 ##---------other parameters---------
47 spec_list$SPECIES_MATURE_MIRNA = c('species_mature_mirna', 'f', '2', 'character')
48 spec_list$SPECIES_RELATED_MATURE_MIRRNA = c('related_species_mature_mirna', 'g', '2', 'character')
49 spec_list$PRECURSOR_SEQUENCES = c('precursor_sequences', 'h', '2', 'character')
50 spec_list$MIN_READ_STACK_HEIGHT = c('min_read_stack_height', 'j', '2', 'character')
51 spec_list$MIN_SCORE_CUTOFF = c('min_score_cutoff', 'k', '2', 'character')
52 spec_list$RANDFOLD_ANALYSIS = c('randfold_analysis', 'l', '2', 'character')
53 spec_list$MAX_PRECURSOR_NUMBER = c('max_precursor_number', 'm', '2', 'character')
54 spec_list$SPECIES = c('species', 'n', '2', 'character')
55 spec_list$SWITCH = c('switch', 'q', '2', 'character')
56
57 ##--------3. Rmd templates sitting in the tool directory ----------
58
59 ## _site.yml and index.Rmd files
60 spec_list$SITE_YML = c('site_yml', 's', 1, 'character')
61 spec_list$INDEX_Rmd = c('index_rmd', 'i', 1, 'character')
62
63 ## other Rmd body template files
64 spec_list$MIRDEEP2_RMD = c('mirdeep2_rmd', 'p', '1', 'character')
65
66
67
68 ##------------------------------------------------------------------
69
70 spec = t(as.data.frame(spec_list))
71 opt = getopt(spec)
72 # arguments are accessed by long flag name (the first column in the spec matrix)
73 # NOT by element name in the spec_list
74 # example: opt$help, opt$expression_file
75 ##====== End of arguments handling ==========
76
77 #------ Load libraries ---------
78 library(rmarkdown)
79 library(plyr)
80 # library(stringr)
81 library(dplyr)
82 # library(highcharter)
83 # library(DT)
84 # library(reshape2)
85 # library(plotly)
86 # library(formattable)
87 library(htmltools)
88
89
90 #----- 1. create the report directory ------------------------
91 paste0('mkdir -p ', opt$mirdeep2_output_dir) %>%
92 system()
93
94 #----- 2. generate Rmd files with Rmd templates --------------
95 # a. templates without placeholder variables:
96 # copy templates from tool directory to the working directory.
97 # b. templates with placeholder variables:
98 # substitute variables with user input values and place them in the working directory.
99
100
101 #----- mirdeep2.Rmd -----------------------
102 readLines(opt$mirdeep2_rmd) %>%
103 (function(x) {
104 gsub('ECHO', opt$echo, x)
105 }) %>%
106 (function(x) {
107 gsub('COLLAPSED_READS', opt$collapsed_reads, x)
108 }) %>%
109 (function(x) {
110 gsub('REFERENCE_GENOME', opt$reference_genome, x)
111 }) %>%
112 (function(x) {
113 gsub('READS_MAPPING', opt$reads_mapping, x)
114 }) %>%
115 (function(x) {
116 gsub('SPECIES_MATURE_MIRNA', opt$species_mature_mirna, x)
117 }) %>%
118 (function(x) {
119 gsub('SPECIES_RELATED_MATURE_MIRNA', opt$related_species_mature_mirna, x)
120 }) %>%
121 (function(x) {
122 gsub('PRECURSOR_SEQUENCES', opt$precursor_sequences, x)
123 }) %>%
124 (function(x) {
125 gsub('MIN_READ_STACK_HEIGHT', opt$min_read_stack_height, x)
126 }) %>%
127 (function(x) {
128 gsub('MIN_SCORE_CUTOFF', opt$min_score_cutoff, x)
129 }) %>%
130 (function(x) {
131 gsub('RANDFOLD_ANALYSIS', opt$randfold_analysis, x)
132 }) %>%
133 (function(x) {
134 gsub('MAX_PRECURSOR_NUMBER', opt$max_precursor_number, x)
135 }) %>%
136 (function(x) {
137 gsub('SPECIES', opt$species, x)
138 }) %>%
139 (function(x) {
140 gsub('SWITCH', opt$switch, x)
141 }) %>%
142 (function(x) {
143 gsub('OUTPUT_DIR', opt$mirdeep2_output_dir, x)
144 }) %>%
145 (function(x) {
146 fileConn = file('mirdeep2.Rmd')
147 writeLines(x, con=fileConn)
148 close(fileConn)
149 })
150
151
152 #------ 3. render all Rmd files with render() --------
153 render('mirdeep2.Rmd',output_file = opt$mirdeep2_html)
154
155
156 #-------4. manipulate outputs -----------------------------
157 # a. copy non-site files
158 file.copy('result.csv', opt$csv_result, recursive=TRUE)
159 file.copy('result.html', opt$html_result, recursive=TRUE)
160 file.copy('report.log', opt$report_log, recursive=TRUE)
161
162
163
164