下面的这个脚本运行良好,可以获取具有庞大数据集的集群,但我需要将集群放入 newick 文件或文本文件中,以便我可以将其从 R 导出到其他编辑程序,但我找不到将 hclust 转换为 newick 格式的方法,我该怎么做?我觉得new2phylo函数可以完成这项工作,但我们没有设法让它工作。
我非常感谢您的帮助,因为我们到处搜索但找不到解决方案 =(
datos <- read.table("morphclustersred.csv",header=T,sep="t")
head(datos)
distfunc <- function(x) daisy(x,metric="gower")
d <- distfunc(datos)
hclustfunc <- function(x) hclust(x, method="complete")
fit <- hclustfunc(d)
plot(fit)
plot(fit, labels=datos$Species,main='Morphological Clustering')
rect.hclust(fit, k=5, border="red")
您可以使用ape
包中的write.tree
函数来完成。试试这个:
library(ape)
class(fit) # must be hclust class
my_tree <- as.phylo(fit)
write.tree(phy=my_tree, file="exported_tree.newick") # look for the file in your working directory
希望这有帮助。
我只设法通过以下转换步骤从热图中提取.nwk格式:
- 石斛 -->
- hcclust --> phylo --> NWK
我知道这有点黑客,但这是代码:
# Heatmap of data frame 'data' saved as 'heat'
heat <- heatmap.2(as.matrix(data))
# Extract dendrograms for rows and columns from 'heat'
row.dendro <- heat$rowDendrogram
col.dendro <- heat$colDendrogram
# Convert dendrograms to nwk (via .hcclust and .phylo formats!)
as.hclust (row.dendro) ->row.hcclust
as.phylo (row.hcclust) ->row.phylo
write.tree(row.phylo) ->row.nwk
as.hclust (col.dendro) ->col.hcclust
as.phylo (col.hcclust) ->col.phylo
write.tree(col.phylo) ->col.nwk