Dear Joseph, Nick, and Liam, Gosh I love this mailing list. Thank you all for such a rapid response!
So my example was terrible and shouldn't have had the 'knuckles' in there, so that was a bit of a red herring. My apologies! Nonetheless Liam, your code did resolve my issue! I would never have gotten there on my own so thanks very much. Best, Alex On Wed, Nov 20, 2024 at 2:07 PM Liam J. Revell <[email protected]> wrote: > Dear Alex. > > The trouble is that tips are always numbered 1 through N (for N tips) in > the edge matrix, and internal nodes N+1 through N+m (for m nodes). For more > info on the structure of a "phylo" object, I recommend checking out > chapters 1 & 13 of my book with Luke Harmon. ;) > > Getting from your case to a "phylo" object should be possible, though the > example was especially difficult to figure out because nodes & tips are > intermingled in order, and the number of tips is actually 3 not 4, as > indicated in your code. (This can be seen because only three elements, 2, > 6, & 7, are found in the second column of your edge matrix, but not the > first.) > > You might try something like this. > > ## first create edge matrix > edge<-as.matrix(edgetable[,c("parent", "node")]) > ## find nodes > nodes<-unique(edge[,1]) > ## find tips > tips<-setdiff(edge[,2],edge[,1]) > ## substitute nodes for negative integer series > ## and tips for 1:N > snodes<--(length(tips)+1:length(nodes))[order(nodes)] > stips<-(1:length(tips))[order(tips)] > for(i in 1:length(nodes)) > edge[which(edge==nodes[i])]<-snodes[i] > for(i in 1:length(tips)) > edge[which(edge==tips[i])]<-stips[i] > ## set edge to absolute value > edge<-abs(edge) > ## create phylo object > tree <- list(edge=edge, > edge.length=edgetable[,"brlen"], > tip.label = paste0("species_", 1:length(tips)), > Nnode = length(nodes)) > ## assign class > class(tree) <- "phylo" > > Let us know if it works. > > Best wishes, Liam > Liam J. Revell > Professor of Biology, University of Massachusetts Boston > Web: http://faculty.umb.edu/liam.revell/ > Book: Phylogenetic Comparative Methods in R > <https://press.princeton.edu/books/phylogenetic-comparative-methods-in-r> > (*Princeton University Press*, 2022) > > > On 11/19/2024 7:06 PM, Alex Skeels wrote: > > [You don't often get email from [email protected]. Learn why this is > important at https://aka.ms/LearnAboutSenderIdentification ] > > CAUTION: EXTERNAL SENDER > > Hi everyone, > > I have a problem that I've been stuck on for a bit. I am running a custom > simulation which keeps track of species divergences in an edge table > containing parent, descendent, and branch length information. Because it is > a forward-in-time sim it starts labelling nodes at "1" which is different > to how nodes are labelled in ape. > > This is giving me problems reconstructing a tree from this table as I get > the Error in plot.phylo(tree) : tree badly conformed; cannot plot. Check > the edge matrix." when using my numbering system. It's not super intuitive > to know exactly what ape is expecting here and I'm wondering if anyone > knows and whether there's a straight forward way to convert to that format? > > Here's a reproducible example of what I'm talking about. Any ideas how to > resolve this? > > library(ape) > > # set up an edgetable > edgetable <- data.frame(parent=c(0, 1, 1, 3, 3, 4, 5), > node=c(1,2,3,4,5,6,7), brlen=c(0.7, 5.6, 4.9, 0.6, 0.7, 0.1, 0.1)) > > # get the tips "nodes" > terminal_branches <- edgetable$node[which(edgetable$node %in% > edgetable$parent)] > > # create list with necessary ingredients > tree <- list(edge=edgetable[,c("parent", "node")], > edge.length=edgetable[,"brlen"], > tip.label = paste0("species_", terminal_branches), > Nnode = length(terminal_branches)-1) > > # turn list into phylo > class(tree) <- "phylo" > > # ok looks good > tree > > # plot > plot(tree) > # "Error in plot.phylo(tree) : tree badly conformed; cannot plot. Check the > edge matrix." > > Thanks in advance!! > > Cheers, > > Alex Skeels > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-phylo mailing list - > [email protected]https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-phylo&data=05%7C02%7Cliam.revell%40umb.edu%7Cdf6198635af840cd12ab08dd0908bdb6%7Cb97188711ee94425953c1ace1373eb38%7C0%7C0%7C638676655363032562%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=hMAo1yyrTefivbX4cUVBvi2QRZB0VaFxh5jg9F4c3%2Bo%3D&reserved=0 > <https://stat.ethz.ch/mailman/listinfo/r-sig-phylo> > Searchable archive at > https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.mail-archive.com%2Fr-sig-phylo%40r-project.org%2F&data=05%7C02%7Cliam.revell%40umb.edu%7Cdf6198635af840cd12ab08dd0908bdb6%7Cb97188711ee94425953c1ace1373eb38%7C0%7C0%7C638676655363051024%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=iD7uSUUb%2BX6h2EkVmk%2BuZerwrp98hZ88My1kyHfYfXo%3D&reserved=0 > <http://www.mail-archive.com/[email protected]/> > > [[alternative HTML version deleted]] _______________________________________________ R-sig-phylo mailing list - [email protected] https://stat.ethz.ch/mailman/listinfo/r-sig-phylo Searchable archive at http://www.mail-archive.com/[email protected]/
