-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindelligent_test.go
More file actions
285 lines (247 loc) · 8.57 KB
/
indelligent_test.go
File metadata and controls
285 lines (247 loc) · 8.57 KB
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
package indelligent_test
import (
"encoding/json"
"strings"
"testing"
"github.com/SpeciesFileGroup/indelligent"
"github.com/SpeciesFileGroup/indelligent/ent/result"
"github.com/stretchr/testify/assert"
)
func newTestInd(opts ...indelligent.Option) indelligent.Indelligent {
base := []indelligent.Option{indelligent.OptIsTest(true)}
return indelligent.New(indelligent.NewConfig(append(base, opts...)...))
}
// --- Full pipeline integration tests ---
func TestAnalyzeEmpty(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("")
assert.False(t, r.Parsed)
}
func TestAnalyzeInvalid(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("12345")
assert.False(t, r.Parsed)
}
func TestAnalyzeUnambiguous(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("ACGT")
assert.True(t, r.Parsed)
assert.Equal(t, "ACGT", r.Allele1)
assert.Equal(t, "ACGT", r.Allele2)
assert.Equal(t, 4, r.Stats.Length)
assert.Equal(t, 0, r.Stats.Ambiguities)
assert.Equal(t, 0, r.Stats.Mismatches)
assert.Equal(t, 0, r.Stats.InputAmbiguities)
}
func TestAnalyzeTKGKKSCMW(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("TKGKKSCMW")
assert.True(t, r.Parsed)
assert.Equal(t, 9, r.Stats.Length)
assert.Greater(t, r.Stats.InputAmbiguities, 0)
assert.Equal(t, 9, len(r.Allele1))
assert.Equal(t, 9, len(r.Allele2))
assert.Greater(t, len(r.Combined), 0)
assert.Greater(t, r.ElapsedSec, 0.0)
assert.Equal(t, "test_version", r.Version)
}
func TestAnalyzeAmbiguousReduces(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("RYSMKW")
assert.True(t, r.Parsed)
// With 6 ambiguous positions, resolver should resolve at least some.
assert.Equal(t, 6, r.Stats.InputAmbiguities)
// Check allele characters are valid IUPAC.
for i := 0; i < len(r.Allele1); i++ {
ch := r.Allele1[i]
assert.True(t, ch == 'A' || ch == 'C' || ch == 'G' || ch == 'T' ||
ch == 'R' || ch == 'Y' || ch == 'S' || ch == 'W' || ch == 'M' || ch == 'K' ||
ch == 'B' || ch == 'D' || ch == 'H' || ch == 'V' || ch == 'N',
"allele1[%d]=%c should be valid IUPAC", i, ch)
}
}
func TestAnalyzeScaledDown(t *testing.T) {
// Input length=4, maxShift=15. N1 should be 2 (4/2), which is < 15.
ind := newTestInd()
r := ind.Analyze("ACGT")
assert.True(t, r.ScaledDown)
}
func TestAnalyzeNotScaledDown(t *testing.T) {
// Input length=40 (20 ambig characters), maxShift=15. N1=15, not scaled.
seq := strings.Repeat("RYSMKW", 7) // 42 chars
ind := newTestInd()
r := ind.Analyze(seq)
assert.True(t, r.Parsed)
assert.False(t, r.ScaledDown)
}
func TestAnalyzePercentageTruncation(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("ACGTACGT")
// 8 positions, all resolved, 0 mismatches.
assert.Equal(t, 100.0, r.Stats.ResolvedPct)
assert.Equal(t, 0.0, r.Stats.MismatchPct)
assert.Equal(t, 0.0, r.Stats.AmbiguityPct)
}
func TestAnalyzeLongIndels(t *testing.T) {
ind := newTestInd(indelligent.OptLongIndels(true))
r := ind.Analyze("TKGKKSCMW")
assert.True(t, r.Parsed)
}
func TestAnalyzeNoAlign(t *testing.T) {
ind := newTestInd(indelligent.OptAlignAlleles(false))
r := ind.Analyze("TKGKKSCMW")
assert.True(t, r.Parsed)
// Without alignment, aligned equals raw.
assert.Equal(t, r.Allele1, r.Aligned1)
assert.Equal(t, r.Allele2, r.Aligned2)
}
func TestAnalyzeManyOrdered(t *testing.T) {
ind := newTestInd(indelligent.OptJobsNum(2))
seqs := []string{"ACGT", "RYSMKW", "TKGKKSCMW", ""}
res := ind.AnalyzeMany(seqs)
assert.Equal(t, 4, len(res))
assert.True(t, res[0].Parsed)
assert.Equal(t, "ACGT", res[0].Allele1)
assert.True(t, res[1].Parsed)
assert.Equal(t, 6, res[1].Stats.Length)
assert.True(t, res[2].Parsed)
assert.Equal(t, 9, res[2].Stats.Length)
assert.False(t, res[3].Parsed)
}
func TestGetVersion(t *testing.T) {
ind := newTestInd()
v := ind.GetVersion()
assert.Equal(t, "test_version", v.Version)
}
func TestChangeConfig(t *testing.T) {
ind := newTestInd()
ind2 := ind.ChangeConfig(indelligent.OptMaxShift(25))
r := ind2.Analyze("ACGT")
assert.True(t, r.Parsed)
}
// --- Output format tests ---
func TestOutputFASTA(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("ACGT")
out := r.Output(result.FormatFASTA, false, false)
assert.Contains(t, out, ">allele1")
assert.Contains(t, out, ">allele2")
assert.Contains(t, out, "ACGT")
}
func TestOutputFASTADetails(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("TKGKKSCMW")
out := r.Output(result.FormatFASTA, true, false)
assert.Contains(t, out, "; length=9")
assert.Contains(t, out, "; resolved=")
}
func TestOutputFASTAUnparsed(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("")
out := r.Output(result.FormatFASTA, false, false)
assert.Contains(t, out, ">unparsed")
}
func TestOutputCompactJSON(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("ACGT")
out := r.Output(result.FormatCompact, false, false)
// Should be valid JSON on a single line.
assert.False(t, strings.Contains(out, "\n"))
var parsed result.Result
err := json.Unmarshal([]byte(out), &parsed)
assert.NoError(t, err)
assert.True(t, parsed.Parsed)
assert.Equal(t, "ACGT", parsed.Allele1)
}
func TestOutputPrettyJSON(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("ACGT")
out := r.Output(result.FormatPretty, false, false)
// Should be multi-line (indented).
assert.True(t, strings.Contains(out, "\n"))
var parsed result.Result
err := json.Unmarshal([]byte(out), &parsed)
assert.NoError(t, err)
assert.True(t, parsed.Parsed)
}
func TestOutputTSV(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("ACGT")
out := r.Output(result.FormatTSV, false, false)
fields := strings.Split(out, "\t")
assert.Equal(t, 8, len(fields), "basic TSV should have 8 fields")
assert.Equal(t, "ACGT", fields[0]) // Input
assert.Equal(t, "true", fields[1]) // Parsed
}
func TestOutputTSVDetailed(t *testing.T) {
ind := newTestInd()
r := ind.Analyze("TKGKKSCMW")
out := r.Output(result.FormatTSV, true, false)
fields := strings.Split(out, "\t")
assert.Equal(t, 20, len(fields), "detailed TSV should have 20 fields")
}
func TestHeaderTSV(t *testing.T) {
h := result.HeaderTSV(false)
assert.Contains(t, h, "Input")
assert.Contains(t, h, "ResolvedPct")
hd := result.HeaderTSV(true)
assert.Contains(t, hd, "FloatingIndel")
assert.Contains(t, hd, "Shifts")
}
// --- ASP cross-validation tests ---
// These verify the Go port produces identical output to the original
// ASP/VBScript tool for known simulated sequences.
func TestAnalyzeASPSimulated105bp(t *testing.T) {
// Simulated from original ASP tool: two alleles with multiple indels.
// Generated allele 1: TCTACTCCGCATTCTTCAAGAAAATAATCAGAGCTAAGAGACTTGCGCATTATTATGTCGTCTGGTATAGCCTATCAGGAACTCGTAGTGCCATTCATAGTACTG
// Generated allele 2: AATACTCTACTCCGCACTTTTCAAGAAAACAATCAGCGGAAAGACACTTGGGCTCCACATATTTCCGCATTCTCATGACGTGTCGTCTAGCCTATTAGCATCTCG
ind := newTestInd()
r := ind.Analyze("WMTACTCYRCWYYSYWCWWKWMAAKAAWMMRAKCWRMGRRAMKWSMSYWKKRYTMYRYMKWYTKSYRYAKYCTMWYRRSRWSTCGTMKWGCCWWTYAKMRTMYYG")
assert.True(t, r.Parsed)
// Aligned alleles.
assert.Equal(t,
".....TCTACTCCG.......CAYTGCTCAAGTAAAGAAWACGAGCWACG.......GGAATTGCCTWTTRCTMYRCCGATTTSTA.TAGCCTCTCGASGWGTCGTMTWGCCTATCATARTCCTG",
r.Aligned1)
assert.Equal(t,
"AATACTCTACTYCCTACTTTACAATAAWCAA.......ATCWGAGAAACGACAGCWGGRTTMYR.....TATTCTGSCGCATTCTAATAGSAWCTCGTMGWGCCATTTAGCRTATCG........",
r.Aligned2)
// Combined consensus.
assert.Equal(t,
"AATACTCTACTYCSTACTTTACAHTRMWCAAGTAAAGAAWMHGAGMWACGACAGCWGGRWWHYRCCTWTTRYTMYRSCGMWTTSTAATAGSMWCTCGWVGWGYCRTHTWGCVTATCRTARTCCTG",
r.Combined)
// Statistics.
assert.Equal(t, 105, r.Stats.Length)
assert.Equal(t, 65, r.Stats.ResolvedPositions)
assert.Equal(t, 61.9, r.Stats.ResolvedPct)
assert.Equal(t, 20, r.Stats.Mismatches)
assert.Equal(t, 19.04, r.Stats.MismatchPct)
assert.Equal(t, 41, r.Stats.Ambiguities)
assert.Equal(t, 39.04, r.Stats.AmbiguityPct)
assert.Equal(t, 44.59, r.Stats.ResolvedAmbigPct)
assert.Equal(t, 72.45, r.Stats.MaxExpectedByChance)
assert.Equal(t, 74, r.Stats.InputAmbiguities)
// Floating indel.
assert.True(t, r.FloatingIndel)
// Phase shifts (sorted by size ascending).
assert.Equal(t, 4, len(r.PhaseShifts))
assert.Equal(t, result.ShiftInfo{Size: 5, Positions: 16}, r.PhaseShifts[0])
assert.Equal(t, result.ShiftInfo{Size: 7, Positions: 10}, r.PhaseShifts[1])
assert.Equal(t, result.ShiftInfo{Size: 8, Positions: 38}, r.PhaseShifts[2])
assert.Equal(t, result.ShiftInfo{Size: 12, Positions: 41}, r.PhaseShifts[3])
}
// --- Benchmark ---
func BenchmarkAnalyze(b *testing.B) {
ind := newTestInd()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ind.Analyze("TKGKKSCMW")
}
}
func BenchmarkAnalyzeLong(b *testing.B) {
seq := strings.Repeat("RYSMKWACGT", 10) // 100 chars
ind := newTestInd()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ind.Analyze(seq)
}
}