## 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))


# 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))
```


