Files
obitools4/pkg/obiformats/fastseq_write_fastq.go

153 lines
3.0 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obiformats
import (
"bytes"
"fmt"
"io"
"os"
"time"
2022-02-24 12:14:52 +01:00
log "github.com/sirupsen/logrus"
"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
)
func FormatFastq(seq *obiseq.BioSequence, quality_shift int, formater FormatHeader) string {
2022-01-13 23:27:39 +01:00
l := seq.Len()
2022-01-13 23:27:39 +01:00
q := seq.Qualities()
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),
)
}
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,
file io.Writer,
2023-01-22 22:04:17 +01:00
options ...WithOption) (obiiter.IBioSequence, error) {
opt := MakeOptions(options)
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
nwriters := opt.ParallelWorkers()
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
go func() {
newIter.WaitAndClose()
2022-01-13 23:27:39 +01:00
for len(chunkchan) > 0 {
time.Sleep(time.Millisecond)
}
close(chunkchan)
2022-02-24 12:14:52 +01:00
log.Debugln("End of the fastq file writing")
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()
chunk := FileChunck{
2022-01-13 23:27:39 +01:00
FormatFastqBatch(batch, quality, header_format),
batch.Order(),
}
chunkchan <- chunk
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")
go ff(iterator)
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)
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
}
}
if opt.CloseFile() {
switch file := file.(type) {
case *os.File:
file.Close()
}
}
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) {
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
file, err := os.Create(filename)
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
}
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
}