/
alphabet.go
351 lines (300 loc) · 9.7 KB
/
alphabet.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// 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 alphabet describes biological sequence letters, including quality scores.
package alphabet
import (
"github.com/biogo/biogo/feat"
"errors"
"fmt"
"strings"
"unicode"
)
const (
CaseSensitive = true
)
// Package alphabet provides default Alphabets for DNA, RNA and Protein. These
// alphabets are case insensitive and for the non-redundant nucleic acid alphabets
// satisfy the condition that the index of a letter is equal to the bitwise-complement
// of the index of the base-complement, modulo 4.
var (
DNA = MustComplement(NewComplementor(
"acgt",
feat.DNA,
MustPair(NewPairing("acgtnxACGTNX-", "tgcanxTGCANX-")),
'-', 'n',
!CaseSensitive,
))
DNAgapped = MustComplement(NewComplementor(
"-acgt",
feat.DNA,
MustPair(NewPairing("acgtnxACGTNX-", "tgcanxTGCANX-")),
'-', 'n',
!CaseSensitive,
))
DNAredundant = MustComplement(NewComplementor(
"-acmgrsvtwyhkdbn",
feat.DNA,
MustPair(NewPairing("acmgrsvtwyhkdbnxACMGRSVTWYHKDBNX-", "tgkcysbawrdmhvnxTGKCYSBAWRDMHVNX-")),
'-', 'n',
!CaseSensitive,
))
RNA = MustComplement(NewComplementor(
"acgu",
feat.RNA,
MustPair(NewPairing("acgunxACGUNX-", "ugcanxUGCANX-")),
'-', 'n',
!CaseSensitive,
))
RNAgapped = MustComplement(NewComplementor(
"-acgu",
feat.RNA,
MustPair(NewPairing("acgunxACGUNX-", "ugcanxUGCANX-")),
'-', 'n',
!CaseSensitive,
))
RNAredundant = MustComplement(NewComplementor(
"-acmgrsvuwyhkdbn",
feat.RNA,
MustPair(NewPairing("acmgrsvuwyhkdbnxACMGRSVUWYHKDBNX-", "ugkcysbawrdmhvnxUGKCYSBAWRDMHVNX-")),
'-', 'n',
!CaseSensitive,
))
Protein = Must(NewAlphabet(
"-abcdefghijklmnpqrstvwxyz*",
feat.Protein,
'-', 'x',
!CaseSensitive,
))
)
// Must is a helper that wraps a call to a function returning (Alphabet, error)
// and panics if the error is non-nil. It is intended for use in variable
// initializations.
func Must(a Alphabet, err error) Alphabet {
if err != nil {
panic(err)
}
return a
}
// MustComplement is a helper that wraps a call to a function returning (Complementor, error)
// and panics if the error is non-nil. It is intended for use in variable
// initializations.
func MustComplement(c Complementor, err error) Complementor {
if err != nil {
panic(err)
}
return c
}
// MustPair is a helper that wraps a call to a function returning (*Pairing, error)
// and panics if the error is non-nil. It is intended for use in variable
// initializations.
func MustPair(p *Pairing, err error) *Pairing {
if err != nil {
panic(err)
}
return p
}
// Type Index is a pointer to an index table.
type Index *[256]int
// An Alphabet describes valid single character letters within a sequence.
type Alphabet interface {
// IsValid reports whether a letter conforms to the alphabet.
IsValid(Letter) bool
// AllValid reports whether a slice of bytes conforms to the alphabet.
// It returns the index of the first invalid byte,
// or a negative int if all bytes are valid.
AllValid([]Letter) (ok bool, pos int)
// AllValidQLetter reports whether a slice of bytes conforms to the alphabet.
// It returns the index of the first invalid byte,
// or a negative int if all bytes are valid.
AllValidQLetter([]QLetter) (ok bool, pos int)
// Len returns the number of distinct valid letters in the alphabet.
Len() int
// IndexOf returns the index of a given letter.
IndexOf(Letter) int
// Letter returns the letter corresponding to the given index.
Letter(int) Letter
// LetterIndex returns a pointer to the internal array specifying
// letter to index conversion. The returned index should not be altered.
LetterIndex() Index
// Letters returns a string of letters conforming to the alphabet in index
// order. In case insensitive alphabets, both cases are presented.
Letters() string
// ValidLetters returns a slice of the internal []bool indicating valid
// letters. The returned slice should not be altered.
ValidLetters() []bool
// Gap returns the gap character used by the alphabet.
Gap() Letter
// Ambiguous returns the character representing an ambiguous letter.
Ambiguous() Letter
// Moltype returns the molecule type of the alphabet.
Moltype() feat.Moltype
// IsCased returns whether the alphabet is case sensitive.
IsCased() bool
}
// A Complementor is an Alphabet that describes the complementation relationships
// between letters.
type Complementor interface {
Alphabet
Complement(Letter) (Letter, bool)
ComplementTable() []Letter
}
// Single letter alphabet type.
type alpha struct {
letters string
length int
valid [256]bool
index [256]int
gap, ambiguous Letter
caseSensitive bool
molType feat.Moltype
}
func newAlphabet(letters string, molType feat.Moltype, gap, ambiguous Letter, caseSensitive bool) (*alpha, error) {
if strings.IndexFunc(letters, func(r rune) bool { return r < 0 || r > unicode.MaxASCII }) > -1 {
return nil, errors.New("alphabet: letters contains non-ASCII rune")
}
a := &alpha{
length: len(letters),
gap: gap,
ambiguous: ambiguous,
caseSensitive: caseSensitive,
molType: molType,
}
for i := range a.index {
a.index[i] = -1
}
if caseSensitive {
a.letters = letters
for i, l := range a.letters {
a.valid[l] = true
a.index[l] = i
}
return a, nil
}
a.letters = strings.ToLower(letters) + strings.ToUpper(letters)
for i, l := range a.letters[:len(letters)] {
a.valid[l] = true
a.index[l] = i
}
for i, l := range a.letters[len(letters):] {
a.valid[l] = true
a.index[l] = a.index[a.letters[i]]
}
return a, nil
}
func (a *alpha) Moltype() feat.Moltype { return a.molType }
func (a *alpha) Len() int { return a.length }
func (a *alpha) IsCased() bool { return a.caseSensitive }
func (a *alpha) Gap() Letter { return a.gap }
func (a *alpha) Ambiguous() Letter { return a.ambiguous }
func (a *alpha) AllValidQLetter(n []QLetter) (bool, int) {
for i, v := range n {
if !a.valid[v.L] {
return false, i
}
}
return true, -1
}
func (a *alpha) AllValid(n []Letter) (bool, int) {
for i, v := range n {
if !a.valid[v] {
return false, i
}
}
return true, -1
}
func (a *alpha) IsValid(n Letter) bool {
return a.valid[n]
}
func (a *alpha) Letter(i int) Letter {
return Letter(a.letters[:a.length][i])
}
func (a *alpha) IndexOf(n Letter) int {
return a.index[n]
}
func (a *alpha) ValidLetters() []bool { return a.valid[:] }
func (a *alpha) LetterIndex() Index { return Index(&a.index) }
func (a *alpha) Letters() string { return a.letters }
// A Pairing provides a lookup table between a letter and its complement.
type Pairing struct {
pair []Letter
ok []bool
complements [256]Letter
}
// NewPairing create a new Pairing from a pair of strings. Pairing definitions must be
// a bijection and must contain only ASCII characters.
func NewPairing(s, c string) (*Pairing, error) {
if len(s) != len(c) {
return nil, errors.New("alphabet: length of pairing definitions do not match")
}
p := &Pairing{
pair: make([]Letter, 256),
ok: make([]bool, 256),
}
for i := range p.pair {
p.pair[i] = Letter(i)
}
cr := []rune(c)
for i, v := range s {
if v < 0 || cr[i] < 0 || v > unicode.MaxASCII || cr[i] > unicode.MaxASCII {
return nil, errors.New("alphabet: pairing definition contains non-ASCII rune")
}
p.pair[v] = Letter(cr[i])
p.ok[v] = true
}
for i, l := range s {
if Letter(l) != p.pair[p.pair[l]] {
return nil, errors.New("alphabet: pairing definition is not a bijection")
}
if Letter(c[i]) != p.pair[p.pair[c[i]]] {
return nil, errors.New("alphabet: pairing definition is not a bijection")
}
}
copy(p.complements[:], p.pair)
for i, ok := range p.ok {
if !ok {
p.complements[i] |= unicode.MaxASCII + 1
}
}
return p, nil
}
// Returns the complement of a letter and true if the complement is a valid letter otherwise unchanged and false.
func (p *Pairing) Complement(l Letter) (c Letter, ok bool) { return p.pair[l], p.ok[l] }
// Returns a complementation table based on the internal representation. Invalid pairs hold a value outside the ASCII range.
// The caller must not modify the returned table.
func (p *Pairing) ComplementTable() []Letter {
return p.complements[:]
}
type nucleic struct {
*alpha
*Pairing
}
// NewComplementor returns a complementing alphabet. The Complement table is checked for
// validity and an error is returned if an invalid complement pair is found. Pairings
// that result in no change but would otherwise be invalid are allowed. Letter parameter
// handling is the same as for NewAlphabet.
func NewComplementor(letters string, molType feat.Moltype, pairs *Pairing, gap, ambiguous Letter, caseSensitive bool) (Complementor, error) {
a, err := newAlphabet(letters, molType, gap, ambiguous, caseSensitive)
if err != nil {
return nil, err
}
if pairs != nil {
for i, v := range pairs.pair {
if !(pairs.ok[i] || Letter(i&unicode.MaxASCII) == v&unicode.MaxASCII) && !(a.valid[i] && a.valid[v]) {
return nil, fmt.Errorf("alphabet: invalid pairing: %c (%d) -> %c (%d)", i, i, v, v)
}
}
}
return &nucleic{
alpha: a,
Pairing: pairs,
}, nil
}
// NewAlphabet returns a new Alphabet based on the provided definitions. Index values
// for letters reflect order of the letters parameter. Letters must be within the
// ASCII range. No check is performed to determine whether letters appear more than once,
// the index of a letter will be the position of the last occurrence of that letter in the
// letters parameter.
func NewAlphabet(letters string, molType feat.Moltype, gap, ambiguous Letter, caseSensitive bool) (Alphabet, error) {
return newAlphabet(letters, molType, gap, ambiguous, caseSensitive)
}