R/cnvrt-methods.R

#' phylo class
#'
#' @name phylo-class
#' @aliases phylo
#'
#' @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}. This will
#' preserve node labels if they are different from the default labels (n#).
#' @seealso 
#' \code{\link{phylo-to-TreeMan}},
#' \code{\link{TreeMen-to-multiPhylo}}
#' \code{\link{multiPhylo-to-TreeMen}}
#' \code{\link{TreeMan-class}}
#' @examples 
#' library(treeman)
#' library(ape)
#' tree <- randTree(10)
#' tree <- as(tree, 'phylo')
setAs(from="phylo", to="TreeMan", def=function(from, to) {
  temp_file <- tempfile(pattern="temp_tree", fileext=".tre")
  ape::write.tree(from, file=temp_file)
  tree <- readTree(file=temp_file)
  file.remove(temp_file)
  return(tree)
})

#' @name phylo-to-TreeMan
#' @title Convert phylo to TreeMan
#' @description Return a \code{TreeMan} from ape's \code{phylo}. This will
#' preserve node labels, if they are a alphanumeric.
#' @seealso
#' \code{\link{TreeMan-to-phylo}},
#' \code{\link{TreeMen-to-multiPhylo}}
#' \code{\link{multiPhylo-to-TreeMen}}
#' \code{\link{TreeMan-class}}
#' @examples 
#' library(treeman)
#' library(ape)
#' tree <- compute.brlen(rtree(10))
#' tree <- as(tree, 'TreeMan')
setAs(from="TreeMan", to="phylo", def=function(from, to) {
  temp_file <- tempfile(pattern="temp_tree", fileext=".tre")
  
  # use a node label function function that writes the node labels during the 
  # conversion, if they are non default.
  if (all(from["nds"] == paste0("n", seq_along(from["nds"])))) {
    nodeLabeller <- function(nd) { return(NULL) } # no node labels 
  } else {
    nodeLabeller <- function(nd) { nd[['id']] } # writes node labels 
  }

  writeTree(from, file=temp_file, ndLabels = nodeLabeller)
  tree <- ape::read.tree(file=temp_file)
  file.remove(temp_file)
  return(tree)
})

#' @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) {
  temp_file <- tempfile(pattern="temp_tree", fileext=".tre")
  ape::write.tree(from, file=temp_file)
  tree <- readTree(file=temp_file)
  file.remove(temp_file)
  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) {
  temp_file <- tempfile(pattern="temp_tree", fileext=".tre")
  writeTree(from, file=temp_file)
  tree <- ape::read.tree(file=temp_file)
  file.remove(temp_file)
  return(tree)
})
DomBennett/treeman documentation built on Nov. 12, 2020, 1:35 p.m.