Skip to content

Commit

Permalink
fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtPoon committed Nov 11, 2022
1 parent ecb57fd commit 8f04a49
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions R/tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,37 @@ as.phyloData <- function(phy, unscaled=FALSE) {
#' @return numeric vector of length Nnode(phy), sorted in preorder
#' @export
count.tips <- function(phy) {
ntips <- list()
for (i in 1:Ntip(phy)) {
ntips[i] <- 1
}
# assumes that internal nodes are sorted for preorder traversal
# i.e., root node has lowest index
tryCatch(
{
for (i in seq(Ntip(phy)+Nnode(phy), Ntip(phy)+1, -1)) {
ntips[i] <- sum(sapply(phy$edge[phy$edge[,1]==i, 2],
function(j) ntips[[j]]))
}
},
error=function(cond) {
# phangorn::midpoint does something weird to node indices
phy <- read.tree(text=write.tree(phy))
ntips <- list()
for (i in 1:Ntip(phy)) { ntips[i] <- 1 }
for (i in seq(Ntip(phy)+Nnode(phy), Ntip(phy)+1, -1)) {
ntips[i] <- sum(sapply(phy$edge[phy$edge[,1]==i, 2],
function(j) ntips[[j]]))
}
# check inputs
if (!is.element('phylo', class(phy))) {
stop("`phy` must be an object of class ape::phylo")
}
if(!all(table(phy$edge[,2])==1)) {
stop("Each node index should appear only once in phy$edge[,2]")
}

check.idx <- sapply(1:nrow(phy$edge), function(i) {
cidx <- phy$edge[i,2]
if (is.element(cidx, phy$edge[,1])) {
j <- min(which(phy$edge[,1]==cidx), na.rm=T)
return(i < j)
}
)
return(unlist(ntips)[(Ntip(phy)+1):length(ntips)])
return(NA)
})
if (!all(check.idx, na.rm=TRUE)) {
stop("All nodes must appear as child (phy$edge[,2]) before parent (phy$edge[,1])")
}

ntips <- rep(0, Ntip(phy) + Nnode(phy))
for (i in seq(nrow(phy$edge), 1)) {
parent <- phy$edge[i, 1]
child <- phy$edge[i, 2]
if (child <= Ntip(phy)) {
ntips[parent] <- ntips[parent] + 1 # child is a tip
} else {
ntips[parent] <- ntips[parent] + ntips[child]
}
}
return(ntips[(Ntip(phy)+1):length(ntips)])
}


Expand Down
Binary file modified vignettes/man/figures/README-unnamed-chunk-10-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/man/figures/README-unnamed-chunk-11-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/man/figures/README-unnamed-chunk-14-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/man/figures/README-unnamed-chunk-2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/man/figures/README-unnamed-chunk-4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/man/figures/README-unnamed-chunk-5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/man/figures/README-unnamed-chunk-7-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/man/figures/README-unnamed-chunk-8-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/man/figures/README-unnamed-chunk-9-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8f04a49

Please sign in to comment.