2023-08-14 00:12:17 +02:00
|
|
|
// Package obiformats provides functions for formatting and writing biosequences in various formats.
|
2022-01-13 23:27:39 +01:00
|
|
|
package obiformats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2024-11-29 18:15:03 +01:00
|
|
|
"time"
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2022-02-24 12:14:52 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-11-29 12:14:37 +01:00
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiiter"
|
2025-03-25 16:44:46 +01:00
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obilog"
|
2023-11-29 12:14:37 +01:00
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiseq"
|
|
|
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
|
2022-01-13 23:27:39 +01:00
|
|
|
)
|
|
|
|
|
2023-08-14 10:04:16 +02:00
|
|
|
// min returns the minimum of two integers.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - x: an integer
|
|
|
|
// - y: an integer
|
|
|
|
//
|
|
|
|
// Return:
|
|
|
|
// - the minimum of x and y (an integer)
|
2022-01-13 23:27:39 +01:00
|
|
|
func min(x, y int) int {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:04:16 +02:00
|
|
|
// FormatFasta formats a BioSequence into a FASTA formatted string.
|
|
|
|
//
|
|
|
|
// seq is a pointer to the BioSequence to be formatted.
|
|
|
|
// formater is the FormatHeader function to be used for formatting the sequence header.
|
|
|
|
// It returns a string containing the formatted FASTA sequence.
|
2022-02-21 19:00:23 +01:00
|
|
|
func FormatFasta(seq *obiseq.BioSequence, formater FormatHeader) string {
|
2022-01-13 23:27:39 +01:00
|
|
|
var fragments strings.Builder
|
|
|
|
|
2022-02-24 07:08:40 +01:00
|
|
|
if seq == nil {
|
2022-02-21 19:00:23 +01:00
|
|
|
log.Panicln("try to format a nil BioSequence")
|
|
|
|
}
|
|
|
|
|
2022-01-13 23:27:39 +01:00
|
|
|
s := seq.Sequence()
|
|
|
|
l := len(s)
|
|
|
|
|
2022-11-16 09:22:55 +01:00
|
|
|
folded := ""
|
|
|
|
if l == 0 {
|
|
|
|
log.Println("Writing a BioSequence of length zero")
|
|
|
|
} else {
|
|
|
|
fragments.Grow(l + int(l/60)*2 + 100)
|
|
|
|
|
|
|
|
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]
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
info := formater(seq)
|
2023-10-05 07:31:31 +02:00
|
|
|
return fmt.Sprintf(">%s %s\n%s",
|
2022-01-13 23:27:39 +01:00
|
|
|
seq.Id(), info,
|
|
|
|
folded)
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:04:16 +02:00
|
|
|
// FormatFastaBatch formats a batch of biosequences in FASTA format.
|
|
|
|
//
|
|
|
|
// It takes the following parameters:
|
|
|
|
// - batch: a BioSequenceBatch representing the batch of sequences to format.
|
|
|
|
// - formater: a FormatHeader function that formats the header of each sequence.
|
|
|
|
// - skipEmpty: a boolean indicating whether empty sequences should be skipped or not.
|
|
|
|
//
|
|
|
|
// It returns a byte array containing the formatted sequences.
|
2024-08-02 12:35:46 +02:00
|
|
|
func FormatFastaBatch(batch obiiter.BioSequenceBatch, formater FormatHeader, skipEmpty bool) *bytes.Buffer {
|
2023-08-14 10:04:16 +02:00
|
|
|
// Create a buffer to store the formatted sequences
|
2022-01-13 23:27:39 +01:00
|
|
|
var bs bytes.Buffer
|
2023-08-14 10:04:16 +02:00
|
|
|
|
2024-06-26 18:39:42 +02:00
|
|
|
lt := 0
|
|
|
|
|
|
|
|
for _, seq := range batch.Slice() {
|
|
|
|
lt += seq.Len()
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:04:16 +02:00
|
|
|
// Iterate over each sequence in the batch
|
2024-06-26 18:39:42 +02:00
|
|
|
log.Debugf("FormatFastaBatch: #%d : %d seqs", batch.Order(), batch.Len())
|
|
|
|
first := true
|
|
|
|
for _, seq := range batch.Slice() {
|
2023-08-14 10:04:16 +02:00
|
|
|
// Check if the sequence is empty
|
2023-07-17 14:24:02 +02:00
|
|
|
if seq.Len() > 0 {
|
2023-08-14 10:04:16 +02:00
|
|
|
// Format the sequence using the provided formater function
|
|
|
|
formattedSeq := FormatFasta(seq, formater)
|
|
|
|
|
2024-06-26 18:39:42 +02:00
|
|
|
if first {
|
|
|
|
bs.Grow(lt + (len(formattedSeq)-seq.Len())*batch.Len()*5/4)
|
|
|
|
first = false
|
2024-06-22 22:32:31 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 10:04:16 +02:00
|
|
|
// Append the formatted sequence to the buffer
|
|
|
|
bs.WriteString(formattedSeq)
|
|
|
|
bs.WriteByte('\n')
|
2023-07-17 14:24:02 +02:00
|
|
|
} else {
|
2023-08-14 10:04:16 +02:00
|
|
|
// Handle empty sequences
|
2023-07-17 14:24:02 +02:00
|
|
|
if skipEmpty {
|
2023-08-14 10:04:16 +02:00
|
|
|
// Skip empty sequences if skipEmpty is true
|
2025-03-25 16:44:46 +01:00
|
|
|
obilog.Warnf("Sequence %s is empty and skipped in output", seq.Id())
|
2023-07-17 14:24:02 +02:00
|
|
|
} else {
|
2023-08-14 10:04:16 +02:00
|
|
|
// Terminate the program if skipEmpty is false
|
|
|
|
log.Fatalf("Sequence %s is empty", seq.Id())
|
2023-07-17 14:24:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
2023-08-14 10:04:16 +02:00
|
|
|
|
|
|
|
// Return the byte array representation of the buffer
|
2024-08-02 12:35:46 +02:00
|
|
|
return &bs
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 10:04:16 +02:00
|
|
|
// WriteFasta writes a given iterator of bio sequences to a file in FASTA format.
|
|
|
|
//
|
|
|
|
// The function takes an iterator of bio sequences, a file to write to, and
|
|
|
|
// optional options. It returns a new iterator of bio sequences and an error.
|
2023-01-22 22:04:17 +01:00
|
|
|
func WriteFasta(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-03-24 10:25:12 +07:00
|
|
|
file, _ = obiutils.CompressStream(file, opt.CompressedFile(), opt.CloseFile())
|
2023-02-18 12:06:52 +01:00
|
|
|
|
2023-03-07 11:12:13 +07:00
|
|
|
newIter := obiiter.MakeIBioSequence()
|
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
|
|
|
|
2024-11-29 18:15:03 +01:00
|
|
|
chunkchan := WriteFileChunk(file, opt.CloseFile())
|
2022-01-13 23:27:39 +01:00
|
|
|
|
|
|
|
header_format := opt.FormatFastSeqHeader()
|
|
|
|
|
2022-01-14 23:11:36 +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()
|
2024-11-29 18:15:03 +01:00
|
|
|
for len(chunkchan) > 0 {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
2022-01-13 23:27:39 +01:00
|
|
|
close(chunkchan)
|
2024-08-05 10:48:28 +02:00
|
|
|
log.Debugf("Writing fasta file done")
|
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() {
|
2022-08-23 15:07:06 +02:00
|
|
|
|
2022-01-13 23:27:39 +01:00
|
|
|
batch := iterator.Get()
|
2022-08-23 15:07:06 +02:00
|
|
|
|
2024-06-26 18:39:42 +02:00
|
|
|
log.Debugf("Formating fasta chunk %d", batch.Order())
|
|
|
|
|
2024-11-29 18:15:03 +01:00
|
|
|
chunkchan <- FileChunk{
|
2024-08-02 12:35:46 +02:00
|
|
|
Source: batch.Source(),
|
|
|
|
Raw: FormatFastaBatch(batch, header_format, opt.SkipEmptySequence()),
|
|
|
|
Order: batch.Order(),
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
2024-08-02 12:35:46 +02:00
|
|
|
|
2024-06-26 18:39:42 +02:00
|
|
|
log.Debugf("Fasta chunk %d formated", batch.Order())
|
|
|
|
|
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 fasta file writing")
|
2022-01-18 13:09:32 +01:00
|
|
|
go ff(iterator)
|
2024-11-29 18:15:03 +01:00
|
|
|
for i := 1; i < nwriters; i++ {
|
2022-01-13 23:27:39 +01:00
|
|
|
go ff(iterator.Split())
|
|
|
|
}
|
|
|
|
|
2022-01-14 23:11:36 +01:00
|
|
|
return newIter, nil
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 10:04:16 +02:00
|
|
|
// WriteFastaToStdout writes the given bio sequence iterator to standard output in FASTA format.
|
|
|
|
//
|
|
|
|
// The function takes an iterator of bio sequences as the first parameter and optional
|
|
|
|
// configuration options as variadic arguments. It appends the option to not close the file
|
|
|
|
// to the options slice and then calls the WriteFasta function passing the iterator,
|
|
|
|
// os.Stdout as the output file, and the options slice.
|
2023-10-05 07:31:31 +02:00
|
|
|
//
|
2023-08-14 10:04:16 +02:00
|
|
|
// The function returns the same bio sequence iterator and an error if any occurred.
|
2023-01-22 22:04:17 +01:00
|
|
|
func WriteFastaToStdout(iterator obiiter.IBioSequence,
|
|
|
|
options ...WithOption) (obiiter.IBioSequence, error) {
|
2024-08-13 09:45:28 +02:00
|
|
|
// options = append(options, OptionDontCloseFile())
|
|
|
|
options = append(options, OptionCloseFile())
|
2022-11-16 17:13:03 +01:00
|
|
|
return WriteFasta(iterator, os.Stdout, options...)
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 10:04:16 +02:00
|
|
|
// WriteFastaToFile writes the given iterator of biosequences to a file with the specified filename,
|
|
|
|
// using the provided options. It returns the updated iterator and any error that occurred.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - iterator: The biosequence iterator to write to the file.
|
|
|
|
// - filename: The name of the file to write to.
|
|
|
|
// - options: Zero or more optional parameters to customize the writing process.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - obiiter.IBioSequence: The updated biosequence iterator.
|
|
|
|
// - error: Any error that occurred during the writing process.
|
2023-01-22 22:04:17 +01:00
|
|
|
func WriteFastaToFile(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
|
2024-06-19 13:15:30 +02:00
|
|
|
} else {
|
|
|
|
flags |= os.O_TRUNC
|
2023-02-18 12:06:52 +01:00
|
|
|
}
|
2024-06-19 13:15:30 +02:00
|
|
|
|
2023-02-18 12:06:52 +01:00
|
|
|
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())
|
|
|
|
|
2023-02-23 23:35:58 +01:00
|
|
|
iterator, err = WriteFasta(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 = WriteFasta(iterator.PairedWith(), revfile, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return iterator, err
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|