mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-12-08 08:40:26 +00:00
Changes to be committed:
modified: cmd/obitools/obitag/main.go modified: cmd/obitools/obitag2/main.go modified: go.mod modified: go.sum modified: pkg/obiformats/ncbitaxdump/read.go modified: pkg/obioptions/version.go modified: pkg/obiseq/attributes.go modified: pkg/obiseq/taxonomy_lca.go modified: pkg/obiseq/taxonomy_methods.go modified: pkg/obiseq/taxonomy_predicate.go modified: pkg/obitax/inner.go modified: pkg/obitax/lca.go new file: pkg/obitax/taxid.go modified: pkg/obitax/taxon.go modified: pkg/obitax/taxonomy.go modified: pkg/obitax/taxonslice.go modified: pkg/obitools/obicleandb/obicleandb.go modified: pkg/obitools/obigrep/options.go modified: pkg/obitools/obilandmark/obilandmark.go modified: pkg/obitools/obilandmark/options.go modified: pkg/obitools/obirefidx/famlilyindexing.go modified: pkg/obitools/obirefidx/geomindexing.go modified: pkg/obitools/obirefidx/obirefidx.go modified: pkg/obitools/obirefidx/options.go modified: pkg/obitools/obitag/obigeomtag.go modified: pkg/obitools/obitag/obitag.go modified: pkg/obitools/obitag/options.go modified: pkg/obiutils/strings.go
This commit is contained in:
60
pkg/obitax/taxid.go
Normal file
60
pkg/obitax/taxid.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package obitax
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
|
||||
)
|
||||
|
||||
// Taxid represents a taxonomic identifier as a pointer to a string.
|
||||
type Taxid *string
|
||||
|
||||
// TaxidFactory is a factory for creating Taxid instances from strings and integers.
|
||||
type TaxidFactory struct {
|
||||
inner *InnerString
|
||||
code string
|
||||
alphabet obiutils.AsciiSet
|
||||
}
|
||||
|
||||
// NewTaxidFactory creates and returns a new instance of TaxidFactory.
|
||||
func NewTaxidFactory(code string, alphabet obiutils.AsciiSet) *TaxidFactory {
|
||||
return &TaxidFactory{
|
||||
inner: NewInnerString(),
|
||||
code: code + ":",
|
||||
alphabet: alphabet,
|
||||
}
|
||||
// Initialize and return a new TaxidFactory.
|
||||
}
|
||||
|
||||
// FromString converts a string representation of a taxonomic identifier into a Taxid.
|
||||
// It extracts the relevant part of the string after the first colon (':') if present.
|
||||
func (f *TaxidFactory) FromString(taxid string) (Taxid, error) {
|
||||
taxid = obiutils.AsciiSpaceSet.TrimLeft(taxid)
|
||||
part1, part2 := obiutils.SplitInTwo(taxid, ':')
|
||||
if len(part2) == 0 {
|
||||
taxid = part1
|
||||
} else {
|
||||
if part1 != f.code {
|
||||
return nil, fmt.Errorf("taxid %s string does not start with taxonomy code %s", taxid, f.code)
|
||||
}
|
||||
taxid = part2
|
||||
}
|
||||
|
||||
taxid, err := f.alphabet.FirstWord(taxid) // Get the first word from the input string.
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return a new Taxid by innerizing the extracted taxid string.
|
||||
rep := Taxid(f.inner.Innerize(taxid))
|
||||
return rep, nil
|
||||
}
|
||||
|
||||
// FromInt converts an integer taxonomic identifier into a Taxid.
|
||||
// It first converts the integer to a string and then innerizes it.
|
||||
func (f *TaxidFactory) FromInt(taxid int) (Taxid, error) {
|
||||
s := strconv.Itoa(taxid) // Convert the integer to a string.
|
||||
return f.inner.Innerize(s), nil // Return a new Taxid by innerizing the string.
|
||||
}
|
||||
Reference in New Issue
Block a user