Skip to content

Instantly share code, notes, and snippets.

@DomBennett
Created May 4, 2016 17:05
Show Gist options
  • Save DomBennett/72ea9843a4f6717fc028badc95e80c2c to your computer and use it in GitHub Desktop.
Save DomBennett/72ea9843a4f6717fc028badc95e80c2c to your computer and use it in GitHub Desktop.
Extract list of node IDs and descendant tips from a phylogeny in R
# Libraries
library(MoreTreeTools) # package in dev, install via github: https://github.com/DomBennett/MoreTreeTools
library(doMC) # use doSNOW if using Windows
# find out how many cores you have
n <- detectCores()
# set-up
registerDoMC(cores=n)
# example tree
tree <- rtree(1000)
# find all internal nodes in tree
nds <- (getSize(tree) + 1):(tree$Nnode + getSize(tree))
# generate inital 'loop dataframe' for plyr
l_data <- data.frame(node=nds, stringsAsFactors=FALSE)
res <- plyr::mlply(.data=l_data, .fun=getChildren, tree=tree, .parallel=TRUE)
res <- res[1:length(res)] # remove the 'attr's
# add children for each tip, which is just the tip, for consistency
res[tree$tip.label] <- tree$tip.label
# Libraries
library(treeman)
library(doMC) # use doSNOW if using Windows
# find out how many cores you have
n <- detectCores()
# set-up
registerDoMC(cores=n)
# random tree, otherwise use readTree()
tree <- randTree(10000)
# get all internal node IDs of tree
nids <- tree['nds']
# find all children for each node
res <- getNdsKids(tree, ids=nids, .parallel=TRUE)
# add children for each tip, which is just the tip, for consistency
res[tree['tips']] <- tree['tips']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment