Skip to content

Commit

Permalink
Now convert between multiPhylo and TreeMen
Browse files Browse the repository at this point in the history
  • Loading branch information
DomBennett committed Apr 28, 2017
1 parent 6ecee6d commit 40b46de
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -69,6 +69,7 @@ export(writeTrmn)
exportClasses(Node)
exportClasses(TreeMan)
exportClasses(TreeMen)
exportClasses(multiPhylo)
exportClasses(phylo)
exportMethods("[")
exportMethods("[[")
Expand Down
52 changes: 51 additions & 1 deletion R/cnvrt-methods.R
Expand Up @@ -6,11 +6,21 @@
#' @exportClass phylo
setOldClass('phylo')

#' multiPhylo class
#'
#' @name multiPhylo-class
#' @aliases multiPhylo
#'
#' @exportClass multiPhylo
setOldClass('multiPhylo')

#' @name TreeMan-to-phylo
#' @title Convert TreeMan to phylo
#' @description Return ape's \code{phylo} from a \code{TreeMan}
#' @seealso
#' \code{\link{phylo-to-TreeMan}},
#' \code{\link{TreeMen-to-multiPhylo}}
#' \code{\link{multiPhylo-to-TreeMen}}
#' \code{\link{TreeMan-class}}
#' @examples
#' library(treeman)
Expand All @@ -29,6 +39,8 @@ setAs(from="phylo", to="TreeMan", def=function(from, to) {
#' @description Return a \code{TreeMan} from ape's \code{phylo}
#' @seealso
#' \code{\link{TreeMan-to-phylo}},
#' \code{\link{TreeMen-to-multiPhylo}}
#' \code{\link{multiPhylo-to-TreeMen}}
#' \code{\link{TreeMan-class}}
#' @examples
#' library(treeman)
Expand All @@ -42,4 +54,42 @@ setAs(from="TreeMan", to="phylo", def=function(from, to) {
return(tree)
})

# TODO: multiphylo conversions
#' @name multiPhylo-to-TreeMen
#' @title Convert multiPhylo to TreeMen
#' @description Return a \code{TreeMen} from ape's \code{mutlPhylo}
#' @seealso
#' \code{\link{TreeMan-to-phylo}},
#' \code{\link{phylo-to-TreeMan}},
#' \code{\link{TreeMen-to-multiPhylo}}
#' \code{\link{TreeMan-class}}
#' @examples
#' library(treeman)
#' library(ape)
#' trees <- c(rtree(10), rtree(10), rtree(10))
#' trees <- as(trees, 'TreeMen')
setAs(from="multiPhylo", to="TreeMen", def=function(from, to) {
ape::write.tree(from, file='temp.tre')
tree <- readTree(file='temp.tre')
file.remove('temp.tre')
return(tree)
})

#' @name TreeMen-to-multiPhylo
#' @title Convert TreeMen to multiPhylo
#' @description Return ape's \code{multiPhylo} from a \code{TreeMen}
#' @seealso
#' \code{\link{TreeMan-to-phylo}},
#' \code{\link{phylo-to-TreeMan}},
#' \code{\link{multiPhylo-to-TreeMen}}
#' \code{\link{TreeMan-class}}
#' @examples
#' library(treeman)
#' library(ape)
#' trees <- cTrees(randTree(10), randTree(10), randTree(10))
#' trees <- as(trees, 'multiPhylo')
setAs(from="TreeMen", to="multiPhylo", def=function(from, to) {
writeTree(from, file='temp.tre')
tree <- ape::read.tree(file='temp.tre')
file.remove('temp.tre')
return(tree)
})
2 changes: 2 additions & 0 deletions man/TreeMan-to-phylo.Rd

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

20 changes: 20 additions & 0 deletions man/TreeMen-to-multiPhylo.Rd

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

9 changes: 9 additions & 0 deletions man/multiPhylo-class.Rd

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

20 changes: 20 additions & 0 deletions man/multiPhylo-to-TreeMen.Rd

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

2 changes: 2 additions & 0 deletions man/phylo-to-TreeMan.Rd

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

12 changes: 11 additions & 1 deletion tests/testthat/test-cnvrt-methods.R
Expand Up @@ -7,10 +7,20 @@ context('Testing \'cnvrt-methods\'')
test_that('TreeMan-to-phylo works', {
tree <- randTree(100)
tree <- as(tree, "phylo")
expect_equal(class(tree)[1], "phylo")
expect_true("phylo" %in% class(tree))
})
test_that('phylo-to-TreeMan works', {
tree <- ape::rtree(100)
tree <- as(tree, "TreeMan")
expect_equal(class(tree)[1], "TreeMan")
})
test_that('multiPhylo-to-TreeMen works', {
trees <- c(ape::rtree(100), ape::rtree(100), ape::rtree(100))
trees <- as(trees, "TreeMen")
expect_equal(is(trees), "TreeMen")
})
test_that('TreeMen-to-multiPhylo works', {
trees <- cTrees(randTree(100), randTree(100), randTree(100))
trees <- as(trees, "multiPhylo")
expect_true("multiPhylo" %in% class(trees))
})

0 comments on commit 40b46de

Please sign in to comment.