Files
obitools4/pkg/obiformats/fastseq_json_header.go

84 lines
1.4 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obiformats
import (
2022-05-27 11:53:29 +03:00
"math"
2022-01-13 23:27:39 +01:00
"strings"
2022-05-27 11:53:29 +03:00
log "github.com/sirupsen/logrus"
2022-01-13 23:43:01 +01:00
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
2022-01-13 23:27:39 +01:00
"github.com/goccy/go-json"
)
func _parse_json_header_(header string, annotations obiseq.Annotation) string {
start := -1
stop := -1
level := 0
lh := len(header)
for i := 0; (i < lh) && (stop < 0); i++ {
// fmt.Printf("[%d,%d-%d] : %d (%c) (%d,%c)\n", i, start, stop, header[i], header[i], '{', '{')
if level == 0 && header[i] == '{' {
start = i
}
if header[i] == '{' {
level++
}
if header[i] == '}' {
level--
}
if start >= 0 && level == 0 {
stop = i
}
}
if start < 0 || stop < 0 {
return header
}
stop++
2022-02-09 22:00:38 +01:00
err := json.Unmarshal([]byte(header)[start:stop], &annotations)
2022-05-27 11:53:29 +03:00
for k, v := range annotations {
switch vt := v.(type) {
case float64 :
if vt == math.Floor(vt) {
annotations[k] = int(vt)
}
}
}
2022-02-09 22:00:38 +01:00
if err != nil {
log.Fatalf("annotation parsing error on %s : %v\n", header, err)
}
2022-01-13 23:27:39 +01:00
return strings.TrimSpace(header[stop:])
}
func ParseFastSeqJsonHeader(sequence *obiseq.BioSequence) {
2022-01-13 23:27:39 +01:00
sequence.SetDefinition(_parse_json_header_(sequence.Definition(),
sequence.Annotations()))
}
func FormatFastSeqJsonHeader(sequence *obiseq.BioSequence) string {
2022-01-13 23:27:39 +01:00
annotations := sequence.Annotations()
if annotations != nil {
text, err := json.Marshal(sequence.Annotations())
if err != nil {
panic(err)
}
return string(text)
}
return ""
}