EcoJulia / Taxonomies.jl

a prototype of a taxonomy system in julia
MIT License
2 stars 0 forks source link

Type system? #1

Open tpoisot opened 2 years ago

tpoisot commented 2 years ago

So when trying to add comparisons to NCBITaxonomy, I ended up writing code like


abstract type NCBIRank end
struct RankSuperkingdom <: NCBIRank end
struct RankKingdom <: NCBIRank end
struct RankSubkingdom <: NCBIRank end
struct RankSuperphylum <: NCBIRank end
struct RankPhylum <: NCBIRank end
struct RankSubphylum <: NCBIRank end
struct RankSuperclass <: NCBIRank end
struct RankClass <: NCBIRank end
struct RankSubclass <: NCBIRank end
struct RankInfraclass <: NCBIRank end
struct RankSuperorder <: NCBIRank end
struct RankOrder <: NCBIRank end
struct RankSuborder <: NCBIRank end
struct RankInfraorder <: NCBIRank end
struct RankSuperfamily <: NCBIRank end
struct RankFamily <: NCBIRank end
struct RankSubfamily <: NCBIRank end
struct RankGenus <: NCBIRank end
struct RankSubgenus <: NCBIRank end
struct RankSpecies <: NCBIRank end
struct RankSubspecies <: NCBIRank end
struct RankForma <: NCBIRank end
struct RankFormaspecialis <: NCBIRank end
struct RankGenotype <: NCBIRank end
struct RankIsolate <: NCBIRank end
struct RankMorph <: NCBIRank end
struct RankParvorder <: NCBIRank end
struct RankPathogroup <: NCBIRank end
struct RankSection <: NCBIRank end
struct RankSubsection <: NCBIRank end
struct RankSeries <: NCBIRank end
struct RankSerogroup <: NCBIRank end
struct RankSerotype <: NCBIRank end
struct RankSpeciesgroup <: NCBIRank end
struct RankSpeciessubgroup <: NCBIRank end
struct RankStrain <: NCBIRank end
struct RankSubcohort <: NCBIRank end
struct RankBiotype <: NCBIRank end
struct RankCohort <: NCBIRank end
struct RankSubtribe <: NCBIRank end
struct RankTribe <: NCBIRank end
struct RankVarietas <: NCBIRank end
struct RankClade <: NCBIRank end
struct RankNorank <: NCBIRank end
tpoisot commented 2 years ago

And writing the actual comparison code is not too bad:

import Base: isless

hierarchy = [
    NCBITaxonomy.RankSuperkingdom,
    NCBITaxonomy.RankKingdom,
    NCBITaxonomy.RankSubkingdom,
    NCBITaxonomy.RankSuperphylum,
    NCBITaxonomy.RankPhylum,
    NCBITaxonomy.RankSubphylum,
    NCBITaxonomy.RankSuperclass,
    NCBITaxonomy.RankClass,
    NCBITaxonomy.RankSubclass,
    NCBITaxonomy.RankInfraclass,
    NCBITaxonomy.RankSuperorder,
    NCBITaxonomy.RankOrder,
    NCBITaxonomy.RankSuborder,
    NCBITaxonomy.RankInfraorder,
    NCBITaxonomy.RankSuperfamily,
    NCBITaxonomy.RankFamily,
    NCBITaxonomy.RankSubfamily,
    NCBITaxonomy.RankGenus,
    NCBITaxonomy.RankSubgenus,
    NCBITaxonomy.RankSpecies,
    NCBITaxonomy.RankSubspecies,
]

for higherlevel_index in 1:(length(hierarchy)-1)
    for lowerlevel_index in (higherlevel_index+1):length(hierarchy)
        eval(quote
            Base.isless(::Type{$(hierarchy[lowerlevel_index])}, ::Type{$(hierarchy[higherlevel_index])}) = true
            Base.isless(::Type{$(hierarchy[higherlevel_index])}, ::Type{$(hierarchy[lowerlevel_index])}) = false
        end)
    end
end
tpoisot commented 2 years ago

But this is a good use-case for this package. If the types were defined in Taxonomies.jl, we could use a standard set of ranks across the EcoJulia ecosystem, and then map the specific type to them.

tpoisot commented 2 years ago

One issue is that not all of these ranks have a hierarchical relationship, necessarilly

gottacatchenall commented 2 years ago

One issue is that not all of these ranks have a hierarchical relationship, necessarilly

That was part of my thinking when putting together FixedRank vs FlexibleRank taxonomy, might revisit this soon