---
title: "'SangerAlignment' Report"
author: "Author: Kuan-Hao Chao (kuanhao.chao@gmail.com)"
date: "Last update: `r format(Sys.time(), '%d %B, %Y')`"
output:
  html_document:
    toc: true
    toc_float: true
# output: 
#   BiocStyle::html_document:
#     toc_float: true
#     fig_caption: yes
#   BiocStyle::pdf_document: default
#bibliography: bibliography.bib
params:
  SangerAlignment: NULL
  outputDir: ""
  contigsFN: NULL
  colors: NULL
fontsize: 14pt
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
  %\usepackage[utf8]{inputenc}
---

<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>


<div style="margin-left:50px; padding-left:-50;margin-top:50px; margin-bottom:50px; border-left: 4px solid #d3d6db;">
<p style = "font-size: 15px; font-weight: bold; margin-left:10px"><i class="fas fa-arrow-circle-right" style="margin-right: 10px; color: green"></i>SangerAlignment Level</p>
<p style="font-size: 15px; margin-left: 35px; color: gray;">SangerContig Level</p>
<p style='font-size: 15px; margin-left: 35px; color: gray;'>SangerRead Level</p>
</div>

```{r echo=FALSE}
library(sangeranalyseR)
```

# Basic Information:
<details class="description"><summary data-open="CLOSE" data-close="MORE DETAILS"></summary>
<div style="overflow: scroll;">
<ul style="list-style-type:disc;">
  <li>**Output Directory : **  <span>*`r params$outputDir`*</span></li>

```{r results='asis', echo=FALSE, warning=FALSE}
cat("<li>**Input Source : **  <span><i>", params$SangerAlignment@inputSource, "</i></span></li>")
cat("<li>**Process Method : **  <span><i>", params$SangerAlignment@processMethod, "</i></span></li>")
if (params$SangerAlignment@inputSource == "ABIF" && !is.null(params$SangerAlignment@ABIF_Directory)) {
    cat("<li>**Input ABIF Directory : **  <span><i>", params$SangerAlignment@ABIF_Directory, "</i></span></li>")
} else if (params$SangerAlignment@inputSource == "FASTA" && !is.null(params$SangerAlignment@FASTA_File)){
    cat("<li>**Fasta File Name : **  <span><i>", params$SangerAlignment@FASTA_File, "</i></span></li>")
}

if (params$SangerAlignment@processMethod == "REGEX" && !is.null(params$SangerAlignment@REGEX_SuffixForward) && !is.null(params$SangerAlignment@REGEX_SuffixReverse)) {
      cat("<li>**Forward Reads Suffix Regex : **  <span><i>", params$SangerAlignment@REGEX_SuffixForward, "</i></span></li>")
      cat("<li>**Reverse Reads Suffix Regex : **  <span><i>", params$SangerAlignment@REGEX_SuffixReverse, "</i></span></li>")
} else if (params$SangerAlignment@processMethod == "CSV" && !is.null(params$SangerAlignment@CSV_NamesConversion)) {
        cat("<li>**Names Conversion CSV file : **  <span><i>", params$SangerAlignment@CSV_NamesConversion, "</i></span></li>")
} 
```
  
</ul>
</div>
</details>

---

# Contigs Consensus

<details class="description"><summary data-open="CLOSE" data-close="MORE DETAILS"></summary>
<h3> Primary Sequence </h3>
<div style="overflow: scroll;">
<br>
```{r echo=FALSE, warning=FALSE}
if (params$colors == "default") {
  A_color = "#1eff00"
  T_color = "#ff7a7a"
  C_color = "#7ac3ff"
  G_color = "#c9c9c9"
  unknown_color = "purple"
} else if (params$colors == "cb_friendly") {
  A_color = rgb(122, 122, 122, max = 255)
  T_color = rgb(199, 199, 199, max = 255)
  C_color = rgb(0, 114, 178, max = 255)
  G_color = rgb(213, 94, 0, max = 255)
  unknown_color = rgb(204, 121, 167, max = 255)
} else {
  A_color = colors[1]
  T_color = colors[2]
  C_color = colors[3]
  G_color = colors[4]
  unknown_color = colors[5]
}
contigSeqsCon <- unlist(strsplit(as.character(params$SangerAlignment@contigsConsensus), ""))
contigSeqsConDF <- data.frame(t(data.frame(contigSeqsCon)), stringsAsFactors = FALSE)
colnames(contigSeqsConDF) <- substr(colnames(contigSeqsConDF), 2, 100)
rownames(contigSeqsConDF) <- NULL
AstyleList <- SetCharStyleList(contigSeqsConDF, "A", A_color)
TstyleList <- SetCharStyleList(contigSeqsConDF, "T", T_color)
CstyleList <- SetCharStyleList(contigSeqsConDF, "C", C_color)
GstyleList <- SetCharStyleList(contigSeqsConDF, "G", G_color)
styleList <- c(AstyleList, TstyleList, CstyleList, GstyleList)
suppressWarnings(suppressMessages(
    excelTable(data = contigSeqsConDF, defaultColWidth = 30,
               editable = FALSE, rowResize = FALSE,
               columnResize = FALSE, allowInsertRow = FALSE,
               allowInsertColumn = FALSE, allowDeleteRow = FALSE,
               allowDeleteColumn = FALSE, allowRenameColumn = FALSE,
               style = styleList, loadingSpin = TRUE, autoWidth = FALSE)
))
```
</div>
</details>

---

# Contigs Alignment

<details class="description"><summary data-open="CLOSE" data-close="MORE DETAILS"></summary>
<div style='overflow: scroll;'>
```{r results='asis', echo=FALSE, warning=FALSE}
browseSeqHTML <- file.path(params$outputDir, "Contigs_Alignment_BrowseSeqs.html")
BrowseSeqs(DNAStringSet(params$SangerAlignment@contigsAlignment),
           openURL=FALSE, htmlFile=browseSeqHTML)
shiny::includeHTML(browseSeqHTML)
```
</div>
</details>

---

# Contigs Tree

<details class="description"><summary data-open="CLOSE" data-close="MORE DETAILS"></summary>
<div style="overflow: scroll;">
```{r results='asis', echo=FALSE, warning=FALSE}
if (length(params$SangerAlignment@contigsTree$tip.label) == 0) {
  cat("<p style='font-size: 13px; font-weight:bold'> The number of contigs is less than 3 or quality of reads are too low. 'Contigs Tree' cannot be created. </p>")
} else {
  plot(params$SangerAlignment@contigsTree) 
}
```
</div>
</details>

---

<br>
<br>
<h1 style="font-weight: bold; color:"><i style="margin-left: 20px" class="fa fa-chevron-circle-right"></i> Contigs Reports </h1>

<details class="description"><summary data-open="CLOSE" data-close="MORE DETAILS"></summary>
<ul style="list-style-type:disc;">
```{r, results='asis', echo=FALSE, warning=FALSE}
if (!is.null(params$contigsFN)) {
  for (i in 1:length(params$contigsFN)){
      cat('<li style = "margin-top: 35px; font-weight: bold">')
      cat('<a style = "margin-top: -10px" role="button" class="btn btn-info" href="',   file.path(".", basename(dirname(params$contigsFN[[i]])), basename(params$contigsFN[[i]])), '">')
      cat(paste0('Go to Contig: "', basename(params$SangerAlignment@contigList[[i]]@contigName), '" Report'))
      cat('</a>\n') 
      cat('</li>')
  }
}
```
</ul>
```{r, results='asis', echo=FALSE, warning=FALSE}
if (is.null(params$contigsFN)) {
    cat("<p style='font-size: 13px; font-weight:bold'> 'includeSangerContig' is 'FALSE'. Contigs are not included in this report. </p>")
}
```
</details>

---


<style>
li{
    margin-top: 16px;
}
li:first-child {
    margin-top:0;
}

.tab {position:absolute;left:380px; }

.jexcel_content { 
    height: 60px !important;
}

details.description[open] summary::after {
  content: attr(data-open);
  color: #d45050;
  border-bottom: 2px solid #d45050;
}

details.description:not([open]) summary::after {
  content: attr(data-close);
  color: #4c9c6b;
  border-bottom: 2px solid #4c9c6b;
}

.description {
border-left: 4px solid #d3d6db;
padding-left: 10px;
}
.description summary{
  font-weight: bold;
  font-size: 10px;
  color: #383838;
}

span {
  white-space:nowrap;
}

pre {
  display: inline-block;
}
</style>
