Files
obitools4/pkg/obiformats/fastseq_write_fastq.go

229 lines
4.7 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obiformats
import (
"bytes"
"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"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiiter"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiseq"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
2022-01-13 23:27:39 +01:00
)
func _formatFastq(buff *bytes.Buffer, seq *obiseq.BioSequence, formater FormatHeader) {
2022-01-13 23:27:39 +01:00
info := ""
if formater != nil {
info = formater(seq)
}
buff.WriteByte('@')
buff.WriteString(seq.Id())
buff.WriteByte(' ')
buff.WriteString(info)
buff.WriteByte('\n')
buff.Write(seq.Sequence())
buff.WriteString("\n+\n")
q := seq.QualitiesString()
buff.WriteString(q)
buff.WriteByte('\n')
}
// The function FormatFastq takes a BioSequence object, a quality shift value, and a header formatter
// function as input, and returns a formatted string in FASTQ format.
func FormatFastq(seq *obiseq.BioSequence, formater FormatHeader) string {
var buff bytes.Buffer
_formatFastq(&buff, seq, formater)
return buff.String()
2022-01-13 23:27:39 +01:00
}
func FormatFastqBatch(batch obiiter.BioSequenceBatch,
formater FormatHeader, skipEmpty bool) []byte {
2022-01-13 23:27:39 +01:00
var bs bytes.Buffer
lt := 0
for _, seq := range batch.Slice() {
lt += seq.Len()
}
// Iterate over each sequence in the batch
first := true
for _, seq := range batch.Slice() {
if seq.Len() > 0 {
_formatFastq(&bs, seq, formater)
if first {
growing := lt + (len(bs.Bytes())-2*seq.Len())*batch.Len()*5/4
log.Debugf("Grow Fastq block of %d", growing)
bs.Grow(growing)
first = false
}
} else {
if skipEmpty {
log.Warnf("Sequence %s is empty and skiped in output", seq.Id())
} else {
log.Fatalf("Sequence %s is empty", seq.Id())
}
}
2022-01-13 23:27:39 +01:00
}
chunk := bs.Bytes()
return chunk
2022-01-13 23:27:39 +01:00
}
2024-08-01 16:43:23 +02:00
type FileChunk struct {
2022-01-13 23:27:39 +01:00
text []byte
order int
}
2023-01-22 22:04:17 +01:00
func WriteFastq(iterator obiiter.IBioSequence,
file io.WriteCloser,
2023-01-22 22:04:17 +01:00
options ...WithOption) (obiiter.IBioSequence, error) {
opt := MakeOptions(options)
iterator = iterator.Rebatch(opt.BatchSize())
file, _ = obiutils.CompressStream(file, opt.CompressedFile(), opt.CloseFile())
newIter := obiiter.MakeIBioSequence()
2022-01-13 23:27:39 +01:00
nwriters := opt.ParallelWorkers()
2022-01-13 23:27:39 +01:00
obiiter.RegisterAPipe()
2024-08-01 16:43:23 +02:00
chunkchan := make(chan FileChunk)
2022-01-13 23:27:39 +01:00
header_format := opt.FormatFastSeqHeader()
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() {
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()
2024-08-01 16:43:23 +02:00
chunk := FileChunk{
FormatFastqBatch(batch, header_format, opt.SkipEmptySequence()),
2022-01-13 23:27:39 +01:00
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
2024-08-01 16:43:23 +02:00
received := make(map[int]FileChunk, 100)
2022-01-13 23:27:39 +01:00
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 {
if chunk.text[0] != '@' {
log.Panicln("WriteFastq: FASTQ format error")
}
2022-01-13 23:27:39 +01:00
file.Write(chunk.text)
next_to_send++
chunk, ok := received[next_to_send]
for ok {
if chunk.text[0] != '@' {
log.Panicln("WriteFastq: FASTQ format error")
}
2022-01-13 23:27:39 +01:00
file.Write(chunk.text)
delete(received, next_to_send)
next_to_send++
chunk, ok = received[next_to_send]
}
} else {
if _, ok := received[chunk.order]; ok {
log.Panicln("WriteFastq: Two chunks with the same number")
}
2022-01-13 23:27:39 +01:00
received[chunk.order] = chunk
}
}
file.Close()
2023-02-08 13:56:50 +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) {
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
opt := MakeOptions(options)
flags := os.O_WRONLY | os.O_CREATE
if opt.AppendFile() {
flags |= os.O_APPEND
} else {
flags |= os.O_TRUNC
}
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
}
options = append(options, OptionCloseFile())
iterator, err = WriteFastq(iterator, file, options...)
if opt.HaveToSavePaired() {
var revfile *os.File
revfile, err = os.OpenFile(opt.PairedFileName(), flags, 0660)
if err != nil {
log.Fatalf("open file error: %v", err)
return obiiter.NilIBioSequence, err
}
iterator, err = WriteFastq(iterator.PairedWith(), revfile, options...)
}
return iterator, err
2022-01-13 23:27:39 +01:00
}