A go implementation of the fasta reader

Former-commit-id: 603592c4761fb0722e9e0501d78de1bd3ba238fa
This commit is contained in:
2023-09-01 09:30:12 +02:00
parent 3f8c0d6a2f
commit 62b57f4ede
15 changed files with 1403 additions and 77 deletions

View File

@ -1,8 +1,10 @@
package obiseq
import (
log "github.com/sirupsen/logrus"
"sync"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
)
// BioSequenceSlice represents a collection or a set of BioSequence.
@ -18,22 +20,39 @@ var _BioSequenceSlicePool = sync.Pool{
},
}
// > This function returns a pointer to a new `BioSequenceSlice` object
// NewBioSequenceSlice returns a new BioSequenceSlice with the specified size.
//
// The size parameter is optional. If provided, the returned slice will be
// resized accordingly.
//
// Returns a pointer to the newly created BioSequenceSlice.
func NewBioSequenceSlice(size ...int) *BioSequenceSlice {
slice := _BioSequenceSlicePool.Get().(*BioSequenceSlice)
if len(size) > 0 {
s := size[0]
slice = slice.InsureCapacity(s)
(*slice)=(*slice)[0:s]
(*slice) = (*slice)[0:s]
}
return slice
}
// `MakeBioSequenceSlice()` returns a pointer to a new `BioSequenceSlice` struct
// MakeBioSequenceSlice creates a new BioSequenceSlice with the specified size(s).
//
// Parameters:
// - size: The size(s) of the BioSequenceSlice to create (optional).
//
// Return:
// A new BioSequenceSlice with the specified size(s).
func MakeBioSequenceSlice(size ...int) BioSequenceSlice {
return *NewBioSequenceSlice(size...)
}
// Recycle cleans up the BioSequenceSlice by recycling its elements and resetting its length.
//
// If including_seq is true, each element of the BioSequenceSlice is recycled using the Recycle method,
// and then set to nil. If including_seq is false, each element is simply set to nil.
//
// The function does not return anything.
func (s *BioSequenceSlice) Recycle(including_seq bool) {
if s == nil {
log.Panicln("Trying too recycle a nil pointer")
@ -42,60 +61,113 @@ func (s *BioSequenceSlice) Recycle(including_seq bool) {
// Code added to potentially limit memory leaks
if including_seq {
for i := range *s {
(*s)[i] .Recycle()
(*s)[i].Recycle()
(*s)[i] = nil
}
} else {
for i := range *s {
(*s)[i] = nil
}
}
}
*s = (*s)[:0]
_BioSequenceSlicePool.Put(s)
}
// Making sure that the slice has enough capacity to hold the number of elements that are being added
// to it.
// InsureCapacity ensures that the BioSequenceSlice has a minimum capacity
//
// It takes an integer `capacity` as a parameter, which represents the desired minimum capacity of the BioSequenceSlice.
// It returns a pointer to the BioSequenceSlice.
func (s *BioSequenceSlice) InsureCapacity(capacity int) *BioSequenceSlice {
var c int
if s != nil {
c = cap(*s)
c = cap(*s)
} else {
c = 0
}
if c < capacity {
sl := make(BioSequenceSlice, 0,capacity)
s = &sl
}
*s = slices.Grow[BioSequenceSlice](*s, capacity-c)
return s
}
// Appending the sequence to the slice.
// Push appends a BioSequence to the BioSequenceSlice.
//
// It takes a pointer to a BioSequenceSlice and a BioSequence as parameters.
// It does not return anything.
func (s *BioSequenceSlice) Push(sequence *BioSequence) {
*s = append(*s, sequence)
}
// Returning the last element of the slice and removing it from the slice.
// Pop returns and removes the last element from the BioSequenceSlice.
//
// It does not take any parameters.
// It returns *BioSequence, the last element of the slice.
func (s *BioSequenceSlice) Pop() *BioSequence {
_s := (*s)[len(*s)-1]
(*s)[len(*s)-1] = nil
*s = (*s)[:len(*s)-1]
return _s
// Get the length of the slice
length := len(*s)
// If the slice is empty, return nil
if length == 0 {
return nil
}
// Get the last element of the slice
lastElement := (*s)[length-1]
// Set the last element to nil
(*s)[length-1] = nil
// Remove the last element from the slice
*s = (*s)[:length-1]
// Return the last element
return lastElement
}
// Returning the first element of the slice and removing it from the slice.
// Pop0 returns and removes the first element of the BioSequenceSlice.
//
// It does not take any parameters.
// It returns a pointer to a BioSequence object.
func (s *BioSequenceSlice) Pop0() *BioSequence {
_s := (*s)[0]
if len(*s) == 0 {
return nil
}
firstElement := (*s)[0]
(*s)[0] = nil
*s = (*s)[1:]
return _s
return firstElement
}
// Test that a slice of sequences contains at least a sequence.
// NotEmpty checks if the BioSequenceSlice is not empty.
//
// No parameters.
// Returns a boolean value indicating if the BioSequenceSlice is not empty.
func (s BioSequenceSlice) NotEmpty() bool {
return len(s) > 0
}
// Len returns the length of the BioSequenceSlice.
//
// It has no parameters.
// It returns an integer.
func (s BioSequenceSlice) Len() int {
return len(s)
}
// Size returns the total size of the BioSequenceSlice.
//
// It calculates the size by iterating over each BioSequence in the slice
// and summing up their lengths.
//
// Returns an integer representing the total size of the BioSequenceSlice.
func (s BioSequenceSlice) Size() int {
size := 0
for _, s := range s {
size += s.Len()
}
return size
}