liamrevell / phytools

GNU General Public License v3.0
198 stars 56 forks source link

collapse.to.star not working as before #110

Closed dominicev closed 2 years ago

dominicev commented 2 years ago

Had some code to collapse branches based on node values which was working fine a few months ago but no longer. No error codes or anything but it simply doesn't return the tree with any of the corresponding nodes collapsed.

Tried updating R and all packages and still, same issue.

Below is an example where the resulting tree has no collapsed nodes.

data("wasp.trees")
tree<-wasp.trees[[2]]
collapseList<-as.numeric(tree$node.label)<70
collapseList[1]<-FALSE
collapseList<-unlist(lapply(collapseList,function(x) ifelse(is.na(x),TRUE,x)))

{newTree<-tree
for(i in 1:length(collapseList)){
ifelse(collapseList[i],  newTree<-collapse.to.star(newTree,i ),newTree<-newTree)
}
plot(newTree, show.tip.label = FALSE, show.node.label=TRUE)
}

Even the simple example below doesn't seem to do anything

{newTree<-collapse.to.star(wasp.trees[[2]],5 ) 
plot(newTree, show.tip.label = FALSE, show.node.label=TRUE)}
KlausVigo commented 2 years ago

Dear @dominicev,

the function still works, but you used the wrong index:

plot(tree)
nodelabels() 

nodes 1:Ntip(tree) are the tips and Ntip(tree) + 1:Nnode(tree) are the the internal nodes.
The if you want to collapse the i-th node label the index is i + Ntip(tree) E.g.

plot(wasp.trees[[2]])
nodelabels()
{newTree<-collapse.to.star(wasp.trees[[2]], 5+Ntip(wasp.trees[[2]]) ) 
plot(newTree, show.tip.label = FALSE, show.node.label=TRUE)}

I also have a function called pruneTree in phangorn, which might be useful in your case. I improved it a bit, so you want to install the development version:

remotes::install_github("KlausVigo/phangorn")
library(phangorn)
tree2 <- tree
tree2$node.label[tree2$node.label == ""] <- "0"
tree2$node.label[1] <- ""
tree2 <- pruneTree(tree2, 70)
plot(tree2, show.node.label=TRUE)
liamrevell commented 2 years ago

Thanks @KlausVigo.