How to get correct order of tip labels in APE after calling ladderize function

前端 未结 2 1417
说谎
说谎 2021-01-04 09:02

I\'m trying to order the rows of a dataframe based on the tip labels found in a phylogenetic tree. The way I was going to do this was to use the match function

相关标签:
2条回答
  • 2021-01-04 09:55

    for different package (ggtree), but same issue I quote, "To get tip order of a ggtree plot, do something like this: data(bird.orders) p <- ggtree::ggtree(bird.orders) p[["data"]] # extract 'label' and 'y' columns

    coming from this website, and package... though how many R packages does it take to change a light bulb? https://rdrr.io/github/joelnitta/jntools/man/get_tips_in_ape_plot_order.html

    0 讨论(0)
  • 2021-01-04 09:59

    It seems the key is to look at the edge property. The tips are always the first nodes to be given an ID, which will simply correspond to the position in the tip.label vector.

    library(ape)
    tree <- read.tree(text = "(((A,B),(C,D)),E);")
    tree2 <- ladderize(tree, right = FALSE)
    tree$tip.label
    #> [1] "A" "B" "C" "D" "E"
    tree2$tip.label
    #> [1] "A" "B" "C" "D" "E"
    plot(tree2)
    nodelabels()
    tiplabels()
    

    First step is to filter out internal nodes from the the second column of the edge matrix:

    is_tip <- tree2$edge[,2] <= length(tree2$tip.label)
    #> [1]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
    
    ordered_tips <- tree2$edge[is_tip, 2]
    #> [1] 5 1 2 3 4
    

    Then you can use this vector to extract the tips in the right order:

    tree2$tip.label[ordered_tips]
    #> [1] "E" "A" "B" "C" "D"
    
    0 讨论(0)
提交回复
热议问题