Merge branch 'master' into taxonomy

This commit is contained in:
Eric Coissac
2024-12-20 20:06:57 +01:00
20 changed files with 286 additions and 54 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"reflect"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiiter"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obioptions"
@ -119,11 +120,17 @@ func LuaWorker(proto *lua.FunctionProto) obiseq.SeqWorker {
case *obiseq.BioSequenceSlice:
return *val, err
default:
return nil, fmt.Errorf("worker function doesn't return the correct type %T", val)
r := reflect.TypeOf(val)
return nil, fmt.Errorf("worker function doesn't return the correct type %s", r)
}
}
return nil, fmt.Errorf("worker function doesn't return the correct type")
// If worker retuns nothing then it is considered as nil biosequence
if _, ok = lreponse.(*lua.LNilType); ok {
return nil, nil
}
return nil, fmt.Errorf("worker function doesn't return the correct type %T", lreponse)
}
return f

View File

@ -46,6 +46,8 @@ func pushInterfaceToLua(L *lua.LState, val interface{}) {
pushSliceNumericToLua(L, v)
case []bool:
pushSliceBoolToLua(L, v)
case []interface{}:
pushSliceInterfaceToLua(L, v)
case nil:
L.Push(lua.LNil)
case *sync.Mutex:
@ -78,6 +80,29 @@ func pushMapStringInterfaceToLua(L *lua.LState, m map[string]interface{}) {
L.Push(luaTable)
}
func pushSliceInterfaceToLua(L *lua.LState, s []interface{}) {
// Create a new Lua table
luaTable := L.NewTable()
// Iterate over the Go map and set the key-value pairs in the Lua table
for _, value := range s {
switch v := value.(type) {
case int:
luaTable.Append(lua.LNumber(v))
case float64:
luaTable.Append(lua.LNumber(v))
case bool:
luaTable.Append(lua.LBool(v))
case string:
luaTable.Append(lua.LString(v))
default:
log.Fatalf("Doesn't deal with slice containing value %v of type %T", v, v)
}
}
// Push the Lua table onto the stack
L.Push(luaTable)
}
// pushMapStringIntToLua creates a new Lua table and iterates over the Go map to set key-value pairs in the Lua table. It then pushes the Lua table onto the stack.
//
// L *lua.LState - the Lua state

View File

@ -47,18 +47,21 @@ func newObiSeq(luaState *lua.LState) int {
}
var bioSequenceMethods = map[string]lua.LGFunction{
"id": bioSequenceGetSetId,
"sequence": bioSequenceGetSetSequence,
"qualities": bioSequenceGetSetQualities,
"definition": bioSequenceGetSetDefinition,
"count": bioSequenceGetSetCount,
"taxid": bioSequenceGetSetTaxid,
"attribute": bioSequenceGetSetAttribute,
"len": bioSequenceGetLength,
"has_sequence": bioSequenceHasSequence,
"has_qualities": bioSequenceHasQualities,
"source": bioSequenceGetSource,
"md5": bioSequenceGetMD5,
"id": bioSequenceGetSetId,
"sequence": bioSequenceGetSetSequence,
"qualities": bioSequenceGetSetQualities,
"definition": bioSequenceGetSetDefinition,
"count": bioSequenceGetSetCount,
"taxid": bioSequenceGetSetTaxid,
"attribute": bioSequenceGetSetAttribute,
"len": bioSequenceGetLength,
"has_sequence": bioSequenceHasSequence,
"has_qualities": bioSequenceHasQualities,
"source": bioSequenceGetSource,
"md5": bioSequenceGetMD5,
"md5_string": bioSequenceGetMD5String,
"subsequence": bioSequenceGetSubsequence,
"reverse_complement": bioSequenceGetRevcomp,
}
// checkBioSequence checks if the first argument in the Lua stack is a *obiseq.BioSequence.
@ -224,3 +227,30 @@ func bioSequenceGetMD5(luaState *lua.LState) int {
luaState.Push(rt)
return 1
}
func bioSequenceGetMD5String(luaState *lua.LState) int {
s := checkBioSequence(luaState)
md5 := s.MD5String()
luaState.Push(lua.LString(md5))
return 1
}
func bioSequenceGetSubsequence(luaState *lua.LState) int {
s := checkBioSequence(luaState)
start := luaState.CheckInt(2)
end := luaState.CheckInt(3)
subseq, err := s.Subsequence(start, end, false)
if err != nil {
luaState.RaiseError("%s : Error on subseq: %v", s.Id(), err)
return 0
}
luaState.Push(obiseq2Lua(luaState, subseq))
return 1
}
func bioSequenceGetRevcomp(luaState *lua.LState) int {
s := checkBioSequence(luaState)
revcomp := s.ReverseComplement(false)
luaState.Push(obiseq2Lua(luaState, revcomp))
return 1
}