终于写这个课程的最后一章节了,之前就因为一念之差,拖了不知道多久。这样的事情,我想已经遇到过无数次了,有些事情不及时去解决,那么肯定会拖很久,你自己都没有办法意料到。今天这一章是讲热图。
代码
1. 数据准备
# 如果是第一次运行,那么需要安装complexheatmap包
source("https://bioconductor.org/biocLite.R")
biocLite("ComplexHeatmap")
library(ComplexHeatmap)
# Heatmap from single-cell copy number data
# Select a data file
filename <- "Lesson-08/copy_number_data.txt"
# Read the data into a data.frame
my_data <- read.table(filename, sep="\t", quote="", stringsAsFactors=FALSE,header=TRUE)
head(my_data)
dim(my_data) # (rows columns)
nrow(my_data) # rows: locations (bins) in genome
ncol(my_data) # columns: cells
# Make the heatmap data into a matrix
my_matrix <- as.matrix(my_data[ ,c(4:100)]) # [all rows, columns 4-100]
# leave out the first 3 columns (chrom,start,end) since they don't belong in the heatmap itself
# We can check the classes:
class(my_data)
class(my_matrix)
head(my_matrix)
# Save chromosome column for annotating the heatmap later
chromosome_info <- data.frame(chrom = my_data$CHR)
chromosome_info
2. 热图绘制
## Now we make our first heatmap
# Default parameters
Heatmap(my_matrix)
# Flip rows and columns around
my_matrix <- t(my_matrix) # "transpose"
Heatmap(my_matrix)
# Keep genome bins in order, not clustered
Heatmap(my_matrix, cluster_columns=FALSE)
fontsize <- 0.6
# Put cell labels on the left side
Heatmap(my_matrix, cluster_columns=FALSE,
row_names_side = "left",
row_hclust_side = "left",
row_names_gp=gpar(cex=fontsize))
# Make the dendrogram wider
Heatmap(my_matrix,
cluster_columns=FALSE,
row_names_side = "left",
row_hclust_side = "left",
row_names_gp=gpar(cex=fontsize),
row_hclust_width = unit(3, "cm"))
# Different distance calculation methods
# "euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski", "pearson", "spearman", "kendall"
# euclidean is the default
# Different clustering methods
# "ward.D", "ward.D2", "single", "complete", "average" (= UPGMA), "mcquitty" (= WPGMA), "median" (= WPGMC) or "centroid" (= UPGMC)
# Watch the dendrogram and heatmap change when we change the methods
Heatmap(my_matrix,
cluster_columns=FALSE,
row_names_side = "left",
row_hclust_side = "left",
row_names_gp=gpar(cex=fontsize),
row_hclust_width = unit(3, "cm"),
clustering_distance_rows ="maximum",
clustering_method_rows = "ward.D")
# Coloring clusters in dendrogram
# install dendextend
library(dendextend)
# Need to build dendrogram first so we can use it for the color_brances() function
# 1. calculate distances (method="maximum")
# 2. cluster (method="ward.D")
dend = hclust(dist(my_matrix,method="maximum"),method="ward.D")
Heatmap(my_matrix,
cluster_columns=FALSE,
row_names_side = "left",
row_hclust_side = "left",
row_names_gp=gpar(cex=fontsize),
row_hclust_width = unit(3, "cm"),
cluster_rows = color_branches(dend, k = 3))
# We can split the heatmap into clusters
Heatmap(my_matrix,
cluster_columns=FALSE,
row_names_side = "left",
row_hclust_side = "left",
row_names_gp=gpar(cex=fontsize),
row_hclust_width = unit(3, "cm"),
clustering_distance_rows ="maximum",
clustering_method_rows = "ward.D",
km=2) # number of clusters you want
# Split columns of plot up into chromosomes using extra_info
chromosome_info
chromosome.colors <- c(rep(c("black","white"),11),"red")
chromosome.colors
names(chromosome.colors) <- paste("chr",c(seq(1,22),"X"),sep="")
chromosome.colors
Heatmap(my_matrix,
cluster_columns=FALSE,
row_names_side = "left",
row_hclust_side = "left",
row_names_gp=gpar(cex=fontsize),
row_hclust_width = unit(3, "cm"),
clustering_distance_rows ="maximum",
clustering_method_rows = "ward.D",
km=2, # number of clusters you want
bottom_annotation = HeatmapAnnotation(chromosome_info,col = list(chrom=chromosome.colors),show_legend=FALSE)
)
准备好数据矩阵后直接出图
将矩阵转置,得到的热图
去掉了纵列聚类 将树和ID都放在左侧
使用不同的算法,得到的聚类图是不一样的 image.png 将热图分成几块
根据染色体信息,添加颜色
虽然看着很长,但是就是跑了一遍代码,而且代码的注释写得非常详细,只要你跟着做一遍,那么就比较清楚了。甚至可以无脑出图,但是细节的改变,还是需要去仔细观看这个热图包的详细说明。
网友评论