3

我正在尝试使用 heatmap.2 函数从二进制矩阵创建热图,并从热图中提取树状图,并将树状图保存为 newick 文件格式。该矩阵在行上有基因组,在列上有基因。

为此,我正在运行以下代码。

library(ggdendro)
library(ape)
library(gplots)
library(vegan)

profile <-as.data.frame(read.delim("profile_file.txt",row.names=1,sep="\t", as.is=T))

dist_func<-function(x) vegdist(x,"jaccard",binary=TRUE)

hclust_func<-function(x) hclust(x,method="ward.D2")

heat<-heatmap.2(as.matrix(profile),Rowv=TRUE,Colv=TRUE,distfun=dist_func,hclustfun=hclust_func,trace="none")

row.dendro<-heat$rowDendrogram

row.hcclust<-as.hclust(row.dendro)

row.phylo<-as.phylo(row.hcclust)

write.tree(phy=row.phylo, file="tree_file.nwk")

当我尝试运行它将完整配置文件时,此代码运行良好。但是当我减少基因列的数量时,我在尝试将树状图转换为 hclust 对象的步骤中遇到错误。

row.hcclust<-as.hclust(row.dendro)

错误:all(vapply(s, is.integer, NA)) 不是 TRUE

我尝试在我的数据集中查找任何“NA”值,但没有,否则它也不应该适用于完整数据集。

任何人都可以帮我解决这个错误吗?或建议发生此错误的可能原因是什么?

4

1 回答 1

1

heatmap.2 使用 hclust 构建树状图。如果您想要的只是树状图,则没有理由使用它。如果原因是您想要树状图的排序,只需使用dendextend 包中的 sort即可获得所需的顺序。否则,请继续使用 hclust。方法如下:

library(ape); library(vegan); 
library(magrittr) # for the pipes: %>%

profile <- as.data.frame(read.delim("profile_file.txt",row.names=1,sep="\t", as.is=T))
row_hclust <- profile %>% as.matrix %>% t %>% vegdist("jaccard",binary=TRUE) %>% hclust(method="ward.D2")
row.phylo <- as.phylo(row_hclust)
write.tree(phy=row.phylo, file="tree_file.nwk")
于 2016-04-08T06:54:50.927 回答