---
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}
if (params$SangerAlignment@inputSource == "ABIF" && !is.null(params$SangerAlignment@parentDirectory)) {
    cat("<li>**Input Parent Directory : **  <span><i>", params$SangerAlignment@parentDirectory, "</i></span></li>")
} else if (params$SangerAlignment@inputSource == "FASTA" && !is.null(params$SangerAlignment@fastaFileName)){
    cat("<li>**Fasta File Name : **  <span><i>", params$SangerAlignment@fastaFileName, "</i></span></li>")
  if (!is.null(params$SangerAlignment@namesConversionCSV)) {
    cat("<li>**Filename Conversion CSV : **  <span><i>", params$SangerAlignment@namesConversionCSV, "</i></span></li>")
  }
}
```
  <li>**Trimming Method : **  <span>*`r params$SangerAlignment@contigList[[1]]@trimmingMethodSC`*</span></li>
  
  <li>**Forward Reads Suffix Regex : **  <span>*`r params$SangerAlignment@suffixForwardRegExp`*</span></li>

  <li>**Reverse Reads Suffic Regex : **  <span>*`r params$SangerAlignment@suffixReverseRegExp`*</span></li>
  
  <li>**Minimum Fraction Call (SA) : **  <span>*`r params$SangerAlignment@minFractionCallSA`*</span></li>
  
  <li>**Maximum Fraction Lost (SA) : **  <span>*`r params$SangerAlignment@maxFractionLostSA`*</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 (params$SangerAlignment@contigsTree$tip.label == '') {
  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>
