#################################################
# E06: heatmap 熱圖                             #
# 吳漢銘 國立政治大學統計學系                   #
# https://hmwu.idv.tw                           #
#################################################

# 11/25 

# install.packages("fields")
library(fields)
gbr <- two.colors(start="green", middle="black", end="red")
cell.raw <- read.table("trad_alpha103.txt", row.names=1, header=T)
cell.data <- t(scale(t(cell.raw[,2:19]), center=T, scale=T))    
n <- nrow(cell.data)
p <- ncol(cell.data)
gene.phase <- cell.raw[,1]
range(cell.data)
cell.data[cell.data > 2.802712] <- 2.802712 
cellcycle.color <- c("darkgreen", "blue", "red", "gray50", "orange")
rc <- cellcycle.color[gene.phase+1]
cc <- rainbow(ncol(cell.data))

hv1 <- heatmap(cell.data[n:1,], col = gbr, Colv=NA, Rowv=NA,
               RowSideColors = rc, 
               ColSideColors = cc, margins = c(5,10),
               xlab = "Times", ylab =  "Genes",main = "Heatmap of Microarray Data")

hv2 <- heatmap(cell.data, col = gbr, Colv=NA, Rowv=NULL,
               RowSideColors = rc, 
               ColSideColors = cc, margins = c(5,10),
               xlab = "Times", ylab =  "Genes",main = "Heatmap of Microarray Data")

dd <- as.dendrogram(hclust(as.dist(1-cor(t(cell.data)))))
hv3 <- heatmap(cell.data, col = gbr, Colv=NA, Rowv=dd,
              RowSideColors = rc, 
              ColSideColors = cc, margins = c(5,10),
              scale = "row",
              xlab = "Times", ylab =  "Genes",main = "Heatmap of Microarray Data")



# 13/25

library(pheatmap)
DESeq_subset <- read.csv("data/DESeq_subset.csv")
dim(DESeq_subset)
head(DESeq_subset)
DESeq.X <- as.matrix(DESeq_subset[,2:ncol(DESeq_subset)])
colnames(DESeq.X)
rownames(DESeq.X) <- DESeq_subset[,1]
dimnames(DESeq.X)
str(DESeq.X)
pheatmap(DESeq.X)
pheatmap(DESeq.X, cluster_rows = F, cluster_cols = F)

pheatmap(as.matrix(iris[,1:4]), cluster_rows = F, cluster_cols = F)


DESeq.X.std <- t(apply(DESeq.X, 1, scale)) 
classE
dimnames(DESeq.X.std) <- dimnames(DESeq.X)
pheatmap(DESeq.X.std) # note the color spectrum



# 14/25
sample.group <- data.frame(cell = rep(c("tumour", "normal"), c(4,2)),
                           gender = sample(c("male", "female"), 6, replace=T))
row.names(sample.group) <- colnames(DESeq.X)
sample.group

gene.cluster <- data.frame(KMcluster = as.character(kmeans(DESeq.X.std, 2)$cluster))
row.names(gene.cluster) <- rownames(DESeq.X)
head(gene.cluster)


pheatmap(DESeq.X.std,
         annotation_row = gene.cluster,
         annotation_col = sample.group)

pheatmap(DESeq.X.std,
         annotation_row = gene.cluster,
         annotation_col = sample.group,
         cutree_rows = 4,
         cutree_cols = 2,
        display_numbers = TRUE)




# 15/25
if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("ComplexHeatmap") 
library(ComplexHeatmap)
# Heatmap(exprs(selected.eset))


# 16/25
head(mtcars)
str(mtcars)
mtcars.df <- scale(mtcars)
class(mtcars.df)
library(ComplexHeatmap) 
? Heatmap


# 17/25
Heatmap(mtcars.df)

Heatmap(mtcars.df, name = "mtcars", 
        row_title = "Samples",
        column_title = "Variables")


# 18/25
Heatmap(mtcars.df, name = "mtcars", 
        row_title = "Samples",
        column_title = "Variables",
        cluster_rows = FALSE,         
        column_dend_side = "bottom")

Heatmap(mtcars.df, name = "mtcars",  
        rect_gp = gpar(col = "white", lwd = 1),
        border = TRUE,
        row_names_gp = gpar(fontsize = 7))

# 19/25
Heatmap(mtcars.df, name = "mtcars",         
        cell_fun = function(j, i, x, y, width, height, fill) {
          grid.text(sprintf("%.1f", mtcars.df[i, j]), x, y, 
                    gp = gpar(fontsize = 8))},
        row_names_gp = gpar(fontsize = 7))

Heatmap(mtcars.df, name = "mtcars", 
        clustering_distance_rows = "pearson",
        clustering_method_rows = "average",
        row_names_gp = gpar(fontsize = 7))


# 20/25
library(fields)
Heatmap(mtcars.df, name = "mtcars", 
        col = tim.colors(),
        row_names_gp = gpar(fontsize = 7))

library(circlize) # Circular Visualization
mycolorRamp <- colorRamp2(c(-3, 0, 3), c("green", "black", "red"))
Heatmap(mtcars.df, name = "mtcars", 
        col = mycolorRamp,
        row_names_gp = gpar(fontsize = 7))


# 21/25
library(dendextend)
row.dend <- hclust(dist(mtcars.df)) 
column.dend <- hclust(dist(t(mtcars.df))) 
Heatmap(mtcars.df, name = "mtcars",         
        cluster_rows = color_branches(row.dend, k = 4),
        cluster_columns = color_branches(column.dend, k = 2),
        row_names_gp = gpar(fontsize = 7))

# plit the dendrogram using k-means 
Heatmap(mtcars.df, name = "mtcars",         
        row_km = 4,
        column_km = 3,
        row_names_gp = gpar(fontsize = 7))

# 22/25
# split by a vector specifying rowgroups
Heatmap(mtcars.df, name = "mtcars",         
        row_split = mtcars$cyl,
        row_names_gp = gpar(fontsize = 7))


# Split by combining multiple variables
Heatmap(mtcars.df, name ="mtcars", 
        row_split = data.frame(cyl = mtcars$cyl, am = mtcars$am),
        row_names_gp = gpar(fontsize = 7))



# install.packages("ggplot2")
library(ggplot2)
ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
 geom_point(size=3)


# 23/25
# install.packages("cluster")
library(cluster)
set.seed(12345)
pa <- pam(mtcars.df, k = 3)
Heatmap(mtcars.df, name = "mtcars", 
        row_split = paste0("PAM C", pa$clustering),
        row_names_gp = gpar(fontsize = 7))


# 24/25
# HeatmapAnnotation for "top_annotation", "bottom_annotation"
# rowAnnotation for "left_annotation", "right_annotation"
var.colors <- list(cyl = c("4" = "green", "6" = "gray", "8" = "darkred"),
                   am = c("0" = "yellow", "1" = "orange"),
                   mpg = colorRamp2(c(17, 25), c("lightblue", "purple")))

ha <- rowAnnotation(cyl = mtcars$cyl, am = mtcars$am, mpg = mtcars$mpg,
                    col = var.colors)

myvars <- colnames(mtcars.df) %in% c("cyl", "am", "mpg")
Heatmap(mtcars.df[, !myvars], name = "mtcars",
        left_annotation = ha,
        row_names_gp = gpar(fontsize = 7))


# 25/25
# anno_points, anno_barplot, anno_boxplot, anno_density, anno_histogram
h <- anno_histogram(mtcars.df, gp = gpar(fill = "lightblue"))
d <- anno_density(mtcars.df, type = "line", gp = gpar(col = "blue"))
ha.top <- HeatmapAnnotation(histogram = h, density = d, height = unit(3.8, "cm"))

v <- anno_density(mtcars.df, type = "violin", which = "row")
b <- anno_boxplot(mtcars.df, which = "row")
ha.right <- HeatmapAnnotation(violin = v, boxplot = b, which = "row", width = unit(4, "cm"))

Heatmap(mtcars.df, name = "mtcars",         
        top_annotation = ha.top,
        right_annotation = ha.right,
        row_names_gp = gpar(fontsize = 7))

