2026-02-05 14:46:52 +01:00
|
|
|
package obikmer
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiseq"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// KmerSetGroup represents a vector of KmerSet
|
|
|
|
|
// Used to manage multiple k-mer sets (for example, by frequency level)
|
2026-02-05 14:46:52 +01:00
|
|
|
type KmerSetGroup struct {
|
2026-02-05 16:35:38 +01:00
|
|
|
id string // Unique identifier of the KmerSetGroup
|
|
|
|
|
k int // Size of k-mers (immutable)
|
|
|
|
|
sets []*KmerSet // Vector of KmerSet
|
|
|
|
|
Metadata map[string]interface{} // Group metadata (not individual sets)
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// NewKmerSetGroup creates a new group of n KmerSets
|
2026-02-05 14:46:52 +01:00
|
|
|
func NewKmerSetGroup(k int, n int) *KmerSetGroup {
|
|
|
|
|
if n < 1 {
|
|
|
|
|
panic("KmerSetGroup size must be >= 1")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sets := make([]*KmerSet, n)
|
|
|
|
|
for i := range sets {
|
|
|
|
|
sets[i] = NewKmerSet(k)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &KmerSetGroup{
|
2026-02-05 15:32:19 +01:00
|
|
|
k: k,
|
2026-02-05 15:02:27 +01:00
|
|
|
sets: sets,
|
2026-02-05 15:32:19 +01:00
|
|
|
Metadata: make(map[string]interface{}),
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// K returns the size of k-mers (immutable)
|
2026-02-05 15:32:19 +01:00
|
|
|
func (ksg *KmerSetGroup) K() int {
|
|
|
|
|
return ksg.k
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Size returns the number of KmerSet in the group
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) Size() int {
|
|
|
|
|
return len(ksg.sets)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Get returns the KmerSet at the given index
|
|
|
|
|
// Returns nil if the index is invalid
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) Get(index int) *KmerSet {
|
|
|
|
|
if index < 0 || index >= len(ksg.sets) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return ksg.sets[index]
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Set replaces the KmerSet at the given index
|
|
|
|
|
// Panics if the index is invalid or if k does not match
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) Set(index int, ks *KmerSet) {
|
|
|
|
|
if index < 0 || index >= len(ksg.sets) {
|
|
|
|
|
panic(fmt.Sprintf("Index out of bounds: %d (size: %d)", index, len(ksg.sets)))
|
|
|
|
|
}
|
2026-02-05 15:32:19 +01:00
|
|
|
if ks.k != ksg.k {
|
|
|
|
|
panic(fmt.Sprintf("KmerSet k mismatch: expected %d, got %d", ksg.k, ks.k))
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
ksg.sets[index] = ks
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Len returns the number of k-mers in a specific KmerSet
|
|
|
|
|
// Without argument: returns the number of k-mers in the last KmerSet
|
|
|
|
|
// With argument index: returns the number of k-mers in the KmerSet at this index
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) Len(index ...int) uint64 {
|
|
|
|
|
if len(index) == 0 {
|
2026-02-05 16:35:38 +01:00
|
|
|
// Without argument: last KmerSet
|
2026-02-05 14:46:52 +01:00
|
|
|
return ksg.sets[len(ksg.sets)-1].Len()
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// With argument: specific KmerSet
|
2026-02-05 14:46:52 +01:00
|
|
|
idx := index[0]
|
|
|
|
|
if idx < 0 || idx >= len(ksg.sets) {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return ksg.sets[idx].Len()
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// MemoryUsage returns the total memory usage in bytes
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) MemoryUsage() uint64 {
|
|
|
|
|
total := uint64(0)
|
|
|
|
|
for _, ks := range ksg.sets {
|
|
|
|
|
total += ks.MemoryUsage()
|
|
|
|
|
}
|
|
|
|
|
return total
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Clear empties all KmerSet in the group
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) Clear() {
|
|
|
|
|
for _, ks := range ksg.sets {
|
|
|
|
|
ks.Clear()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Copy creates a complete copy of the group (consistent with BioSequence.Copy)
|
2026-02-05 15:32:19 +01:00
|
|
|
func (ksg *KmerSetGroup) Copy() *KmerSetGroup {
|
|
|
|
|
copiedSets := make([]*KmerSet, len(ksg.sets))
|
2026-02-05 14:46:52 +01:00
|
|
|
for i, ks := range ksg.sets {
|
2026-02-05 16:35:38 +01:00
|
|
|
copiedSets[i] = ks.Copy() // Copy each KmerSet with its metadata
|
2026-02-05 15:32:19 +01:00
|
|
|
}
|
2026-02-05 15:02:27 +01:00
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Copy group metadata
|
2026-02-05 15:32:19 +01:00
|
|
|
groupMetadata := make(map[string]interface{}, len(ksg.Metadata))
|
|
|
|
|
for k, v := range ksg.Metadata {
|
|
|
|
|
groupMetadata[k] = v
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
2026-02-05 15:02:27 +01:00
|
|
|
|
2026-02-05 14:46:52 +01:00
|
|
|
return &KmerSetGroup{
|
2026-02-05 15:32:19 +01:00
|
|
|
id: ksg.id,
|
|
|
|
|
k: ksg.k,
|
|
|
|
|
sets: copiedSets,
|
|
|
|
|
Metadata: groupMetadata,
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Id returns the identifier of the KmerSetGroup (consistent with BioSequence.Id)
|
2026-02-05 15:32:19 +01:00
|
|
|
func (ksg *KmerSetGroup) Id() string {
|
|
|
|
|
return ksg.id
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// SetId sets the identifier of the KmerSetGroup (consistent with BioSequence.SetId)
|
2026-02-05 15:32:19 +01:00
|
|
|
func (ksg *KmerSetGroup) SetId(id string) {
|
|
|
|
|
ksg.id = id
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// AddSequence adds all k-mers from a sequence to a specific KmerSet
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) AddSequence(seq *obiseq.BioSequence, index int) {
|
|
|
|
|
if index < 0 || index >= len(ksg.sets) {
|
|
|
|
|
panic(fmt.Sprintf("Index out of bounds: %d (size: %d)", index, len(ksg.sets)))
|
|
|
|
|
}
|
|
|
|
|
ksg.sets[index].AddSequence(seq)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// AddSequences adds all k-mers from multiple sequences to a specific KmerSet
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) AddSequences(sequences *obiseq.BioSequenceSlice, index int) {
|
|
|
|
|
if index < 0 || index >= len(ksg.sets) {
|
|
|
|
|
panic(fmt.Sprintf("Index out of bounds: %d (size: %d)", index, len(ksg.sets)))
|
|
|
|
|
}
|
|
|
|
|
ksg.sets[index].AddSequences(sequences)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Union returns the union of all KmerSet in the group
|
|
|
|
|
// Optimization: starts from the largest set to minimize operations
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) Union() *KmerSet {
|
|
|
|
|
if len(ksg.sets) == 0 {
|
2026-02-05 15:32:19 +01:00
|
|
|
return NewKmerSet(ksg.k)
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:26:00 +01:00
|
|
|
if len(ksg.sets) == 1 {
|
|
|
|
|
return ksg.sets[0].Copy()
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Find the index of the largest set (the one with the most k-mers)
|
2026-02-05 16:26:00 +01:00
|
|
|
maxIdx := 0
|
|
|
|
|
maxCard := ksg.sets[0].Len()
|
2026-02-05 14:46:52 +01:00
|
|
|
for i := 1; i < len(ksg.sets); i++ {
|
2026-02-05 16:26:00 +01:00
|
|
|
card := ksg.sets[i].Len()
|
|
|
|
|
if card > maxCard {
|
|
|
|
|
maxCard = card
|
|
|
|
|
maxIdx = i
|
|
|
|
|
}
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
2026-02-05 16:26:00 +01:00
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Copy the largest set and perform unions in-place
|
2026-02-05 16:26:00 +01:00
|
|
|
result := ksg.sets[maxIdx].bitmap.Clone()
|
|
|
|
|
for i := 0; i < len(ksg.sets); i++ {
|
|
|
|
|
if i != maxIdx {
|
|
|
|
|
result.Or(ksg.sets[i].bitmap)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NewKmerSetFromBitmap(ksg.k, result)
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Intersect returns the intersection of all KmerSet in the group
|
|
|
|
|
// Optimization: starts from the smallest set to minimize operations
|
2026-02-05 14:46:52 +01:00
|
|
|
func (ksg *KmerSetGroup) Intersect() *KmerSet {
|
|
|
|
|
if len(ksg.sets) == 0 {
|
2026-02-05 15:32:19 +01:00
|
|
|
return NewKmerSet(ksg.k)
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:26:00 +01:00
|
|
|
if len(ksg.sets) == 1 {
|
|
|
|
|
return ksg.sets[0].Copy()
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Find the index of the smallest set (the one with the fewest k-mers)
|
2026-02-05 16:26:00 +01:00
|
|
|
minIdx := 0
|
|
|
|
|
minCard := ksg.sets[0].Len()
|
2026-02-05 14:46:52 +01:00
|
|
|
for i := 1; i < len(ksg.sets); i++ {
|
2026-02-05 16:26:00 +01:00
|
|
|
card := ksg.sets[i].Len()
|
|
|
|
|
if card < minCard {
|
|
|
|
|
minCard = card
|
|
|
|
|
minIdx = i
|
|
|
|
|
}
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
2026-02-05 16:26:00 +01:00
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Copy the smallest set and perform intersections in-place
|
2026-02-05 16:26:00 +01:00
|
|
|
result := ksg.sets[minIdx].bitmap.Clone()
|
|
|
|
|
for i := 0; i < len(ksg.sets); i++ {
|
|
|
|
|
if i != minIdx {
|
|
|
|
|
result.And(ksg.sets[i].bitmap)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NewKmerSetFromBitmap(ksg.k, result)
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:35:38 +01:00
|
|
|
// Stats returns statistics for each KmerSet in the group
|
2026-02-05 14:46:52 +01:00
|
|
|
type KmerSetGroupStats struct {
|
|
|
|
|
K int
|
2026-02-05 16:35:38 +01:00
|
|
|
Size int // Number of KmerSet
|
|
|
|
|
TotalBytes uint64 // Total memory used
|
|
|
|
|
Sets []KmerSetStats // Stats of each KmerSet
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type KmerSetStats struct {
|
2026-02-05 16:35:38 +01:00
|
|
|
Index int // Index of the KmerSet in the group
|
|
|
|
|
Len uint64 // Number of k-mers
|
|
|
|
|
SizeBytes uint64 // Size in bytes
|
2026-02-05 14:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ksg *KmerSetGroup) Stats() KmerSetGroupStats {
|
|
|
|
|
stats := KmerSetGroupStats{
|
2026-02-05 15:32:19 +01:00
|
|
|
K: ksg.k,
|
2026-02-05 14:46:52 +01:00
|
|
|
Size: len(ksg.sets),
|
|
|
|
|
Sets: make([]KmerSetStats, len(ksg.sets)),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, ks := range ksg.sets {
|
|
|
|
|
sizeBytes := ks.MemoryUsage()
|
|
|
|
|
stats.Sets[i] = KmerSetStats{
|
|
|
|
|
Index: i,
|
|
|
|
|
Len: ks.Len(),
|
|
|
|
|
SizeBytes: sizeBytes,
|
|
|
|
|
}
|
|
|
|
|
stats.TotalBytes += sizeBytes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stats
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ksgs KmerSetGroupStats) String() string {
|
|
|
|
|
result := fmt.Sprintf(`KmerSetGroup Statistics (k=%d, size=%d):
|
|
|
|
|
Total memory: %.2f MB
|
|
|
|
|
|
|
|
|
|
Set breakdown:
|
|
|
|
|
`, ksgs.K, ksgs.Size, float64(ksgs.TotalBytes)/1024/1024)
|
|
|
|
|
|
|
|
|
|
for _, set := range ksgs.Sets {
|
|
|
|
|
result += fmt.Sprintf(" Set[%d]: %d k-mers (%.2f MB)\n",
|
|
|
|
|
set.Index,
|
|
|
|
|
set.Len,
|
|
|
|
|
float64(set.SizeBytes)/1024/1024)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|