mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
89 lines
1.6 KiB
Go
89 lines
1.6 KiB
Go
package obitax
|
|
|
|
import (
|
|
"regexp"
|
|
|
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
|
)
|
|
|
|
type TaxNode struct {
|
|
taxid int
|
|
parent int
|
|
pparent *TaxNode
|
|
rank string
|
|
scientificname *string
|
|
alternatenames *map[string]*string
|
|
}
|
|
|
|
func NewTaxNode(taxid int, parent int, rank string) *TaxNode {
|
|
n := TaxNode{taxid, parent, nil, rank, nil, nil}
|
|
return &n
|
|
}
|
|
|
|
func (node *TaxNode) ScientificName() string {
|
|
n := node.scientificname
|
|
if n == nil {
|
|
return ""
|
|
}
|
|
|
|
return *n
|
|
}
|
|
|
|
func (node *TaxNode) Rank() string {
|
|
return node.rank
|
|
}
|
|
|
|
func (node *TaxNode) Taxid() int {
|
|
return node.taxid
|
|
}
|
|
|
|
func (node *TaxNode) Parent() *TaxNode {
|
|
return node.pparent
|
|
}
|
|
|
|
func (node *TaxNode) IsNameEqual(name string) bool {
|
|
if *(node.scientificname) == name {
|
|
return true
|
|
}
|
|
if node.alternatenames != nil {
|
|
_, ok := (*node.alternatenames)[name]
|
|
return ok
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (node *TaxNode) IsNameMatching(pattern *regexp.Regexp) bool {
|
|
if pattern.MatchString(*(node.scientificname)) {
|
|
return true
|
|
}
|
|
if node.alternatenames != nil {
|
|
for n := range *node.alternatenames {
|
|
if pattern.MatchString(n) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (node *TaxNode) HasRankDefined(rank string) bool {
|
|
|
|
for node.rank != rank && node.parent != node.taxid {
|
|
node = node.pparent
|
|
}
|
|
|
|
return node.rank == rank
|
|
|
|
}
|
|
|
|
func HasRankDefined(taxonomy Taxonomy, rank string) obiseq.SequencePredicate {
|
|
|
|
f := func(sequence *obiseq.BioSequence) bool {
|
|
taxon, err := taxonomy.Taxon(sequence.Taxid())
|
|
return err == nil && taxon.HasRankDefined(rank)
|
|
}
|
|
|
|
return f
|
|
}
|