Files
obitools4/pkg/obiseq/merge.go

255 lines
6.0 KiB
Go
Raw Normal View History

package obiseq
import (
"fmt"
"reflect"
"strings"
2022-05-27 11:53:29 +03:00
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
2022-05-27 11:53:29 +03:00
log "github.com/sirupsen/logrus"
)
type StatsOnValues map[string]int
// StatsOnSlotName returns the name of the slot that summarizes statistics of occurrence for a given attribute.
//
// Parameters:
// - key: the attribute key (string)
//
// Return type:
// - string
func StatsOnSlotName(key string) string {
return "merged_" + key
}
// HasStatsOn tests if the sequence has already a slot summarizing statistics of occurrence for a given attribute.
//
// Parameters:
// - key: the attribute key (string)
//
// Return type:
// - bool
func (sequence *BioSequence) HasStatsOn(key string) bool {
if !sequence.HasAnnotation() {
return false
}
mkey := StatsOnSlotName(key)
annotations := sequence.Annotations()
_, ok := annotations[mkey]
return ok
}
// StatsOn returns the slot summarizing statistics of occurrence for a given attribute.
//
// Parameters:
// - key: the attribute key (string) to be summarized
// - na: the value to be used if the attribute is not present
//
// Return type:
// - StatsOnValues
func (sequence *BioSequence) StatsOn(key string, na string) StatsOnValues {
mkey := StatsOnSlotName(key)
annotations := sequence.Annotations()
istat, ok := annotations[mkey]
var stats StatsOnValues
var newstat bool
if ok {
switch istat := istat.(type) {
case StatsOnValues:
stats = istat
newstat = false
2022-08-20 18:01:07 +02:00
case map[string]int:
stats = istat
newstat = false
2022-05-27 11:53:29 +03:00
case map[string]interface{}:
stats = make(StatsOnValues, len(istat))
newstat = false
2022-05-27 11:53:29 +03:00
var err error
for k, v := range istat {
stats[k], err = obiutils.InterfaceToInt(v)
2022-05-27 11:53:29 +03:00
if err != nil {
log.Panicf("In sequence %s : %s stat tag not only containing integer values %s",
sequence.Id(), mkey, istat)
}
}
default:
stats = make(StatsOnValues, 100)
annotations[mkey] = stats
newstat = true
}
} else {
stats = make(StatsOnValues, 100)
annotations[mkey] = stats
newstat = true
}
2022-02-18 10:00:42 +01:00
if newstat && sequence.StatsPlusOne(key, sequence, na) {
delete(sequence.Annotations(), key)
}
return stats
}
// StatsPlusOne adds the count of the sequence toAdd to the count of the key in the stats.
//
// Parameters:
// - key: the attribute key (string) to be summarized
// - toAdd: the BioSequence to add to the stats
// - na: the value to be used if the attribute is not present
// Return type:
// - bool
func (sequence *BioSequence) StatsPlusOne(key string, toAdd *BioSequence, na string) bool {
2022-02-18 10:00:42 +01:00
sval := na
annotations := sequence.Annotations()
stats := sequence.StatsOn(key, na)
2022-02-18 10:00:42 +01:00
retval := false
if toAdd.HasAnnotation() {
value, ok := toAdd.Annotations()[key]
if ok {
switch value := value.(type) {
case string:
sval = value
case int,
uint8, uint16, uint32, uint64,
int8, int16, int32, int64, bool:
sval = fmt.Sprint(value)
default:
log.Fatalf("Trying to make stats on a none string, integer or boolean value (%v)", value)
}
2022-02-18 10:00:42 +01:00
retval = true
}
2022-02-18 10:00:42 +01:00
}
2022-02-18 10:00:42 +01:00
old, ok := stats[sval]
if !ok {
old = 0
}
stats[sval] = old + toAdd.Count()
annotations[StatsOnSlotName(key)] = stats // TODO: check if this is necessary
2022-02-18 10:00:42 +01:00
return retval
}
// Merge merges the given StatsOnValues with the current StatsOnValues.
//
// It takes a parameter `toMerged` of type StatsOnValues, which represents the StatsOnValues to be merged.
// It returns a value of type StatsOnValues, which represents the merged StatsOnValues.
func (stats StatsOnValues) Merge(toMerged StatsOnValues) StatsOnValues {
for k, val := range toMerged {
old, ok := stats[k]
if !ok {
old = 0
}
stats[k] = old + val
}
return stats
}
// Merge merges two sequences into a single sequence.
//
// Parameters:
// - tomerge: the sequence to be merged (BioSequence)
// - na: the value to be used if the attribute is not present (string)
// - inplace: a boolean indicating whether to merge in place or not (bool)
// - statsOn: a variadic string parameter representing the attributes to be summarized (string)
//
// Return type:
// - *BioSequence: the merged sequence (BioSequence)
func (sequence *BioSequence) Merge(tomerge *BioSequence, na string, inplace bool, statsOn ...string) *BioSequence {
if !inplace {
sequence = sequence.Copy()
}
if sequence.HasQualities() {
sequence.SetQualities(nil)
}
annotations := sequence.Annotations()
2022-02-18 10:00:42 +01:00
count := sequence.Count() + tomerge.Count()
2022-02-18 10:00:42 +01:00
for _, key := range statsOn {
if tomerge.HasStatsOn(key) {
smk := sequence.StatsOn(key, na)
mmk := tomerge.StatsOn(key, na)
annotations[StatsOnSlotName(key)] = smk.Merge(mmk)
} else {
sequence.StatsPlusOne(key, tomerge, na)
}
}
if tomerge.HasAnnotation() {
ma := tomerge.Annotations()
for k, va := range annotations {
if !strings.HasPrefix(k, "merged_") {
vm, ok := ma[k]
if ok {
switch vm := vm.(type) {
case int, float64, string, bool:
if va != vm {
delete(annotations, k)
}
default:
if !reflect.DeepEqual(va, vm) {
delete(annotations, k)
}
}
} else {
delete(annotations, k)
}
}
}
} else {
for k := range annotations {
if !strings.HasPrefix(k, "merged_") {
delete(annotations, k)
}
}
}
annotations["count"] = count
return sequence
}
// Merge merges the given sequences into a single sequence.
//
// Parameters:
// - sequences: a slice of BioSequence objects to be merged (BioSequenceSlice)
// - na: the value to be used if the attribute is not present (string)
// - statsOn: a slice of strings representing the attributes to be summarized ([]string)
//
// Return type:
// - *BioSequence: the merged sequence (BioSequence)
func (sequences BioSequenceSlice) Merge(na string, statsOn []string) *BioSequence {
2022-02-18 10:00:42 +01:00
seq := sequences[0]
//sequences[0] = nil
2022-02-18 10:00:42 +01:00
seq.SetQualities(nil)
if len(sequences) == 1 {
seq.Annotations()["count"] = seq.Count()
for _, v := range statsOn {
seq.StatsOn(v, na)
}
} else {
for k, toMerge := range sequences[1:] {
seq.Merge(toMerge, na, true, statsOn...)
toMerge.Recycle()
sequences[1+k] = nil
}
}
sequences.Recycle(false)
return seq
}