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