2022-02-14 00:01:01 +01:00
|
|
|
package obiformats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-02-17 22:52:53 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-02-17 12:52:19 +01:00
|
|
|
"strings"
|
2022-02-14 00:01:01 +01:00
|
|
|
"sync"
|
|
|
|
|
2024-06-23 00:36:08 +02:00
|
|
|
"github.com/goccy/go-json"
|
|
|
|
|
2023-01-22 22:04:17 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-11-29 12:14:37 +01:00
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiiter"
|
2022-02-14 00:01:01 +01:00
|
|
|
)
|
|
|
|
|
2023-01-22 22:04:17 +01:00
|
|
|
type SequenceBatchWriterToFile func(iterator obiiter.IBioSequence,
|
2022-02-14 00:01:01 +01:00
|
|
|
filename string,
|
2023-01-22 22:04:17 +01:00
|
|
|
options ...WithOption) (obiiter.IBioSequence, error)
|
2022-02-14 00:01:01 +01:00
|
|
|
|
|
|
|
func WriterDispatcher(prototypename string,
|
2022-02-24 07:08:40 +01:00
|
|
|
dispatcher obiiter.IDistribute,
|
2022-02-14 00:01:01 +01:00
|
|
|
formater SequenceBatchWriterToFile,
|
|
|
|
options ...WithOption) {
|
|
|
|
|
|
|
|
jobDone := sync.WaitGroup{}
|
|
|
|
jobDone.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
2023-02-17 12:52:19 +01:00
|
|
|
opt := MakeOptions(options)
|
2022-02-14 00:01:01 +01:00
|
|
|
for newflux := range dispatcher.News() {
|
2022-02-15 00:47:02 +01:00
|
|
|
jobDone.Add(1)
|
2022-02-18 22:53:09 +01:00
|
|
|
go func(newflux int) {
|
2022-02-15 00:47:02 +01:00
|
|
|
data, err := dispatcher.Outputs(newflux)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot retreive the new chanel : %v", err)
|
|
|
|
}
|
|
|
|
|
2023-02-17 22:52:53 +01:00
|
|
|
key := dispatcher.Classifier().Value(newflux)
|
|
|
|
directory := ""
|
|
|
|
if dispatcher.Classifier().Type == "DualAnnotationClassifier" {
|
|
|
|
var keys [2]string
|
|
|
|
err := json.Unmarshal([]byte(key), &keys)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error in parsing dispatch key %s", key)
|
|
|
|
}
|
|
|
|
key = keys[0]
|
|
|
|
directory = keys[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf(prototypename, key)
|
|
|
|
if opt.CompressedFile() && !strings.HasSuffix(name, ".gz") {
|
2023-02-17 12:52:19 +01:00
|
|
|
name = name + ".gz"
|
|
|
|
}
|
2023-02-17 22:52:53 +01:00
|
|
|
|
|
|
|
if directory != "" {
|
|
|
|
info, err := os.Stat(directory)
|
|
|
|
switch {
|
|
|
|
case !os.IsNotExist(err) && !info.IsDir():
|
2023-08-14 15:21:30 +02:00
|
|
|
log.Fatalf("Cannot Create the directory %s", directory)
|
2023-02-17 22:52:53 +01:00
|
|
|
case os.IsNotExist(err):
|
|
|
|
os.Mkdir(directory, 0755)
|
|
|
|
}
|
|
|
|
|
|
|
|
name = filepath.Join(directory, name)
|
|
|
|
}
|
|
|
|
|
2022-02-14 09:12:57 +01:00
|
|
|
out, err := formater(data,
|
2023-02-17 12:52:19 +01:00
|
|
|
name,
|
2022-02-14 09:12:57 +01:00
|
|
|
options...)
|
2022-02-15 00:47:02 +01:00
|
|
|
|
2022-02-14 09:12:57 +01:00
|
|
|
if err != nil {
|
2022-02-21 19:00:23 +01:00
|
|
|
log.Fatalf("cannot open the output file for key %s",
|
|
|
|
dispatcher.Classifier().Value(newflux))
|
2022-02-14 09:12:57 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 00:01:01 +01:00
|
|
|
out.Recycle()
|
|
|
|
jobDone.Done()
|
2022-02-14 09:12:57 +01:00
|
|
|
}(newflux)
|
2022-02-14 00:01:01 +01:00
|
|
|
}
|
2022-02-15 00:47:02 +01:00
|
|
|
jobDone.Done()
|
2022-02-14 00:01:01 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
jobDone.Wait()
|
|
|
|
}
|