Files
obitools4/pkg/obiseq/biosequence.go

259 lines
4.9 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obiseq
import (
"crypto/md5"
"log"
"sync/atomic"
2022-01-13 23:27:39 +01:00
2022-01-13 23:43:01 +01:00
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
2022-01-13 23:27:39 +01:00
)
var _NewSeq = int32(0)
var _RecycleSeq = int32(0)
var _InMemSeq = int32(0)
var _MaxInMemSeq = int32(0)
var _BioLogRate = int(100000)
func LogBioSeqStatus() {
log.Printf("@@@@>>>> Created seq : %d Destroyed : %d In Memory : %d", _NewSeq, _RecycleSeq, _InMemSeq)
}
2022-01-13 23:27:39 +01:00
type Quality []uint8
var __default_qualities__ = make(Quality, 0, 500)
func __make_default_qualities__(length int) Quality {
cl := len(__default_qualities__)
if cl < length {
for i := cl; i <= length; i++ {
__default_qualities__ = append(__default_qualities__, 40)
}
}
return __default_qualities__[0:length]
}
type Annotation map[string]interface{}
type BioSequence struct {
2022-01-16 00:21:42 +01:00
id string
definition string
sequence []byte
qualities []byte
feature []byte
2022-01-13 23:27:39 +01:00
annotations Annotation
}
2022-01-16 00:21:42 +01:00
func MakeEmptyBioSequence() BioSequence {
atomic.AddInt32(&_NewSeq, 1)
atomic.AddInt32(&_InMemSeq, 1)
//if atomic.CompareAndSwapInt32()()
// if int(_NewSeq)%int(_BioLogRate) == 0 {
// LogBioSeqStatus()
// }
return BioSequence{
2022-01-16 00:21:42 +01:00
id: "",
definition: "",
sequence: nil,
qualities: nil,
feature: nil,
annotations: nil,
}
}
func NewEmptyBioSequence() *BioSequence {
s := MakeEmptyBioSequence()
return &s
2022-01-16 00:21:42 +01:00
}
func MakeBioSequence(id string,
sequence []byte,
definition string) BioSequence {
bs := MakeEmptyBioSequence()
bs.SetId(id)
bs.SetSequence(sequence)
bs.SetDefinition(definition)
return bs
}
func NewBioSequence(id string,
sequence []byte,
definition string) *BioSequence {
s := MakeBioSequence(id, sequence, definition)
return &s
}
2022-01-16 00:21:42 +01:00
func (sequence *BioSequence) Recycle() {
2022-01-16 00:21:42 +01:00
atomic.AddInt32(&_RecycleSeq, 1)
atomic.AddInt32(&_InMemSeq, -1)
2022-01-16 00:21:42 +01:00
// if int(_RecycleSeq)%int(_BioLogRate) == 0 {
// LogBioSeqStatus()
// }
2022-01-13 23:27:39 +01:00
if sequence != nil {
RecycleSlice(&sequence.sequence)
sequence.sequence = nil
RecycleSlice(&sequence.feature)
sequence.feature = nil
RecycleSlice(&sequence.qualities)
sequence.qualities = nil
2022-01-13 23:27:39 +01:00
RecycleAnnotation(&sequence.annotations)
sequence.annotations = nil
}
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) Copy() *BioSequence {
2022-01-16 00:21:42 +01:00
newSeq := MakeEmptyBioSequence()
2022-01-13 23:27:39 +01:00
newSeq.id = s.id
newSeq.definition = s.definition
2022-01-13 23:27:39 +01:00
newSeq.sequence = GetSlice(s.sequence...)
newSeq.qualities = GetSlice(s.qualities...)
newSeq.feature = GetSlice(s.feature...)
2022-01-13 23:27:39 +01:00
if len(s.annotations) > 0 {
newSeq.annotations = GetAnnotation(s.annotations)
2022-01-13 23:27:39 +01:00
}
return &newSeq
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) Id() string {
return s.id
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) Definition() string {
return s.definition
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) Sequence() []byte {
return s.sequence
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) String() string {
return string(s.sequence)
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) Length() int {
return len(s.sequence)
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) HasQualities() bool {
return len(s.qualities) > 0
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) Qualities() Quality {
2022-01-13 23:27:39 +01:00
if s.HasQualities() {
return s.qualities
2022-01-13 23:27:39 +01:00
} else {
return __make_default_qualities__(len(s.sequence))
2022-01-13 23:27:39 +01:00
}
}
func (s *BioSequence) Features() string {
return string(s.feature)
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) HasAnnotation() bool {
return len(s.annotations) > 0
}
func (s *BioSequence) Annotations() Annotation {
2022-02-01 17:31:28 +01:00
if s.annotations == nil {
s.annotations = GetAnnotation()
2022-01-16 00:21:42 +01:00
}
2022-02-01 17:31:28 +01:00
return s.annotations
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) MD5() [16]byte {
return md5.Sum(s.sequence)
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) Count() int {
if s.annotations == nil {
2022-01-13 23:27:39 +01:00
return 1
}
if val, ok := (s.annotations)["count"]; ok {
2022-01-13 23:27:39 +01:00
val, err := goutils.InterfaceToInt(val)
if err == nil {
return val
}
}
return 1
}
func (s *BioSequence) Taxid() int {
if s.annotations == nil {
2022-01-13 23:27:39 +01:00
return 1
}
if val, ok := (s.annotations)["taxid"]; ok {
2022-01-13 23:27:39 +01:00
val, err := goutils.InterfaceToInt(val)
if err == nil {
return val
}
}
return 1
}
func (s *BioSequence) SetId(id string) {
s.id = id
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) SetDefinition(definition string) {
s.definition = definition
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) SetFeatures(feature []byte) {
if cap(s.feature) >= 300 {
RecycleSlice(&s.feature)
2022-01-16 00:21:42 +01:00
}
s.feature = feature
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) SetSequence(sequence []byte) {
if s.sequence != nil {
RecycleSlice(&s.sequence)
2022-01-16 00:21:42 +01:00
}
s.sequence = sequence
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) SetQualities(qualities Quality) {
if s.qualities != nil {
RecycleSlice(&s.qualities)
2022-01-16 00:21:42 +01:00
}
s.qualities = qualities
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) WriteQualities(data []byte) (int, error) {
s.qualities = append(s.qualities, data...)
2022-01-16 00:21:42 +01:00
return len(data), nil
}
func (s *BioSequence) WriteByteQualities(data byte) error {
s.qualities = append(s.qualities, data)
2022-01-16 00:21:42 +01:00
return nil
}
func (s *BioSequence) Write(data []byte) (int, error) {
s.sequence = append(s.sequence, data...)
2022-01-16 00:21:42 +01:00
return len(data), nil
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) WriteString(data string) (int, error) {
2022-01-16 00:21:42 +01:00
bdata := []byte(data)
return s.Write(bdata)
2022-01-13 23:27:39 +01:00
}
func (s *BioSequence) WriteByte(data byte) error {
s.sequence = append(s.sequence, data)
2022-01-16 00:21:42 +01:00
return nil
2022-01-13 23:27:39 +01:00
}