Files
obitools4/pkg/obitax/issuubcladeof.go

55 lines
1.5 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obitax
2024-11-14 19:10:23 +01:00
import log "github.com/sirupsen/logrus"
2022-01-13 23:27:39 +01:00
2024-11-16 05:59:41 +01:00
// IsSubCladeOf checks if the current Taxon is a subclade of the specified parent Taxon.
// It returns true if the current Taxon is a descendant of the parent Taxon in the taxonomy hierarchy.
//
// Parameters:
// - parent: A pointer to the parent Taxon to check against.
//
// Returns:
// - A boolean indicating whether the current Taxon is a subclade of the parent Taxon.
// - Logs a fatal error if the two taxa do not belong to the same taxonomy.
2024-11-08 09:48:16 +01:00
func (taxon *Taxon) IsSubCladeOf(parent *Taxon) bool {
if taxon.Taxonomy != parent.Taxonomy {
log.Fatalf(
"Both taxa %s and %s must belong to the same taxonomy",
taxon.String(),
parent.String(),
)
}
2022-01-13 23:27:39 +01:00
2024-11-08 09:48:16 +01:00
for t := range taxon.IPath() {
if t.Node.Id() == parent.Node.Id() {
return true
}
2022-01-13 23:27:39 +01:00
}
2024-11-08 09:48:16 +01:00
return false
2022-01-13 23:27:39 +01:00
}
2024-11-14 19:10:23 +01:00
2024-11-16 05:59:41 +01:00
// IsBelongingSubclades checks if the current Taxon belongs to any of the specified subclades.
// It traverses up the taxonomy hierarchy to determine if the current Taxon or any of its ancestors
// belong to the provided TaxonSet.
//
// Parameters:
// - clades: A pointer to a TaxonSet containing the subclades to check against.
//
// Returns:
// - A boolean indicating whether the current Taxon or any of its ancestors belong to the specified subclades.
2024-11-14 19:10:23 +01:00
func (taxon *Taxon) IsBelongingSubclades(clades *TaxonSet) bool {
ok := clades.Contains(taxon.Node.id)
for !ok && !taxon.IsRoot() {
taxon = taxon.Parent()
ok = clades.Contains(taxon.Node.id)
}
if taxon.IsRoot() {
ok = clades.Contains(taxon.Node.id)
}
return ok
}