/
qseq.go
266 lines (233 loc) · 6.88 KB
/
qseq.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright ©2011-2013 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package linear
import (
"github.com/biogo/biogo/alphabet"
"github.com/biogo/biogo/feat"
"github.com/biogo/biogo/seq"
"fmt"
"unicode"
)
// A QSeq is a basic linear sequence with Phred quality scores.
type QSeq struct {
seq.Annotation
Seq alphabet.QLetters
Threshold alphabet.Qphred // Threshold for returning valid letter.
QFilter seq.QFilter // How to represent below threshold letter.
Encode alphabet.Encoding
}
// Interface guarantees
var (
_ feat.Feature = (*QSeq)(nil)
_ seq.Sequence = (*QSeq)(nil)
_ seq.Scorer = (*QSeq)(nil)
)
// NewQSeq create a new QSeq with the given id, letter sequence, alphabet and quality encoding.
func NewQSeq(id string, ql []alphabet.QLetter, alpha alphabet.Alphabet, enc alphabet.Encoding) *QSeq {
return &QSeq{
Annotation: seq.Annotation{
ID: id,
Alpha: alpha,
Strand: seq.Plus,
},
Seq: append(alphabet.QLetters(nil), ql...),
Encode: enc,
Threshold: 3,
QFilter: seq.AmbigFilter,
}
}
// Append append Letters to the sequence, the DefaultQphred value is used for quality scores.
func (s *QSeq) AppendLetters(a ...alphabet.Letter) error {
l := s.Len()
s.Seq = append(s.Seq, make([]alphabet.QLetter, len(a))...)[:l]
for _, v := range a {
s.Seq = append(s.Seq, alphabet.QLetter{L: v, Q: seq.DefaultQphred})
}
return nil
}
// Append appends QLetters to the sequence.
func (s *QSeq) AppendQLetters(a ...alphabet.QLetter) error {
s.Seq = append(s.Seq, a...)
return nil
}
// Slice returns the sequence data as a alphabet.Slice.
func (s *QSeq) Slice() alphabet.Slice { return s.Seq }
// SetSlice sets the sequence data represented by the sequence. SetSlice will panic if sl
// is not a alphabet.QLetters.
func (s *QSeq) SetSlice(sl alphabet.Slice) { s.Seq = sl.(alphabet.QLetters) }
// At returns the letter at position pos.
func (s *QSeq) At(i int) alphabet.QLetter {
return s.Seq[i-s.Offset]
}
// QEncode encodes the quality at position pos to a letter based on the sequence encoding setting.
func (s *QSeq) QEncode(i int) byte {
return s.Seq[i-s.Offset].Q.Encode(s.Encode)
}
// Encoding returns the quality encoding scheme.
func (s *QSeq) Encoding() alphabet.Encoding { return s.Encode }
// SetEncoding sets the quality encoding scheme to e.
func (s *QSeq) SetEncoding(e alphabet.Encoding) error { s.Encode = e; return nil }
// EAt returns the probability of a sequence error at position pos.
func (s *QSeq) EAt(i int) float64 {
return s.Seq[i-s.Offset].Q.ProbE()
}
// Set sets the letter at position pos to l.
func (s *QSeq) Set(i int, l alphabet.QLetter) error {
s.Seq[i-s.Offset] = l
return nil
}
// SetE sets the quality at position pos to e to reflect the given p(Error).
func (s *QSeq) SetE(i int, e float64) error {
s.Seq[i-s.Offset].Q = alphabet.Ephred(e)
return nil
}
// Len returns the length of the sequence.
func (s *QSeq) Len() int { return len(s.Seq) }
// Start return the start position of the sequence in coordinates relative to the
// sequence location.
func (s *QSeq) Start() int { return s.Offset }
// End returns the end position of the sequence in coordinates relative to the
// sequence location.
func (s *QSeq) End() int { return s.Offset + s.Len() }
// Validate validates the letters of the sequence according to the sequence alphabet.
func (s *QSeq) Validate() (bool, int) {
for i, ql := range s.Seq {
if !s.Alpha.IsValid(ql.L) {
return false, i
}
}
return true, -1
}
// Clone returns a copy of the sequence.
func (s *QSeq) Clone() seq.Sequence {
c := *s
c.Seq = append([]alphabet.QLetter(nil), s.Seq...)
return &c
}
// New returns an empty *QSeq sequence with the same alphabet.
func (s *QSeq) New() seq.Sequence {
return &QSeq{Annotation: seq.Annotation{Alpha: s.Alpha}}
}
// RevComp reverse complements the sequence. RevComp will panic if the alphabet used by
// the receiver is not a Complementor.
func (s *QSeq) RevComp() {
l, comp := s.Seq, s.Alphabet().(alphabet.Complementor).ComplementTable()
i, j := 0, len(l)-1
for ; i < j; i, j = i+1, j-1 {
l[i].L, l[j].L = comp[l[j].L], comp[l[i].L]
l[i].Q, l[j].Q = l[j].Q, l[i].Q
}
if i == j {
l[i].L = comp[l[i].L]
}
s.Strand = -s.Strand
}
// Reverse reverses the order of letters in the the sequence without complementing them.
func (s *QSeq) Reverse() {
l := s.Seq
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i]
}
s.Strand = seq.None
}
// String returns a string representation of the sequence data only.
func (s *QSeq) String() string {
cs := make([]alphabet.Letter, 0, len(s.Seq))
for _, ql := range s.Seq {
cs = append(cs, s.QFilter(s.Alpha, s.Threshold, ql))
}
return alphabet.Letters(cs).String()
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
// Format is a support routine for fmt.Formatter. It accepts the formats 'v' and 's'
// (string), 'a' (fasta) and 'q' (fastq). String, fasta and fastq formats support
// truncated output via the verb's precision. Fasta format supports sequence line
// specification via the verb's width field. Fastq format supports optional inclusion
// of the '+' line descriptor line with the '+' flag. The 'v' verb supports the '#'
// flag for Go syntax output. The 's' and 'v' formats support the '-' flag for
// omission of the sequence name.
func (s *QSeq) Format(fs fmt.State, c rune) {
if s == nil {
fmt.Fprint(fs, "<nil>")
return
}
var (
w, wOk = fs.Width()
p, pOk = fs.Precision()
buf []alphabet.QLetter
)
if pOk {
buf = s.Seq[:min(p, len(s.Seq))]
} else {
buf = s.Seq
}
switch c {
case 'v':
if fs.Flag('#') {
fmt.Fprintf(fs, "&%#v", *s)
return
}
fallthrough
case 's':
if !fs.Flag('-') {
fmt.Fprintf(fs, "%q ", s.ID)
}
for _, ql := range buf {
fmt.Fprintf(fs, "%c", s.QFilter(s.Alpha, s.Threshold, ql))
}
if pOk && p < s.Len() {
fmt.Fprint(fs, "...")
}
case 'a':
s.formatDescLineTo(fs, '>')
for i, ql := range buf {
fmt.Fprintf(fs, "%c", s.QFilter(s.Alpha, s.Threshold, ql))
if wOk && i < s.Len()-1 && i%w == w-1 {
fmt.Fprintln(fs)
}
}
if pOk && p < s.Len() {
fmt.Fprint(fs, "...")
}
case 'q':
s.formatDescLineTo(fs, '@')
for _, ql := range buf {
fmt.Fprintf(fs, "%c", s.QFilter(s.Alpha, s.Threshold, ql))
}
if pOk && p < s.Len() {
fmt.Fprintln(fs, "...")
} else {
fmt.Fprintln(fs)
}
if fs.Flag('+') {
s.formatDescLineTo(fs, '+')
} else {
fmt.Fprintln(fs, "+")
}
for _, ql := range buf {
e := ql.Q.Encode(s.Encode)
if e >= unicode.MaxASCII {
e = unicode.MaxASCII - 1
}
fmt.Fprintf(fs, "%c", e)
}
if pOk && p < s.Len() {
fmt.Fprint(fs, "...")
}
default:
fmt.Fprintf(fs, "%%!%c(linear.QSeq=%.10s)", c, s)
}
}
func (s *QSeq) formatDescLineTo(fs fmt.State, p rune) {
fmt.Fprintf(fs, "%c%s", p, s.ID)
if s.Desc != "" {
fmt.Fprintf(fs, " %s", s.Desc)
}
fmt.Fprintln(fs)
}