mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-12-08 16:50:27 +00:00
Replace MakeBioSequence call by NewBioSequence call,
Implements a new file format guesser Adds some more API doc Former-commit-id: 9837bf1c28beca6ddb599b367f93548950ba83c1
This commit is contained in:
@@ -34,6 +34,10 @@ type Quality []uint8
|
||||
|
||||
var __default_qualities__ = make(Quality, 0, 500)
|
||||
|
||||
// __make_default_qualities__ generates a default quality slice of the given length.
|
||||
//
|
||||
// It takes an integer parameter 'length' which specifies the desired length of the quality slice.
|
||||
// It returns a Quality slice which is a subset of the '__default_qualities__' slice.
|
||||
func __make_default_qualities__(length int) Quality {
|
||||
cl := len(__default_qualities__)
|
||||
if cl < length {
|
||||
@@ -59,11 +63,14 @@ type BioSequence struct {
|
||||
feature []byte
|
||||
paired *BioSequence // A pointer to the paired sequence
|
||||
annotations Annotation
|
||||
annot_lock *sync.Mutex
|
||||
annot_lock sync.Mutex
|
||||
}
|
||||
|
||||
// MakeEmptyBioSequence() creates a new BioSequence object with no data
|
||||
func MakeEmptyBioSequence(preallocate int) BioSequence {
|
||||
// NewEmptyBioSequence creates a new BioSequence object with an empty sequence.
|
||||
//
|
||||
// The preallocate parameter specifies the number of bytes to preallocate for the sequence. If preallocate is greater than 0, the sequence will be preallocated with the specified number of bytes. If preallocate is 0, the sequence will not be preallocated.
|
||||
// The function returns a pointer to the newly created BioSequence object.
|
||||
func NewEmptyBioSequence(preallocate int) *BioSequence {
|
||||
atomic.AddInt32(&_NewSeq, 1)
|
||||
atomic.AddInt32(&_InMemSeq, 1)
|
||||
|
||||
@@ -72,7 +79,7 @@ func MakeEmptyBioSequence(preallocate int) BioSequence {
|
||||
seq = GetSlice(preallocate)
|
||||
}
|
||||
|
||||
return BioSequence{
|
||||
return &BioSequence{
|
||||
id: "",
|
||||
definition: "",
|
||||
source: "",
|
||||
@@ -81,36 +88,33 @@ func MakeEmptyBioSequence(preallocate int) BioSequence {
|
||||
feature: nil,
|
||||
paired: nil,
|
||||
annotations: nil,
|
||||
annot_lock: &sync.Mutex{},
|
||||
annot_lock: sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
// `NewEmptyBioSequence()` returns a pointer to a new empty BioSequence
|
||||
func NewEmptyBioSequence(preallocate int) *BioSequence {
|
||||
s := MakeEmptyBioSequence(preallocate)
|
||||
return &s
|
||||
}
|
||||
|
||||
// `MakeBioSequence` creates a new `BioSequence` with the given `id`, `sequence`, and `definition`
|
||||
func MakeBioSequence(id string,
|
||||
// NewBioSequence creates a new BioSequence object with the given ID, sequence, and definition.
|
||||
//
|
||||
// Parameters:
|
||||
// - id: the ID of the BioSequence.
|
||||
// - sequence: the sequence data of the BioSequence.
|
||||
// - definition: the definition of the BioSequence.
|
||||
//
|
||||
// Returns:
|
||||
// - *BioSequence: the newly created BioSequence object.
|
||||
func NewBioSequence(id string,
|
||||
sequence []byte,
|
||||
definition string) BioSequence {
|
||||
bs := MakeEmptyBioSequence(0)
|
||||
definition string) *BioSequence {
|
||||
bs := NewEmptyBioSequence(0)
|
||||
bs.SetId(id)
|
||||
bs.SetSequence(sequence)
|
||||
bs.SetDefinition(definition)
|
||||
return bs
|
||||
}
|
||||
|
||||
// `NewBioSequence` creates a new `BioSequence` struct and returns a pointer to it
|
||||
func NewBioSequence(id string,
|
||||
sequence []byte,
|
||||
definition string) *BioSequence {
|
||||
s := MakeBioSequence(id, sequence, definition)
|
||||
return &s
|
||||
}
|
||||
|
||||
// A method that is called when the sequence is no longer needed.
|
||||
// Recycle recycles the BioSequence object.
|
||||
//
|
||||
// It decreases the count of in-memory sequences and increases the count of recycled sequences.
|
||||
// It also recycles the various slices and annotations of the BioSequence object.
|
||||
func (sequence *BioSequence) Recycle() {
|
||||
|
||||
atomic.AddInt32(&_RecycleSeq, 1)
|
||||
@@ -133,9 +137,15 @@ func (sequence *BioSequence) Recycle() {
|
||||
}
|
||||
}
|
||||
|
||||
// Copying the BioSequence.
|
||||
// Copy returns a new BioSequence that is a copy of the original BioSequence.
|
||||
//
|
||||
// It copies the id and definition fields of the original BioSequence to the new BioSequence.
|
||||
// It also creates new slices and copies the values from the original BioSequence's sequence, qualities, and feature fields to the new BioSequence.
|
||||
// If the original BioSequence has annotations, it locks the annot_lock and copies the annotations to the new BioSequence.
|
||||
//
|
||||
// The function returns the new BioSequence.
|
||||
func (s *BioSequence) Copy() *BioSequence {
|
||||
newSeq := MakeEmptyBioSequence(0)
|
||||
newSeq := NewEmptyBioSequence(0)
|
||||
|
||||
newSeq.id = s.id
|
||||
newSeq.definition = s.definition
|
||||
@@ -150,30 +160,45 @@ func (s *BioSequence) Copy() *BioSequence {
|
||||
newSeq.annotations = GetAnnotation(s.annotations)
|
||||
}
|
||||
|
||||
return &newSeq
|
||||
return newSeq
|
||||
}
|
||||
|
||||
// A method that returns the id of the sequence.
|
||||
// Id returns the ID of the BioSequence.
|
||||
//
|
||||
// No parameters.
|
||||
// Returns a string.
|
||||
func (s *BioSequence) Id() string {
|
||||
return s.id
|
||||
}
|
||||
|
||||
// A method that returns the definition of the sequence.
|
||||
// Definition returns the definition of the BioSequence.
|
||||
//
|
||||
// No parameters.
|
||||
// Returns a string.
|
||||
func (s *BioSequence) Definition() string {
|
||||
return s.definition
|
||||
}
|
||||
|
||||
// A method that returns the sequence as a byte slice.
|
||||
// Sequence returns the sequence of the BioSequence.
|
||||
//
|
||||
// Returns:
|
||||
// - []byte: The sequence of the BioSequence.
|
||||
func (s *BioSequence) Sequence() []byte {
|
||||
return s.sequence
|
||||
}
|
||||
|
||||
// A method that returns the sequence as a string.
|
||||
// String returns the string representation of the Sequence.
|
||||
//
|
||||
// No parameters.
|
||||
// Returns a string.
|
||||
func (s *BioSequence) String() string {
|
||||
return string(s.sequence)
|
||||
}
|
||||
|
||||
// Returning the length of the sequence.
|
||||
// Len returns the length of the BioSequence.
|
||||
//
|
||||
// It does not take any parameters.
|
||||
// It returns an integer representing the length of the sequence.
|
||||
func (s *BioSequence) Len() int {
|
||||
return len(s.sequence)
|
||||
}
|
||||
@@ -301,39 +326,47 @@ func (s *BioSequence) WriteString(data string) (int, error) {
|
||||
return s.Write(bdata)
|
||||
}
|
||||
|
||||
// A method that appends a byte to the sequence.
|
||||
// WriteByte appends a byte to the BioSequence's sequence.
|
||||
//
|
||||
// data: the byte to append to the sequence.
|
||||
// error: an error if the append operation fails.
|
||||
func (s *BioSequence) WriteByte(data byte) error {
|
||||
s.sequence = append(s.sequence, data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Clearing the sequence.
|
||||
// Clear clears the BioSequence by resetting the sequence to an empty slice.
|
||||
//
|
||||
// No parameters.
|
||||
// No return values.
|
||||
func (s *BioSequence) Clear() {
|
||||
s.sequence = s.sequence[0:0]
|
||||
}
|
||||
|
||||
// Composition calculates the composition of the BioSequence.
|
||||
//
|
||||
// It counts the occurrences of each nucleotide (a, c, g, t) in the sequence
|
||||
// and returns a map with the counts.
|
||||
//
|
||||
// No parameters.
|
||||
// Returns a map of byte to int, with the counts of each nucleotide.
|
||||
func (s *BioSequence) Composition() map[byte]int {
|
||||
counts := map[byte]int{
|
||||
'a': 0,
|
||||
'c': 0,
|
||||
'g': 0,
|
||||
't': 0,
|
||||
'o': 0,
|
||||
}
|
||||
|
||||
a := 0
|
||||
c := 0
|
||||
g := 0
|
||||
t := 0
|
||||
other := 0
|
||||
for _, char := range s.sequence {
|
||||
switch char {
|
||||
case 'a':
|
||||
a++
|
||||
case 'c':
|
||||
c++
|
||||
case 'g':
|
||||
g++
|
||||
case 't':
|
||||
t++
|
||||
switch char | byte(32) {
|
||||
case 'a', 'c', 'g', 't':
|
||||
counts[char]++
|
||||
default:
|
||||
other++
|
||||
|
||||
counts['o']++
|
||||
}
|
||||
}
|
||||
|
||||
return map[byte]int{'a': a, 'c': c, 'g': g, 't': t, 'o': other}
|
||||
return counts
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user