Skip to content

Commit

Permalink
Updating for merge example
Browse files Browse the repository at this point in the history
  • Loading branch information
DomBennett committed Jun 13, 2017
1 parent a980ba3 commit 94ae8aa
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -70,6 +70,7 @@ export(setNdsSpn)
export(setPD)
export(setTxnyms)
export(taxaResolve)
export(twoer)
export(ultrTree)
export(unblncdTree)
export(updateSlts)
Expand Down
33 changes: 32 additions & 1 deletion R/gen-methods.R
Expand Up @@ -120,4 +120,35 @@ unblncdTree <- function(n, wndmtrx=FALSE, parallel=FALSE) {
tree <- addNdmtrx(tree)
}
tree
}
}

#' @name twoer
#' @title Generate a tree of two tips
#' @description Returns a \code{TreeMan} tree with two tips and a root.
#' @details Useful for building larger trees with \code{addClade()}.
#' Note, a node matrix cannot be added to a tree of two tips.
#' @param tids tip IDs
#' @param spns tip spans
#' @param rid root ID
#' @param root_spn root span
#' @seealso
#' \code{\link{TreeMan-class}}, \code{\link{randTree}}
#' @export
#' @examples
#' library(treeman)
#' tree <- twoer()
twoer <- function(tids=c('t1', 't2'), spns=c(1,1),
rid='root', root_spn=0) {
ndlst <- list()
ndlst[[rid]] <- list('id'=rid, 'prid'=rid,
'ptid'=tids[1:2], 'spn'=root_spn)
ndlst[[tids[1]]] <- list('id'=tids[[1]], 'prid'=rid,
'ptid'=NULL, 'spn'=spns[1])
ndlst[[tids[2]]] <- list('id'=tids[[2]], 'prid'=rid,
'ptid'=NULL, 'spn'=spns[2])
prinds <- c(1, 1, 1)
tinds <- c(2, 3)
tree <- new('TreeMan', ndlst=ndlst, root='root', wtxnyms=FALSE,
ndmtrx=NULL, prinds=prinds, tinds=tinds)
updateSlts(tree)
}
2 changes: 1 addition & 1 deletion R/manip-methods.R
Expand Up @@ -240,7 +240,7 @@ addTip <- function(tree, tid, sid, strt_age=NULL,
#' @name addClade
#' @title Add clade to tree
#' @description Returns a tree with added clade
#' @details Add a \code{TreeMan} object to an exisiting \code{TreeMan}
#' @details Add a \code{TreeMan} object to an existing \code{TreeMan}
#' object by specifying an ID at which to attach. If the id specified
#' is an internal node, then the original clade descending from that
#' node will be replaced. Before running, ensure no IDs are shared
Expand Down
3 changes: 3 additions & 0 deletions R/update-methods.R
Expand Up @@ -74,6 +74,9 @@ updateSlts <- function(tree) {
#' tree <- addNdmtrx(tree)
#' summary(tree)
addNdmtrx <- function(tree, shared=FALSE, ...) {
if(tree@ntips < 3) {
stop('Too small for node matrix.')
}
if(!checkTreeMan(tree)) {
stop('Invalid tree')
}
Expand Down
Binary file modified data/birds.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/addClade.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions man/twoer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions other/4_merge.R
@@ -0,0 +1,37 @@
# LIB
library(treeman)

# Load BIG trees and get ages
data(mammals)
data(birds)
mml_age <- getAge(mammals)
brd_age <- getAge(birds)

# Rename internal nodes
mammals['nds'][1:10]
birds['nds'][1:10] # possible node IDs are not unique
mml_nds <- paste0('mml_', mammals['nds'])
mml_nds[1:10]
avs_nds <- paste0('avs_', birds['nds'])
mammals <- setNdsID(mammals, mammals['nds'], mml_nds)
birds <- setNdsID(birds, birds['nds'], avs_nds)

# Generate incipient tree
tree <- twoer()

# Add to mammals and birds to tree
tree <- setNdsID(tree, c('t1', 't2'), c('Mammalia', 'Aves'))
# set spans to make tree ultrametric
tree <- setNdSpn(tree, 'Aves', abs(brd_age - mml_age) + 1)
# list of form: id=txnym
txnyms <- list('Aves'='Aves', 'Mammalia'= 'Mammalia',
'root'='Amniota')
tree <- setTxnyms(tree, txnyms)
tree <- addClade(tree=tree, id='Mammalia', clade=mammals)
tree <- addClade(tree=tree, id='Aves', clade=birds)

# Complete
tree[['root']]
summary(tree)
# Note, plotting takes time.... have to convert to APE
plot(as(tree, 'phylo'), show.tip.label=FALSE)
2 changes: 1 addition & 1 deletion other/birds.tre

Large diffs are not rendered by default.

Binary file added other/merge.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/testthat/test-gen-methods.R
Expand Up @@ -30,4 +30,8 @@ test_that('unblncdTree() works', {
tree_age <- getAge(tree)
expect_that(tree['pd'], is_more_than(tree_age))
}
})
test_that('twoer() works', {
tree <- twoer()
expect_equal(tree['ntips'], 2)
})

0 comments on commit 94ae8aa

Please sign in to comment.