Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tree_subset method for both phylo and treedata objects #8

Merged
merged 7 commits into from
May 24, 2018

Conversation

tbradley1013
Copy link
Contributor

Description

This PR adds a function for subsetting phylogenetic trees by specifying the desired node to focus on and the number of ancestry levels back to look. It returns an object of class phylo or treedata, depending on the input, with all related nodes to the one specified.

Related Issue

This fixes YuLab-SMU/tidytree#5 and is related to a similar PR (YuLab-SMU/tidytree#6) but moved the implementation to this package rather then tidytree.

Example

library(treeio)
library(ggplot2)
library(ggtree)
#> ggtree v1.13.0.002  For help: https://guangchuangyu.github.io/software/ggtree
#> 
#> If you use ggtree in published research, please cite:
#> Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36, doi:10.1111/2041-210X.12628

set.seed(42)

# example with a phylo object
bi_tree <- ape::rtree(10)
bi_tree$tip.label <- paste0("t", 1:10)

bi_tree %>% 
  ggtree() +
  geom_tiplab()

bi_subset <- tree_subset(bi_tree, "t5", 4)

bi_subset %>% 
  ggtree(aes(color = group)) + 
  geom_tiplab() +
  scale_color_manual(values = c(`1` = "red", `0` = "black"))

#example with treedata object
beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
rst_file <- system.file("examples/rst", package="ggtree")
mlc_file <- system.file("examples/mlc", package="ggtree")
beast_tree <- read.beast(beast_file)
codeml_tree <- read.codeml(rst_file, mlc_file)

merged_tree <- merge_tree(beast_tree, codeml_tree)

merged_tree %>% 
  ggtree() + 
  geom_tiplab() +
  theme_tree2() +
  lims(x = c(0, 25))

merged_subset <- tree_subset(merged_tree, "A/Swine/GX/2242/2011", 3)

merged_subset %>%
  ggtree(aes(color = group)) + 
  geom_tiplab() +
  scale_color_manual(values = c(`1` = "red", `0` = "black")) +
  theme_tree2() +
  lims(x = c(0, 2))

Created on 2018-05-23 by the reprex package (v0.2.0).

Tests

All tests can be found in tests/testthat/test-tree-subset.R and all tests pass when devtools::test() is run

@GuangchuangYu GuangchuangYu merged commit 6b9ba34 into YuLab-SMU:master May 24, 2018
@GuangchuangYu
Copy link
Member

thanks @tbradley1013 , pls add your name in author list as a contributor. I do want to acknowledge all contributors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

converting from subsetted tbl_tree to phylo drops labels
2 participants