Adds a JSON output format

Former-commit-id: 26f07460772c0f735bf705d473f892878d3e57f0
This commit is contained in:
2023-11-07 11:56:49 +02:00
parent 61c30f9b6a
commit 185b974d13
5 changed files with 254 additions and 5 deletions

View File

@@ -0,0 +1,47 @@
package obiformats
import (
"bytes"
log "github.com/sirupsen/logrus"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiiter"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
)
type BioSequenceBatchFormater func(batch obiiter.BioSequenceBatch) []byte
type BioSequenceFormater func(sequence *obiseq.BioSequence) string
func BuildFastxSeqFormater(format string, header FormatHeader) BioSequenceFormater {
var f BioSequenceFormater
switch format {
case "fastq":
f = func(sequence *obiseq.BioSequence) string {
return FormatFastq(sequence, header)
}
case "fasta":
f = func(sequence *obiseq.BioSequence) string {
return FormatFasta(sequence, header)
}
default:
log.Fatal("Unknown output format")
}
return f
}
func BuildFastxFormater(format string, header FormatHeader) BioSequenceBatchFormater {
fs := BuildFastxSeqFormater(format, header)
f := func(batch obiiter.BioSequenceBatch) []byte {
var bs bytes.Buffer
for _, seq := range batch.Slice() {
bs.WriteString(fs(seq))
bs.WriteString("\n")
}
return bs.Bytes()
}
return f
}