Skip to content

Commit

Permalink
Fixes for phylogenetic adjacency tree (#3406)
Browse files Browse the repository at this point in the history
* fixers for phylogenetic adjacency tree

* get rid of whitespace

* fix for tests
  • Loading branch information
antonydellavecchia committed Feb 22, 2024
1 parent d32efdd commit c65cb77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 27 additions & 9 deletions src/Combinatorics/PhylogeneticTrees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ end
@doc raw"""
phylogenetic_tree(T::Type{<:Union{Float64, QQFieldElem}}, newick::String)
Constructs a phylogenetic tree with Newick representation `newick`. `T` indicates
Constructs a phylogenetic tree with Newick representation `newick`. `T` indicates
the numerical type of the edge lengths.
# Examples
Make a phylogenetic tree with 4 leaves from its Newick representation and print
Make a phylogenetic tree with 4 leaves from its Newick representation and print
its taxa and cophenetic matrix.
```jldoctest
julia> phylo_t = phylogenetic_tree(Float64, "((H:3,(C:1,B:1):2):1,G:4);");
Expand All @@ -38,7 +38,7 @@ function phylogenetic_tree(T::Type{<:Union{Float64, QQFieldElem}}, newick::Strin

# load graph properties
pm_ptree.ADJACENCY

return PhylogeneticTree{T}(pm_ptree)
end

Expand Down Expand Up @@ -92,20 +92,38 @@ end
adjacency_tree(ptree::PhylogeneticTree)
Returns the underlying graph of the phylogenetic tree `ptree`.
# Examples
Make a phylogenetic tree with given Newick format and print its underlying graph.
```jldoctest
julia> ptree = phylogenetic_tree(Float64, "((H:3,(C:1,B:1):2):1,G:4);");
julia> adjacency_tree(ptree)
Undirected graph with 7 nodes and the following edges:
(2, 1)(3, 2)(4, 2)(5, 4)(6, 4)(7, 1)
Directed graph with 7 nodes and the following edges:
(1, 2)(1, 7)(2, 3)(2, 4)(4, 5)(4, 6)
```
"""
function adjacency_tree(ptree::PhylogeneticTree)
return Graph{Undirected}(pm_object(ptree).ADJACENCY)
udir_tree = Graph{Undirected}(ptree.pm_ptree.ADJACENCY)
n = nv(udir_tree)

dir_tree = Graph{Directed}(n)

queue = [1]
visited = fill(false, n)
visited[1] = true
while length(queue) > 0
x = popfirst!(queue)
for y in neighbors(udir_tree, x)
if visited[y] == false
add_edge!(dir_tree, x, y)
push!(queue, y)
visited[y] = true
end
end
end

return dir_tree
end

function Base.show(io::IO, ptree::PhylogeneticTree{T}) where T
Expand Down Expand Up @@ -248,9 +266,9 @@ function tropical_median_consensus(arr::Vector{PhylogeneticTree{T}}) where {T <:

phylo_type = Polymake.bigobject_type(pm_object(first(arr)))
pm_arr = Polymake.Array{Polymake.BigObject}(phylo_type, n)

pm_arr .= pm_object.(arr)

pm_cons_tree = Polymake.tropical.tropical_median_consensus(pm_arr)
return PhylogeneticTree{T}(pm_cons_tree)
end
Expand Down
2 changes: 1 addition & 1 deletion test/Combinatorics/PhylogeneticTrees.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset "Phylogenetic Trees" begin
ptree1 = phylogenetic_tree(Float64, "((A:4,B:4):5,C:9);")
test_tree = graph_from_edges([[2, 1], [3, 2], [4, 2], [5, 1]])
test_tree = graph_from_edges(Directed, [[1, 2], [2, 3], [2, 4], [1, 5]])
@test is_isomorphic(adjacency_tree(ptree1), test_tree)
@test equidistant(ptree1) == true

Expand Down

0 comments on commit c65cb77

Please sign in to comment.