changeset 11:ce76fb2791e5 draft

planemo upload commit d28f1764ef50ca42f996cada585cfcea9ec04e5a
author mingchen0919
date Tue, 01 May 2018 09:32:50 -0400
parents ec906e72784d
children 67e55865b9e7
files build-and-run-job-scripts.sh command-line-arguments.csv expose-outputs-to-galaxy-history.sh expose-outputs.sh getopt_specification.csv helper.R htseq_count.Rmd htseq_count.xml htseq_count_render.R rmarkdown_report.Rmd rmarkdown_report.xml rmarkdown_report_render.R shell-script-template.sh
diffstat 13 files changed, 421 insertions(+), 391 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/build-and-run-job-scripts.sh	Tue May 01 09:32:50 2018 -0400
@@ -0,0 +1,23 @@
+# run job scripts within the tool outputs directory
+cd ${REPORT_FILES_PATH}
+
+#========== build and run job 1 script ============
+cat >htseq-count.sh <<EOF
+ htseq-count \\
+    $(echo ${X_A} | sed 's/,/ /g') \\
+    ${X_G} \\
+    -f ${X_f} \\
+    -r ${X_r} \\
+    -s ${X_S} \\
+    -a ${X_a} \\
+    -t ${X_T} \\
+    -i ${X_i} \\
+    -m ${X_m} > htseq-counts-raw.txt
+
+  grep -v '__no_feature\|__ambiguous\|__too_low_aQual\|__not_aligned\|__alignment_not_unique' htseq-counts-raw.txt > counts.txt
+
+EOF
+
+# run job 1 script
+sh htseq-count.sh
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/command-line-arguments.csv	Tue May 01 09:32:50 2018 -0400
@@ -0,0 +1,14 @@
+short flag,argument mask,data type,variable name
+o,1,character,report
+d,1,character,report.files_path
+A,1,character,alignment_files
+B,1,character,sample_names
+G,1,character,gff
+f,1,character,format
+r,1,character,order
+S,1,character,stranded
+a,1,character,minaqual
+T,1,character,feature_type
+i,1,character,idattr
+m,1,character,mode
+c,1,character,count
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/expose-outputs-to-galaxy-history.sh	Tue May 01 09:32:50 2018 -0400
@@ -0,0 +1,13 @@
+# change directory to tool outputs directory
+cd ${REPORT_FILES_PATH}
+
+# copy outputs from tool outputs directory to corresponding galaxy output path
+if [ -e "rmarkdown_report.html" ]; then
+  cp rmarkdown_report.html ${REPORT}
+fi
+
+if [ -e "index.html" ]; then
+  cp index.html ${REPORT}
+fi
+
+cp count_data.csv ${X_c}
\ No newline at end of file
--- a/expose-outputs.sh	Thu Apr 26 12:24:33 2018 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,6 +0,0 @@
-# change directory to tool outputs directory
-cd ${REPORT_FILES_PATH}
-
-# copy files to corresponding tool output paths.
-cp htseq_count.html ${REPORT}
-cp count_data.csv ${X_c}
\ No newline at end of file
--- a/getopt_specification.csv	Thu Apr 26 12:24:33 2018 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-short flag,argument mask,data type,variable name
-o,1,character,report
-d,1,character,report.files_path
-A,1,character,alignment_files
-B,1,character,sample_names
-G,1,character,gff
-f,1,character,format
-r,1,character,order
-S,1,character,stranded
-a,1,character,minaqual
-T,1,character,feature_type
-i,1,character,idattr
-m,1,character,mode
-c,1,character,count
--- a/helper.R	Thu Apr 26 12:24:33 2018 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,82 +0,0 @@
-#' \code{getopt_specification_matrix} returns a getopt specification matrix.
-#'
-#' @param specification_file a cvs file within the \code{galaxy_tool_directory} which stores getopt specification matrix data.
-#' The first column are short flags, the second column are argument masks, the third column
-#' is data types. The fourth column are variable names used in the tool XML. These three columns are required.
-#' @param gtg_name the name of a running GTG.
-getopt_specification_matrix = function(specification_file,
-                                       gtg_name = 'gtg',
-                                       tool_dir = Sys.getenv('TOOL_DIR')) {
-  df = read.csv(
-    paste0(tool_dir, '/', specification_file),
-    header = TRUE,
-    stringsAsFactors = FALSE
-  )
-  # check if there are duplicated short flags
-  short_flags = df[, 1]
-  if (length(unique(short_flags)) < length(short_flags)) {
-    cat('----Duplicated short flags found ----\n')
-    cat('short flags: ', df[, 1][duplicated(df[, 1])], '\n')
-    stop('Duplicated short flags are not allowed.')
-  }
-  
-  # use short flags to generate long flags
-  long_flags = paste0('X_', df[, 1])
-  
-  # specification matrix
-  df2 = data.frame(
-    long_flags = long_flags,
-    short_flags = df[, 1],
-    argument_mask = df[, 2],
-    data_type = df[, 3]
-  )
-  
-  as.matrix(df2)
-}
-
-
-
-#' \code{file_tree} generate file tree of a directory in the format of HTML lists.
-#'
-#' @param dir the path to the directory for generating the file tree.
-#' @param output_dir the REPORT_FILES_PATH folder name, which has the name style: dataset_NUMBER_files.
-# define a recursive function to build html string of the file tree
-file_tree = function(dir = '.') {
-  # get the OUTPUT_DIR folder data: dataset_NUMBER_files
-  report_files_path = Sys.getenv('REPORT_FILES_PATH')
-  output_dir = tail(strsplit(report_files_path, '/')[[1]], 1)
-  
-  files = list.files(path = dir,
-                     recursive = FALSE,
-                     full.names = TRUE)
-  # files also include directorys, need to remove directorys
-  files = files[!dir.exists(files)]
-  dirs = list.dirs(path = dir,
-                   recursive = FALSE,
-                   full.names = TRUE)
-  tags$ul({
-    if (length(files) > 0) {
-      lapply(files, function(x) {
-        path_end = tail(strsplit(x, '/')[[1]], 1)
-        href_path = strsplit(x, paste0(output_dir, '/'))[[1]][2]
-        li_item = tags$li(tags$a(path_end, href = href_path))
-        li_item$attribs = list('data-jstree' = '{"icon":"jstree-file"}')
-        li_item
-      })
-    }
-  },
-  {
-    if (length(dirs) > 0) {
-      lapply(dirs, function(x) {
-        path_end = tail(strsplit(x, '/')[[1]], 1)
-        # hide vakata-jstree-3.3.5 folder
-        if (path_end != 'vakata-jstree-3.3.5') {
-          # x_path = strsplit(x, paste0(output_dir, '/'))[[1]][2]
-          li_item = tags$li(path_end, file_tree(x))
-          li_item$attribs = list('data-jstree' = '{"icon":"jstree-folder"}')
-          li_item
-        }
-      })
-    }
-  })
-}
--- a/htseq_count.Rmd	Thu Apr 26 12:24:33 2018 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
----
-title: 'htseq-count analysis report'
-output:
-    html_document:
-      highlight: pygments
----
-
-```{css echo=FALSE}
-# code chunks scrollable
-pre code, pre, code {
-  white-space: pre !important;
-  overflow-x: scroll !important;
-  word-break: keep-all !important;
-  word-wrap: initial !important;
-}
-```
-
-```{r setup, include=FALSE, warning=FALSE, message=FALSE}
-knitr::opts_knit$set(progress = FALSE)
-knitr::opts_chunk$set(error = TRUE, echo = FALSE)
-```
-
-```{r, echo=FALSE}
-# to make the css theme to work, <link></link> tags cannot be added directly 
-# as <script></script> tags as below.
-# it has to be added using a code chunk with the htmltool functions!!!
-css_link = tags$link()
-css_link$attribs = list(rel="stylesheet", href="vakata-jstree-3.3.5/dist/themes/default/style.min.css")
-css_link
-```
-
-```{r, eval=FALSE, echo=FALSE}
-# this code chunk is purely for adding comments
-# below is to add jQuery and jstree javascripts
-```
-<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
-<script src="vakata-jstree-3.3.5/dist/jstree.min.js"></script>
-
-```{r, eval=FALSE, echo=FALSE}
-# this code chunk is purely for adding comments
-# javascript code below is to build the file tree interface
-# see this for how to implement opening hyperlink: https://stackoverflow.com/questions/18611317/how-to-get-i-get-leaf-nodes-in-jstree-to-open-their-hyperlink-when-clicked-when
-```
-<script>
-  $(function () {
-    // create an instance when the DOM is ready
-    $('#jstree').jstree().bind("select_node.jstree", function (e, data) {
-     window.open( data.node.a_attr.href, data.node.a_attr.target )
-    });
-  });
-</script>
------------------------------------------
-
-
-## Job script
-
-```{bash, echo=FALSE}
-sh ${TOOL_INSTALL_DIR}/shell-script-template.sh
-```
-
-```{r echo=FALSE, comment='', results='asis'}
-cat('```bash\n')
-cat(readLines(paste0(Sys.getenv('REPORT_FILES_PATH'), '/htseq-count.sh')), sep = '\n')
-cat('\n```')
-```
-
-## Counts
-
-Only the first few rows are displayed. 
-
-```{r, echo=FALSE}
-count_data = read.table(paste0(opt$X_d, '/counts.txt'), row.names = 1)
-sample_names = trimws(strsplit(opt$X_B, ',')[[1]])
-colnames(count_data) = rep(sample_names, length = ncol(count_data))
-kable(head(count_data))
-
-# modify column names
-count_data = data.frame(feature_id = rownames(count_data), count_data)
-write.csv(count_data, 
-          file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/count_data.csv'),
-          quote = FALSE, row.names = FALSE)
-```
-
-
------------------------------------------
-## Output
-
-```{r, echo=FALSE}
-# create a div container to store the file tree interface
-tags$div(
-  id="jstree",
-  file_tree(Sys.getenv('REPORT_FILES_PATH'))
-)
-```
--- a/htseq_count.xml	Thu Apr 26 12:24:33 2018 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,121 +0,0 @@
-<tool id="aurora_htseq" name="aurora_htseq" version="2.0.0">
-    <description>Counting reads in features.</description>
-    <requirements>
-        <requirement type="package" version="1.15.0.6-0">pandoc</requirement>
-        <requirement type="package" version="1.20.0">r-getopt</requirement>
-        <requirement type="package" version="1.6">r-rmarkdown</requirement>
-        <requirement type="package" version="0.9.1">htseq</requirement>
-    </requirements>
-    <stdio>
-        <regex match="XXX" source="stderr" level="warning"
-               description="Check the warnings_and_errors.txt file for more details."/>
-    </stdio>
-    <command><![CDATA[
-
-        ######### each aurora tool generates a html file and have an files path directory associated with it.
-        mkdir -p $report.files_path &&
-
-        ######### three important paths:
-        #########   1. path to tool installation directory
-        #########   2. path to report html
-        #########   3. path to files_path directory associated with the report output.
-        export TOOL_INSTALL_DIR='${__tool_directory__}' &&
-        export REPORT='$report' &&
-        export REPORT_FILES_PATH='$report.files_path' &&
-
-        ############ create a hidden file to store r markdown rendering log
-        echo '------ Rmd rendering log -------\n' > $report.files_path/.r_rendering.log.txt &&
-
-    Rscript '${__tool_directory__}/htseq_count_render.R'
-
-			-o $report
-			-d $report.files_path
-
-			-A '$alignment_files'
-			-B '$sample_names'
-			-G $gff
-			-f $format
-			-r $order
-			-S $stranded
-			-a $minaqual
-			-T $feature_type
-			-i $idattr
-			-m $mode
-			-c $count
-
-    ]]></command>
-    <inputs>
-        <param type="data" name="alignment_files" label="Alignment file (SAM/BAM)" optional="False" format="sam,bam"
-               multiple="True"/>
-        <param type="text" name="sample_names" label="Sample names"
-               help="sample names for each input SAM/BAM file. Use comma to separate multiple names, for example, sample_1, sample_2, sample_3. The number of sample names should be the same as the number of input SAM/BAM files."
-               optional="False" size="5x25"/>
-        <param type="select" name="format" argument="-f" label="Input data format" optional="False" multiple="False">
-            <option value="sam" selected="false">SAM</option>
-            <option value="bam" selected="true">BAM</option>
-        </param>
-        <param type="data" name="gff" label="GFF file" optional="False" format="gff, gff3"/>
-        <param type="select" name="order" argument="-r" label="Order"
-               help="Use this option to indicate how the input data has been sorted. The default is name.&#13;&#10;&#13;&#10;"
-               optional="False" multiple="False">
-            <option value="name" selected="false">Name</option>
-            <option value="pos" selected="false">Position</option>
-        </param>
-        <param type="select" name="stranded" argument="-s" label="Stranded" optional="False" multiple="False">
-            <option value="yes" selected="true">Yes</option>
-            <option value="no" selected="false">No</option>
-            <option value="reverse" selected="false">Reverse</option>
-        </param>
-        <param type="integer" name="minaqual" argument="-a" label="Alignment quality "
-               help="Skip all reads with alignment quality lower than the given minimum value (default: 10)"
-               optional="False" value="10" min="0"/>
-        <param type="text" name="feature_type" argument="-t" label="Feature type"
-               help="Feature type (3rd column in GFF file) to be used, all features of other type are ignored (default, suitable for RNA-Seq analysis using an Ensembl GTF file: exon)"
-               optional="False" value="exon"/>
-        <param type="text" name="idattr" argument="-i" label="ID attribute"
-               help="GFF attribute to be used as feature ID. Several GFF lines with the same feature ID will be considered as parts of the same feature. The feature ID is used to identity the counts in the output table. The default, suitable for RNA-Seq analysis using an Ensembl GTF file, is gene_id."
-               optional="False" value="gene_id"/>
-        <param type="select" name="mode" argument="-m" label="Mode"
-               help="Mode to handle reads overlapping more than one feature. Possible values for &lt;mode&gt; are union, intersection-strict and intersection-nonempty (default: union)"
-               optional="False" multiple="False">
-            <option value="union" selected="true">Union</option>
-            <option value="intersection-strict" selected="false">Intersection (strict)</option>
-            <option value="intersection-nonempty" selected="false">Intersection (nonempty)</option>
-        </param>
-    </inputs>
-    <outputs>
-        <data name="report" format="html" label="${tool.name} report on ${on_string}" hidden="false"/>
-        <data name="count" format="csv" label="${tool.name} txt count on ${on_string}" hidden="false"/>
-    </outputs>
-    <citations>
-        <citation type="bibtex"><![CDATA[
-            @article{allaire2016rmarkdown,
-            title={rmarkdown: Dynamic Documents for R, 2016},
-            author={Allaire, J and Cheng, Joe and Xie, Yihui and McPherson, Jonathan and Chang, Winston and Allen, Jeff
-            and Wickham, Hadley and Atkins, Aron and Hyndman, Rob},
-            journal={R package version 0.9},
-            volume={6},
-            year={2016}
-            }
-        ]]></citation>
-        <citation type="bibtex"><![CDATA[
-            @book{xie2015dynamic,
-            title={Dynamic Documents with R and knitr},
-            author={Xie, Yihui},
-            volume={29},
-            year={2015},
-            publisher={CRC Press}
-            }
-        ]]></citation>
-        <citation type="bibtex"><![CDATA[@article{anders2015htseq,
-  title={HTSeq—a Python framework to work with high-throughput sequencing data},
-  author={Anders, Simon and Pyl, Paul Theodor and Huber, Wolfgang},
-  journal={Bioinformatics},
-  volume={31},
-  number={2},
-  pages={166--169},
-  year={2015},
-  publisher={Oxford University Press}
-}]]></citation>
-    </citations>
-</tool>
--- a/htseq_count_render.R	Thu Apr 26 12:24:33 2018 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-##============ Sink warnings and errors to a file ==============
-## use the sink() function to wrap all code within it.
-##==============================================================
-zz = file(paste0(Sys.getenv('REPORT_FILES_PATH'), '/.r_rendering.log.txt'))
-sink(zz)
-sink(zz, type = 'message')
-
-#-------------------preparation -----------------
-options(stringsAsFactors = FALSE)
-# import libraries
-library(getopt)
-library(rmarkdown)
-library(htmltools)
-library(magrittr)
-library(knitr)
-# load helper functions
-source(paste0(Sys.getenv('TOOL_INSTALL_DIR'), '/helper.R'))
-# import getopt specification matrix from a csv file
-opt = getopt(getopt_specification_matrix('getopt_specification.csv', 
-                                         tool_dir=Sys.getenv('TOOL_INSTALL_DIR')))
-# define a unix variable versions for all input values. this is useful when we 
-# want to use input values by other programming language in r markdown
-do.call(Sys.setenv, opt[-1])
-#------------------------------------------------
-
-
-#-----------------render Rmd files --------------
-# NOTICE: 
-#       we should copy all rmarkdown files from tool install directory to REPORT_FILES_PATH directory.
-#       and render rmarkdown files in the REPORT_FILES_PATH directory.
-file.copy(from = paste0(Sys.getenv('TOOL_INSTALL_DIR'), '/vakata-jstree-3.3.5'),
-          to = Sys.getenv('REPORT_FILES_PATH'), recursive = TRUE)
-system(command = 'cp -r ${TOOL_INSTALL_DIR}/*.Rmd ${REPORT_FILES_PATH}')
-# render Rmd files in order
-render(input = paste0(Sys.getenv('REPORT_FILES_PATH'), '/htseq_count.Rmd'))
-# add more lines below if there are more Rmd files to be rendered
-
-#------------------------------------------
-
-
-#--------- expose outputs to galaxy history ----
-system(command = 'sh ${TOOL_INSTALL_DIR}/expose-outputs.sh')
-#==============the end==============
-
-
-##--------end of code rendering .Rmd templates----------------
-sink()
-##=========== End of sinking output=============================
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rmarkdown_report.Rmd	Tue May 01 09:32:50 2018 -0400
@@ -0,0 +1,84 @@
+---
+title: 'HTSeq-count Report'
+output:
+    html_document:
+      highlight: pygments
+---
+
+```{r setup, include=FALSE, warning=FALSE, message=FALSE}
+knitr::opts_chunk$set(error = TRUE, echo = FALSE)
+```
+
+```{css echo=FALSE}
+# code chunks scrollable
+pre code, pre, code {
+  white-space: pre !important;
+  overflow-x: scroll !important;
+  word-break: keep-all !important;
+  word-wrap: initial !important;
+}
+```
+
+
+```{r, echo=FALSE}
+# to make the css theme to work, <link></link> tags cannot be added directly 
+# as <script></script> tags as below.
+# it has to be added using a code chunk with the htmltool functions!!!
+css_link = tags$link()
+css_link$attribs = list(rel="stylesheet", href="vakata-jstree-3.3.5/dist/themes/default/style.min.css")
+css_link
+```
+
+```{r, eval=FALSE, echo=FALSE}
+# this code chunk is purely for adding comments
+# below is to add jQuery and jstree javascripts
+```
+<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
+<script src="vakata-jstree-3.3.5/dist/jstree.min.js"></script>
+
+---
+# javascript code below is to build the file tree interface
+# see this for how to implement opening hyperlink: https://stackoverflow.com/questions/18611317/how-to-get-i-get-leaf-nodes-in-jstree-to-open-their-hyperlink-when-clicked-when
+---
+<script>
+  $(function () {
+    // create an instance when the DOM is ready
+    $('#jstree').jstree().bind("select_node.jstree", function (e, data) {
+     window.open( data.node.a_attr.href, data.node.a_attr.target )
+    });
+  });
+</script>
+
+---
+# ADD YOUR DATA ANALYSIS CODE AND MARKUP TEXT BELOW TO EXTEND THIS R MARKDOWN FILE
+---
+
+## Job script
+
+```{bash, echo=FALSE}
+sh ${TOOL_INSTALL_DIR}/build-and-run-job-scripts.sh
+```
+
+```{r echo=FALSE, comment='', results='asis'}
+cat('```bash\n')
+cat(readLines(paste0(Sys.getenv('REPORT_FILES_PATH'), '/htseq-count.sh')), sep = '\n')
+cat('\n```')
+```
+
+## Counts
+
+Only the first few rows are displayed. 
+
+```{r, echo=FALSE}
+count_data = read.table(paste0(opt$X_d, '/counts.txt'), row.names = 1)
+sample_names = trimws(strsplit(opt$X_B, ',')[[1]])
+colnames(count_data) = rep(sample_names, length = ncol(count_data))
+kable(head(count_data))
+
+# modify column names
+count_data = data.frame(feature_id = rownames(count_data), count_data)
+write.csv(count_data, 
+          file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/count_data.csv'),
+          quote = FALSE, row.names = FALSE)
+```
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rmarkdown_report.xml	Tue May 01 09:32:50 2018 -0400
@@ -0,0 +1,130 @@
+<tool name="aurora_htseq" id='aurora_htseq_report' version="2.0.1">
+    <description>
+        some description
+    </description>
+    <requirements>
+        <requirement type="package" version="1.15.0.6-0">pandoc</requirement>
+        <requirement type="package" version="1.20.0">r-getopt</requirement>
+        <requirement type="package" version="1.6">r-rmarkdown</requirement>
+        <requirement type="package" version="0.9.1">htseq</requirement>
+    </requirements>
+    <command><![CDATA[
+
+        ######### each aurora tool generates a html file and have an files path directory associated with it.
+        mkdir -p $report.files_path &&
+
+        ######### three important paths:
+        #########   1. path to tool installation directory
+        #########   2. path to report html
+        #########   3. path to files_path directory associated with the report output.
+        export TOOL_INSTALL_DIR='${__tool_directory__}' &&
+        export REPORT='$report' &&
+        export REPORT_FILES_PATH='$report.files_path' &&
+
+        ############ create a hidden file to store r markdown rendering log
+        touch $report.files_path/.r_rendering.log.txt &&
+
+        ############ finally run the render.R script
+        Rscript '${__tool_directory__}/rmarkdown_report_render.R'
+        
+            -o $report
+            -d $report.files_path
+
+            -A '$alignment_files'
+			-B '$sample_names'
+			-G $gff
+			-f $format
+			-r $order
+			-S $stranded
+			-a $minaqual
+			-T $feature_type
+			-i $idattr
+			-m $mode
+			-c $count
+
+
+    ]]></command>
+    <inputs>
+        <param type="data" name="alignment_files" label="Alignment file (SAM/BAM)" optional="False" format="sam,bam"
+               multiple="True"/>
+        <param type="text" name="sample_names" label="Sample names"
+               help="sample names for each input SAM/BAM file. Use comma to separate multiple names, for example, sample_1, sample_2, sample_3. The number of sample names should be the same as the number of input SAM/BAM files."
+               optional="False" size="5x25"/>
+        <param type="select" name="format" argument="-f" label="Input data format" optional="False" multiple="False">
+            <option value="sam" selected="false">SAM</option>
+            <option value="bam" selected="true">BAM</option>
+        </param>
+        <param type="data" name="gff" label="GFF file" optional="False" format="gff, gff3"/>
+        <param type="select" name="order" argument="-r" label="Order"
+               help="Use this option to indicate how the input data has been sorted. The default is name.&#13;&#10;&#13;&#10;"
+               optional="False" multiple="False">
+            <option value="name" selected="false">Name</option>
+            <option value="pos" selected="false">Position</option>
+        </param>
+        <param type="select" name="stranded" argument="-s" label="Stranded" optional="False" multiple="False">
+            <option value="yes" selected="true">Yes</option>
+            <option value="no" selected="false">No</option>
+            <option value="reverse" selected="false">Reverse</option>
+        </param>
+        <param type="integer" name="minaqual" argument="-a" label="Alignment quality "
+               help="Skip all reads with alignment quality lower than the given minimum value (default: 10)"
+               optional="False" value="10" min="0"/>
+        <param type="text" name="feature_type" argument="-t" label="Feature type"
+               help="Feature type (3rd column in GFF file) to be used, all features of other type are ignored (default, suitable for RNA-Seq analysis using an Ensembl GTF file: exon)"
+               optional="False" value="exon"/>
+        <param type="text" name="idattr" argument="-i" label="ID attribute"
+               help="GFF attribute to be used as feature ID. Several GFF lines with the same feature ID will be considered as parts of the same feature. The feature ID is used to identity the counts in the output table. The default, suitable for RNA-Seq analysis using an Ensembl GTF file, is gene_id."
+               optional="False" value="gene_id"/>
+        <param type="select" name="mode" argument="-m" label="Mode"
+               help="Mode to handle reads overlapping more than one feature. Possible values for &lt;mode&gt; are union, intersection-strict and intersection-nonempty (default: union)"
+               optional="False" multiple="False">
+            <option value="union" selected="true">Union</option>
+            <option value="intersection-strict" selected="false">Intersection (strict)</option>
+            <option value="intersection-nonempty" selected="false">Intersection (nonempty)</option>
+        </param>
+    </inputs>
+    <outputs>
+        <data name="report" format="html" label="${tool.name} report on ${on_string}" hidden="false"/>
+        <data name="count" format="csv" label="${tool.name} txt count on ${on_string}" hidden="false"/>
+    </outputs>
+    <citations>
+        <citation type="bibtex"><![CDATA[
+            @article{allaire2016rmarkdown,
+            title={rmarkdown: Dynamic Documents for R, 2016},
+            author={Allaire, J and Cheng, Joe and Xie, Yihui and McPherson, Jonathan and Chang, Winston and Allen, Jeff
+            and Wickham, Hadley and Atkins, Aron and Hyndman, Rob},
+            journal={R package version 0.9},
+            volume={6},
+            year={2016}
+            }
+        ]]></citation>
+        <citation type="bibtex"><![CDATA[
+            @book{xie2015dynamic,
+            title={Dynamic Documents with R and knitr},
+            author={Xie, Yihui},
+            volume={29},
+            year={2015},
+            publisher={CRC Press}
+            }
+        ]]></citation>
+        <citation type="bibtex"><![CDATA[
+            @online{jstree,
+            author={Bozhanov, Ivan},
+            year = 2018,
+            url = {https://www.jstree.com/}
+            }
+        ]]></citation>
+        <citation type="bibtex"><![CDATA[
+            @article{anders2015htseq,
+            title={HTSeq—a Python framework to work with high-throughput sequencing data},
+            author={Anders, Simon and Pyl, Paul Theodor and Huber, Wolfgang},
+            journal={Bioinformatics},
+            volume={31},
+            number={2},
+            pages={166--169},
+            year={2015},
+            publisher={Oxford University Press}
+            }
+        ]]></citation>
+    </citations>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rmarkdown_report_render.R	Tue May 01 09:32:50 2018 -0400
@@ -0,0 +1,157 @@
+##============ Sink warnings and errors to a file ==============
+## use the sink() function to wrap all code within it.
+##==============================================================
+zz = file(paste0(Sys.getenv('REPORT_FILES_PATH'), '/.r_rendering.log.txt'))
+sink(zz)
+sink(zz, type = 'message')
+
+#============== preparation ====================================
+# import libraries
+#------------------------------------------------------------------
+# ADD MORE LIBRARIES HERE IF YOUR TOOL DEPENDS ON OTHER R LIBRARIES
+#------------------------------------------------------------------
+library('getopt')
+library('rmarkdown')
+library('htmltools')
+#------------------------------------------------------------------
+options(stringsAsFactors = FALSE)
+
+
+# define two helper functions
+#-----: helper function 1
+#' \code{getopt_specification_matrix} returns a getopt specification matrix.
+#'
+#' @param specification_file a cvs file within the \code{galaxy_tool_directory} which stores getopt specification matrix data.
+#' The first column are short flags, the second column are argument masks, the third column
+#' is data types. The fourth column are variable names used in the tool XML. These three columns are required.
+#' @param gtg_name the name of a running GTG.
+getopt_specification_matrix = function(specification_file,
+                                       gtg_name = 'gtg',
+                                       tool_dir = Sys.getenv('TOOL_INSTALL_DIR')) {
+  df = read.csv(
+    paste0(tool_dir, '/', specification_file),
+    header = TRUE,
+    stringsAsFactors = FALSE
+  )
+  # check if there are duplicated short flags
+  short_flags = df[, 1]
+  if (length(unique(short_flags)) < length(short_flags)) {
+    cat('----Duplicated short flags found ----\n')
+    cat('short flags: ', df[, 1][duplicated(df[, 1])], '\n')
+    stop('Duplicated short flags are not allowed.')
+  }
+  
+  # use short flags to generate long flags
+  long_flags = paste0('X_', df[, 1])
+  
+  # specification matrix
+  df2 = data.frame(
+    long_flags = long_flags,
+    short_flags = df[, 1],
+    argument_mask = df[, 2],
+    data_type = df[, 3]
+  )
+  
+  as.matrix(df2)
+}
+
+#-----: helper function 2
+#' \code{file_tree} generate file tree of a directory in the format of HTML lists.
+#'
+#' @param dir the path to the directory for generating the file tree.
+#' @param output_dir the REPORT_FILES_PATH folder name, which has the name style: dataset_NUMBER_files.
+# define a recursive function to build html string of the file tree
+file_tree = function(dir = '.') {
+  # get the OUTPUT_DIR folder data: dataset_NUMBER_files
+  report_files_path = Sys.getenv('REPORT_FILES_PATH')
+  output_dir = tail(strsplit(report_files_path, '/')[[1]], 1)
+  
+  files = list.files(path = dir,
+                     recursive = FALSE,
+                     full.names = TRUE)
+  # files also include directorys, need to remove directorys
+  files = files[!dir.exists(files)]
+  dirs = list.dirs(path = dir,
+                   recursive = FALSE,
+                   full.names = TRUE)
+  tags$ul({
+    if (length(files) > 0) {
+      lapply(files, function(x) {
+        path_end = tail(strsplit(x, '/')[[1]], 1)
+        href_path = strsplit(x, paste0(output_dir, '/'))[[1]][2]
+        li_item = tags$li(tags$a(path_end, href = href_path))
+        li_item$attribs = list('data-jstree' = '{"icon":"jstree-file"}')
+        li_item
+      })
+    }
+  },
+  {
+    if (length(dirs) > 0) {
+      lapply(dirs, function(x) {
+        path_end = tail(strsplit(x, '/')[[1]], 1)
+        # hide vakata-jstree-3.3.5 folder
+        if (path_end != 'vakata-jstree-3.3.5') {
+          # x_path = strsplit(x, paste0(output_dir, '/'))[[1]][2]
+          li_item = tags$li(path_end, file_tree(x))
+          li_item$attribs = list('data-jstree' = '{"icon":"jstree-folder"}')
+          li_item
+        }
+      })
+    }
+  })
+}
+#----------------- end of help functions -------------------------
+
+
+# import getopt specification matrix from a csv file
+opt = getopt(getopt_specification_matrix('command-line-arguments.csv',
+                                         tool_dir = Sys.getenv('TOOL_INSTALL_DIR')))
+# define environment variables for all input values. this is useful when we
+# want to use input values by other programming language in r markdown
+do.call(Sys.setenv, opt[-1])
+#===============================================================
+
+
+#======================== render Rmd files =========================
+# copy jstree javascript library to tool output directory
+file.copy(
+  from = paste0(Sys.getenv('TOOL_INSTALL_DIR'), '/vakata-jstree-3.3.5'),
+  to = Sys.getenv('REPORT_FILES_PATH'),
+  recursive = TRUE
+)
+
+# if '_site.yml' file exists, this tool is assumed to render a website.
+# otherwise, it renders a single html.
+if (file.exists(paste0(Sys.getenv('TOOL_INSTALL_DIR'), '/_site.yml'))) {
+  # render a website
+  system(command = 'cp -r ${TOOL_INSTALL_DIR}/*.Rmd ${REPORT_FILES_PATH}')
+  system(command = 'cp -r ${TOOL_INSTALL_DIR}/_site.yml ${REPORT_FILES_PATH}')
+  render_site(input = Sys.getenv('REPORT_FILES_PATH'))
+} else {
+  # render a single html
+  system(command = 'cp -r ${TOOL_INSTALL_DIR}/rmarkdown_report.Rmd ${REPORT_FILES_PATH}')
+  # add a few lines to 'rmarkdown_report.Rmd' to generate file tree outputs
+  jstree_lines = '
+## Outputs
+
+```{r, echo=FALSE}
+tags$div(id="jstree", file_tree(Sys.getenv(\'REPORT_FILES_PATH\')))
+```'
+  write(
+    x = jstree_lines,
+    append = TRUE,
+    file = paste0(Sys.getenv('REPORT_FILES_PATH'), '/rmarkdown_report.Rmd')
+  )
+  render(input = paste0(Sys.getenv('REPORT_FILES_PATH'), '/rmarkdown_report.Rmd'))
+}
+#===============================================================
+
+
+#============== expose outputs to galaxy history ===============
+system(command = 'sh ${TOOL_INSTALL_DIR}/expose-outputs-to-galaxy-history.sh')
+#===============================================================
+
+
+##--------end of code rendering .Rmd templates----------------
+sink()
+##=========== End of sinking output=============================
--- a/shell-script-template.sh	Thu Apr 26 12:24:33 2018 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-# SHELL_SCRIPT file name
-SHELL_SCRIPT='htseq-count.sh'
-
-# run SHELL_SCRIPT within tool outputs directory
-cd ${REPORT_FILES_PATH}
-
-# build job-script.sh
-cat >${SHELL_SCRIPT} <<EOF
-
-  htseq-count \\
-    $(echo ${X_A} | sed 's/,/ /g') \\
-    ${X_G} \\
-    -f ${X_f} \\
-    -r ${X_r} \\
-    -s ${X_S} \\
-    -a ${X_a} \\
-    -t ${X_T} \\
-    -i ${X_i} \\
-    -m ${X_m} > htseq-counts-raw.txt 
-    
-  grep -v '__no_feature\|__ambiguous\|__too_low_aQual\|__not_aligned\|__alignment_not_unique' htseq-counts-raw.txt > counts.txt
-
-EOF
-
-# run SHELL_SCRIPT
-sh ${SHELL_SCRIPT}
\ No newline at end of file