【问题标题】:How to create a newick file from a cluster in R?如何从 R 中的集群创建 newick 文件?
【发布时间】:2014-02-12 12:22:06
【问题描述】:

下面的这个脚本运行良好,可以很好地获得包含大量数据集的集群,但我需要将集群放入一个 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")

【问题讨论】:

标签: r cluster-computing hclust


【解决方案1】:

您可以使用 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

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我只能通过以下转换步骤从热图中提取 .nwk 格式:

    • dendro --> 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
    

    【讨论】:

      猜你喜欢
      • 2017-03-02
      • 2014-05-10
      • 2017-09-23
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      相关资源
      最近更新 更多