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 [email protected]. Learn why this is 
> important athttps://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://stat.ethz.ch/mailman/listinfo/r-sig-phylo
> Searchable archive athttp://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]/

Reply via email to