YuLab-SMU / treeio

:seedling: Base Classes and Functions for Phylogenetic Tree Input and Output
https://yulab-smu.top/treedata-book/
94 stars 24 forks source link

fix a bug of as.treedata for tbl_df #101

Closed xiangpin closed 1 year ago

xiangpin commented 1 year ago

Description

as.treedata for tbl_df do not work well This pr fixed it.

Related Issue

https://github.com/YuLab-SMU/tidytree/issues/35

Example

library(purrr)
library(tidytree)
library(treeio)

phy <- rtree(20, br = runif)

nodedata <- tibble(node = 21:39, nodeinfo = letters[1:19])
tipdata <- tibble(node = 1:20,  tipinfo = LETTERS[1:20])

phytbl <- as_tibble(phy) %>% 
  left_join(nodedata, by = "node") %>%
  left_join(tipdata, by = "node") %>%
  mutate(label = paste(label, tipinfo))

as.treedata(phytbl)@phylo # Exactly as expected

phytbl <- 
  phytbl %>%
  dplyr::mutate(branchinfo = as.character(NA)) %>%
  dplyr::group_split(branch.length > 0.6) %>%
  modify_at(2, ~ mutate(., branchinfo = "info")) %>%
  dplyr::bind_rows()

# phytbl is a tbl_df class

trda <- phytbl %>% as.treedata(branch.length, label)