Skip to content

tree_age_cumul() / tp_phylo_to_tpjson() default age="" fails with a cryptic data.frame error #18

Description

@maribraga

Environment

  • treepplr 0.14.0

Bug

tp_phylo_to_tpjson(phylo_tree, age = "") (the documented default) always fails:

library(treepplr)
tree <- ape::read.tree(text = "((1:1.0,2:1.0):0.5,(3:1.2,4:1.2):0.3);")
tp_phylo_to_tpjson(tree)
#> Error in `$<-.data.frame`(`*tmp*`, "Age", value = integer(0)) :
#>   replacement has 0 rows, data has 7

Root cause

In treepplr:::tree_age_cumul():

tree_age_cumul <- function(tree, root_index, age = "branch-length") {
    age_cumul <- rep(length(tree$Type), 0)
    ...

rep(length(tree$Type), 0) is rep(x = <tree size>, times = 0), which always returns a zero-length vector, regardless of tree size (looks like the arguments were meant to be swapped: rep(0, length(tree$Type))).

For age = "root-to-tip" or age = "tip-to-root", the subsequent traversal happens to populate age_cumul[i] by index during the walk, which auto-extends the vector in R, so those two paths work despite the bug. But for any other value of age (including the documented default ""), age_cumul stays length 0, and the final tree$Age <- age_cumul throws the data.frame replacement error above with no indication of what's actually wrong.

Impact

tp_phylo_to_tpjson()'s default argument is unusable — every caller must discover (via this error, or via source-diving) that they need to pass age = "tip-to-root" (or "root-to-tip") explicitly. This isn't mentioned in the function's documentation.

Suggested fix

Fix the rep() call: rep(0, length(tree$Type)). That alone would make age = "" behave sanely (probably as a no-op / all-zero ages, which should also be documented), and would remove the reliance on the auto-extension side-effect for the two branches that currently "work".

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions