2023-02-16 13:32:27 +01:00
|
|
|
package obiseq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-03-07 11:12:13 +07:00
|
|
|
func Expression(expression string) func(*BioSequence) (interface{}, error) {
|
2023-02-16 13:32:27 +01:00
|
|
|
|
2023-03-07 11:12:13 +07:00
|
|
|
exp, err := OBILang.NewEvaluable(expression)
|
2023-02-16 13:32:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error in the expression : %s", expression)
|
|
|
|
}
|
|
|
|
|
2023-03-07 11:12:13 +07:00
|
|
|
f := func(sequence *BioSequence) (interface{}, error) {
|
2023-02-16 13:32:27 +01:00
|
|
|
return exp(context.Background(),
|
|
|
|
map[string]interface{}{
|
2023-03-07 11:12:13 +07:00
|
|
|
"annotations": sequence.Annotations(),
|
|
|
|
"sequence": sequence,
|
2023-02-16 13:32:27 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func EditIdWorker(expression string) SeqWorker {
|
|
|
|
e := Expression(expression)
|
2024-03-02 16:03:46 -04:00
|
|
|
f := func(sequence *BioSequence) (BioSequenceSlice, error) {
|
2023-03-07 11:12:13 +07:00
|
|
|
v, err := e(sequence)
|
2024-03-02 16:03:46 -04:00
|
|
|
if err == nil {
|
|
|
|
sequence.SetId(fmt.Sprintf("%v", v))
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Expression '%s' cannot be evaluated on sequence %s : %v",
|
2023-02-16 13:32:27 +01:00
|
|
|
expression,
|
2024-03-02 16:03:46 -04:00
|
|
|
sequence.Id(),
|
|
|
|
err)
|
2023-02-16 13:32:27 +01:00
|
|
|
}
|
2024-03-02 16:03:46 -04:00
|
|
|
|
|
|
|
return BioSequenceSlice{sequence}, err
|
2023-02-16 13:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func EditAttributeWorker(key string, expression string) SeqWorker {
|
|
|
|
e := Expression(expression)
|
2024-03-02 16:03:46 -04:00
|
|
|
f := func(sequence *BioSequence) (BioSequenceSlice, error) {
|
2023-03-07 11:12:13 +07:00
|
|
|
v, err := e(sequence)
|
2024-03-02 16:03:46 -04:00
|
|
|
if err == nil {
|
|
|
|
sequence.SetAttribute(key, v)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Expression '%s' cannot be evaluated on sequence %s : %v",
|
2023-02-16 13:32:27 +01:00
|
|
|
expression,
|
2024-03-02 16:03:46 -04:00
|
|
|
sequence.Id(),
|
|
|
|
err)
|
2023-02-16 13:32:27 +01:00
|
|
|
}
|
2024-03-02 16:03:46 -04:00
|
|
|
|
|
|
|
return BioSequenceSlice{sequence}, err
|
2023-02-16 13:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
2023-03-07 11:12:13 +07:00
|
|
|
}
|