Mercurial > repos > mingchen0919 > rmarkdown_deseq2_test
annotate DESeq_render.R @ 0:61c184384d02 draft default tip
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
| author | mingchen0919 |
|---|---|
| date | Tue, 07 Nov 2017 10:02:57 -0500 |
| parents | |
| children |
| rev | line source |
|---|---|
|
0
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
1 library(getopt) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
2 library(rmarkdown) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
3 library(htmltools) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
4 library(dplyr) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
5 library(stringi) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
6 library(DESeq2) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
7 library(pheatmap) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
8 library(RColorBrewer) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
9 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
10 ##============ Sink warnings and errors to a file ============== |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
11 ## use the sink() function to wrap all code within it. |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
12 ##============================================================== |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
13 zz = file('warnings_and_errors.txt') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
14 sink(zz) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
15 sink(zz, type = 'message') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
16 ##---------below is the code for rendering .Rmd templates----- |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
17 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
18 ##=============STEP 1: handle command line arguments========== |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
19 ## |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
20 ##============================================================ |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
21 # column 1: the long flag name |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
22 # column 2: the short flag alias. A SINGLE character string |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
23 # column 3: argument mask |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
24 # 0: no argument |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
25 # 1: argument required |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
26 # 2: argument is optional |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
27 # column 4: date type to which the flag's argument shall be cast. |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
28 # possible values: logical, integer, double, complex, character. |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
29 #------------------------------------------------------------- |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
30 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++ |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
31 # 1. short flag alias should match the flag in the command section in the XML file. |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
32 # 2. long flag name can be any legal R variable names |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
33 # 3. two names in args_list can have common string but one name should not be a part of another name. |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
34 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems. |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
35 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
36 args_list=list() |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
37 ##------- 1. input data --------------------- |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
38 args_list$ECHO = c('echo', 'e', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
39 args_list$COUNT_FILES = c('count_files', 'c', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
40 args_list$SAMPLE_TABLE = c('sample_table', 'S', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
41 args_list$DESIGN_FORMULA = c('design_formula', 'p', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
42 ##--------2. output report and outputs -------------- |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
43 args_list$REPORT_HTML = c('report_html', 'r', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
44 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
45 args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
46 args_list$WORKSPACE = c('deseq_workspace', 'w', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
47 ##--------3. .Rmd templates in the tool directory ---------- |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
48 args_list$deseq_RMD = c('deseq_rmd', 't', '1', 'character') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
49 ##----------------------------------------------------------- |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
50 opt = getopt(t(as.data.frame(args_list))) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
51 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
52 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
53 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
54 ##=======STEP 2: create report directory (optional)========== |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
55 ## |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
56 ##=========================================================== |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
57 dir.create(opt$report_dir) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
58 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
59 ##=STEP 3: replace placeholders in .Rmd with argument values= |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
60 ## |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
61 ##=========================================================== |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
62 #++ need to replace placeholders with args values one by one+ |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
63 readLines(opt$deseq_rmd) %>% |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
64 (function(x) { |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
65 gsub('ECHO', opt$echo, x) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
66 }) %>% |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
67 (function(x) { |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
68 gsub('DESEQ_WORKSPACE', opt$deseq_workspace, x) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
69 }) %>% |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
70 (function(x) { |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
71 gsub('DESIGN_FORMULA', opt$design_formula, x) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
72 }) %>% |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
73 (function(x) { |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
74 gsub('REPORT_DIR', opt$output_dir, x) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
75 }) %>% |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
76 (function(x) { |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
77 fileConn = file('deseq.Rmd') |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
78 writeLines(x, con=fileConn) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
79 close(fileConn) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
80 }) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
81 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
82 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
83 ##=============STEP 4: render .Rmd templates================= |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
84 ## |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
85 ##=========================================================== |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
86 render('deseq.Rmd', output_file = opt$report_html) |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
87 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
88 |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
89 ##--------end of code rendering .Rmd templates---------------- |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
90 sink() |
|
61c184384d02
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_deseq2
mingchen0919
parents:
diff
changeset
|
91 ##=========== End of sinking output============================= |
