-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfasta_test.go
154 lines (150 loc) · 3.23 KB
/
fasta_test.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
package fasta
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strconv"
"testing"
)
func TestEquals(t *testing.T) {
d1 := []byte("ACCGT")
s1 := NewSequence("s1", d1)
s2 := NewSequence("s1", d1)
if !(s1.Equals(s2) && s2.Equals(s1)) {
t.Error("equal sequences declared unequal")
}
s3 := NewSequence("s3", d1)
if s1.Equals(s3) || s3.Equals(s1) {
t.Error("sequences with unequal headers declared equal")
}
d2 := []byte("ACGGT")
s4 := NewSequence("s1", d2)
if s1.Equals(s4) || s4.Equals(s1) {
t.Error("sequences with unequal data declared equal")
}
}
func TestString(t *testing.T) {
seq := NewSequence("seq", []byte("ACCGT"))
ll := []int{4, 5, 10, 0}
for _, l := range ll {
seq.SetLineLength(l)
f, _ := ioutil.TempFile("", "test_*")
fmt.Fprintf(f, "%s\n", seq)
f.Close()
b1, _ := ioutil.ReadFile(f.Name())
b2 := bytes.Split(b1, []byte("\n"))
os.Remove(f.Name())
h := string(b2[0][1:])
if h != seq.Header() {
t.Errorf("did not write header correctly; "+
"want %q; get %q\n", h, seq.Header())
}
var counter int
for i, s := range b2 {
if i < 1 {
continue
}
for _, c := range s {
d := seq.Data()
if c != d[counter] {
t.Error("did not write data correctly")
}
counter++
}
}
if l <= 5 && l > 0 {
if len(b2[1]) != l {
t.Errorf("did not format data correctly: "+
"want: %d; get: %d", l, len(b2[1]))
}
}
}
}
func TestShuffle(t *testing.T) {
orig := NewSequence("", []byte("ACCGT"))
shuf := []byte("GTACC")
r := rand.New(rand.NewSource(13))
orig.Shuffle(r)
if !bytes.Equal(orig.data, shuf) {
t.Errorf("want:\n%s\nget:\n%s\n",
string(shuf),
string(orig.data))
}
}
func TestReverse(t *testing.T) {
ori := NewSequence("", []byte("ACCGT"))
rev := []byte("TGCCA")
ori.Reverse()
if !bytes.Equal(ori.data, rev) {
t.Errorf("want:\n%s\nget:\n%s\n",
string(rev),
string(ori.data))
}
}
func TestReverseComplement(t *testing.T) {
ori := NewSequence("", []byte("ACCGT"))
rc := []byte("ACGGT")
ori.ReverseComplement()
if !bytes.Equal(ori.data, rc) {
t.Errorf("want:\n%s\nget:\n%s\n",
string(rc),
string(ori.data))
}
}
func TestScanner(t *testing.T) {
for i := 1; i <= 9; i++ {
name := "./data/seq" + strconv.Itoa(i) + ".fasta"
in, err := os.Open(name)
if err != nil {
t.Errorf("couldn't open %q\n", name)
}
out, _ := ioutil.TempFile(".", "test_*")
scanner := NewScanner(in)
var foundSequence = false
for scanner.ScanSequence() {
foundSequence = true
seq := scanner.Sequence()
fmt.Fprintf(out, "%s\n", seq)
}
in.Close()
out.Close()
if foundSequence {
id, _ := ioutil.ReadFile(name)
if i == 9 {
id = append(id, '\n')
}
od, _ := ioutil.ReadFile(out.Name())
if !bytes.Equal(id, od) {
t.Errorf("failed to reproduce %q\n", name)
}
}
os.Remove(out.Name())
}
}
func TestFlush(t *testing.T) {
f, _ := os.Open("data/seq8.fasta")
sc := NewScanner(f)
w := 5085
g := 0
for sc.ScanLine() {
g += len(sc.Line())
}
g += len(sc.Flush())
if g != w {
t.Errorf("get:\n%d\nwant:\n%d\n", g, w)
}
f.Close()
f, _ = os.Open("data/seq9.fasta")
sc = NewScanner(f)
g = 0
for sc.ScanLine() {
g += len(sc.Line())
}
g += len(sc.Flush())
if g != w {
t.Errorf("get:\n%d\nwant:\n%d\n", g, w)
}
f.Close()
}