---
title: 'DESeq2: Visualization'
output:
    html_document:
      number_sections: true
      toc: true
      theme: cosmo
      highlight: tango
---

```{r setup, include=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(
  echo = ECHO
)

library(stringi)
library(DESeq2)
library(pheatmap)
# library(PoiClaClu)
library(RColorBrewer)
```

# Import workspace

```{r eval=TRUE}
fcp = file.copy("DESEQ_WORKSPACE", "deseq.RData")
load("deseq.RData")
```

# Visualization

## Heatmaps of sample-to-sample distances {.tabset}

### rlog-transformed values

```{r}
sampleDistMatrix <- as.matrix( sampleDists )
colors <- colorRampPalette( rev(brewer.pal(9, "Blues")) )(255)
pheatmap(sampleDistMatrix,
         # clustering_distance_rows = sampleDists,
         clustering_distance_cols = sampleDists,
         col = colors)
```

### Poisson Distance

```{r eval=FALSE}
count_t = t(counts(dds))
rownames(count_t) = colnames(counts(dds))
poisd <- PoissonDistance(count_t)
samplePoisDistMatrix <- as.matrix( poisd$dd )
rownames(samplePoisDistMatrix) = rownames(count_t)
colnames(samplePoisDistMatrix) = rownames(count_t)
pheatmap(samplePoisDistMatrix,
         # clustering_distance_rows = poisd$dd,
         clustering_distance_cols = poisd$dd,
         col = colors)
```


## PCA plots {.tabset}

### Using `plotPCA()` function 

```{r}
# interest groups
col_index = as.numeric(strsplit("INTGROUPS_PCA", ',')[[1]])
intgroup_pca = colnames(sample_table)[col_index]
```

```{r}
plotPCA(rld, intgroup = intgroup_pca)
```


### Using *ggplot2*

```{r}
pcaData <- plotPCA(rld, intgroup = intgroup_pca, returnData = TRUE)
percentVar <- round(100 * attr(pcaData, "percentVar"))
ggplot(pcaData, aes(x = PC1, y = PC2, color = time)) +
  geom_point(size =3) +
  xlab(paste0("PC1: ", percentVar[1], "% variance")) +
  ylab(paste0("PC2: ", percentVar[2], "% variance")) +
  coord_fixed()
```

### PCA data table

```{r}
knitr::kable(pcaData)
```


## MDS plots {.tabset}

### Using rlog-transformed values

```{r}
mds <- as.data.frame(colData(rld))  %>%
         cbind(cmdscale(sampleDistMatrix))
mds
ggplot(mds, aes(x = `1`, y = `2`, col = time)) +
  geom_point(size = 3) + coord_fixed()
```

### Using the *Poisson Distance*

```{r eval=FALSE}
mdsPois <- as.data.frame(colData(dds)) %>%
   cbind(cmdscale(samplePoisDistMatrix))
ggplot(mdsPois, aes(x = `1`, y = `2`, col = time)) +
  geom_point(size = 3) + coord_fixed()
```
