-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathseq.go
218 lines (191 loc) · 5.74 KB
/
seq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package mica
import (
"fmt"
"log"
"strings"
"sync"
"github.com/TuftsBCB/seq"
)
// SeqIdentity computes the Sequence identity of two byte slices.
// The number returned is an integer in the range 0-100, inclusive.
// SeqIdentity returns zero if the lengths of both seq1 and seq2 are zero.
//
// If the lengths of seq1 and seq2 are not equal, SeqIdentity will panic.
func SeqIdentity(seq1, seq2 []byte) int {
if len(seq1) != len(seq2) {
log.Panicf("Sequence identity requires that len(seq1) == len(seq2), "+
"but %d != %d.", len(seq1), len(seq2))
}
if len(seq1) == 0 && len(seq2) == 0 {
return 0
}
same := 0
for i, r1 := range seq1 {
if r1 == seq2[i] {
same++
}
}
return (same * 100) / len(seq1)
}
// IsLowComplexity detects whether the residue at the given offset is in
// a region of low complexity, where low complexity is defined as a window
// where every residue is the same (no variation in composition).
func IsLowComplexity(residues []byte, offset, window int) bool {
repeats := 1
last := byte(0)
start := max(0, offset-window)
end := min(len(residues), offset+window)
for i := start; i < end; i++ {
if residues[i] == last {
repeats++
if repeats >= window {
return true
}
continue
}
// The last residue isn't the same as this residue, so reset.
last = residues[i]
repeats = 1
}
return false
}
// repetitive returns true if every byte in `bs` is the same.
func repetitive(bs []byte) bool {
if len(bs) <= 1 {
return false
}
b1 := bs[0]
for _, b2 := range bs[1:] {
if b1 != b2 {
return false
}
}
return true
}
// Sequence is the underlying (i.e., embedded) type of reference and original
// Sequences used in cablast.
type Sequence struct {
Name string
Residues []byte
Offset uint
Id int
}
// newSeq creates a new Sequence and upper cases the given residues.
func newSeq(id int, name string, residues []byte) *Sequence {
residuesStr := strings.ToUpper(string(residues))
residuesStr = strings.Replace(residuesStr, "*", "", -1)
return &Sequence{
Name: name,
Residues: []byte(residuesStr),
Offset: 0,
Id: id,
}
}
// newFastaSeq creates a new *Sequence value from seq's Sequence type, and
// ensures that all residues in the Sequence are upper cased.
func newFastaSeq(id int, s seq.Sequence) *Sequence {
return newSeq(id, s.Name, s.Bytes())
}
// newSubSequence returns a new *Sequence value that corresponds to a
// subSequence of 'Sequence'. 'start' and 'end' specify an inclusive range in
// 'Sequence'. newSubSequence panics if the range is invalid.
func (seq *Sequence) newSubSequence(start, end uint) *Sequence {
if start < 0 || start >= end || end > uint(seq.Len()) {
panic(fmt.Sprintf("Invalid sub Sequence (%d, %d) for Sequence "+
"with length %d.", start, end, seq.Len()))
}
s := newSeq(seq.Id, seq.Name, seq.Residues[start:end])
s.Offset += start
return s
}
// FastaSeq returns a new seq.Sequence from TuftsBCB/seq.
func (s *Sequence) FastaSeq() seq.Sequence {
rs := make([]seq.Residue, len(s.Residues))
for i := range s.Residues {
rs[i] = seq.Residue(s.Residues[i])
}
return seq.Sequence{s.Name, rs}
}
// Len retuns the number of residues in this Sequence.
func (seq *Sequence) Len() int {
return len(seq.Residues)
}
// String returns a string (fasta) representation of this Sequence. If this
// Sequence is a subSequence, then the range of the subSequence (with respect
// to the original Sequence) is also printed.
func (seq *Sequence) String() string {
if seq.Offset == 0 {
return fmt.Sprintf("> %s (%d)\n%s",
seq.Name, seq.Id, string(seq.Residues))
}
return fmt.Sprintf("> %s (%d) (%d, %d)\n%s",
seq.Name, seq.Id, seq.Offset, seq.Len(), string(seq.Residues))
}
// referenceSeq embeds a Sequence and serves as a typing mechanism to
// distguish reference Sequences in the compressed database with original
// Sequences from the input FASTA file.
type CoarseSeq struct {
*Sequence
Links *LinkToCompressed
linkLock *sync.RWMutex
}
func NewCoarseSeq(id int, name string, residues []byte) *CoarseSeq {
return &CoarseSeq{
Sequence: newSeq(id, name, residues),
Links: nil,
linkLock: &sync.RWMutex{},
}
}
func NewFastaCoarseSeq(id int, s seq.Sequence) *CoarseSeq {
return NewCoarseSeq(id, s.Name, s.Bytes())
}
func (rseq *CoarseSeq) NewSubSequence(start, end uint) *CoarseSeq {
return &CoarseSeq{
Sequence: rseq.Sequence.newSubSequence(start, end),
Links: nil,
}
}
func (rseq *CoarseSeq) AddLink(link *LinkToCompressed) {
rseq.linkLock.Lock()
rseq.addLink(link)
rseq.linkLock.Unlock()
}
func (rseq *CoarseSeq) addLink(link *LinkToCompressed) {
if rseq.Links == nil {
rseq.Links = link
} else {
lk := rseq.Links
for ; lk.Next != nil; lk = lk.Next {
}
lk.Next = link
}
}
// OriginalSeq embeds a Sequence and serves as a typing mechanism to
// distguish reference Sequences in the compressed database with original
// Sequences from the input FASTA file.
type OriginalSeq struct {
*Sequence
}
func NewOriginalSeq(id int, name string, residues []byte) *OriginalSeq {
return &OriginalSeq{Sequence: newSeq(id, name, residues)}
}
func NewFastaOriginalSeq(id int, s seq.Sequence) *OriginalSeq {
return &OriginalSeq{Sequence: newFastaSeq(id, s)}
}
func (oseq *OriginalSeq) NewSubSequence(start, end uint) *OriginalSeq {
return &OriginalSeq{oseq.Sequence.newSubSequence(start, end)}
}
// ReducedSeq embeds a Sequence and serves as a typing mechanism to
// distguish reduced-alphabet (DNA) Sequences from amino acid Sequences.
type ReducedSeq struct {
*Sequence
}
//
func NewReducedSeq(oseq *OriginalSeq) *ReducedSeq {
return &ReducedSeq{Sequence: newSeq(oseq.Sequence.Id,
oseq.Sequence.Name,
Reduce(oseq.Sequence.Residues))}
}
func (rseq *ReducedSeq) NewSubSequence(start, end uint) *ReducedSeq {
return &ReducedSeq{rseq.Sequence.newSubSequence(start, end)}
}