Skip to content

Instantly share code, notes, and snippets.

@joelnitta
Created June 22, 2021 06:27
Show Gist options
  • Save joelnitta/2db3a83538e79ee58ce71bdb1e551f2e to your computer and use it in GitHub Desktop.
Save joelnitta/2db3a83538e79ee58ce71bdb1e551f2e to your computer and use it in GitHub Desktop.
Force the branch lengths of a tree to be ultrametric
#' force_ultrametric
#'
#' Force the branch lengths of a tree to be ultrametric. Most useful in cases
#' when a time-tree is imported into R but the branch lengths are not exactly
#' ultrametric due to rounding error. Uses a different (faster) method from
#' phytools::force.ultrametric()
#'
#' @param tree Input tree; list of class "phylo". Should already be ultrametric
#' within rounding error.
#' @param force.positive Logical; should the branch lengths be forced to be
#' positive? NOTE: this may f*** up your tree! Be sure to visually inspect it
#' if you set `force.positive = TRUE`
#'
#' @return Object of class "phylo"; the ultrametric tree.
force_ultrametric <- function (tree, force.positive = FALSE) {
br_times <- ape::branching.times(tree)
ape::compute.brtime(tree, br_times, force.positive = force.positive)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment