annotate rmarkdown_feature_counts_render.R @ 3:40704501d8c4 draft default tip

update
author mingchen0919
date Fri, 29 Dec 2017 22:17:46 -0500
parents 5af86972b408
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
1 ##============ Sink warnings and errors to a file ==============
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
2 ## use the sink() function to wrap all code within it.
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
3 ##==============================================================
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
4 zz = file('warnings_and_errors.txt')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
5 sink(zz)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
6 sink(zz, type = 'message')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
7
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
8 ##============== load packages ===============================
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
9 library(getopt)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
10 library(rmarkdown)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
11 library(htmltools)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
12 library(dplyr)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
13 library(Rsubread)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
14 library(DT)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
15 ##============================================================
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
16
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
17 ##---------below is the code for rendering .Rmd templates-----
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
18
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
19 ##=============STEP 1: handle command line arguments==========
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
20 ##
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
21 ##============================================================
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
22 # column 1: the long flag name
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
23 # column 2: the short flag alias. A SINGLE character string
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
24 # column 3: argument mask
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
25 # 0: no argument
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
26 # 1: argument required
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
27 # 2: argument is optional
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
28 # column 4: date type to which the flag's argument shall be cast.
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
29 # possible values: logical, integer, double, complex, character.
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
30 #-------------------------------------------------------------
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
31 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
32 # 1. short flag alias should match the flag in the command section in the XML file.
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
33 # 2. long flag name can be any legal R variable names
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
34 # 3. two names in args_list can have common string but one name should not be a part of another name.
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
35 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems.
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
36 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
37 args_list=list()
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
38 ##------- 1. input data ---------------------
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
39 args_list$e = c('echo', 'e', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
40 args_list$a = c('input_bam_paths', 'a', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
41 args_list$N = c('input_bam_names', 'N', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
42 args_list$b = c('annot_inbuilt', 'b', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
43 args_list$c = c('annot_ext', 'c', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
44 args_list$f = c('isGTFAnnotationFile', 'f', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
45 args_list$g = c('gtf_feature_type', 'g', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
46 args_list$h = c('gtf_attr_type', 'h', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
47 args_list$i = c('chr_aliases', 'i', '2', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
48 args_list$j = c('use_meta_features', 'j', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
49 args_list$k = c('allow_multi_overlap', 'k', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
50 args_list$l = c('min_overlap', 'l', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
51 args_list$m = c('largest_overlap', 'm', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
52 args_list$n = c('read_extension_5', 'n', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
53 args_list$o = c('read_extension_3', 'o', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
54 args_list$p = c('read_2_pos', 'p', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
55 args_list$q = c('count_multi_mapping_reads', 'q', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
56 args_list$u = c('fraction', 'u', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
57 args_list$v= c('min_mqs', 'v', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
58 args_list$w= c('split_only', 'w', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
59 args_list$x= c('non_split_only', 'x', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
60 args_list$y= c('primary_only', 'y', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
61 args_list$z= c('ignore_dup', 'z', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
62 args_list$A= c('strand_specific', 'A', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
63 args_list$B= c('junc_counts', 'B', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
64 args_list$C= c('genome', 'C', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
65 args_list$D= c('is_paired_end', 'D', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
66 args_list$E= c('require_both_ends_mapped', 'E', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
67 args_list$F= c('check_frag_length', 'F', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
68 args_list$G= c('min_frag_length', 'G', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
69 args_list$H= c('max_frag_length', 'H', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
70 args_list$I= c('count_chimeric_fragments', 'I', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
71 args_list$J= c('auto_sort', 'J', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
72 args_list$K= c('n_threads', 'K', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
73 args_list$L= c('max_mop', 'L', '1', 'integer')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
74 args_list$M= c('report_reads', 'M', '1', 'logical')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
75
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
76 ##--------2. output report and outputs --------------
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
77 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
78 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
79 args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
80 ##--------3. .Rmd templates in the tool directory ----------
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
81 args_list$TOOL_TEMPLATE_RMD = c('tool_template_rmd', 't', '1', 'character')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
82 ##-----------------------------------------------------------
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
83 opt = getopt(t(as.data.frame(args_list)))
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
84
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
85
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
86
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
87 ##=======STEP 2: create report directory (optional)==========
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
88 ##
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
89 ##===========================================================
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
90 dir.create(opt$report_dir)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
91
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
92 ##=STEP 3: replace placeholders in .Rmd with argument values=
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
93 ##
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
94 ##===========================================================
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
95 #++ need to replace placeholders with args values one by one+
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
96 readLines(opt$tool_template_rmd) %>%
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
97 (function(x) {
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
98 gsub('ECHO', opt$echo, x)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
99 }) %>%
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
100 (function(x) {
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
101 gsub('REPORT_DIR', opt$report_dir, x)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
102 }) %>%
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
103 (function(x) {
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
104 fileConn = file('tool_template.Rmd')
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
105 writeLines(x, con=fileConn)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
106 close(fileConn)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
107 })
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
108
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
109
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
110 ##=============STEP 4: render .Rmd templates=================
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
111 ##
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
112 ##===========================================================
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
113 render('tool_template.Rmd', output_file = opt$report_html)
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
114
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
115
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
116 ##--------end of code rendering .Rmd templates----------------
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
117 sink()
5af86972b408 planemo upload
mingchen0919
parents:
diff changeset
118 ##=========== End of sinking output=============================