---
title: "Seurat Results"
author: "`r params$authors`"
date: "`r format(Sys.time(), '%B %d, %Y')`"
params:
  subtitle: !r NULL
  authors: !r NULL
  sce: !r NULL
  biological.group: !r NULL
  phenotype.groups: !r NULL
  selected.markers: !r NULL
  clustering.resolution: 0.8
  pc.count: 10
  outputPath: !r NULL
  showSession: FALSE
  pdf: TRUE
  plotTSNE: TRUE
  plotUMAP: TRUE
  runClustering: TRUE
  minResolution: 0.3
  maxResolution: 1.5
  runMSClusters: TRUE
  runMSBioGroup: TRUE
  numTopFeatures: 10
  forceRun: FALSE
output:
  html_document:
    toc: yes
    toc_float: yes
    number_sections: yes
    theme: cosmo
    code_folding: hide
    self_contained: false
    keep_md: yes
  pdf_document:
    toc: yes
subtitle: "`r params$subtitle`"
editor_options:
  chunk_output_type: console
---

```{r, echo=FALSE}
if(!exists("significant_PC")) significant_PC <- pc.count
```

```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(Seurat)
library(dplyr)
library(cowplot)
library(RColorBrewer)
library(ggplot2)
library(knitr)
library(kableExtra)
library(SingleCellExperiment)
library(scater)
library(gridExtra)
library(grid)
library(ggpubr)
library(patchwork)
library(singleCellTK)
```

# Seurat Results

## Selected Resolution {.tabset .tabset-fade}
A final resolution of **`r clustering.resolution`** was selected for downstream clustering that better reflects the input data.

<!-- Clustering -->
```{r, echo=FALSE, results='hide', warning=FALSE, message=FALSE}
minResolution <- clustering.resolution
maxResolution <- clustering.resolution
numClusters <- 10 #remove this later
showClusterDesc <- FALSE

resClustering <- knitr::knit_child(system.file("rmarkdown/seurat", "reportSeuratClustering.Rmd", package = "singleCellTK"), quiet = TRUE, envir = environment())
```

```{r, echo=FALSE, results='asis', warning=FALSE, message=FALSE}
cat(resClustering, sep = '\n')
```

<!-- Marker Selection (Clusters) -->
```{r markerSelection Clusters, echo=FALSE, warning=FALSE, message=FALSE, eval=runMSClusters}
runMarkerSelection <- TRUE
plotMarkerSelection <- TRUE
numClusters <- length(unique(colData(data)[[paste0("Seurat_louvain_Resolution", clustering.resolution)]]))
selectedOption <- paste0("Seurat_louvain_Resolution", clustering.resolution)
groupTitle <- "Clusters"
titleMarkerPlots <- "Gene Plots of Top Markers by Clusters"
headingMS <- "##"
resMSC <- knitr::knit_child(system.file("rmarkdown/seurat", "reportSeuratMarkerSelection.Rmd", package = "singleCellTK"), quiet = TRUE, envir = environment())
numMarkerGenesCluster <- nrow(data.markers[data.markers$p_val_adj < 0.05, ])
```

<!-- Marker Selection (Group) -->
```{r, echo=FALSE, warning=FALSE, message=FALSE, eval=runMSBioGroup}
runMarkerSelection <- TRUE
plotMarkerSelection <- TRUE
numClusters <- length(unique(colData(data)[[biological.group]]))
selectedOption <- biological.group
groupTitle <- biological.group
titleMarkerPlots <- paste0("Gene Plots of Top Markers by Group: ", biological.group)
headingMS <- "##"
resMSB <- knitr::knit_child(system.file("rmarkdown/seurat", "reportSeuratMarkerSelection.Rmd", package = "singleCellTK"), quiet = TRUE)
numMarkerGenesBio <- nrow(data.markers[data.markers$p_val_adj < 0.05, ])
```

<!-- Pre-selected Markers -->
```{r preselected markers, echo=FALSE, warning=FALSE, message=FALSE, eval=!is.null(selected.markers)}
runMarkerSelection <- FALSE
plotMarkerSelection <- TRUE
groupTitle <- NULL
numClusters <- 1
selectedOption <- paste0("Seurat_louvain_Resolution", clustering.resolution)
titleMarkerPlots <- "Plot Selected Features"
numTopFeatures <- length(selected.markers)
headingMS <- "#"

resPSM <- knitr::knit_child(system.file("rmarkdown/seurat", "reportSeuratMarkerSelection.Rmd", package = "singleCellTK"), quiet = TRUE)
```

```{r, echo=FALSE, results='asis', warning=FALSE, message=FALSE, eval=!is.null(selected.markers)}
cat(resPSM, sep = '\n')
```

```{r, echo=FALSE, results='asis', warning=FALSE, message=FALSE, eval=runMSClusters}
cat(resMSC, sep = '\n')
```

```{r, echo=FALSE, results='asis', warning=FALSE, message=FALSE, eval=runMSBioGroup}
cat(resMSB, sep = '\n')
```

```{r, results='asis', eval=showSession, echo=showSession}
cat("# Session Information\n\n")
```

```{r, results='markup', eval = showSession, echo = showSession}
sessionInfo()
```

