2022-01-13 23:27:39 +01:00
|
|
|
package obiformats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2023-02-08 13:56:50 +01:00
|
|
|
"sync"
|
2022-01-13 23:27:39 +01:00
|
|
|
"time"
|
|
|
|
|
2022-02-24 12:14:52 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-02-17 12:52:19 +01:00
|
|
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
|
2022-02-24 07:08:40 +01:00
|
|
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiiter"
|
2022-01-13 23:43:01 +01:00
|
|
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
2022-01-13 23:27:39 +01:00
|
|
|
)
|
|
|
|
|
2022-02-21 19:00:23 +01:00
|
|
|
func FormatFastq(seq *obiseq.BioSequence, quality_shift int, formater FormatHeader) string {
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2022-11-17 11:09:58 +01:00
|
|
|
l := seq.Len()
|
2022-01-13 23:27:39 +01:00
|
|
|
q := seq.Qualities()
|
2022-11-17 11:09:58 +01:00
|
|
|
ascii := make([]byte, seq.Len())
|
2022-01-13 23:27:39 +01:00
|
|
|
|
|
|
|
for j := 0; j < l; j++ {
|
|
|
|
ascii[j] = uint8(q[j]) + uint8(quality_shift)
|
|
|
|
}
|
|
|
|
|
|
|
|
info := ""
|
|
|
|
if formater != nil {
|
|
|
|
info = formater(seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("@%s %s %s\n%s\n+\n%s",
|
|
|
|
seq.Id(), info,
|
|
|
|
seq.Definition(),
|
|
|
|
string(seq.Sequence()),
|
|
|
|
string(ascii),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-24 07:08:40 +01:00
|
|
|
func FormatFastqBatch(batch obiiter.BioSequenceBatch, quality_shift int,
|
2022-01-13 23:27:39 +01:00
|
|
|
formater FormatHeader) []byte {
|
|
|
|
var bs bytes.Buffer
|
|
|
|
for _, seq := range batch.Slice() {
|
|
|
|
bs.WriteString(FormatFastq(seq, quality_shift, formater))
|
|
|
|
bs.WriteString("\n")
|
|
|
|
}
|
|
|
|
return bs.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileChunck struct {
|
|
|
|
text []byte
|
|
|
|
order int
|
|
|
|
}
|
|
|
|
|
2023-01-22 22:04:17 +01:00
|
|
|
func WriteFastq(iterator obiiter.IBioSequence,
|
2023-02-18 12:06:52 +01:00
|
|
|
file io.WriteCloser,
|
2023-01-22 22:04:17 +01:00
|
|
|
options ...WithOption) (obiiter.IBioSequence, error) {
|
2022-01-14 23:11:36 +01:00
|
|
|
opt := MakeOptions(options)
|
|
|
|
|
2023-02-18 12:06:52 +01:00
|
|
|
file,_ = goutils.CompressStream(file,opt.CompressedFile(),opt.CloseFile())
|
|
|
|
|
2022-01-13 23:27:39 +01:00
|
|
|
buffsize := iterator.BufferSize()
|
2023-01-22 22:04:17 +01:00
|
|
|
newIter := obiiter.MakeIBioSequence(buffsize)
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2022-01-14 23:11:36 +01:00
|
|
|
nwriters := opt.ParallelWorkers()
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2023-02-08 13:14:26 +01:00
|
|
|
obiiter.RegisterAPipe()
|
2022-01-13 23:27:39 +01:00
|
|
|
chunkchan := make(chan FileChunck)
|
|
|
|
|
|
|
|
header_format := opt.FormatFastSeqHeader()
|
|
|
|
quality := opt.QualityShift()
|
|
|
|
|
2022-01-14 17:32:12 +01:00
|
|
|
newIter.Add(nwriters)
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2023-02-08 13:56:50 +01:00
|
|
|
var waitWriter sync.WaitGroup
|
|
|
|
|
2022-01-13 23:27:39 +01:00
|
|
|
go func() {
|
2022-02-21 19:00:23 +01:00
|
|
|
newIter.WaitAndClose()
|
2022-01-13 23:27:39 +01:00
|
|
|
for len(chunkchan) > 0 {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
close(chunkchan)
|
2023-02-08 13:56:50 +01:00
|
|
|
waitWriter.Wait()
|
2022-01-13 23:27:39 +01:00
|
|
|
}()
|
|
|
|
|
2023-01-22 22:04:17 +01:00
|
|
|
ff := func(iterator obiiter.IBioSequence) {
|
2022-01-13 23:27:39 +01:00
|
|
|
for iterator.Next() {
|
|
|
|
batch := iterator.Get()
|
2022-01-14 23:11:36 +01:00
|
|
|
chunk := FileChunck{
|
2022-01-13 23:27:39 +01:00
|
|
|
FormatFastqBatch(batch, quality, header_format),
|
|
|
|
batch.Order(),
|
|
|
|
}
|
2022-01-14 23:11:36 +01:00
|
|
|
chunkchan <- chunk
|
2022-02-21 19:00:23 +01:00
|
|
|
newIter.Push(batch)
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
2022-01-14 17:32:12 +01:00
|
|
|
newIter.Done()
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 12:14:52 +01:00
|
|
|
log.Debugln("Start of the fastq file writing")
|
2022-02-15 00:47:02 +01:00
|
|
|
go ff(iterator)
|
2022-01-14 23:11:36 +01:00
|
|
|
for i := 0; i < nwriters-1; i++ {
|
2022-01-13 23:27:39 +01:00
|
|
|
go ff(iterator.Split())
|
|
|
|
}
|
|
|
|
|
|
|
|
next_to_send := 0
|
|
|
|
received := make(map[int]FileChunck, 100)
|
|
|
|
|
2023-02-08 13:56:50 +01:00
|
|
|
waitWriter.Add(1)
|
2022-01-13 23:27:39 +01:00
|
|
|
go func() {
|
|
|
|
for chunk := range chunkchan {
|
|
|
|
if chunk.order == next_to_send {
|
|
|
|
file.Write(chunk.text)
|
|
|
|
next_to_send++
|
|
|
|
chunk, ok := received[next_to_send]
|
|
|
|
for ok {
|
|
|
|
file.Write(chunk.text)
|
|
|
|
delete(received, next_to_send)
|
|
|
|
next_to_send++
|
|
|
|
chunk, ok = received[next_to_send]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
received[chunk.order] = chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-02-15 00:47:02 +01:00
|
|
|
|
2023-02-18 12:06:52 +01:00
|
|
|
file.Close()
|
2023-02-08 13:56:50 +01:00
|
|
|
|
2023-02-17 22:52:53 +01:00
|
|
|
log.Debugln("End of the fastq file writing")
|
|
|
|
obiiter.UnregisterPipe()
|
2023-02-08 13:56:50 +01:00
|
|
|
waitWriter.Done()
|
2022-01-13 23:27:39 +01:00
|
|
|
}()
|
|
|
|
|
2022-01-14 17:32:12 +01:00
|
|
|
return newIter, nil
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 22:04:17 +01:00
|
|
|
func WriteFastqToStdout(iterator obiiter.IBioSequence,
|
|
|
|
options ...WithOption) (obiiter.IBioSequence, error) {
|
2022-02-15 00:47:02 +01:00
|
|
|
options = append(options, OptionDontCloseFile())
|
2022-11-16 17:13:03 +01:00
|
|
|
return WriteFastq(iterator, os.Stdout, options...)
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 22:04:17 +01:00
|
|
|
func WriteFastqToFile(iterator obiiter.IBioSequence,
|
2022-01-13 23:27:39 +01:00
|
|
|
filename string,
|
2023-01-22 22:04:17 +01:00
|
|
|
options ...WithOption) (obiiter.IBioSequence, error) {
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2023-02-16 16:13:13 +01:00
|
|
|
opt := MakeOptions(options)
|
2023-02-18 12:06:52 +01:00
|
|
|
flags := os.O_WRONLY | os.O_CREATE
|
2023-02-16 16:13:13 +01:00
|
|
|
|
2023-02-18 12:06:52 +01:00
|
|
|
if opt.AppendFile() {
|
|
|
|
flags |= os.O_APPEND
|
|
|
|
}
|
|
|
|
file, err := os.OpenFile(filename, flags, 0660)
|
|
|
|
|
2022-01-13 23:27:39 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("open file error: %v", err)
|
2023-01-22 22:04:17 +01:00
|
|
|
return obiiter.NilIBioSequence, err
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 00:47:02 +01:00
|
|
|
options = append(options, OptionCloseFile())
|
|
|
|
|
2022-11-16 17:13:03 +01:00
|
|
|
return WriteFastq(iterator, file, options...)
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|