Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I rename the leaf nodes of a ::RootedTree? #70

Closed
casasgomezuribarri opened this issue Mar 1, 2022 · 7 comments · Fixed by #91
Closed

How can I rename the leaf nodes of a ::RootedTree? #70

casasgomezuribarri opened this issue Mar 1, 2022 · 7 comments · Fixed by #91

Comments

@casasgomezuribarri
Copy link

I would like to rename all the leaves of my tree, of type ::RootedTree.

setleafinfo!(mytree, vector) throws the error

ERROR: LoadError: MethodError: no method matching _setleafinfo!(::RootedTree, ::Vector{String})
Closest candidates are:
  _setleafinfo!(::Phylo.AbstractBranchTree, ::Any) at C:\Users\ivica\.julia\packages\Phylo\0WXtf\src\Tree.jl:68
  _setleafinfo!(::LinkTree{RT, NL, N, B, TD}, ::TD) where {RT, NL, N, B, TD} at C:\Users\ivica\.julia\packages\Phylo\0WXtf\src\LinkTree.jl:420

I couldn't find any information on the ::Phylo.AbstractBranchTree type in the docs, but the docstring of the function suggests that the tree to be edited must be of type ::AbstractTree. I tried AbstractTree(mytree), but got the error

ERROR: MethodError: no method matching AbstractTree(::RootedTree)

Really, all I want to do is to plot my tree with a nicer format for the leaf names. How can I do it? (either editing the tree itself or specifying the names in the plot call)

@richardreeve
Copy link
Member

richardreeve commented Mar 1, 2022

setleafinfo!() is for setting the data associated with the tips / leaves of a tree rather than for renaming them. I don't remember ever creating a function to actually rename nodes because the idea is that the (internal and terminal) nodes are created with fixed names. How are you creating the tree originally so that it has the wrong tip names?

@mkborregaard
Copy link
Member

You can e.g. read the tree from a Newick file and want to change the names from eg common to scientific names? This seems like a common use case to me.

@casasgomezuribarri
Copy link
Author

Yup @mkborregaard that is exactly my case (well, species ID -weird format- to scientific species name). I didn't create the tree myself, and was expecting to be able to do everything I wanted to do with it in Julia (since it's the only language I know to a good level).

What about editing the plotting pane instead of the original tree?

How can the recipe in plot.jl be changed so that there is an extra kwarg (tips?) that overwrites getleafnames(tree) in

@recipe function f(tree::Phylo.AbstractTree; treetype = :dendrogram, marker_group = nothing, line_group = nothing, showtips = true, tipfont = (7,))

    linecolor --> :black
    grid --> false
    framestyle --> :none
    legend --> false
    colorbar --> true
    size --> (1000, 1000)

    d, h, n = _findxy(tree)
    adj = 0.03maximum(values(d))
    tipannotations = map(x->(d[x] + adj, h[x], x), getleafnames(tree))
....#continues

@mkborregaard
Copy link
Member

mkborregaard commented Mar 2, 2022

You could just add a tipnames = getleafnames(tree) kwarg to the recipe function, and then use tipnames in line 14. Maybe add a check to ensure that the length of the iterable is correct. That is probably sensible under all circumstances, though the other use case sounds reasonable too.

@casasgomezuribarri
Copy link
Author

casasgomezuribarri commented Mar 2, 2022

Thanks for your help. I had never played with the source code of a package before, so it took me some time to realise what I was doing. I am quite behind schedule with other things right now so won't be submitting a PR (again, I've never done it before and can't really spare the time to learn that at the minute).

However, the following edit of /src/plot.jl worked for me:

using RecipesBase
using AxisArrays # need to ensure this package is loaded in the environment

@recipe function f(tree::Phylo.AbstractTree; tipnames = getleafnames(tree), treetype = :dendrogram, marker_group = nothing, line_group = nothing, showtips = true, tipfont = (7,)) # add extra kwarg 'tipnames'

    linecolor --> :black
    grid --> false
    framestyle --> :none
    legend --> false
    colorbar --> true
    size --> (1000, 1000)

    d, h, n = _findxy(tree)
    adj = 0.03maximum(values(d))

#################
    if length(tipnames) == length(getleafnames(tree))
        dict = Dict(getleafnames(tree) .=> string.(tipnames)) 
        tipannotations = map(x->(d[x] + adj, h[x], dict[x]), getleafnames(tree))
    else
        @warn "tipnames attribute must have $(length(getleafnames(tree))) elements. Using original tipnames instead."
        tipannotations = map(x->(d[x] + adj, h[x], x), getleafnames(tree))
    end
################
    lz = get(plotattributes, :line_z, nothing)
    #.... continues

Note: the part of the code wrapped in '###' is the only thing that is changed from the original file

@richardreeve
Copy link
Member

Thanks @casasgomezuribarri / @mkborregaard - this seems like a useful thing to be able to do. I'm less certain about interactively changing the actually names of nodes (or branches) in the tree as that would disrupt a few things. In any event, maybe we should add this plotting functionality for now.

@richardreeve richardreeve reopened this Mar 3, 2022
@richardreeve richardreeve mentioned this issue Jan 5, 2024
2 tasks
@richardreeve
Copy link
Member

Okay, renamenode!(tree, node, "new name") now works for some tree types in #91 (including RootedTree) and allows you to rename any node including leaves. It'll return true if it succeeds (it may fail even for supported tree types if the new name is a duplicate), and false if it fails or the tree type is not supported. The new RecursiveTree types are able to rename nodes (so long as there is no leaf information, or it's a Dict (which includes RootedTree).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants