mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
Add the --with-taxon-at-rank option code to obiannotate
This commit is contained in:
@ -33,7 +33,7 @@ func (taxon *TaxNode) TaxonAtRank(rank string) *TaxNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if taxon == taxon.pparent {
|
if taxon == taxon.pparent && taxon.rank != rank {
|
||||||
taxon = nil
|
taxon = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,26 +12,37 @@ import (
|
|||||||
// If the taxon at the given rank doesn't exist for the taxonomy annotation
|
// If the taxon at the given rank doesn't exist for the taxonomy annotation
|
||||||
// of the sequence, nothing happens.
|
// of the sequence, nothing happens.
|
||||||
func (taxonomy *Taxonomy) SetTaxonAtRank(sequence *obiseq.BioSequence, rank string) *TaxNode {
|
func (taxonomy *Taxonomy) SetTaxonAtRank(sequence *obiseq.BioSequence, rank string) *TaxNode {
|
||||||
|
var taxonAtRank *TaxNode
|
||||||
|
|
||||||
taxid := sequence.Taxid()
|
taxid := sequence.Taxid()
|
||||||
taxon, err := taxonomy.Taxon(taxid)
|
taxon, err := taxonomy.Taxon(taxid)
|
||||||
taxonAtRank := taxon.TaxonAtRank(rank)
|
taxonAtRank = nil
|
||||||
|
if err == nil {
|
||||||
if err == nil && taxonAtRank != nil {
|
taxonAtRank = taxon.TaxonAtRank(rank)
|
||||||
|
if taxonAtRank != nil {
|
||||||
|
// log.Printf("Taxid: %d Rank: %s --> proposed : %d (%s)", taxid, rank, taxonAtRank.taxid, *(taxonAtRank.scientificname))
|
||||||
sequence.SetAttribute(rank, taxonAtRank.taxid)
|
sequence.SetAttribute(rank, taxonAtRank.taxid)
|
||||||
sequence.SetAttribute(rank+"_name", taxonAtRank.scientificname)
|
sequence.SetAttribute(rank+"_name", *taxonAtRank.scientificname)
|
||||||
|
} else {
|
||||||
|
sequence.SetAttribute(rank, -1)
|
||||||
|
sequence.SetAttribute(rank+"_name", "NA")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return taxonAtRank
|
return taxonAtRank
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setting the species of a sequence.
|
||||||
func (taxonomy *Taxonomy) SetSpecies(sequence *obiseq.BioSequence) *TaxNode {
|
func (taxonomy *Taxonomy) SetSpecies(sequence *obiseq.BioSequence) *TaxNode {
|
||||||
return taxonomy.SetTaxonAtRank(sequence, "species")
|
return taxonomy.SetTaxonAtRank(sequence, "species")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setting the genus of a sequence.
|
||||||
func (taxonomy *Taxonomy) SetGenus(sequence *obiseq.BioSequence) *TaxNode {
|
func (taxonomy *Taxonomy) SetGenus(sequence *obiseq.BioSequence) *TaxNode {
|
||||||
return taxonomy.SetTaxonAtRank(sequence, "genus")
|
return taxonomy.SetTaxonAtRank(sequence, "genus")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setting the family of a sequence.
|
||||||
func (taxonomy *Taxonomy) SetFamily(sequence *obiseq.BioSequence) *TaxNode {
|
func (taxonomy *Taxonomy) SetFamily(sequence *obiseq.BioSequence) *TaxNode {
|
||||||
return taxonomy.SetTaxonAtRank(sequence, "family")
|
return taxonomy.SetTaxonAtRank(sequence, "family")
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package obiannotate
|
|||||||
import (
|
import (
|
||||||
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiiter"
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiiter"
|
||||||
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
||||||
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obitax"
|
||||||
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obitools/obigrep"
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obitools/obigrep"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -43,6 +44,17 @@ func RenameAttributeWorker(toBeRenamed map[string]string) obiseq.SeqWorker {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddTaxonAtRankWorker(taxonomy *obitax.Taxonomy, ranks ...string) obiseq.SeqWorker {
|
||||||
|
f := func(s *obiseq.BioSequence) *obiseq.BioSequence {
|
||||||
|
for _, r := range ranks {
|
||||||
|
taxonomy.SetTaxonAtRank(s,r)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
func CLIAnnotationWorker() obiseq.SeqWorker {
|
func CLIAnnotationWorker() obiseq.SeqWorker {
|
||||||
var annotator obiseq.SeqWorker
|
var annotator obiseq.SeqWorker
|
||||||
annotator = nil
|
annotator = nil
|
||||||
@ -62,6 +74,12 @@ func CLIAnnotationWorker() obiseq.SeqWorker {
|
|||||||
annotator = annotator.ChainWorkers(w)
|
annotator = annotator.ChainWorkers(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if CLIHasTaxonAtRank() {
|
||||||
|
taxo := obigrep.CLILoadSelectedTaxonomy()
|
||||||
|
w := AddTaxonAtRankWorker(taxo,CLITaxonAtRank()...)
|
||||||
|
annotator = annotator.ChainWorkers(w)
|
||||||
|
}
|
||||||
|
|
||||||
return annotator
|
return annotator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,3 +121,10 @@ func CLIToBeKeptAttributes() map[string]bool {
|
|||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CLIHasTaxonAtRank() bool {
|
||||||
|
return len(_taxonAtRank) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func CLITaxonAtRank() []string {
|
||||||
|
return _taxonAtRank
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user