EcoJulia / Phylo.jl

Simple phylogenetic trees in Julia to work with Diversity.jl - https://github.com/EcoJulia/Diversity.jl
BSD 2-Clause "Simplified" License
33 stars 13 forks source link

How to create RootedTree objects from their components? #76

Open RiboRings opened 1 year ago

RiboRings commented 1 year ago

Hello!

I am trying to disassemble a RootedTree object into its components and then reassemble it. However, I get a method error as follows:

# generate random tree
nu = Nonultrametric(20)
tree = rand(nu)

# reassemble tree components into a RootedTree
RootedTree(getfield(tree, :name),
           getfield(tree, :nodedict),
           getfield(tree, :roots),
           getfield(tree, :nodes),
           getfield(tree, :branches),
           getfield(tree, :data),
           getfield(tree, :tipdata),
           getfield(tree, :rootheight),
           getfield(tree, :isvalid),
           getfield(tree, :cache))

ERROR: MethodError: no method matching RootedTree(::Missing, ::Dict{String, Int64}, ::Vector{LinkNode{OneRoot, String,
 Dict{String, Any}, LinkBranch{OneRoot, String, Dict{String, Any}, Float64}}}, ::Vector{Union{Missing, LinkNode{OneRoot,
 String, Dict{String, Any}, LinkBranch{OneRoot, String, Dict{String, Any}, Float64}}}}, ::Vector{Union{Missing, 
LinkBranch{OneRoot, String, Dict{String, Any}, Float64}}}, ::Dict{String, Any}, ::Dict{String, Any}, ::Missing, ::Missing, 
::Dict{TraversalOrder, Vector{LinkNode{OneRoot, String, Dict{String, Any}, LinkBranch{OneRoot, String, Dict{String, Any},
 Float64}}}})
Closest candidates are:
  LinkTree{RT, NL, N, B, TD}(::TD) where {RT, NL, N, B, TD} at ~/.julia/packages/Phylo/0WXtf/src/LinkTree.jl:61

It would be great if you could provide a tutorial on how to build trees from scratch starting from their components.

Thank you in advance!

richardreeve commented 1 year ago

@RiboRings - apologies for the delay in getting back to you - this is peak teaching for me at the moment. We've explicitly disabled the ability to make create a tree from its components as there are so many parts that need to align correctly - essentially every single component needs to match the elements in every other component, so the only way to easily construct a tree is to read one in or to generate a random one at the moment. Can I ask what you are trying to do?

If you want to construct a tree from scratch the way to manage that is to create an empty tree with the correct number of tips and then to use createnode!() and createbranch!() to create all of the internal nodes in sequence. An example of this in action is the code for rand() here, though there is some potentially superfluous code at the end of that function for adjusting all of the branch lengths retrospectively to fit a standard length.