emmanuelparadis / ape

analysis of phylogenetics and evolution

Home Page:http://ape-package.ird.fr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can't write tree

lpipes opened this issue · comments

Hello, I can't seem to write this tree. I also get the same error when I try to write in nexus format.

write.tree(tree.fixed,file="tree.nw")
Error in kids[[parent[i]]] : subscript out of bounds

I can't seem to attach the .RData file but the error seems to occur when i is 656235.
kids[[parent[656235]]]<-c(kids[[parent[656235]]],children[656235])
Error in kids[[parent[656235]]] : subscript out of bounds

Specifically:
kids[[parent[656235]]]
Error in kids[[parent[656235]]] : subscript out of bounds

parent[656235]
[1] 6234289

Hi. Hard to see what's going on from these bits of code. Did you run checkValidPhylo() on the tree? If that doesn't help you, you could try to upload the .RData file somewhere and post the link here.

Thanks for your response! I ran checkValidPhylo() and it looks like there is an error
FATAL: some numbers in 'edge' are larger than 'n + m'
This is a phylo object output from MakeTreeBinary() from TreeTools. I am just trying to resolve polytomies in a tree. I also used multi2di() which passed the checkValidPhylo() check but did not pass the is.binary() check. I just want to read in a tree, resolve the polytomies, and then print the binary tree in newick format.

Strange! A tree returned by multi2di() should always appear as binary when passed to is.binary(). If that tree passes checkValidPhylo, then you should be able to write it into a Newick file.

Yes I can write the tree to a newick file but I have another program that needs to read in a binary tree from a newick file.

> tree<-read.tree("global.multi2di.fixed")
> str(tree)
List of 4
$ edge : int [1:6238373, 1:2] 3117133 3117134 3117135 3117135 3117134 3117133 3117136 3117137 3117138 3117139 ...
$ edge.length: num [1:6238373] 14.5 0 0 1 1 14.5 0 0 0 0 ...
$ Nnode : int 3121242
$ tip.label : chr [1:3117132] "EPI_ISL_3355900" "EPI_ISL_3355903" "EPI_ISL_3117762" "EPI_ISL_700223" ...
- attr(*, "class")= chr "phylo"
- attr(*, "order")= chr "cladewise"
> is.binary(tree)
[1] FALSE
> is.binary(multi2di(tree))
[1] FALSE

Your original tree isn't binary not because of multichotomies but because some (internal) nodes are of degree 2 (aka elbow nodes). You can see that by counting how many times each tip or node appears in the tree (i.e., its degree):

R> table(tabulate(tree$edge))

      1       2       3 
3117132    4112 3117130 

The nodes of degree 1 are the tips:

R> Ntip(tree)
[1] 3117132
R> Nnode(tr)
[1] 3121242

The fact that there are more nodes than tips is a clear sign of the presence of elbow nodes (the other way round is not true). Thus, multi2di does not change your tree. A trivial example could be:

R> is.binary(multi2di(read.tree(text = "((a),b);")))
[1] FALSE

Ok so multi2di creates elbow nodes in the tree? This was my original tree that I used multi2di on to resolve the multichotomies.
global.tree.gz

No. multi2di transforms all nodes of degree 4 or more so that they become of degree 3, except for the root node which becomes of degree 2. Elbow nodes are not changed. You can see that with:

R> tree <- read.tree("global.tree.gz")
R> degree <- table(tabulate(tree$edge))
R> degree["2"]
   2
4112

The function collapse.singles removes these elbow nodes (if you want to get rid of them): if you apply it to your tree, the output tree will have one node of degree 2, the root.

Thanks for you help! I used collapse.singles and now am able to read the tree into my program!