---
title: "plot.pred_muta"
output: html_document
---

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

```{r}
suppressPackageStartupMessages(library("data.table"))
suppressPackageStartupMessages(library("plyr"))
library(hues) # scale_colour_iwanthue()
suppressPackageStartupMessages(library("RColorBrewer"))
suppressPackageStartupMessages(library("viridis"))
suppressPackageStartupMessages(library("ggplot2"))
suppressPackageStartupMessages(library(scales))
suppressPackageStartupMessages(library(reshape2))
library("pvclust")
library("dendsort")
suppressPackageStartupMessages(library("pheatmap"))
suppressPackageStartupMessages(library("ComplexHeatmap"))
suppressPackageStartupMessages(library("circlize"))
library("wesanderson")
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(dendextend))
sort_hclust <- function(...) as.hclust(dendsort(as.dendrogram(...)))
```


```{r}
path2data <- "human.25k.multi.30way.4gpu4batch32accum11atten8head1152embed.model-1682666429.best_loss.pred_variant.rs7922621.peak.chr10:80491485-80492198.muta_res.txt"
outprfx <- "human.25k.MGC.30way.4gpu4batch32accum11atten8head1152embed.model-1682666429.best_loss.pred_variant.rs7910643.peak.chr10:80491485-80492198.muta_res"

# chr10:80492013 # rs7910643
index = 515:545
```


```{r}
dat <- fread(path2data, sep="\t", header=T)
dat <- as.data.frame(dat)
dat$nuc <- factor(dat$nuc, levels = c('A', 'C', 'T', 'G'))

# norm: 0 to 1, z-score
range01 <- function(x){(x-min(x))/(max(x)-min(x))}

dat.norm01 <- dat
dat.norm01$alt2ref <- range01(dat.norm01$alt2ref)

dat.d <- dcast(dat, index~nuc)
rownames(dat.d) <- dat.d$index
dat.d$index <- NULL

dat.d.norm01 <- t(apply(dat.d, 1, range01))

dat.normz <- dat
dat.normz$alt2ref <- scale(dat.normz$alt2ref)

# zoom in 
dat.zoomin <- dat[which(dat$index %in% index), ]
dat.norm01.zoomin <- dat.norm01[which(dat.norm01$index %in% index), ]
dat.normz.zoomin <- dat.normz[which(dat.normz$index %in% index), ]

# plotting

cols <- rev(colorRampPalette(brewer.pal(8,name="RdBu"))(30))
#cols <- viridis(30)

pdf(paste0(outprfx, ".headtmap.pdf"), width = 12, height = 2)
pheatmap(t(dat.d), cluster_cols = F, cluster_rows = F, color = cols)
pheatmap(t(dat.d[index, ]), cluster_cols = F, cluster_rows = F, color = cols)

brks <- seq(-10,10,length.out=30)  
pheatmap(t(dat.d), cluster_cols = F, cluster_rows = F, color = cols, breaks = brks)
pheatmap(t(dat.d[index, ]), cluster_cols = F, cluster_rows = F, color = cols, breaks = brks)

brks <- seq(-5,5,length.out=30) 
pheatmap(t(dat.d), cluster_cols = F, cluster_rows = F, color = cols, breaks = brks)
pheatmap(t(dat.d[index, ]), cluster_cols = F, cluster_rows = F, color = cols, breaks = brks)
dev.off()

```


# test
```{r}
ggplot(dat, aes(index, nuc, fill= alt2ref)) + 
  geom_tile() + 
  scale_fill_viridis(30) +
  theme_bw()

ggplot(dat[which(dat$index %in% index), ], aes(index, nuc, fill= alt2ref)) + 
  geom_tile() + 
  scale_fill_viridis(30) +
  theme_bw()

ggplot(dat.norm01, aes(index, nuc, fill= alt2ref)) + 
  geom_tile() + 
  scale_fill_viridis(30) +
  theme_bw()

ggplot(dat.normz, aes(index, nuc, fill= alt2ref)) + 
  geom_tile() + 
  scale_fill_viridis(30) +
  theme_bw()

ggplot(dat.zoomin, aes(index, nuc, fill= alt2ref)) + 
  geom_tile() + 
  scale_fill_viridis(30) +
  theme_bw()

ggplot(dat.norm01.zoomin, aes(index, nuc, fill= alt2ref)) + 
  geom_tile() + 
  scale_fill_viridis(30) +
  theme_bw()

ggplot(dat.normz.zoomin, aes(index, nuc, fill= alt2ref)) + 
  geom_tile() + 
  scale_fill_viridis(30) +
  theme_bw()

pheatmap(dat.d.norm01, cluster_cols = F, cluster_rows = F, color = viridis(30))

```