mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
First commit
This commit is contained in:
66
pkg/obiformats/fastseq_json_header.go
Normal file
66
pkg/obiformats/fastseq_json_header.go
Normal file
@ -0,0 +1,66 @@
|
||||
package obiformats
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.metabarcoding.org/lecasofts/go/oa2/pkg/obiseq"
|
||||
"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++
|
||||
|
||||
json.Unmarshal([]byte(header)[start:stop], annotations)
|
||||
return strings.TrimSpace(header[stop:])
|
||||
}
|
||||
|
||||
func ParseFastSeqJsonHeader(sequence obiseq.BioSequence) {
|
||||
sequence.SetDefinition(_parse_json_header_(sequence.Definition(),
|
||||
sequence.Annotations()))
|
||||
}
|
||||
|
||||
func FormatFastSeqJsonHeader(sequence obiseq.BioSequence) string {
|
||||
annotations := sequence.Annotations()
|
||||
|
||||
if annotations != nil {
|
||||
text, err := json.Marshal(sequence.Annotations())
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return string(text)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
Reference in New Issue
Block a user