Mercurial > repos > mingchen0919 > rmarkdown_fastqc_site
annotate fastqc_site_render.R @ 15:a6f8382f852c draft default tip
remove unnecessary dependencies
| author | mingchen0919 |
|---|---|
| date | Thu, 09 Nov 2017 09:49:39 -0500 |
| parents | 507eec497730 |
| children |
| rev | line source |
|---|---|
| 11 | 1 library(getopt) |
|
7
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
2 library(rmarkdown) |
| 11 | 3 library(htmltools) |
|
7
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
4 library(plyr) |
| 11 | 5 library(dplyr) |
|
7
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
6 library(stringr) |
|
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
7 library(DT) |
|
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
8 library(reshape2) |
|
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
9 library(plotly) |
| 11 | 10 options(stringsAsFactors=FALSE, useFancyQuotes=FALSE) |
|
7
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
11 |
| 11 | 12 ##============ Sink warnings and errors to a file ============== |
| 13 ## use the sink() function to wrap all code within it. | |
| 14 ##============================================================== | |
| 15 zz = file('warnings_and_errors.txt') | |
| 16 sink(zz) | |
| 17 sink(zz, type = 'message') | |
| 18 ##---------below is the code for rendering .Rmd templates----- | |
| 19 | |
| 20 ##=============STEP 1: handle command line arguments========== | |
| 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 #------------------------------------------------------------- | |
| 32 #++++++++++++++++++++ Best practice ++++++++++++++++++++++++++ | |
| 33 # 1. short flag alias should match the flag in the command section in the XML file. | |
| 34 # 2. long flag name can be any legal R variable names | |
| 35 # 3. two names in args_list can have common string but one name should not be a part of another name. | |
| 36 # for example, one name is "ECHO", if another name is "ECHO_XXX", it will cause problems. | |
| 37 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| 38 args_list=list() | |
| 39 ##------- 1. input data --------------------- | |
| 40 args_list$ECHO = c('echo', 'e', '1', 'character') | |
| 41 args_list$READS_1 = c('reads_1', 'r', '1', 'character') | |
| 42 args_list$NAME_1 = c('name_1', 'n', '1', 'character') | |
| 43 args_list$READS_2 = c('reads_2', 'R', '1', 'character') | |
| 44 args_list$NAME_2 = c('name_2', 'N', '1', 'character') | |
| 45 args_list$CONTAMINANTS = c('contaminants', 'c', '1', 'character') | |
| 46 args_list$LIMITS = c('limits', 'l', '1', 'character') | |
| 47 ##--------2. output report and outputs -------------- | |
| 48 args_list$REPORT_HTML = c('report_html', 'o', '1', 'character') | |
| 49 args_list$REPORT_DIR = c('report_dir', 'd', '1', 'character') | |
| 50 args_list$SINK_MESSAGE = c('sink_message', 's', '1', 'character') | |
| 51 ##--------3. .Rmd templates in the tool directory ---------- | |
| 52 args_list$SITE_YML = c('site_yml', 'S', '1', 'character') | |
| 53 args_list$INDEX_RMD = c('index_rmd', 'I', '1', 'character') | |
| 54 args_list$X01_EVALUATION_OVERVIEW = c('x01_evaluation_overview', 'A', '1', 'character') | |
| 55 args_list$X02_PER_BASE_SEQUENCE_QUALITY = c('x02_per_base_sequence_quality', 'B', '1', 'character') | |
| 56 args_list$X03_PER_TILE_SEQUENCE_QUALITY = c('x03_per_tile_sequence_quality', 'C', '1', 'character') | |
| 57 args_list$X04_PER_SEQUENCE_QUALITY_SCORE = c('x04_per_sequence_quality_score', 'D', '1', 'character') | |
| 58 args_list$X05_PER_BASE_SEQUENCE_CONTENT = c('x05_per_base_sequence_content', 'E', '1', 'character') | |
| 59 args_list$X06_PER_SEQUENCE_GC_CONTENT = c('x06_per_sequence_gc_content', 'F', '1', 'character') | |
| 60 args_list$X07_PER_BASE_N_CONTENT = c('x07_per_base_n_content', 'G', '1', 'character') | |
| 61 args_list$X08_SEQUENCE_LENGTH_DISTRIBUTION = c('x08_sequence_length_distribution', 'H', '1', 'character') | |
| 62 args_list$X09_SEQUENCE_DUPLICATION_LEVELS = c('x09_sequence_duplication_levels', 'J', '1', 'character') | |
| 63 args_list$X10_ADAPTER_CONTENT = c('x10_adapter_content', 'K', '1', 'character') | |
| 64 args_list$X11_KMER_CONTENT = c('x11_kmer_content', 'L', '1', 'character') | |
| 65 ##----------------------------------------------------------- | |
| 66 opt = getopt(t(as.data.frame(args_list))) | |
|
7
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
67 |
|
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
68 |
| 11 | 69 |
| 70 ##=======STEP 2: create report directory (optional)========== | |
| 71 ## | |
| 72 ##=========================================================== | |
| 73 dir.create(opt$report_dir) | |
| 74 | |
| 75 ##==STEP 3: copy index.Rmd and _site.yml to job working directory====== | |
| 76 ## | |
| 77 ##===================================================================== | |
| 78 file.copy(opt$index_rmd, 'index.Rmd') | |
| 79 file.copy(opt$site_yml, '_site.yml') | |
| 80 | |
| 81 ##=STEP 4: replace placeholders in .Rmd files with argument values= | |
| 82 ## | |
| 83 ##================================================================= | |
| 84 #++ need to replace placeholders with args values one by one+ | |
| 85 | |
| 86 # 01_evaluation_overview.Rmd | |
| 87 readLines(opt$x01_evaluation_overview) %>% | |
| 88 (function(x) { | |
| 89 gsub('ECHO', opt$echo, x) | |
| 90 }) %>% | |
| 91 (function(x) { | |
| 92 gsub('READS_1', opt$reads_1, x) | |
| 93 }) %>% | |
| 94 (function(x) { | |
| 95 gsub('NAME_1', opt$name_1, x) | |
| 96 }) %>% | |
| 97 (function(x) { | |
| 98 gsub('READS_2', opt$reads_2, x) | |
| 99 }) %>% | |
| 100 (function(x) { | |
| 101 gsub('NAME_2', opt$name_1, x) | |
| 102 }) %>% | |
| 103 (function(x) { | |
| 104 gsub('CONTAMINANTS', opt$contaminants, x) | |
| 105 }) %>% | |
| 106 (function(x) { | |
| 107 gsub('LIMITS', opt$limits, x) | |
| 108 }) %>% | |
| 109 (function(x) { | |
| 110 gsub('REPORT_DIR', opt$report_dir, x) | |
| 111 }) %>% | |
| 112 (function(x) { | |
| 113 fileConn = file('x01_evaluation_overview.Rmd') | |
| 114 writeLines(x, con=fileConn) | |
| 115 close(fileConn) | |
| 116 }) | |
| 117 | |
| 118 # 02_per_base_sequence_quality.Rmd | |
| 119 readLines(opt$x02_per_base_sequence_quality) %>% | |
| 120 (function(x) { | |
| 121 gsub('ECHO', opt$echo, x) | |
| 122 }) %>% | |
| 123 (function(x) { | |
| 124 gsub('REPORT_DIR', opt$report_dir, x) | |
| 125 }) %>% | |
| 126 (function(x) { | |
| 127 fileConn = file('x02_per_base_sequence_quality.Rmd') | |
| 128 writeLines(x, con=fileConn) | |
| 129 close(fileConn) | |
| 130 }) | |
| 131 | |
| 132 # 03_per_tile_sequence_quality.Rmd | |
| 133 readLines(opt$x03_per_tile_sequence_quality) %>% | |
| 134 (function(x) { | |
| 135 gsub('ECHO', opt$echo, x) | |
| 136 }) %>% | |
| 137 (function(x) { | |
| 138 gsub('REPORT_DIR', opt$report_dir, x) | |
| 139 }) %>% | |
| 140 (function(x) { | |
| 141 fileConn = file('x03_per_tile_sequence_quality.Rmd') | |
| 142 writeLines(x, con=fileConn) | |
| 143 close(fileConn) | |
| 144 }) | |
| 145 | |
| 146 # 04_per_sequence_quality_score.Rmd | |
| 147 readLines(opt$x04_per_sequence_quality_score) %>% | |
| 148 (function(x) { | |
| 149 gsub('ECHO', opt$echo, x) | |
| 150 }) %>% | |
| 151 (function(x) { | |
| 152 gsub('REPORT_DIR', opt$report_dir, x) | |
| 153 }) %>% | |
| 154 (function(x) { | |
| 155 fileConn = file('x04_per_sequence_quality_score.Rmd') | |
| 156 writeLines(x, con=fileConn) | |
| 157 close(fileConn) | |
| 158 }) | |
| 159 | |
| 160 # 05_per_base_sequence_content.Rmd | |
| 161 readLines(opt$x05_per_base_sequence_content) %>% | |
| 162 (function(x) { | |
| 163 gsub('ECHO', opt$echo, x) | |
| 164 }) %>% | |
| 165 (function(x) { | |
| 166 gsub('REPORT_DIR', opt$report_dir, x) | |
| 167 }) %>% | |
| 168 (function(x) { | |
| 169 fileConn = file('x05_per_base_sequence_content.Rmd') | |
| 170 writeLines(x, con=fileConn) | |
| 171 close(fileConn) | |
| 172 }) | |
| 173 | |
| 174 # 06_per_sequence_gc_content.Rmd | |
| 175 readLines(opt$x06_per_sequence_gc_content) %>% | |
| 176 (function(x) { | |
| 177 gsub('ECHO', opt$echo, x) | |
| 178 }) %>% | |
| 179 (function(x) { | |
| 180 gsub('REPORT_DIR', opt$report_dir, x) | |
| 181 }) %>% | |
| 182 (function(x) { | |
| 183 fileConn = file('x06_per_sequence_gc_content.Rmd') | |
| 184 writeLines(x, con=fileConn) | |
| 185 close(fileConn) | |
| 186 }) | |
| 187 | |
| 188 # 07_per_base_n_content.Rmd | |
| 189 readLines(opt$x07_per_base_n_content) %>% | |
| 190 (function(x) { | |
| 191 gsub('ECHO', opt$echo, x) | |
| 192 }) %>% | |
| 193 (function(x) { | |
| 194 gsub('REPORT_DIR', opt$report_dir, x) | |
| 195 }) %>% | |
| 196 (function(x) { | |
| 197 fileConn = file('x07_per_base_n_content.Rmd') | |
| 198 writeLines(x, con=fileConn) | |
| 199 close(fileConn) | |
| 200 }) | |
|
7
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
201 |
| 11 | 202 # 08_sequence_length_distribution.Rmd |
| 203 readLines(opt$x08_sequence_length_distribution) %>% | |
| 204 (function(x) { | |
| 205 gsub('ECHO', opt$echo, x) | |
| 206 }) %>% | |
| 207 (function(x) { | |
| 208 gsub('REPORT_DIR', opt$report_dir, x) | |
| 209 }) %>% | |
| 210 (function(x) { | |
| 211 fileConn = file('x08_sequence_length_distribution.Rmd') | |
| 212 writeLines(x, con=fileConn) | |
| 213 close(fileConn) | |
| 214 }) | |
| 215 | |
| 216 # 09_sequence_duplication_levels.Rmd | |
| 217 readLines(opt$x09_sequence_duplication_levels) %>% | |
| 218 (function(x) { | |
| 219 gsub('ECHO', opt$echo, x) | |
| 220 }) %>% | |
| 221 (function(x) { | |
| 222 gsub('REPORT_DIR', opt$report_dir, x) | |
| 223 }) %>% | |
| 224 (function(x) { | |
| 225 fileConn = file('x09_sequence_duplication_levels.Rmd') | |
| 226 writeLines(x, con=fileConn) | |
| 227 close(fileConn) | |
| 228 }) | |
| 229 | |
| 230 # 10_adapter_content.Rmd | |
| 231 readLines(opt$x10_adapter_content) %>% | |
| 232 (function(x) { | |
| 233 gsub('ECHO', opt$echo, x) | |
| 234 }) %>% | |
| 235 (function(x) { | |
| 236 gsub('REPORT_DIR', opt$report_dir, x) | |
| 237 }) %>% | |
| 238 (function(x) { | |
| 239 fileConn = file('x10_adapter_content.Rmd') | |
| 240 writeLines(x, con=fileConn) | |
| 241 close(fileConn) | |
| 242 }) | |
| 243 | |
| 244 # 11_kmer_content.Rmd | |
| 245 readLines(opt$x11_kmer_content) %>% | |
| 246 (function(x) { | |
| 247 gsub('ECHO', opt$echo, x) | |
| 248 }) %>% | |
| 249 (function(x) { | |
| 250 gsub('REPORT_DIR', opt$report_dir, x) | |
| 251 }) %>% | |
| 252 (function(x) { | |
| 253 fileConn = file('x11_kmer_content.Rmd') | |
| 254 writeLines(x, con=fileConn) | |
| 255 close(fileConn) | |
| 256 }) | |
| 257 | |
| 258 ##=============STEP 5: render all .Rmd templates================= | |
| 259 ## | |
| 260 ##=========================================================== | |
| 261 extract_data_module = function(fastqc_data, module_name, header = TRUE, comment.char = "") { | |
| 262 f = readLines(fastqc_data) | |
| 263 start_line = grep(module_name, f) | |
| 264 end_module_lines = grep('END_MODULE', f) | |
| 265 end_line = end_module_lines[which(end_module_lines > start_line)[1]] | |
| 266 module_data = f[(start_line+1):(end_line-1)] | |
| 267 writeLines(module_data, 'temp.txt') | |
| 268 read.csv('temp.txt', sep = '\t', header = header, comment.char = comment.char) | |
| 269 } | |
| 270 render_site() | |
| 271 | |
| 272 ##=============STEP 6: manipulate outputs==================== | |
| 273 ## | |
| 274 ##=========================================================== | |
| 275 file.copy('my_site/index.html', opt$report_html, recursive = TRUE) | |
| 276 system(paste0('cp -r my_site/* ', opt$report_dir)) | |
|
7
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
277 |
|
d820be692d74
planemo upload for repository https://github.com/statonlab/docker-GRReport/tree/master/my_tools/rmarkdown_fastqc_site commit d91f269e8bc09a488ed2e005122bbb4a521f44a0-dirty
mingchen0919
parents:
diff
changeset
|
278 |
| 11 | 279 ##--------end of code rendering .Rmd templates---------------- |
| 280 sink() | |
| 281 ##=========== End of sinking output============================= |
