File: alphabet_test.go

package info (click to toggle)
golang-github-biogo-biogo 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,332 kB
  • sloc: sh: 282; makefile: 2
file content (187 lines) | stat: -rw-r--r-- 4,225 bytes parent folder | download | duplicates (2)
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
// Copyright ©2011-2012 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

import (
	"strings"
	"testing"
	"unicode"

	"gopkg.in/check.v1"
)

// Tests
func Test(t *testing.T) { check.TestingT(t) }

type S struct{}

var _ = check.Suite(&S{})

func (s *S) TestInterfaces(c *check.C) {
	var (
		alpha Alphabet
		comp  Complementor
	)

	for _, a := range []interface{}{DNA, RNA, Protein} {
		c.Check(a, check.Implements, &alpha)
	}

	for _, a := range []interface{}{DNA, DNAredundant, RNA, RNAredundant} {
		c.Check(a, check.Implements, &comp)
	}

	c.Check(Protein, check.Not(check.Implements), &comp)
}

type testAlphabets struct {
	alphabet Alphabet
	letters  string
}

func (s *S) TestIsValid(c *check.C) {
	for _, t := range []testAlphabets{
		{DNA, "acgt"},
		{DNAgapped, "-acgt"},
		{RNA, "acgu"},
		{RNAgapped, "-acgu"},
		{Protein, "-abcdefghijklmnpqrstvwxyz*"},
	} {
		for i := 0; i < 256; i++ {
			c.Check(t.alphabet.IsValid(Letter(i)), check.Equals, strings.ContainsRune(t.letters, unicode.ToUpper(rune(i))) || strings.ContainsRune(t.letters, unicode.ToLower(rune(i))))
		}
	}
}

func uc(l Letter) Letter {
	return Letter(unicode.ToUpper(rune(l)))
}

func (s *S) TestLetter(c *check.C) {
	for _, t := range []Alphabet{
		DNA,
		RNA,
		Protein,
	} {
		for i := 0; i < t.Len(); i++ {
			c.Check(t.IndexOf(t.Letter(i)), check.Equals, i,
				check.Commentf("Index %d: %c == %d", i, t.Letter(i), t.IndexOf(t.Letter(i))))
			c.Check(t.IndexOf(uc(t.Letter(i))), check.Equals, i,
				check.Commentf("Index %d: %c == %d", i, uc(t.Letter(i)), t.IndexOf(uc(t.Letter(i)))))
		}
	}
}

func (s *S) TestComplement(c *check.C) {
	for _, t := range []Complementor{
		DNA,
		RNA,
	} {
		for i := 0; i < 256; i++ {
			if sc, ok := t.Complement(Letter(i)); ok {
				dc, ok := t.Complement(sc)
				c.Check(ok, check.Equals, true)
				c.Check(dc, check.Equals, Letter(i))
			}
		}
	}
}

func (s *S) TestComplementDirect(c *check.C) {
	for _, t := range []Complementor{
		DNA,
		RNA,
	} {
		complement := t.ComplementTable()
		for i := 0; i < 256; i++ {
			if sc := complement[i]; sc <= unicode.MaxASCII {
				dc := complement[sc]
				c.Check(dc <= unicode.MaxASCII, check.Equals, true)
				c.Check(dc, check.Equals, Letter(i))
			} else {
				c.Check(sc&unicode.MaxASCII, check.Equals, Letter(i&unicode.MaxASCII))
			}
		}
	}
}

func (s *S) TestLetters(c *check.C) {
	for _, t := range []testAlphabets{
		{DNA, "acgtACGT"},
		{DNA, "acgtACGT"},
		{RNAgapped, "-acgu-ACGU"},
		{RNAgapped, "-acgu-ACGU"},
		{Protein, "-abcdefghijklmnpqrstvwxyz*-ABCDEFGHIJKLMNPQRSTVWXYZ*"},
	} {
		c.Check(t.alphabet.Letters(), check.Equals, t.letters)
	}
}

func (s *S) TestRangeCheck(c *check.C) {
	var err error
	_, err = newAlphabet(string([]rune{256}), 0, 0, 0, !CaseSensitive)
	c.Check(err, check.Not(check.IsNil))
	_, err = newAlphabet(string([]rune{0}), 0, 0, 0, !CaseSensitive)
	c.Check(err, check.IsNil)
	_, err = newAlphabet(string([]rune{127}), 0, 0, 0, !CaseSensitive)
	c.Check(err, check.IsNil)
	_, err = newAlphabet(string([]rune{-1}), 0, 0, 0, !CaseSensitive)
	c.Check(err, check.Not(check.IsNil))
}

func BenchmarkIsValid(b *testing.B) {
	g, _ := newAlphabet("-abcdefghijklmnpqrstvwxyz*", 0, 0, 0, !CaseSensitive)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		g.IsValid(Letter(i))
	}
}

func BenchmarkIsValidProtein(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Protein.IsValid(Letter(i))
	}
}

func BenchmarkIsValidDNA(b *testing.B) {
	for i := 0; i < b.N; i++ {
		DNA.IsValid(Letter(i))
	}
}

func BenchmarkIsValidDNADirect(b *testing.B) {
	valid := DNA.ValidLetters()
	for i := 0; i < b.N; i++ {
		_ = valid[byte(i)]
	}
}

func BenchmarkIndexDNA(b *testing.B) {
	for i := 0; i < b.N; i++ {
		DNA.IndexOf(Letter(i))
	}
}

func BenchmarkIndexDNADirect(b *testing.B) {
	index := DNA.LetterIndex()
	for i := 0; i < b.N; i++ {
		_ = index[byte(i)]
	}
}

func BenchmarkComplementDNA(b *testing.B) {
	for i := 0; i < b.N; i++ {
		DNA.Complement(Letter(i))
	}
}

func BenchmarkComplementDNADirect(b *testing.B) {
	comp := DNA.ComplementTable()
	var c Letter
	for i := 0; i < b.N; i++ {
		if c = comp[Letter(i)]; c != 0x80 {
		}
	}
}