Files
obitools4/pkg/obiseq/subseq.go

52 lines
1.2 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obiseq
import (
"errors"
"fmt"
)
// Returns a sub sequence start from position 'from' included,
// to position 'to' excluded. Coordinates start at position 0.
func (sequence *BioSequence) Subsequence(from, to int, circular bool) (*BioSequence, error) {
2022-01-13 23:27:39 +01:00
if from >= to && !circular {
return nil, errors.New("from greater than to")
2022-01-13 23:27:39 +01:00
}
if from < 0 || from >= sequence.Length() {
return nil, errors.New("from out of bounds")
2022-01-13 23:27:39 +01:00
}
if to <= 0 || to > sequence.Length() {
return nil, errors.New("to out of bounds")
2022-01-13 23:27:39 +01:00
}
var newSeq *BioSequence
2022-01-13 23:27:39 +01:00
if from < to {
newSeq = NewEmptyBioSequence()
2022-01-16 00:21:42 +01:00
newSeq.Write(sequence.Sequence()[from:to])
if sequence.HasQualities() {
newSeq.WriteQualities(sequence.Qualities()[from:to])
}
newSeq.id = fmt.Sprintf("%s_sub[%d..%d]", sequence.Id(), from+1, to)
newSeq.definition = sequence.definition
2022-01-13 23:27:39 +01:00
} else {
2022-01-16 00:21:42 +01:00
newSeq, _ = sequence.Subsequence(from, sequence.Length(), false)
newSeq.Write(sequence.Sequence()[0:to])
if sequence.HasQualities() {
newSeq.WriteQualities(sequence.Qualities()[0:to])
}
2022-01-13 23:27:39 +01:00
}
if len(sequence.Annotations()) > 0 {
newSeq.annotations = GetAnnotation(sequence.Annotations())
2022-01-13 23:27:39 +01:00
}
2022-01-16 00:21:42 +01:00
return newSeq, nil
2022-01-13 23:27:39 +01:00
}