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"
|
|
|
|
|
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
|
|
|
|
|
|
|
l := seq.Length()
|
|
|
|
q := seq.Qualities()
|
|
|
|
ascii := make([]byte, seq.Length())
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-16 17:13:03 +01:00
|
|
|
func WriteFastq(iterator obiiter.IBioSequenceBatch,
|
2022-02-24 07:08:40 +01:00
|
|
|
file io.Writer,
|
|
|
|
options ...WithOption) (obiiter.IBioSequenceBatch, error) {
|
2022-01-14 23:11:36 +01:00
|
|
|
opt := MakeOptions(options)
|
|
|
|
|
2022-01-13 23:27:39 +01:00
|
|
|
buffsize := iterator.BufferSize()
|
2022-02-24 07:08:40 +01:00
|
|
|
newIter := obiiter.MakeIBioSequenceBatch(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
|
|
|
|
|
|
|
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() {
|
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)
|
2022-02-24 12:14:52 +01:00
|
|
|
log.Debugln("End of the fastq file writing")
|
2022-01-13 23:27:39 +01:00
|
|
|
}()
|
|
|
|
|
2022-02-24 07:08:40 +01:00
|
|
|
ff := func(iterator obiiter.IBioSequenceBatch) {
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-16 17:13:03 +01:00
|
|
|
func WriteFastqToStdout(iterator obiiter.IBioSequenceBatch,
|
2022-02-24 07:08:40 +01:00
|
|
|
options ...WithOption) (obiiter.IBioSequenceBatch, 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
|
|
|
}
|
|
|
|
|
2022-11-16 17:13:03 +01:00
|
|
|
func WriteFastqToFile(iterator obiiter.IBioSequenceBatch,
|
2022-01-13 23:27:39 +01:00
|
|
|
filename string,
|
2022-02-24 07:08:40 +01:00
|
|
|
options ...WithOption) (obiiter.IBioSequenceBatch, error) {
|
2022-01-13 23:27:39 +01:00
|
|
|
|
|
|
|
file, err := os.Create(filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("open file error: %v", err)
|
2022-02-24 07:08:40 +01:00
|
|
|
return obiiter.NilIBioSequenceBatch, 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
|
|
|
}
|