gpt4 book ai didi

r - 从热图中提取树状图时出错

转载 作者:行者123 更新时间:2023-12-02 04:25:34 26 4
gpt4 key购买 nike

我正在尝试使用 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)

Error: all(vapply(s, is.integer, NA)) is not TRUE

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

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

最佳答案

heatmap.2 使用 hclust 构建树状图。如果您想要的只是树状图,则没有理由使用它。如果原因是您想要树状图的排序,只需使用 dendextend package 中的排序即可以获得您想要的订单。否则,就留在 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")

关于r - 从热图中提取树状图时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36482253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com