How to convert a tree to a dendrogram in R?

强颜欢笑 提交于 2019-12-21 05:24:10

问题


How can I convert a tree (which is the output of my Java program) to a dendrogram in R?

Currently, I am converting the tree into the Newick format, using the suggestion given here. And then I use the ape package in R to read the Newick-formatted tree:

library("ape")
cPhylo <- read.tree(file = "gc.tree")

Finally I use as.hclust in R to convert the tree into a dendrogram:

dendrogram <- as.hclust(gcPhylo)

However, the dendrogram requires the branch lengths. Although I insert the branch lengths, I am getting an error saying that the tree is not ultrametric:

Error in as.hclust.phylo(gcPhylo) : the tree is not ultrametric

I guess I am doing something wrong while inserting the branch lengths.

Is there any other way that I can follow? Or how can I insert the branch lengths while converting the tree into the Newick format? Equal branch lengths would be fine.


回答1:


This is an old question, but all of the previous answers require the tree to be made ultrametric before being converted to a dendrogram object.

You can use the DECIPHER package to read a dendrogram object from a Newick formatted file:

dend <- ReadDendrogram(path_to_newick_file)



回答2:


This is an old question, but it has inadequate answers so far. Since I had the same problem, and my googlefoo was having problems finding the answer, here you go:

library("ape")
cPhylo <- read.tree(file = "gc.tree")
dendrogram <- chronos(cPhylo)



回答3:


I think the problem is in fact that your "gc.tree" isn't ultrametric. Make sure that the distance from the root to each tip is the same. The following code works:

library('ape')
tree <- read.tree(text='(((A:4.2,B:4.2):3.1,C:7.3):6.3,D:13.6);')
is.ultrametric(tree)
is.binary.tree(tree)
is.rooted(tree)
hc <- as.hclust.phylo(tree)

but make the tree non-ultrametric (note D's branch length):

tree <- read.tree(text='(((A:4.2,B:4.2):3.1,C:7.3):6.3,D:15);')

and as.hclust.phylo will raise an error.



来源:https://stackoverflow.com/questions/7445684/how-to-convert-a-tree-to-a-dendrogram-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!