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

Adding functionality #93

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
4 changes: 2 additions & 2 deletions src/LinkTree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ newempty(::Type{Data}) where {Data} = Data()

A branch type that connects LinkNodes in a LinkTree
"""
struct LinkBranch{RT, NL, Data, LenUnits} <: AbstractBranch{RT, NL}
mutable struct LinkBranch{RT, NL, Data, LenUnits} <: AbstractBranch{RT, NL}
name::Int
inout::Tuple{AbstractNode{RT, NL}, AbstractNode{RT, NL}}
inout::Tuple{<:AbstractNode{RT, NL}, <:AbstractNode{RT, NL}}
length::Union{Missing, LenUnits}
data::Data
end
Expand Down
57 changes: 57 additions & 0 deletions src/Phylo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,63 @@ include("metrics.jl")
export mrca, nodeheights
export distance, distances, heighttoroot, heightstoroot

"""
sort!(::AbstractTree; uselength = [if available], rev = false)

Sorts the branches descending from each node by total number of descendants
(or branch length is `uselength = true`). This creates a clearer tree for
plotting. The process is also called "ladderizing" the tree. Use `rev = true`
to reverse the sorting order.
"""
function Base.sort!(tree::T;
uselength = all(haslength.(Ref(tree), getbranches(tree))),
rev = false) where
{T <: AbstractTree{OneTree, <:Rooted}}
function loc!(clade::String)
if isleaf(tree, clade)
return uselength ?
zero(nonmissingtype(typeof(getlength(tree,
getinbound(tree,
clade))))) :
1.0
end

sizes = uselength ?
[getlength(tree, child) +
loc!(getnodename(tree, dst(tree, child)))
for child in getoutbounds(tree, clade)] :
map(loc!, getchildren(tree, clade))
node = getnode(tree, clade)
if T <: LinkTree
node.other .= node.other[sortperm(sizes, rev = rev)]
elseif T <: RecursiveTree
node.conns .= node.conns[sortperm(sizes, rev = rev)]
end
if uselength
return maximum(sizes)
else
return sum(sizes) + 1.0
end
end

for root in getroots(tree)
loc!(getnodename(tree, root))
end

return tree
end

"""
sort(::AbstractTree; uselength = [if available], rev = false)

Copies a tree and sorts its branches. See `sort!` for further details.
"""
function Base.sort(tree::AbstractTree;
uselength = all(haslength.(Ref(tree), getbranches(tree))),
rev = false)
return sort!(deepcopy(tree), uselength = uselength, rev = rev)
end

# Path into package
path(path...; dir::String = "test") = joinpath(@__DIR__, "..", dir, path...)

Expand Down
4 changes: 0 additions & 4 deletions src/RecursiveTree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ function RecursiveTree{RT, NL, NodeData, BranchData, BT, LenUnits, TD}(leafinfos
end

import Phylo.API: _prefernodeobjects, _preferbranchobjects
_prefernodeobjects(::Type{<:RecursiveTree}) = true
_prefernodeobjects(::Type{<:RecursiveNode}) = true
_prefernodeobjects(::Type{<:RecursiveElt}) = true
_preferbranchobjects(::Type{<:RecursiveTree}) = true
_preferbranchobjects(::Type{<:RecursiveBranch}) = true
_preferbranchobjects(::Type{<:RecursiveElt}) = true

import Phylo.API: _validate!
Expand Down
35 changes: 0 additions & 35 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,41 +220,6 @@ function _extend(tmp, x)
return ret
end

"""
sort!(::AbstractTree; rev = false)

Sorts the branches descending from each node by total number of
descendants. This creates a clearer tree for plotting. The
process is also called "ladderizing" the tree. Use `rev=true` to
reverse the sorting order.
"""
function Base.sort!(tree::T; rev = false) where {T <: AbstractTree}
function loc!(clade::String)
if isleaf(tree, clade)
return 1
end

sizes = map(loc!, getchildren(tree, clade))
node = getnode(tree, clade)
if T <: LinkTree
node.other .= node.other[sortperm(sizes, rev = rev)]
elseif T <: RecursiveTree
node.conns .= node.conns[sortperm(sizes, rev = rev)]
end
return sum(sizes) + 1
end

loc!(first(nodenamefilter(isroot, tree)))
return tree
end

"""
sort(::AbstractTree; rev = false)

Copies a tree and sorts its branches. See `sort!` for further details.
"""
Base.sort(tree::AbstractTree; rev = false) = sort!(copy(tree))

function _nodedepths(tree::Phylo.AbstractTree)
function finddepths!(clade::String)
if !in(clade, keys(depth))
Expand Down
15 changes: 15 additions & 0 deletions test/test_Phylo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module TestPhylo
using Test

using Phylo
using Random

@testset "Deprecations" begin
t = NamedTree()
Expand All @@ -16,4 +17,18 @@ using Phylo
@test_deprecated treenameiter(t)
end

@testset "Sort $TreeType" for TreeType in [RootedTree, Phylo.LTD{OneRoot, Float64}]
tree = rand(Nonultrametric{TreeType}(100))
@test validate!(deepcopy(tree))
t2 = sort(tree)
leaves = getleaves(t2, inorder)
@test maximum(getheight.(Ref(t2), leaves)) == getheight(t2, leaves[end])
t3 = sort(tree, uselength = false, rev = true)
leaves = getleaves(t3, inorder)
for b in getbranches(t3)
b.length = one(b.length)
end
@test maximum(getheight.(Ref(t3), leaves)) == getheight(t3, first(leaves))
end

end
Loading