Skip to content

Commit

Permalink
add_edge_length assign now also edge length to an splits graph (networx)
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausVigo committed Jan 16, 2024
1 parent 927a461 commit ebd9d80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
18 changes: 14 additions & 4 deletions R/draw_CI.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
edge_length_matrix <- function(tree, trees, rooted=TRUE){
if(!inherits(trees, "multiPhylo")) stop("Trees must be of class multiPhylo!")
if(inherits(tree, "networx")) rooted <- FALSE
trees <- .uncompressTipLabel(trees) |> .compressTipLabel(ref=tree$tip.label)
if(!rooted){
trees <- unroot(trees)
Expand All @@ -13,7 +14,9 @@ edge_length_matrix <- function(tree, trees, rooted=TRUE){
el[x$edge[,2]] <- x$edge.length
el
}
bp <- bip(tree)
if(inherits(tree, "networx")) bp <- tree$splits
else bp <- bip(tree)

if(!rooted) bp <- SHORTwise(bp)
m <- length(bp)
res <- matrix(NA_real_, length(trees), m)
Expand All @@ -35,7 +38,8 @@ edge_length_matrix <- function(tree, trees, rooted=TRUE){
##' @title Assign and compute edge lengths from a sample of trees
##' @description This command can infer some average edge lengths and assign
##' them from a (bootstrap/MCMC) sample.
##' @param tree tree where edge lengths are assigned to.
##' @param tree a phylogenetic tree or splitnetwork where edge lengths are
##' assigned to.
##' @param trees an object of class multiPhylo, where the average for the edges
##' is computed from.
##' @param fun a function to compute the average (default is median).
Expand Down Expand Up @@ -63,8 +67,14 @@ add_edge_length <- function(tree, trees, fun=\(x)median(na.omit(x)),
if(!rooted) tree <- unroot(tree)
X <- edge_length_matrix(tree, trees, rooted)
nh <- apply(X, 2, fun)
if(rooted) tree$edge.length <- nh[tree$edge[,1]] - nh[tree$edge[,2]]
else tree$edge.length <- nh[tree$edge[,2]]
if(inherits(tree, "networx")){
tree$edge.length <- nh[tree$splitIndex]
attr(tree$splits, "weights") <- nh
}
else{
if(rooted) tree$edge.length <- nh[tree$edge[,1]] - nh[tree$edge[,2]]
else tree$edge.length <- nh[tree$edge[,2]]
}
tree
}

Expand Down
3 changes: 2 additions & 1 deletion man/add_edge_length.Rd

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

0 comments on commit ebd9d80

Please sign in to comment.