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

support AbstractTrees.jl v4 #6

Merged
merged 5 commits into from
Aug 31, 2022
Merged
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
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"

[compat]
AbstractTrees = "0.3"
DataAPI = "1.6"
julia = "1.5"

Expand Down
7 changes: 1 addition & 6 deletions src/Taxonomy.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module Taxonomy

using AbstractTrees: isempty
using AbstractTrees
import DataAPI,
DataAPI.All,
Expand All @@ -11,9 +9,7 @@ export CanonicalRank,
Lineage,
taxid, name, rank, parent, children, lca,
reformat, print_lineage, isdescendant, isancestor,
PhyloTree,
topolgoy,
children, print_tree, Leaves,
children, print_tree,
All, Between, Cols,
From, Until

Expand All @@ -23,6 +19,5 @@ include("database.jl")
include("taxon.jl")
include("lineage.jl")
include("lca.jl")
include("tree.jl")

end
10 changes: 5 additions & 5 deletions src/lineage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ function Lineage(taxon::Taxon)
line = Taxon[]
current_taxon = taxon
push!(line,current_taxon)
while parent(current_taxon) !== nothing
current_taxon = parent(current_taxon)
while AbstractTrees.parent(current_taxon) !== nothing
current_taxon = AbstractTrees.parent(current_taxon)
push!(line, current_taxon)
end
reverse!(line)
Expand Down Expand Up @@ -151,13 +151,13 @@ Base.show(io::IO, lineage::Lineage) = print_lineage(io, lineage)

Return true if the former taxon is a descendant of the latter taxon.
"""

isdescendant(descendant::Taxon, ancestor::Taxon) = ancestor in Lineage(descendant)
# overload because native AbstractTrees.isdescendant is too slow
AbstractTrees.isdescendant(descendant::Taxon, ancestor::Taxon) = ancestor in Lineage(descendant)

"""
isancestor(ancestor::Taxon, descendant::Taxon)

Return true if the former taxon is an ancestor of the latter taxon.
"""

isancestor(ancestor::Taxon, descendant::Taxon) = isdescendant(descendant, ancestor)
isancestor(ancestor::Taxon, descendant::Taxon) = AbstractTrees.isdescendant(descendant, ancestor)
61 changes: 33 additions & 28 deletions src/taxon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,41 @@ struct Taxon <: AbstractTaxon
db::DB
end

# define Traits
AbstractTrees.ParentLinks(::Type{Taxon}) = StoredParents()
AbstractTrees.ChildIndexing(::Type{Taxon}) = IndexedChildren()
AbstractTrees.NodeType(::Type{Taxon}) = HasNodeType()
AbstractTrees.nodetype(::Type{Taxon}) = Taxon

"""
parent(taxon::Taxon)

Return the `Taxon` object that is the parent of the given `Taxon` object.
"""

function AbstractTrees.parent(taxon::Taxon)
parent_taxid = get(taxon.db.parents, taxon.taxid, nothing)
if parent_taxid === nothing
return nothing
end
parent = Taxon(parent_taxid, taxon.db)
return parent
end

"""
children(taxon::Taxon)

Return the vector of `Taxon` objects that are children of the given `Taxon` object.
"""

function AbstractTrees.children(taxon::Taxon)
children_taxid = findall(isequal(taxon.taxid), taxon.db.parents)
children_taxon = map(x -> Taxon(x, taxon.db), children_taxid)
return children_taxon
end

Base.show(io::IO, taxon::Taxon) = print(io, "$(taxon.taxid) [$(String(taxon.rank))] $(taxon.name)")
AbstractTrees.printnode(io::IO, taxon::Taxon) = print(io, taxon)
AbstractTrees.nodetype(::Taxon) = Taxon

function Taxon(taxid::Int, db::DB)
name = db.names[taxid]
Expand Down Expand Up @@ -60,33 +92,6 @@ Return the taxid of the given `Taxon` object.

taxid(taxon::Taxon) = taxon.taxid

"""
parent(taxon::Taxon)

Return the `Taxon` object that is the parent of the given `Taxon` object.
"""

function Base.parent(taxon::Taxon)
parent_taxid = get(taxon.db.parents, taxon.taxid, nothing)
if parent_taxid === nothing
return nothing
end
parent = Taxon(parent_taxid, taxon.db)
return parent
end

"""
children(taxon::Taxon)

Return the vector of `Taxon` objects that are children of the given `Taxon` object.
"""

function AbstractTrees.children(taxon::Taxon)
children_taxid = findall(isequal(taxon.taxid), taxon.db.parents)
children_taxon = map(x -> Taxon(x, taxon.db), children_taxid)
return children_taxon
end

struct UnclassifiedTaxon <:AbstractTaxon
name::String
rank::Symbol
Expand Down
33 changes: 0 additions & 33 deletions src/tree.jl

This file was deleted.

12 changes: 3 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Taxonomy
using Taxonomy.AbstractTrees
using Test

db = Taxonomy.DB("db/nodes.dmp", "db/names.dmp")
Expand All @@ -18,7 +19,7 @@ db = Taxonomy.DB("db/nodes.dmp", "db/names.dmp")

@test taxid(human) == human.taxid
@test rank(human) == human.rank
@test parent(human) == Taxon(9605,db)
@test AbstractTrees.parent(human) == Taxon(9605,db)
@test children(human) == [Taxon(741158,db), Taxon(63221,db)]
denisova = Taxon(741158, db)
@test children(denisova) == Taxon[]
Expand Down Expand Up @@ -91,11 +92,4 @@ end
pan = Taxon(9598, db)
@test lca([human,gorilla]) == lca(human,gorilla) == Taxon(207598, db)
@test lca([human,gorilla,pan]) == lca(human,gorilla,pan) == Taxon(207598, db)
end

@testset "tree.jl" begin
human = Taxon(9606, db)
gorilla = Taxon(9593, db)
tree = topolgoy([human, gorilla])
@test tree.node == Taxon(207598, db)
end
end