```{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>

```{r, eval=FALSE, echo=FALSE}
# The script below is used to avoid conflicts between different javascript labraries
```

<script>
jQuery.noConflict();
 
jQuery( document ).ready(function( $ ) {
    // You can use the locally-scoped $ in here as an alias to jQuery.
    $( "jstree" ).hide();
});

// The $ variable in the global scope has the prototype.js meaning.
window.onload = function(){
    var mainDiv = $( "main" );
}
</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 > ${REPORT_FILES_PATH}/log.txt 2>&1
```

```{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

Write data into a CSV file.

```{r, echo=TRUE}
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)
```

Display the top 1000 rows with largest average counts.

```{r echo=TRUE}
# Sort count table by count average
rownames(count_data) = count_data$feature_id
count_data = count_data[, -1]
sorted_ct_table = count_data[order(rowMeans(count_data), decreasing = TRUE), ]
DT::datatable(head(sorted_ct_table, 1000))
```



## Outputs

```{r, echo=FALSE}
tags$div(id="jstree", file_tree(Sys.getenv('REPORT_FILES_PATH')))
```
