Files
obitools4/pkg/obitax/taxonslice.go

127 lines
3.5 KiB
Go
Raw Normal View History

2024-11-16 05:59:41 +01:00
/*
Package obitax provides functionality for handling taxonomic data structures,
specifically for representing and manipulating collections of taxon nodes
within a taxonomy.
The primary data structure is the TaxonSlice, which encapsulates a slice of
TaxNode instances and provides methods for accessing, counting, and
formatting these nodes.
*/
2022-01-13 23:27:39 +01:00
package obitax
import (
"bytes"
"fmt"
2024-11-14 19:10:23 +01:00
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
)
2024-11-16 05:59:41 +01:00
// TaxonSlice represents a slice of TaxNode instances within a taxonomy.
2024-11-08 09:48:16 +01:00
// It encapsulates a collection of taxon nodes and the taxonomy they belong to.
//
// Fields:
2024-11-16 05:59:41 +01:00
// - slice: A slice of pointers to TaxNode representing the taxon nodes.
// - taxonomy: A pointer to the Taxonomy instance that these taxon nodes are part of.
2024-11-08 09:48:16 +01:00
type TaxonSlice struct {
slice []*TaxNode
taxonomy *Taxonomy
}
2022-01-13 23:27:39 +01:00
2024-11-16 05:59:41 +01:00
// NewTaxonSlice creates a new TaxonSlice with the specified size and capacity.
// It initializes the slice of TaxNode pointers and associates it with the given taxonomy.
//
// Parameters:
// - size: The initial size of the slice.
// - capacity: The capacity of the slice.
//
// Returns:
// - A pointer to the newly created TaxonSlice.
2024-11-14 19:10:23 +01:00
func (taxonomy *Taxonomy) NewTaxonSlice(size, capacity int) *TaxonSlice {
return &TaxonSlice{
slice: make([]*TaxNode, size, capacity),
taxonomy: taxonomy.OrDefault(true),
2024-11-14 19:10:23 +01:00
}
}
2024-11-16 05:59:41 +01:00
// Get retrieves the TaxNode at the specified index from the TaxonSlice.
2024-11-08 09:48:16 +01:00
// It returns the taxon node corresponding to the provided index.
//
// Parameters:
// - i: An integer representing the index of the taxon node to retrieve.
//
// Returns:
2024-11-16 05:59:41 +01:00
// - A pointer to the TaxNode at the specified index in the slice.
2024-11-08 09:48:16 +01:00
func (slice *TaxonSlice) Get(i int) *TaxNode {
2024-11-14 19:10:23 +01:00
if slice == nil {
return nil
}
2024-11-08 09:48:16 +01:00
return slice.slice[i]
2022-01-13 23:27:39 +01:00
}
2024-11-16 05:59:41 +01:00
// Len returns the number of TaxNode instances in the TaxonSlice.
2024-11-08 09:48:16 +01:00
// It provides the count of taxon nodes contained within the slice.
//
// Returns:
// - An integer representing the total number of taxon nodes in the TaxonSlice.
func (slice *TaxonSlice) Len() int {
2024-11-14 19:10:23 +01:00
if slice == nil {
return 0
}
2024-11-08 09:48:16 +01:00
return len(slice.slice)
2022-01-13 23:27:39 +01:00
}
2024-11-08 09:48:16 +01:00
// String returns a string representation of the TaxonSlice.
// It formats the output to include the IDs, scientific names, and ranks of the taxon nodes
// in the slice, concatenated in reverse order, separated by vertical bars.
//
// Returns:
// - A formatted string representing the TaxonSlice, with each taxon in the format
// "id@scientific_name@rank". If the slice is empty, it returns an empty string.
func (path *TaxonSlice) String() string {
var buffer bytes.Buffer
2024-11-08 09:48:16 +01:00
if path.Len() > 0 {
taxon := path.slice[path.Len()-1]
fmt.Fprintf(&buffer, "%v@%s@%s",
2024-11-24 19:33:24 +01:00
*taxon.Id(),
taxon.ScientificName(),
taxon.Rank())
2024-11-08 09:48:16 +01:00
for i := path.Len() - 2; i >= 0; i-- {
taxon := path.slice[i]
fmt.Fprintf(&buffer, "|%v@%s@%s",
2024-11-24 19:33:24 +01:00
*taxon.Id(),
taxon.ScientificName(),
taxon.Rank())
}
}
return buffer.String()
}
2024-11-14 19:10:23 +01:00
2024-11-16 05:59:41 +01:00
// Reverse reverses the order of the TaxonSlice.
// If inplace is true, the original slice is modified; otherwise, a new reversed
// TaxonSlice is returned.
//
// Parameters:
// - inplace: A boolean indicating whether to reverse the slice in place.
//
// Returns:
// - A pointer to the reversed TaxonSlice. If inplace is true, it returns the original slice.
2024-11-14 19:10:23 +01:00
func (slice *TaxonSlice) Reverse(inplace bool) *TaxonSlice {
if slice == nil {
return nil
}
rep := obiutils.Reverse(slice.slice, inplace)
if inplace {
return slice
}
return &TaxonSlice{
taxonomy: slice.taxonomy,
slice: rep,
}
}