---
title: "Quality"
author: "Renee Oles"
date: "1/19/2023"
output: html_document
params:
  checkm: checkm
  ani: ani
  passed: passed
  ref: ref
---

```{r, include=FALSE}
library(tidyverse)
require(reshape2)
library(RColorBrewer)
library(pheatmap)
library(DT)
library(plotly)
```

```{r,  include=FALSE}
checkm <- read.delim(params$checkm, check.names = FALSE)
ani <- read.delim(params$ani, check.names = FALSE, col.names = c("Sample", "Reference", "PID", "Orthologous Matches", "Total Sequences"))
passed <- as.data.frame(read.delim(params$passed,col.names = "Sample", header=FALSE))
ref <- params$ref
```


```{r, include=FALSE}
passed$Passed <- "yes"
colnames(checkm)[1] <- "Sample"
checkm <- left_join(checkm, passed)
checkm[is.na(checkm)] <- "no"
ani[is.na(ani)] <- 70
```

## General Summary 
This plot summarizes the contamination and completion of each individual sample.
```{r, out.width="100%"}
# plot the log file with all three variables in the same same graph
data_long <- melt(checkm, id.vars=c("Sample", "Passed"))  # convert to long format
data_long$value <- as.numeric(as.character(data_long$value))
p<-ggplot(data_long, aes(fill=Passed, x=Sample, y=value))+
  geom_bar(stat="identity")+
  theme_classic()+
  ggtitle("CheckM")+
  xlab("Isolates")+
  ylab("")+
  theme(axis.text.x = element_blank())+
  scale_fill_manual(values=c("yes"="#62b587","no"="grey"))+
  facet_wrap(~variable, scale = "free_y", nrow=3)
ggplotly(p)
```


## Stats Summary 
This plot summarizes the assembly stats into a histogram. Samples are colored by whether or not they pass all the filter requirements.
```{r, out.width="100%"}
# plot the log file with all three variables in the same same graph
p <- ggplot(data_long, aes(fill=Passed, x=Sample, y=value))+
  geom_bar(stat="identity")+
  theme_classic()+
  ggtitle("CheckM")+
  xlab("Isolates")+
  ylab("")+
  theme(axis.text.x = element_blank())+
  scale_fill_manual(values=c("yes"="#62b587","no"="grey"))+
  theme(panel.spacing.x=unit(0, "lines") , panel.spacing.y=unit(1,"lines"))+
  facet_wrap(~variable, scale = "free_y", nrow = 5)
ggplotly(p, height = 1500, width=1500)
```

## Sample-by-Sample Statistics

```{r}
checkm$Contig_N50 <- as.numeric(as.character(checkm$Contig_N50))
p<-ggplot(checkm, aes(y=Contig_N50, x=Sample, fill=Passed))+
  geom_bar(stat="identity")+
  theme_classic()+
  ggtitle("CheckM: N50 (contigs)")+
  xlab("Isolates")+
  ylab("")+
  guides(fill=guide_legend(title="Passed"))+
  theme(axis.text.x = element_blank())+
  scale_fill_manual(values=c("yes"="#62b587","no"="grey"))
ggplotly(p)
```


```{r}
checkm$Genome_Size <- as.numeric(as.character(checkm$Genome_Size))
p<-ggplot(checkm, aes(y=Genome_Size, x=Sample, fill=Passed))+
  geom_bar(stat="identity")+
  theme_classic()+
  ggtitle("CheckM: Genome size")+
  xlab("Isolates")+
  ylab("")+
  guides(fill=guide_legend(title="Oassed"))+
  theme(axis.text.x = element_blank())+
  scale_fill_manual(values=c("yes"="#62b587","no"="grey"))
ggplotly(p)
```

```{r}
checkm$GC_Content <- as.numeric(as.character(checkm$GC_Content))
p<-ggplot(checkm, aes(y=GC_Content, x=Sample, fill=Passed))+
  geom_bar(stat="identity")+
  theme_classic()+
  ggtitle("CheckM: GC")+
  xlab("Isolates")+
  ylab("")+
  guides(fill=guide_legend(title="Passed"))+
  theme(axis.text.x = element_blank())+
  scale_fill_manual(values=c("yes"="#62b587","no"="grey"))
ggplotly(p)
```

```{r}
checkm$Total_Coding_Sequences <- as.numeric(as.character(checkm$Total_Coding_Sequences))
p<-ggplot(checkm, aes(y=Total_Coding_Sequences, x=Sample, fill=Passed))+
  geom_bar(stat="identity")+
  ggtitle("CheckM: Total coding sequences")+
  theme_classic()+
  xlab("Isolates")+
  ylab("")+
  guides(fill=guide_legend(title="Passed"))+
  theme(axis.text.x = element_blank())+
  scale_fill_manual(values=c("yes"="#62b587","no"="grey"))
ggplotly(p)
```

## Statistic tables
The following tables show the details for each of the assembly quality control parameters
```{r}
datatable(checkm, rownames=F, filter='top')
```

```{r}
datatable(ani, rownames=F, filter='top')
```

