diff --git a/DESCRIPTION b/DESCRIPTION index 0deb851..a0cea72 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: treeman Type: Package Title: Phylogenetic Tree Manipulation Class and Methods -Version: 1.1 +Version: 1.11 Date: 2016-04-16 Author: D.J. Bennett Maintainer: D.J. Bennett diff --git a/NAMESPACE b/NAMESPACE index 133f2d1..c67cc53 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ export(getNdPtids) export(getNdSlt) export(getNdSstr) export(getNdsAge) +export(getNdsFrmTxnyms) export(getNdsKids) export(getNdsLng) export(getNdsPD) @@ -46,7 +47,6 @@ export(getPrnt) export(getSpnAge) export(getSpnsAge) export(getSubtree) -export(getTxnyms) export(getUnqNds) export(isUltrmtrc) export(loadTreeMan) @@ -58,6 +58,7 @@ export(readTrmn) export(rmClade) export(rmNdmtrx) export(rmNodes) +export(rmOtherSlt) export(rmTips) export(saveTreeMan) export(searchTxnyms) diff --git a/R/check_methods.R b/R/check_methods.R index 0ef32cc..17ea8dd 100644 --- a/R/check_methods.R +++ b/R/check_methods.R @@ -75,13 +75,28 @@ checkNdlst <- function(ndlst, root) { FALSE } nds <- names(ndlst) + if(any(duplicated(nds))) { + dups <- nds[duplicated(nds)] + dups <- unique(dups) + msg <- 'These node IDs are duplicated:\n' + if(length(dups) > 1) { + for(i in 1:length(dups) - 1) { + msg <- paste0(msg, dups[i], ', ') + } + } + msg <- paste0(msg, dups[length(dups)], '\n') + cat(msg) + return(FALSE) + } rid <- root nd_checks <- sapply(ndlst, .check) if(!all(nd_checks)) { msg <- 'These nodes are invalid:\n' bad <- which(!nd_checks) - for(i in bad[-length(bad)]) { - msg <- paste0(msg, nds[i], ', ') + if(length(bad) > 1) { + for(i in bad[-length(bad)]) { + msg <- paste0(msg, nds[i], ', ') + } } msg <- paste0(msg, nds[bad[length(bad)]], '\n') cat(msg, '\n') diff --git a/R/get-nd-methods.R b/R/get-nd-methods.R index 03185d1..c20b6e4 100644 --- a/R/get-nd-methods.R +++ b/R/get-nd-methods.R @@ -5,7 +5,7 @@ #' @param tree \code{TreeMan} object #' @param id node id #' @seealso -#' \code{\link{getNdsLng}}, \code{\link{getTxnyms}}, +#' \code{\link{getNdsLng}}, \code{\link{getNdsFrmTxnyms}}, #' \url{https://github.com/DomBennett/treeman/wiki/get-methods} #' @export #' @examples @@ -160,7 +160,7 @@ getNdPrdst <- function(tree, id) { #' @title Get a node slot #' @description Returns the value of named slot. #' @details Returned object depends on name, either character, vector or numeric. -#' Default node slots are: id, spn, prid, ptid and txnym. +#' Default node slots are: id, spn, prid, ptid and txnym. If slot is empty, returns NA. #' @param tree \code{TreeMan} object #' @param slt_nm slot name #' @param id node id @@ -173,7 +173,11 @@ getNdPrdst <- function(tree, id) { #' tree <- randTree(10) #' getNdSlt(tree, slt_nm='spn', id='t1') # return span of t1 getNdSlt <- function(tree, slt_nm, id) { - tree@ndlst[[id]][[slt_nm]] + res <- tree@ndlst[[id]][[slt_nm]] + if(is.null(res)) { + res <- NA + } + res } #' @name getNdPD diff --git a/R/get-nds-methods.R b/R/get-nds-methods.R index d76acb9..a4f5c4b 100644 --- a/R/get-nds-methods.R +++ b/R/get-nds-methods.R @@ -7,7 +7,7 @@ #' @param parallel logical, make parallel? #' @param progress name of the progress bar to use, see \code{\link{create_progress_bar}} #' @seealso -#' \code{\link{getNdLng}}, \code{\link{getTxnyms}}, +#' \code{\link{getNdLng}}, \code{\link{getNdsFrmTxnyms}}, #' \url{https://github.com/DomBennett/treeman/wiki/get-methods} #' @export #' @examples @@ -108,7 +108,7 @@ getNdsPrdst <- function(tree, ids, parallel=FALSE, progress="none") { #' @name getNdsSlt #' @title Get a node slot for multiple nodes -#' @description Returns the value of named slot. +#' @description Returns the values of named slot as a vector for atomic values, else list. #' @details Returned object depends on name, either character, vector or numeric. Parallelizable. #' Default node slots are: id, spn, prid, ptid and txnym. #' @param tree \code{TreeMan} object @@ -129,9 +129,14 @@ getNdsSlt <- function(tree, slt_nm, ids, parallel=FALSE, progress="none") { getNdSlt(tree, slt_nm, ids[i]) } l_data <- data.frame(i=1:length(ids), stringsAsFactors=FALSE) - res <- plyr::mdply(.data=l_data, .fun=.get, .parallel=parallel, + res <- plyr::mlply(.data=l_data, .fun=.get, .parallel=parallel, .progress=progress) - res[ ,2] + res <- res[1:length(res)] + if(all(sapply(res, length) == 1)) { + res <- unlist(res, recursive=FALSE) + } + names(res) <- ids + res } #' @name getNdsKids diff --git a/R/get-spcl-methods.R b/R/get-spcl-methods.R index 4f57a07..9965cff 100644 --- a/R/get-spcl-methods.R +++ b/R/get-spcl-methods.R @@ -218,7 +218,7 @@ getCnnctdNds <- function(tree, tids) { names(counts)[counts < length(tids)] } -#' @name getTxnyms +#' @name getNdsFrmTxnyms #' @title Get IDs for nodes represented txnyms #' @description Return a list of IDs for any node that contains the given txnyms. #' @details Returns a list. Txnyms must be spelt correctly. @@ -232,8 +232,8 @@ getCnnctdNds <- function(tree, tids) { #' library(treeman) #' data(mammals) #' # what ID represents the apes? -#' getTxnyms(mammals, 'Hominoidea') -getTxnyms <- function(tree, txnyms) { +#' getNdsFrmTxnyms(mammals, 'Hominoidea') +getNdsFrmTxnyms <- function(tree, txnyms) { # get nd id(s) for taxonyms .get <- function(id, txnym, ...) { for(t in txnyms) { diff --git a/R/read-write-methods.R b/R/read-write-methods.R index b851c12..2e0e35a 100644 --- a/R/read-write-methods.R +++ b/R/read-write-methods.R @@ -97,8 +97,13 @@ writeTree <- function(tree, file, append=FALSE, ndLabels=function(nd){ #' @description Return a \code{TreeMan} or \code{TreeMen} object from a Newick treefile #' @details Read a single or multiple trees from a file, or a text string. Parallelizable #' when reading multiple trees. +#' The function will add any internal node labels in the Newick tree as a user-defined data slots. +#' The name of this slot is defined with the \code{spcl_slt_nm}. +#' These data can be accessed/manipulated with the \code{`getNdsSlt()`} function. +#' Trees are always read as rooted. (Unrooted trees have polytomous root nodes.) #' @param file file path #' @param text Newick character string +#' @param spcl_slt_nm name of special slot for internal node labels, default 'Unknown'. #' @param wndmtrx T/F add node matrix? Default FALSE. #' @param parallel logical, make parallel? #' @param progress name of the progress bar to use, see \code{\link{create_progress_bar}} @@ -110,9 +115,13 @@ writeTree <- function(tree, file, append=FALSE, ndLabels=function(nd){ #' @export #' @examples #' library(treeman) -#' tree <- readTree(text="((A:1.0,B:1.0):1.0,(C:1.0,D:1.0):1.0);") -readTree <- function(file=NULL, text=NULL, wndmtrx=FALSE, parallel=FALSE, - progress='none') { +#' # tree string with internal node labels as bootstrap results +#' tree <- readTree(text="((A:1.0,B:1.0)0.9:1.0,(C:1.0,D:1.0)0.8:1.0)0.7:1.0;", +#' spcl_slt_nm='bootstrap') +#' # retrieve bootstrap values by node +#' tree['bootstrap'] +readTree <- function(file=NULL, text=NULL, spcl_slt_nm='Unknown', wndmtrx=FALSE, + parallel=FALSE, progress='none') { if(!is.null(file)) { trstr <- scan(file, what="raw", quiet=TRUE) } else { @@ -120,26 +129,27 @@ readTree <- function(file=NULL, text=NULL, wndmtrx=FALSE, parallel=FALSE, } if(length(trstr) > 1) { trstr <- as.list(trstr) - trees <- plyr::mlply(trstr, .fun=.readTree, wndmtrx=wndmtrx, - .progress=progress, .parallel=parallel) + trees <- plyr::mlply(trstr, .fun=.readTree, spcl_slt_nm=spcl_slt_nm, + wndmtrx=wndmtrx, .progress=progress, .parallel=parallel) names(trees) <- NULL + trees <- trees[1:length(trees)] tree <- as(trees, 'TreeMen') } else { - tree <- .readTree(trstr, wndmtrx) + tree <- .readTree(trstr, spcl_slt_nm, wndmtrx) } tree } #' @useDynLib treeman #' @useDynLib treeman cFindPrids -.readTree <- function(trstr, wndmtrx) { +.readTree <- function(trstr, spcl_slt_nm, wndmtrx) { # Internals .idspn <- function(i) { mtdt <- substr(trstr, start=nds[i-1] + 1, stop=nds[i]) mtdt <- gsub("(\\(|\\)|,|;)", "", mtdt) mtdt <- strsplit(mtdt, ":")[[1]] + id <- NA if(length(mtdt) == 0) { - id <- paste0('n', i) spn <- NA } else if(length(mtdt) == 1) { id <- mtdt @@ -148,7 +158,6 @@ readTree <- function(file=NULL, text=NULL, wndmtrx=FALSE, parallel=FALSE, id <- mtdt[1] spn <- as.numeric(mtdt[2]) } else { - id <- paste0('n', i) spn <- as.numeric(mtdt[2]) } c(id, spn) @@ -186,6 +195,24 @@ readTree <- function(file=NULL, text=NULL, wndmtrx=FALSE, parallel=FALSE, tinds <- which(!1:length(ids) %in% prinds) prinds[is.na(prinds)] <- root spns[is.na(spns)] <- 0 + # move internal node labels to other + other <- rep(NA, length(ids)) + intnds <- 1:length(ids) %in% prinds + other[intnds] <- ids[intnds] + ids[intnds] <- paste0('n', which(intnds)) + # rm NAs from IDs + pull <- is.na(ids) + ids[pull] <- paste0('n', which(pull)) + # ensure no dups in ids + dups <- duplicated(ids) + if(any(dups)) { + dups <- unique(ids[dups]) + for(dup in dups) { + pull <- ids == dup + other[pull] <- ids[pull] + ids[pull] <- paste0('n', which(pull)) + } + } ptids <- ids[-root] ptnds_pool <- prinds[-root] ndlst <- lapply(1:length(ids), .add) @@ -193,6 +220,11 @@ readTree <- function(file=NULL, text=NULL, wndmtrx=FALSE, parallel=FALSE, tree <- new('TreeMan', ndlst=ndlst, root=ids[root], ndmtrx=NULL, wtxnyms=FALSE, prinds=prinds, tinds=tinds) + pull <- !is.na(other) + if(any(pull)) { + tree <- setNdsOther(tree, ids=ids[pull], vals=other[pull], + slt_nm=spcl_slt_nm) + } tree <- updateSlts(tree) if(wndmtrx) { tree <- addNdmtrx(tree) diff --git a/R/server-methods.R b/R/server-methods.R index 03f73f8..bf0bdf3 100644 --- a/R/server-methods.R +++ b/R/server-methods.R @@ -14,7 +14,7 @@ #' @param clean T/F, ensure returned names contain no special characters? #' @param infer T/F, infer taxonyms for unfound nodes? #' @seealso -#' \code{\link{taxaResolve}}, \code{\link{setTxnyms}}, \code{\link{getTxnyms}} +#' \code{\link{taxaResolve}}, \code{\link{setTxnyms}}, \code{\link{getNdsFrmTxnyms}} #' @export #' @examples #' tree <- randTree(8) @@ -88,7 +88,7 @@ searchTxnyms <- function (tree, cache=FALSE, parent=NULL, clean=TRUE, #' @param cache T/F, create a local cache of downloaded names? #' @param parent specify parent of all names to prevent false names #' @seealso -#' \code{\link{searchTxnyms}}, \code{\link{setTxnyms}}, \code{\link{getTxnyms}} +#' \code{\link{searchTxnyms}}, \code{\link{setTxnyms}}, \code{\link{getNdsFrmTxnyms}} #' @export #' @examples #' my_lovely_names <- c ('Gallus gallus', 'Pongo pingu', 'Homo sapiens', diff --git a/R/set-methods.R b/R/set-methods.R index 1175c66..3fdd67e 100644 --- a/R/set-methods.R +++ b/R/set-methods.R @@ -304,3 +304,36 @@ setNdsOther <- function(tree, ids, vals, slt_nm, parallel=FALSE, progress="none" } tree } + +#' @name rmOtherSlt +#' @title Remove a user-defined slot +#' @description Returns a tree with a user-defined tree slot removed. +#' @details A user can specify a new slot using the \code{setNdSlt()} function +#' or upon reading a tree. This can be removed using this function by specifying +#' the name of the slot to be removed. +#' @param tree \code{TreeMan} object +#' @param slt_nm name of slot to be removed +#' @seealso +#' \code{\link{setNdOther}}, \code{\link{setNdsOther}}, +#' \url{https://github.com/DomBennett/treeman/wiki/set-methods} +#' @export +#' @examples +#' library(treeman) +#' tree <- randTree(10) +#' vals <- runif(min=0, max=1, n=tree['nall']) +#' tree <- setNdsOther(tree, tree['all'], vals, 'confidence') +#' tree <- updateSlts(tree) +#' summary(tree) +#' tree <- rmOtherSlt(tree, 'confidence') +#' tree <- updateSlts(tree) +#' summary(tree) +rmOtherSlt <- function(tree, slt_nm) { + .set <- function(id) { + tree@ndlst[[id]][[slt_nm]] <<- NULL + } + l_data <- data.frame(id=tree@all, stringsAsFactors=FALSE) + plyr::m_ply(.data=l_data, .fun=.set) + tree@updtd <- FALSE + tree@othr_slt_nms <- tree@othr_slt_nms[tree@othr_slt_nms != slt_nm] + tree +} diff --git a/R/treeman-declaration.R b/R/treeman-declaration.R index e673de1..bda2d56 100644 --- a/R/treeman-declaration.R +++ b/R/treeman-declaration.R @@ -67,9 +67,14 @@ #' tree[['t1']] # return t1 node object #' tree['pd'] # return phylogenetic diversity #' tree['ply'] # is polytomous? -#' # Additional special slots +#' # Additional special slots (calculated upon call) #' tree['age'] # get tree's age #' tree['ultr'] # determine if tree is ultrametric +#' tree['spns'] # get all the spans of the tree IDs +#' tree['prids'] # get all the IDs of preceding nodes +#' tree['ptids'] # get all the IDs of following nodes +#' tree['txnyms'] # get all the taxonyms of all nodes +#' # In addition [] can be used for any user-defined slot #' # Because all nodes are lists with metadata we can readily #' # get specific information on nodes of interest #' nd <- tree[['n2']] @@ -141,8 +146,22 @@ setMethod('[', c('TreeMan', 'character', 'missing', 'missing'), if(i == 'age') { return(getAge(x)) } + # getNdsSlt extractor + xtr_slts <- c('spns', 'prids', 'ptids', 'txnyms') + if(i %in% xtr_slts) { + slt_nm <- sub('s$', '', i) # rm s at end + res <- getNdsSlt(x, slt_nm, x@all) + names(res) <- x@all + return(res) + } + if(i %in% x@othr_slt_nms) { + res <- getNdsSlt(x, i, x@all) + names(res) <- x@all + return(res) + } if(!i %in% slt_nms) { - slt_nms <- paste0(c(slt_nms, 'ultr', 'age'), collapse=', ') + slt_nms <- paste0(c(slt_nms, 'ultr', 'age', xtr_slts, + x@othr_slt_nms), collapse=', ') stop(paste0('`', i, '` not a tree slot. Available slots: ', slt_nms)) } diff --git a/man/TreeMan-class.Rd b/man/TreeMan-class.Rd index 9242e72..fd8123b 100644 --- a/man/TreeMan-class.Rd +++ b/man/TreeMan-class.Rd @@ -119,9 +119,14 @@ tree['root'] # identify root node tree[['t1']] # return t1 node object tree['pd'] # return phylogenetic diversity tree['ply'] # is polytomous? -# Additional special slots +# Additional special slots (calculated upon call) tree['age'] # get tree's age tree['ultr'] # determine if tree is ultrametric +tree['spns'] # get all the spans of the tree IDs +tree['prids'] # get all the IDs of preceding nodes +tree['ptids'] # get all the IDs of following nodes +tree['txnyms'] # get all the taxonyms of all nodes +# In addition [] can be used for any user-defined slot # Because all nodes are lists with metadata we can readily # get specific information on nodes of interest nd <- tree[['n2']] diff --git a/man/getNdLng.Rd b/man/getNdLng.Rd index c0ce250..d158ffe 100644 --- a/man/getNdLng.Rd +++ b/man/getNdLng.Rd @@ -24,6 +24,6 @@ data(mammals) getNdLng(mammals, id='Homo_sapiens') } \seealso{ -\code{\link{getNdsLng}}, \code{\link{getTxnyms}}, +\code{\link{getNdsLng}}, \code{\link{getNdsFrmTxnyms}}, \url{https://github.com/DomBennett/treeman/wiki/get-methods} } diff --git a/man/getNdSlt.Rd b/man/getNdSlt.Rd index 866bd0a..de40a8e 100644 --- a/man/getNdSlt.Rd +++ b/man/getNdSlt.Rd @@ -18,7 +18,7 @@ Returns the value of named slot. } \details{ Returned object depends on name, either character, vector or numeric. -Default node slots are: id, spn, prid, ptid and txnym. +Default node slots are: id, spn, prid, ptid and txnym. If slot is empty, returns NA. } \examples{ library(treeman) diff --git a/man/getTxnyms.Rd b/man/getNdsFrmTxnyms.Rd similarity index 83% rename from man/getTxnyms.Rd rename to man/getNdsFrmTxnyms.Rd index ed9f82c..e3da93e 100644 --- a/man/getTxnyms.Rd +++ b/man/getNdsFrmTxnyms.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/get-spcl-methods.R -\name{getTxnyms} -\alias{getTxnyms} +\name{getNdsFrmTxnyms} +\alias{getNdsFrmTxnyms} \title{Get IDs for nodes represented txnyms} \usage{ -getTxnyms(tree, txnyms) +getNdsFrmTxnyms(tree, txnyms) } \arguments{ \item{tree}{\code{TreeMan} object} @@ -21,7 +21,7 @@ Returns a list. Txnyms must be spelt correctly. library(treeman) data(mammals) # what ID represents the apes? -getTxnyms(mammals, 'Hominoidea') +getNdsFrmTxnyms(mammals, 'Hominoidea') } \seealso{ \code{\link{taxaResolve}}, \code{\link{setTxnyms}}, \code{\link{searchTxnyms}}, diff --git a/man/getNdsLng.Rd b/man/getNdsLng.Rd index bb3fe0e..1c51f89 100644 --- a/man/getNdsLng.Rd +++ b/man/getNdsLng.Rd @@ -28,6 +28,6 @@ data(mammals) getNdsLng(mammals, id=c('Homo_sapiens', 'Gorilla_gorilla')) } \seealso{ -\code{\link{getNdLng}}, \code{\link{getTxnyms}}, +\code{\link{getNdLng}}, \code{\link{getNdsFrmTxnyms}}, \url{https://github.com/DomBennett/treeman/wiki/get-methods} } diff --git a/man/getNdsSlt.Rd b/man/getNdsSlt.Rd index 666f76b..6094b33 100644 --- a/man/getNdsSlt.Rd +++ b/man/getNdsSlt.Rd @@ -18,7 +18,7 @@ getNdsSlt(tree, slt_nm, ids, parallel = FALSE, progress = "none") \item{progress}{name of the progress bar to use, see \code{\link{create_progress_bar}}} } \description{ -Returns the value of named slot. +Returns the values of named slot as a vector for atomic values, else list. } \details{ Returned object depends on name, either character, vector or numeric. Parallelizable. diff --git a/man/readTree.Rd b/man/readTree.Rd index 08fed42..6ba32ec 100644 --- a/man/readTree.Rd +++ b/man/readTree.Rd @@ -4,14 +4,16 @@ \alias{readTree} \title{Read a Newick tree} \usage{ -readTree(file = NULL, text = NULL, wndmtrx = FALSE, parallel = FALSE, - progress = "none") +readTree(file = NULL, text = NULL, spcl_slt_nm = "Unknown", + wndmtrx = FALSE, parallel = FALSE, progress = "none") } \arguments{ \item{file}{file path} \item{text}{Newick character string} +\item{spcl_slt_nm}{name of special slot for internal node labels, default 'Unknown'.} + \item{wndmtrx}{T/F add node matrix? Default FALSE.} \item{parallel}{logical, make parallel?} @@ -24,10 +26,18 @@ Return a \code{TreeMan} or \code{TreeMen} object from a Newick treefile \details{ Read a single or multiple trees from a file, or a text string. Parallelizable when reading multiple trees. +The function will add any internal node labels in the Newick tree as a user-defined data slots. +The name of this slot is defined with the \code{spcl_slt_nm}. +These data can be accessed/manipulated with the \code{`getNdsSlt()`} function. +Trees are always read as rooted. (Unrooted trees have polytomous root nodes.) } \examples{ library(treeman) -tree <- readTree(text="((A:1.0,B:1.0):1.0,(C:1.0,D:1.0):1.0);") +# tree string with internal node labels as bootstrap results +tree <- readTree(text="((A:1.0,B:1.0)0.9:1.0,(C:1.0,D:1.0)0.8:1.0)0.7:1.0;", +spcl_slt_nm='bootstrap') +# retrieve bootstrap values by node +tree['bootstrap'] } \seealso{ \url{https://en.wikipedia.org/wiki/Newick_format}, diff --git a/man/rmOtherSlt.Rd b/man/rmOtherSlt.Rd new file mode 100644 index 0000000..fb615bf --- /dev/null +++ b/man/rmOtherSlt.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/set-methods.R +\name{rmOtherSlt} +\alias{rmOtherSlt} +\title{Remove a user-defined slot} +\usage{ +rmOtherSlt(tree, slt_nm) +} +\arguments{ +\item{tree}{\code{TreeMan} object} + +\item{slt_nm}{name of slot to be removed} +} +\description{ +Returns a tree with a user-defined tree slot removed. +} +\details{ +A user can specify a new slot using the \code{setNdSlt()} function +or upon reading a tree. This can be removed using this function by specifying +the name of the slot to be removed. +} +\examples{ +library(treeman) +tree <- randTree(10) +vals <- runif(min=0, max=1, n=tree['nall']) +tree <- setNdsOther(tree, tree['all'], vals, 'confidence') +tree <- updateSlts(tree) +summary(tree) +tree <- rmOtherSlt(tree, 'confidence') +tree <- updateSlts(tree) +summary(tree) +} +\seealso{ +\code{\link{setNdOther}}, \code{\link{setNdsOther}}, +\url{https://github.com/DomBennett/treeman/wiki/set-methods} +} diff --git a/man/searchTxnyms.Rd b/man/searchTxnyms.Rd index cc1e9e1..6ed1e5c 100644 --- a/man/searchTxnyms.Rd +++ b/man/searchTxnyms.Rd @@ -39,5 +39,5 @@ nd_labels <- searchTxnyms(tree) print(nd_labels) } \seealso{ -\code{\link{taxaResolve}}, \code{\link{setTxnyms}}, \code{\link{getTxnyms}} +\code{\link{taxaResolve}}, \code{\link{setTxnyms}}, \code{\link{getNdsFrmTxnyms}} } diff --git a/man/taxaResolve.Rd b/man/taxaResolve.Rd index 2ed749a..7624895 100644 --- a/man/taxaResolve.Rd +++ b/man/taxaResolve.Rd @@ -40,5 +40,5 @@ lineages <- strsplit(as.vector(res$lineage), '\\\\|') print(lineages[[6]]) # the bacteria has far fewer taxonomic levels } \seealso{ -\code{\link{searchTxnyms}}, \code{\link{setTxnyms}}, \code{\link{getTxnyms}} +\code{\link{searchTxnyms}}, \code{\link{setTxnyms}}, \code{\link{getNdsFrmTxnyms}} } diff --git a/tests/testthat/test-get-nds-methods.R b/tests/testthat/test-get-nds-methods.R index 763da84..fcce6e2 100644 --- a/tests/testthat/test-get-nds-methods.R +++ b/tests/testthat/test-get-nds-methods.R @@ -48,6 +48,8 @@ test_that('getNdsSlt() works', { nd_spns <- getNdsSlt(tree, slt_nm="spn", ids=tree['all']) expect_that(sum(nd_spns), equals(tot_pd)) + ptids <- getNdsSlt(tree, slt_nm='ptid', ids=tree['all']) + expect_true(length(ptids[[sample(tree['tips'], 1)]]) == 0) }) test_that('getNdsKids() works', { tree <- getTestTree(n) diff --git a/tests/testthat/test-get-spcl-methods.R b/tests/testthat/test-get-spcl-methods.R index 4e0ab6f..f3d67ea 100644 --- a/tests/testthat/test-get-spcl-methods.R +++ b/tests/testthat/test-get-spcl-methods.R @@ -35,9 +35,9 @@ test_that('getCnntdNds() works', { expect_false(prnt %in% cnntdnds) expect_true(all(ptids %in% ptids)) }) -test_that('getTxnyms() works', { +test_that('getNdsFrmTxnyms() works', { data(mammals) - res <- getTxnyms(mammals, 'Homo')[[1]] + res <- getNdsFrmTxnyms(mammals, 'Homo')[[1]] expect_true(res == 'Homo_sapiens') }) test_that('getTreeAge() works', { diff --git a/tests/testthat/test-rw-methods.R b/tests/testthat/test-rw-methods.R index 93f3e98..2397a78 100644 --- a/tests/testthat/test-rw-methods.R +++ b/tests/testthat/test-rw-methods.R @@ -13,18 +13,25 @@ if(file.exists('testRData_ndmtrx')) { # RUNNING context('Testing \'read-write-methods\'') test_that('readTree([w/ spans]) works', { - text <- "(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5);" + text <- "(A:0.1,B:0.2,(C:0.3,D:0.4):0.5);" tree <- readTree(text=text, wndmtrx=sample(c(TRUE, FALSE), 1)) expect_that(tree['pd'], equals(0.1+0.2+0.3+0.4+0.5)) expect_that(tree['ntips'], equals(4)) expect_that(tree[['C']]['prdst'], equals(0.3+0.5)) }) test_that('readTree([w/o spans]) works', { - text <- "(A,B,(C,D)E);" + text <- "(A,B,(C,D));" tree <- readTree(text=text, wndmtrx=sample(c(TRUE, FALSE), 1)) expect_that(tree['ntips'], equals(4)) expect_false(tree['wspn']) }) +test_that('readTree([w/ node labels]) works', { + text <- "(A,B,(C,D)0.9)0.8;" + tree <- readTree(text=text, spcl_slt_nm='bootstrap', + wndmtrx=sample(c(TRUE, FALSE), 1)) + expect_that(tree['ntips'], equals(4)) + expect_false(tree['wspn']) +}) test_that('writeTree() works', { t1 <- randTree(100, wndmtrx=sample(c(TRUE, FALSE), 1)) writeTree(t1, 'test.tre') diff --git a/tests/testthat/test-set-methods.R b/tests/testthat/test-set-methods.R index aa5b813..5bcfa2e 100644 --- a/tests/testthat/test-set-methods.R +++ b/tests/testthat/test-set-methods.R @@ -70,5 +70,17 @@ test_that('setNdsOther() works', { tree <- updateSlts(tree) summary(tree) res <- getNdsSlt(tree, ids=tree['all'], slt_nm='binary_val') - expect_that(vals, equals(res)) + names(res) <- NULL + expect_equal(vals, res) +}) +test_that('rmOtherSlt() works', { + tree <- randTree(10, wndmtrx=sample(c(TRUE, FALSE), 1)) + vals <- sample(0:1, size=tree['nall'], replace=TRUE) + tree <- setNdsOther(tree, tree['all'], vals, 'binary_val') + tree <- updateSlts(tree) + tree <- rmOtherSlt(tree, 'binary_val') + tree <- updateSlts(tree) + summary(tree) + res <- getNdsSlt(tree, ids=tree['all'], slt_nm='binary_val') + expect_true(is.na(res[[1]])) }) \ No newline at end of file