RNA*_*RNA 4 r hierarchical-clustering heatmap
我有一个矩阵数据,并希望用热图可视化它.行是物种,所以我想在行旁边显示系统发育树,并根据树重新排列热图的行.我知道heatmapR中的函数可以创建层次聚类热图,但是如何在图中使用我的系统发育聚类而不是默认创建的距离聚类?
pla*_*pus 13
首先,您需要使用包ape将数据作为phylo对象读入.
library(ape)
dat <- read.tree(file="your/newick/file")
#or
dat <- read.tree(text="((A:4.2,B:4.2):3.1,C:7.3);")
Run Code Online (Sandbox Code Playgroud)
以下仅适用于树是超参数的情况.
下一步是将您的系统发育树转换为类dendrogram.
这是一个例子:
data(bird.orders) #This is already a phylo object
hc <- as.hclust(bird.orders) #Compulsory step as as.dendrogram doesn't have a method for phylo objects.
dend <- as.dendrogram(hc)
plot(dend, horiz=TRUE)
Run Code Online (Sandbox Code Playgroud)

mat <- matrix(rnorm(23*23),nrow=23, dimnames=list(sample(bird.orders$tip, 23), sample(bird.orders$tip, 23))) #Some random data to plot
Run Code Online (Sandbox Code Playgroud)
首先,我们需要根据系统发育树中的顺序对矩阵进行排序:
ord.mat <- mat[bird.orders$tip,bird.orders$tip]
Run Code Online (Sandbox Code Playgroud)
然后输入到heatmap:
heatmap(ord.mat, Rowv=dend, Colv=dend)
Run Code Online (Sandbox Code Playgroud)

编辑:这是一个处理超参数和非超参数树的函数.
heatmap.phylo <- function(x, Rowp, Colp, ...){
# x numeric matrix
# Rowp: phylogenetic tree (class phylo) to be used in rows
# Colp: phylogenetic tree (class phylo) to be used in columns
# ... additional arguments to be passed to image function
x <- x[Rowp$tip, Colp$tip]
xl <- c(0.5, ncol(x)+0.5)
yl <- c(0.5, nrow(x)+0.5)
layout(matrix(c(0,1,0,2,3,4,0,5,0),nrow=3, byrow=TRUE),
width=c(1,3,1), height=c(1,3,1))
par(mar=rep(0,4))
plot(Colp, direction="downwards", show.tip.label=FALSE,
xlab="",ylab="", xaxs="i", x.lim=xl)
par(mar=rep(0,4))
plot(Rowp, direction="rightwards", show.tip.label=FALSE,
xlab="",ylab="", yaxs="i", y.lim=yl)
par(mar=rep(0,4), xpd=TRUE)
image((1:nrow(x))-0.5, (1:ncol(x))-0.5, x,
xaxs="i", yaxs="i", axes=FALSE, xlab="",ylab="", ...)
par(mar=rep(0,4))
plot(NA, axes=FALSE, ylab="", xlab="", yaxs="i", xlim=c(0,2), ylim=yl)
text(rep(0,nrow(x)),1:nrow(x),Rowp$tip, pos=4)
par(mar=rep(0,4))
plot(NA, axes=FALSE, ylab="", xlab="", xaxs="i", ylim=c(0,2), xlim=xl)
text(1:ncol(x),rep(2,ncol(x)),Colp$tip, srt=90, pos=2)
}
Run Code Online (Sandbox Code Playgroud)
这是前面的(超参数)示例:
heatmap.phylo(mat, bird.orders, bird.orders)
Run Code Online (Sandbox Code Playgroud)

并且使用非超参数:
cat("owls(((Strix_aluco:4.2,Asio_otus:4.2):3.1,Athene_noctua:7.3):6.3,Tyto_alba:13.5);",
file = "ex.tre", sep = "\n")
tree.owls <- read.tree("ex.tre")
mat2 <- matrix(rnorm(4*4),nrow=4,
dimnames=list(sample(tree.owls$tip,4),sample(tree.owls$tip,4)))
is.ultrametric(tree.owls)
[1] FALSE
heatmap.phylo(mat2,tree.owls,tree.owls)
Run Code Online (Sandbox Code Playgroud)
