Files
obitools4/pkg/obiformats/fastseq_write_fasta.go

191 lines
3.8 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obiformats
import (
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
"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 min(x, y int) int {
if x < y {
return x
}
return y
}
func FormatFasta(seq *obiseq.BioSequence, formater FormatHeader) string {
2022-01-13 23:27:39 +01:00
var fragments strings.Builder
if seq == nil {
log.Panicln("try to format a nil BioSequence")
}
2022-01-13 23:27:39 +01:00
s := seq.Sequence()
l := len(s)
fragments.Grow(l + int(l/60) + 10)
for i := 0; i < l; i += 60 {
to := min(i+60, l)
fmt.Fprintf(&fragments, "%s\n", string(s[i:to]))
}
folded := fragments.String()
folded = folded[:fragments.Len()-1]
info := formater(seq)
return fmt.Sprintf(">%s %s %s\n%s",
seq.Id(), info,
seq.Definition(),
folded)
}
func FormatFastaBatch(batch obiiter.BioSequenceBatch, formater FormatHeader) []byte {
2022-01-13 23:27:39 +01:00
var bs bytes.Buffer
for _, seq := range batch.Slice() {
bs.WriteString(FormatFasta(seq, formater))
bs.WriteString("\n")
}
return bs.Bytes()
}
func WriteFasta(iterator obiiter.IBioSequence, file io.Writer, options ...WithOption) error {
2022-01-13 23:27:39 +01:00
opt := MakeOptions(options)
header_format := opt.FormatFastSeqHeader()
for iterator.Next() {
seq := iterator.Get()
fmt.Fprintln(file, FormatFasta(seq, header_format))
}
if opt.CloseFile() {
switch file := file.(type) {
case *os.File:
file.Close()
}
}
2022-01-13 23:27:39 +01:00
return nil
}
func WriteFastaToFile(iterator obiiter.IBioSequence,
2022-01-13 23:27:39 +01:00
filename string,
options ...WithOption) error {
file, err := os.Create(filename)
if err != nil {
log.Fatalf("open file error: %v", err)
return err
}
options = append(options, OptionCloseFile())
2022-01-13 23:27:39 +01:00
return WriteFasta(iterator, file, options...)
}
func WriteFastaToStdout(iterator obiiter.IBioSequence, options ...WithOption) error {
options = append(options, OptionDontCloseFile())
2022-01-13 23:27:39 +01:00
return WriteFasta(iterator, os.Stdout, options...)
}
func WriteFastaBatch(iterator obiiter.IBioSequenceBatch,
file io.Writer,
options ...WithOption) (obiiter.IBioSequenceBatch, error) {
opt := MakeOptions(options)
2022-01-13 23:27:39 +01:00
buffsize := iterator.BufferSize()
newIter := obiiter.MakeIBioSequenceBatch(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()
newIter.Add(nwriters)
2022-01-13 23:27:39 +01:00
go func() {
newIter.WaitAndClose()
2022-01-13 23:27:39 +01:00
close(chunkchan)
}()
ff := func(iterator obiiter.IBioSequenceBatch) {
2022-01-13 23:27:39 +01:00
for iterator.Next() {
batch := iterator.Get()
chunkchan <- FileChunck{
FormatFastaBatch(batch, header_format),
batch.Order(),
}
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
}
log.Println("Start of the fasta 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
}()
return newIter, nil
2022-01-13 23:27:39 +01:00
}
func WriteFastaBatchToStdout(iterator obiiter.IBioSequenceBatch,
options ...WithOption) (obiiter.IBioSequenceBatch, error) {
options = append(options, OptionDontCloseFile())
2022-01-13 23:27:39 +01:00
return WriteFastaBatch(iterator, os.Stdout, options...)
}
func WriteFastaBatchToFile(iterator obiiter.IBioSequenceBatch,
2022-01-13 23:27:39 +01:00
filename string,
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)
return obiiter.NilIBioSequenceBatch, err
2022-01-13 23:27:39 +01:00
}
options = append(options, OptionCloseFile())
2022-01-13 23:27:39 +01:00
return WriteFastaBatch(iterator, file, options...)
}