liamrevell / phytools

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

Phylo.to.map Error in res[edge[i, 2]] <- res[edge[i, 1]] + el[i] : replacement has length zero #83

Open Kelzor opened 3 years ago

Kelzor commented 3 years ago

I am trying to create a plot with phylo.to.map using the following code.

library(phytools)
library(phylotools)
library(mapdata)

tree <- read.newick("barebones_MP_500BS.nwk")
tipdat <- read.csv("tip_names.csv", header = FALSE)
coord1 <- read.csv("coordinates.csv", header = TRUE)
coord1 <- coord1[ -c(1)]
coord2 <- coord1[,-1]
coord2 <- data.matrix(coord2)
rownames(coord2) <- coord1[,1]

labeledtree <- sub.taxa.label(tree, tipdat)
rootedtree <- root(labeledtree, outgroup= "M. canetti", resolve.root = TRUE)

rootedtree

Phylogenetic tree with 18 tips and 17 internal nodes.

Tip labels:
  L3, L2, L4, L7, L1, L5, ...
Node labels:
  Root, 1.0000, 0.9960, 1.0000, 1.0000, 1.0000, ...

Rooted; no branch lengths.

obj<-phylo.to.map(rootedtree, coord2, plot=FALSE)

objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 46
objective: 26

plot(obj, ftype="i")

Error in res[edge[i, 2]] <- res[edge[i, 1]] + el[i] : 
  replacement has length zero

Everything seems to work as expected until plot(). I can't figure out why a 0 value is being returned. Is it a problem with my tree file? Any help would be greatly appreciated!

tree file: barebones_MP_500BS.txt

tip names file: tip_names.txt

coordinate file: coordinates.txt

twooldridge commented 3 years ago

Hi Kelzor, I'm having an identical issue, and I believe it's because there are no edge lengths in the tree. I would prefer to plot this as a cladogram, as in my case I have a tree generated by ASTRAL (https://github.com/smirarab/ASTRAL/blob/master/astral-tutorial.md#branch-length-and-support) where some terminal branches don't have lengths by design. So I'm unsure how to proceed here. Let me know if you've arrived at a solution, I'll keep troubleshooting

twooldridge commented 3 years ago

Just minutes after posting this, I think I found a quick and dirty solution. I 'reset' my branch lengths with ape using compute.brlen. It will automatically calculate branch lengths for display as a cladogram (where branch lengths are meaningless):

rooted$branch.length = NULL
rooted_cladogram = ape::compute.brlen(rooted)

And then if you use rooted_cladogram, the rest should work, hopefully!

Brock