Refactoring codes for removing buffer size options. An some other changes...

Former-commit-id: 10b57cc1a27446ade3c444217341e9651e89cdce
This commit is contained in:
2023-03-07 11:12:13 +07:00
parent 9811e440b8
commit d88de15cdc
52 changed files with 1172 additions and 421 deletions

View File

@@ -8,6 +8,15 @@ import (
log "github.com/sirupsen/logrus"
)
func (s *BioSequence) HasAttribute(key string) bool {
ok := s.annotations != nil
if ok {
_, ok = s.annotations[key]
}
return ok
}
// A method that returns the value of the key in the annotation map.
func (s *BioSequence) GetAttribute(key string) (interface{}, bool) {
var val interface{}

View File

@@ -278,3 +278,28 @@ func (s *BioSequence) Clear() {
s.sequence = s.sequence[0:0]
}
func (s *BioSequence) Composition() map[byte]int {
a := 0
c := 0
g := 0
t := 0
other := 0
for _, char := range s.sequence {
switch char {
case 'a':
a++
case 'c':
c++
case 'g':
g++
case 't':
t++
default:
other++
}
}
return map[byte]int{'a': a, 'c': c, 'g': g, 't': t, 'o': other}
}

View File

@@ -316,3 +316,4 @@ func RotateClassifier(size int) *BioSequenceClassifier {
c := BioSequenceClassifier{code, value, reset, clone,"RotateClassifier"}
return &c
}

View File

@@ -4,22 +4,21 @@ import (
"context"
"fmt"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obieval"
log "github.com/sirupsen/logrus"
)
func Expression(expression string) func(*BioSequence) (interface{},error) {
func Expression(expression string) func(*BioSequence) (interface{}, error) {
exp, err := obieval.OBILang.NewEvaluable(expression)
exp, err := OBILang.NewEvaluable(expression)
if err != nil {
log.Fatalf("Error in the expression : %s", expression)
}
f := func(sequence *BioSequence) (interface{},error) {
f := func(sequence *BioSequence) (interface{}, error) {
return exp(context.Background(),
map[string]interface{}{
"annotations": sequence.Annotations(),
"sequence": sequence,
"annotations": sequence.Annotations(),
"sequence": sequence,
},
)
}
@@ -30,14 +29,14 @@ func Expression(expression string) func(*BioSequence) (interface{},error) {
func EditIdWorker(expression string) SeqWorker {
e := Expression(expression)
f := func(sequence *BioSequence) *BioSequence {
v,err := e(sequence)
v, err := e(sequence)
if err != nil {
log.Fatalf("Expression '%s' cannot be evaluated on sequence %s",
expression,
sequence.Id())
}
sequence.SetId(fmt.Sprintf("%v",v))
sequence.SetId(fmt.Sprintf("%v", v))
return sequence
}
@@ -47,16 +46,16 @@ func EditIdWorker(expression string) SeqWorker {
func EditAttributeWorker(key string, expression string) SeqWorker {
e := Expression(expression)
f := func(sequence *BioSequence) *BioSequence {
v,err := e(sequence)
v, err := e(sequence)
if err != nil {
log.Fatalf("Expression '%s' cannot be evaluated on sequence %s",
expression,
sequence.Id())
}
sequence.SetAttribute(key,v)
sequence.SetAttribute(key, v)
return sequence
}
return f
}
}

192
pkg/obiseq/language.go Normal file
View File

@@ -0,0 +1,192 @@
package obiseq
import (
"fmt"
"log"
"strings"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
"github.com/PaesslerAG/gval"
)
func maxIntVector(values []int) float64 {
m := values[0]
for _, v := range values {
if v > m {
m = v
}
}
return float64(m)
}
func maxIntMap(values map[string]int) float64 {
var m int
first := true
for _, v := range values {
if first {
first = false
m = v
} else {
if v > m {
m = v
}
}
}
return float64(m)
}
func minIntVector(values []int) float64 {
m := values[0]
for _, v := range values {
if v < m {
m = v
}
}
return float64(m)
}
func minIntMap(values map[string]int) float64 {
var m int
first := true
for _, v := range values {
if first {
first = false
m = v
} else {
if v < m {
m = v
}
}
}
return float64(m)
}
func maxFloatVector(values []float64) float64 {
m := values[0]
for _, v := range values {
if v > m {
m = v
}
}
return m
}
func maxFloatMap(values map[string]float64) float64 {
var m float64
first := true
for _, v := range values {
if first {
first = false
m = v
} else {
if v > m {
m = v
}
}
}
return m
}
func minFloatVector(values []float64) float64 {
m := values[0]
for _, v := range values {
if v < m {
m = v
}
}
return m
}
func minFloatMap(values map[string]float64) float64 {
var m float64
first := true
for _, v := range values {
if first {
first = false
m = v
} else {
if v < m {
m = v
}
}
}
return m
}
// func maxNumeric(args ...interface{}) (interface{}, error) {
// var m float64
// first := true
// for _, v := range args {
// switch {
// case
// }
// }
// }
var OBILang = gval.NewLanguage(
gval.Full(),
gval.Function("len", func(args ...interface{}) (interface{}, error) {
length := goutils.Len(args[0])
return (float64)(length), nil
}),
gval.Function("ismap", func(args ...interface{}) (interface{}, error) {
ismap := goutils.IsAMap(args[0])
return ismap, nil
}),
gval.Function("printf", func(args ...interface{}) (interface{}, error) {
text := fmt.Sprintf(args[0].(string), args[1:]...)
return text, nil
}),
gval.Function("subspc", func(args ...interface{}) (interface{}, error) {
text := strings.ReplaceAll(args[0].(string), " ", "_")
return text, nil
}),
gval.Function("int", func(args ...interface{}) (interface{}, error) {
val, err := goutils.InterfaceToInt(args[0])
if err != nil {
log.Fatalf("%v cannot be converted to an integer value", args[0])
}
return val, nil
}),
gval.Function("numeric", func(args ...interface{}) (interface{}, error) {
val, err := goutils.InterfaceToFloat64(args[0])
if err != nil {
log.Fatalf("%v cannot be converted to a numeric value", args[0])
}
return val, nil
}),
gval.Function("bool", func(args ...interface{}) (interface{}, error) {
val, err := goutils.InterfaceToBool(args[0])
if err != nil {
log.Fatalf("%v cannot be converted to a boolan value", args[0])
}
return val, nil
}),
gval.Function("ifelse", func(args ...interface{}) (interface{}, error) {
if args[0].(bool) {
return args[1], nil
} else {
return args[2], nil
}
}),
gval.Function("gcskew", func(args ...interface{}) (interface{}, error) {
composition := (args[0].(*BioSequence)).Composition()
return float64(composition['g']-composition['c']) / float64(composition['g']+composition['c']), nil
}),
gval.Function("composition", func(args ...interface{}) (interface{}, error) {
return (args[0].(*BioSequence)).Composition(), nil
}))

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"regexp"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obieval"
log "github.com/sirupsen/logrus"
)
@@ -256,7 +255,7 @@ func IsIdIn(ids ...string) SequencePredicate {
func ExpressionPredicat(expression string) SequencePredicate {
exp, err := obieval.OBILang.NewEvaluable(expression)
exp, err := OBILang.NewEvaluable(expression)
if err != nil {
log.Fatalf("Error in the expression : %s", expression)
}