Refactoring of some code

This commit is contained in:
2022-08-23 11:07:39 +02:00
parent 62968aaa26
commit abaede48b5
4 changed files with 71 additions and 36 deletions

View File

@ -1,11 +1,5 @@
package obitax
import (
log "github.com/sirupsen/logrus"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
)
func (taxon *TaxNode) IsSubCladeOf(parent *TaxNode) bool {
for taxon.taxid != parent.taxid && taxon.parent != taxon.taxid {
@ -25,18 +19,3 @@ func (taxon *TaxNode) IsBelongingSubclades(clades *TaxonSet) bool {
return ok
}
func IsSubCladeOf(taxonomy Taxonomy, taxid int) obiseq.SequencePredicate {
parent, err := taxonomy.Taxon(taxid)
if err != nil {
log.Fatalf("Cannot find taxon : %d (%v)", taxid, err)
}
f := func(sequence *obiseq.BioSequence) bool {
taxon, err := taxonomy.Taxon(sequence.Taxid())
return err == nil && taxon.IsSubCladeOf(parent)
}
return f
}

View File

@ -0,0 +1,71 @@
package obitax
import (
log "github.com/sirupsen/logrus"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
)
func (taxonomy *Taxonomy) IsAValidTaxon(withAutoCorrection ...bool) obiseq.SequencePredicate {
deprecatedTaxidsWarning := make(map[int]bool)
autocorrection := false
if len(withAutoCorrection) > 0 {
autocorrection = withAutoCorrection[0]
}
f := func(sequence *obiseq.BioSequence) bool {
taxid := sequence.Taxid()
taxon, err := taxonomy.Taxon(taxid)
if err == nil && taxon.taxid != taxid {
if autocorrection {
sequence.SetTaxid(taxon.taxid)
log.Printf("Sequence %s : Taxid %d updated with %d", taxid, taxon.taxid)
} else {
if _, ok := deprecatedTaxidsWarning[taxid]; !ok {
deprecatedTaxidsWarning[taxid] = true
log.Printf("Taxid %d is deprecated and must be replaced by %d", taxid, taxon.taxid)
}
}
}
return err == nil
}
return f
}
// A function that takes a taxonomy and a taxid as arguments and returns a function that takes a
// pointer to a BioSequence as an argument and returns a boolean.
func (taxonomy *Taxonomy) IsSubCladeOf(taxid int) obiseq.SequencePredicate {
parent, err := taxonomy.Taxon(taxid)
if err != nil {
log.Fatalf("Cannot find taxon : %d (%v)", taxid, err)
}
f := func(sequence *obiseq.BioSequence) bool {
taxon, err := taxonomy.Taxon(sequence.Taxid())
return err == nil && taxon.IsSubCladeOf(parent)
}
return f
}
func (taxonomy *Taxonomy) HasRequiredRank(rank string) obiseq.SequencePredicate {
if !goutils.Contains(taxonomy.RankList(), rank) {
log.Fatalf("%s is not a valid rank (allowed ranks are %v)",
rank,
taxonomy.RankList())
}
f := func(sequence *obiseq.BioSequence) bool {
taxon, err := taxonomy.Taxon(sequence.Taxid())
return err == nil && taxon.HasRankDefined(rank)
}
return f
}

View File

@ -2,8 +2,6 @@ package obitax
import (
"regexp"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
)
type TaxNode struct {
@ -72,17 +70,6 @@ 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
}

View File

@ -2,7 +2,6 @@ package obitax
import (
"fmt"
log "github.com/sirupsen/logrus"
)
type TaxName struct {
@ -63,7 +62,6 @@ func (taxonomy *Taxonomy) Taxon(taxid int) (*TaxNode, error) {
if !aok {
return nil, fmt.Errorf("Taxid %d is not part of the taxonomy", taxid)
}
log.Printf("Taxid %d is deprecated and must be replaced by %d", taxid, a.taxid)
t = a
}
return t, nil