diff --git a/pkg/obiseq/biosequence.go b/pkg/obiseq/biosequence.go index 01257fe..64e67e8 100644 --- a/pkg/obiseq/biosequence.go +++ b/pkg/obiseq/biosequence.go @@ -1,3 +1,13 @@ +// A package that defines a BioSequence struct. +// +// BioSequence are used to représente biological DNA sequences. +// The structure stores not only the sequence itself, but also some +// complementaty information. Among them: +// - an identifier +// - a definition +// - the sequence quality scores +// - the features +// - the annotations package obiseq import ( @@ -260,7 +270,7 @@ func (s *BioSequence) Count() int { // Returning the taxid of the sequence. func (s *BioSequence) Taxid() int { - taxid, ok := s.GetInt("count") + taxid, ok := s.GetInt("taxid") if !ok { taxid = 1 @@ -269,6 +279,11 @@ func (s *BioSequence) Taxid() int { return taxid } +func (s *BioSequence) SetTaxid(taxid int) { + annot := s.Annotations() + annot["taxid"] = taxid +} + // Setting the id of the BioSequence. func (s *BioSequence) SetId(id string) { s.id = id diff --git a/pkg/obiseq/biosequenceslice.go b/pkg/obiseq/biosequenceslice.go index aa39978..78558a4 100644 --- a/pkg/obiseq/biosequenceslice.go +++ b/pkg/obiseq/biosequenceslice.go @@ -1,9 +1,14 @@ package obiseq import ( + log "github.com/sirupsen/logrus" "sync" ) +// BioSequenceSlice represents a collection or a set of BioSequence. +// +// BioSequenceSlice is used to define BioSequenceBatch +// a memory pool of BioSequenceSlice is managed to limit allocations. type BioSequenceSlice []*BioSequence var _BioSequenceSlicePool = sync.Pool{ @@ -13,32 +18,54 @@ var _BioSequenceSlicePool = sync.Pool{ }, } +// > This function returns a pointer to a new `BioSequenceSlice` object func NewBioSequenceSlice() *BioSequenceSlice { return _BioSequenceSlicePool.Get().(*BioSequenceSlice) } +// `MakeBioSequenceSlice()` returns a pointer to a new `BioSequenceSlice` struct func MakeBioSequenceSlice() BioSequenceSlice { return *NewBioSequenceSlice() } func (s *BioSequenceSlice) Recycle() { - // if s == nil { - // log.Panicln("Trying too recycle a nil pointer") - // } + if s == nil { + log.Panicln("Trying too recycle a nil pointer") + } - // // Code added to potentially limit memory leaks - // for i := range *s { - // (*s)[i] = nil - // } + // Code added to potentially limit memory leaks + for i := range *s { + (*s)[i] = nil + } - // *s = (*s)[:0] - // _BioSequenceSlicePool.Put(s) + *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. +func (s *BioSequenceSlice) InsureCapacity(capacity int) *BioSequenceSlice { + var c int + if s != nil { + c = cap(*s) + } else { + c = 0 + } + + if c < capacity { + sl := make(BioSequenceSlice, 0,capacity) + s = &sl + } + + return s +} + +// Appending the sequence to the slice. func (s *BioSequenceSlice) Push(sequence *BioSequence) { *s = append(*s, sequence) } +// Returning the last element of the slice and removing it from the slice. func (s *BioSequenceSlice) Pop() *BioSequence { _s := (*s)[len(*s)-1] (*s)[len(*s)-1] = nil @@ -46,6 +73,7 @@ func (s *BioSequenceSlice) Pop() *BioSequence { return _s } +// Returning the first element of the slice and removing it from the slice. func (s *BioSequenceSlice) Pop0() *BioSequence { _s := (*s)[0] (*s)[0] = nil @@ -53,6 +81,7 @@ func (s *BioSequenceSlice) Pop0() *BioSequence { return _s } +// Test that a slice of sequences contains at least a sequence. func (s BioSequenceSlice) NotEmpty() bool { return len(s) > 0 }