File: gene_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 (476 lines) | stat: -rw-r--r-- 12,617 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright ©2015 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 gene

import (
	"github.com/biogo/biogo/feat"

	"testing"

	"gopkg.in/check.v1"
)

// Assert that interfaces are satisfied
var (
	_ feat.Feature = (*Gene)(nil)
	_ feat.Feature = (*NonCodingTranscript)(nil)
	_ feat.Feature = (*CodingTranscript)(nil)
	_ feat.Feature = (*Exon)(nil)
	_ feat.Feature = (*Intron)(nil)
	_ feat.Feature = (*TranscriptFeature)(nil)

	_ featureOrienter = (*Gene)(nil)
	_ featureOrienter = (*NonCodingTranscript)(nil)
	_ featureOrienter = (*CodingTranscript)(nil)
	_ featureOrienter = (*Exon)(nil)
	_ featureOrienter = (*Intron)(nil)
	_ featureOrienter = (*TranscriptFeature)(nil)

	_ Transcript = (*NonCodingTranscript)(nil)
	_ Transcript = (*CodingTranscript)(nil)

	_ Interface = (*Gene)(nil)
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { check.TestingT(t) }

// Create the test suite
type S struct{}

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

// Chr implements feat.Feature and is used as location for the test objects.
type Chr string

func (c Chr) Start() int             { return 0 }
func (c Chr) End() int               { return 0 }
func (c Chr) Len() int               { return 0 }
func (c Chr) Name() string           { return string(c) }
func (c Chr) Description() string    { return "chrom" }
func (c Chr) Location() feat.Feature { return nil }

// ori implements feat.Feature and is used as location for test objects.
type ori struct {
	start, end int
	name       string
	desc       string
	loc        feat.Feature
	orient     feat.Orientation
}

func (o ori) Name() string                  { return o.name }
func (o ori) Description() string           { return o.desc }
func (o ori) Start() int                    { return o.start }
func (o ori) End() int                      { return o.end }
func (o ori) Len() int                      { return o.end - o.start }
func (o ori) Location() feat.Feature        { return o.loc }
func (o ori) Orientation() feat.Orientation { return o.orient }

// Define some test objects that will be used in the actual tests
var (
	geneA = Gene{
		ID:     "geneA",
		Chrom:  Chr("Y"),
		Offset: 100,
		Orient: feat.Forward,
		Desc:   "forward gene",
	}
	geneB = Gene{
		ID:     "geneB",
		Chrom:  Chr("X"),
		Offset: 100,
		Orient: feat.Reverse,
		Desc:   "reverse gene",
	}
	geneC = Gene{
		ID: "geneC",
		Chrom: ori{
			start:  0,
			end:    800,
			orient: feat.Reverse,
			loc: ori{
				start:  0,
				end:    900,
				orient: feat.Forward,
				loc: ori{
					start:  0,
					end:    1000,
					orient: feat.Reverse,
				}}},
		Offset: 100,
		Orient: feat.Reverse,
		Desc:   "reverse gene on a contig on a supercontig on an ultra contig.",
	}
	codingTranscriptA = CodingTranscript{
		ID:       "codingTranscriptA",
		Loc:      Chr("Y"),
		Offset:   100,
		CDSstart: 100,
		CDSend:   600,
		Orient:   feat.Forward,
		Desc:     "forward transcript with cds",
	}
	codingTranscriptB = CodingTranscript{
		ID:       "codingTranscriptB",
		Loc:      Chr("X"),
		Offset:   500,
		CDSstart: 300,
		CDSend:   1300,
		Orient:   feat.Reverse,
		Desc:     "reverse transcript with cds",
	}
	codingTranscriptC = CodingTranscript{
		ID:       "codingTranscriptC",
		Loc:      &geneC,
		Offset:   20,
		CDSstart: 100,
		CDSend:   500,
		Orient:   feat.Forward,
		Desc:     "forward transcript with cds on reverse gene",
	}
	nonCodingTranscriptA = NonCodingTranscript{
		ID:     "nonCodingTranscriptA",
		Loc:    Chr("Y"),
		Offset: 100,
		Orient: feat.Forward,
		Desc:   "forward non coding transcript",
	}
	nonCodingTranscriptB = NonCodingTranscript{
		ID:     "nonCodingTranscriptB",
		Loc:    Chr("X"),
		Offset: 500,
		Orient: feat.Reverse,
		Desc:   "reverse non coding transcript",
	}
)

// Tests for Gene
var geneTests = []struct {
	Test        string
	Gene        Interface
	Name        string
	Chrom       string
	Start, End  int
	Len         int
	Orientation feat.Orientation
	Feats       []feat.Feature
	SetErr      string
	TransCount  int
}{
	{
		Test:        "forward gene with valid feats",
		Gene:        &geneA,
		Name:        "geneA",
		Chrom:       "Y",
		Start:       100,
		End:         120,
		Len:         20,
		Orientation: feat.Forward,
		Feats: []feat.Feature{
			&NonCodingTranscript{Loc: &geneA, exons: []Exon{{Length: 20}}},
			&NonCodingTranscript{Loc: &geneA},
		},
		TransCount: 2,
	},
	{
		Test:        "reverse gene with valid feats",
		Gene:        &geneB,
		Name:        "geneB",
		Chrom:       "X",
		Start:       100,
		End:         110,
		Len:         10,
		Orientation: feat.Reverse,
		Feats: []feat.Feature{
			&NonCodingTranscript{Loc: &geneB, exons: []Exon{{Length: 10}}},
			&NonCodingTranscript{Loc: &geneB},
		},
		TransCount: 2,
	},
	{
		Test:   "forward gene with feat on wrong location",
		Gene:   &geneA,
		Feats:  []feat.Feature{&NonCodingTranscript{Loc: &geneB}},
		SetErr: "transcript location does not match the gene",
	},
	{
		Test:   "reverse gene with no feat from 0",
		Gene:   &geneB,
		Feats:  []feat.Feature{&NonCodingTranscript{Loc: &geneB, Offset: 5}},
		SetErr: "no transcript with 0 start on gene",
	},
}

func (s *S) TestGene(c *check.C) {
	for _, d := range geneTests {
		g := d.Gene

		// Test SetFeatures
		if err := g.SetFeatures(d.Feats...); err != nil {
			c.Assert(err, check.ErrorMatches, d.SetErr)
		} else {
			c.Check(g.Name(), check.Equals, d.Name)
			c.Check(g.Start(), check.Equals, d.Start)
			c.Check(g.End(), check.Equals, d.End)
			c.Check(g.Len(), check.Equals, d.Len)
			c.Check(g.Location().Name(), check.Equals, d.Chrom)
			c.Check(g.Orientation(), check.Equals, d.Orientation)
			c.Check(len(TranscriptsOf(g)), check.Equals, d.TransCount)
		}
	}
}

// Tests for Transcript
var transcriptTests = []struct {
	Test               string
	Transcript         Transcript
	Name               string
	Loc                feat.Feature
	Start, End         int
	UTR5start, UTR5end int
	CDSstart, CDSend   int
	UTR3start, UTR3end int
	Len                int
	Orientation        feat.Orientation
	Exons              []Exon
	AddErr             string
	ExonicLen          int
}{
	{
		Test:        "forward transcript with cds and valid exons",
		Transcript:  &codingTranscriptA,
		Name:        "codingTranscriptA",
		Loc:         Chr("Y"),
		Orientation: feat.Forward,
		Exons: []Exon{
			{Transcript: &codingTranscriptA, Offset: 0, Length: 300},
			{Transcript: &codingTranscriptA, Offset: 600, Length: 200}},
		Start:     100,
		End:       900,
		UTR5start: 0,
		UTR5end:   100,
		CDSstart:  100,
		CDSend:    600,
		UTR3start: 600,
		UTR3end:   800,
		Len:       800,
		ExonicLen: 500,
	},
	{
		Test:        "reverse transcript with cds and valid exons",
		Transcript:  &codingTranscriptB,
		Name:        "codingTranscriptB",
		Loc:         Chr("X"),
		Orientation: feat.Reverse,
		Exons: []Exon{
			{Transcript: &codingTranscriptB, Offset: 0, Length: 600},
			{Transcript: &codingTranscriptB, Offset: 900, Length: 600}},
		Start:     500,
		End:       2000,
		UTR3start: 0,
		UTR3end:   300,
		CDSstart:  300,
		CDSend:    1300,
		UTR5start: 1300,
		UTR5end:   1500,
		Len:       1500,
		ExonicLen: 1200,
	},
	{
		Test:        "forward transcript with cds and valid exons on reverse gene on a contig on a supercontig on an ultra contig.",
		Transcript:  &codingTranscriptC,
		Name:        "codingTranscriptC",
		Loc:         &geneC,
		Orientation: feat.Forward,
		Exons: []Exon{
			{Transcript: &codingTranscriptC, Offset: 0, Length: 500},
			{Transcript: &codingTranscriptC, Offset: 600, Length: 100}},
		Start:     20,
		End:       720,
		UTR3start: 0,
		UTR3end:   100,
		CDSstart:  100,
		CDSend:    500,
		UTR5start: 500,
		UTR5end:   700,
		Len:       700,
		ExonicLen: 600,
	},
	{
		Test:        "forward non-coding transcript with valid exons",
		Transcript:  &nonCodingTranscriptA,
		Name:        "nonCodingTranscriptA",
		Loc:         Chr("Y"),
		Orientation: feat.Forward,
		Exons: []Exon{
			{Transcript: &nonCodingTranscriptA, Offset: 0, Length: 300},
			{Transcript: &nonCodingTranscriptA, Offset: 600, Length: 200}},
		Start:     100,
		End:       900,
		Len:       800,
		ExonicLen: 500,
	},
	{
		Test:        "reverse non-coding transcript without exon at 0",
		Transcript:  &nonCodingTranscriptB,
		Orientation: feat.Reverse,
		Exons:       []Exon{{Transcript: &nonCodingTranscriptB, Offset: 10}},
		AddErr:      "no exon with a zero start",
	},
	{
		Test:        "reverse non-coding transcript with wrong exon location",
		Transcript:  &nonCodingTranscriptB,
		Orientation: feat.Reverse,
		Exons:       []Exon{{Offset: 0, Length: 10000}},
		AddErr:      "exon location is not the transcript",
	},
}

func (s *S) TestTranscript(c *check.C) {
	for _, d := range transcriptTests {
		t := d.Transcript

		// Test SetExons
		if err := t.SetExons(d.Exons...); err != nil {
			c.Assert(err, check.ErrorMatches, d.AddErr)
		} else {
			t.Exons()[0].Offset = 1000000 // should have no effect on t

			c.Check(t.Name(), check.Equals, d.Name)
			c.Check(t.Start(), check.Equals, d.Start)
			c.Check(t.End(), check.Equals, d.End)
			c.Check(t.Len(), check.Equals, d.Len)
			c.Check(t.Location(), check.Equals, d.Loc)
			c.Check(t.Orientation(), check.Equals, d.Orientation)
			c.Check(t.Exons().SplicedLen(), check.Equals, d.ExonicLen)

			// Test CodingTranscript specifics
			if t, ok := t.(*CodingTranscript); ok {
				utr5, cds, utr3 := t.UTR5(), t.CDS(), t.UTR3()
				c.Check(utr5.Start(), check.Equals, d.UTR5start)
				c.Check(utr5.End(), check.Equals, d.UTR5end)
				c.Check(utr5.Location(), check.Equals, t)
				c.Check(cds.Start(), check.Equals, d.CDSstart)
				c.Check(cds.End(), check.Equals, d.CDSend)
				c.Check(cds.Location(), check.Equals, t)
				c.Check(utr3.Start(), check.Equals, d.UTR3start)
				c.Check(utr3.End(), check.Equals, d.UTR3end)
				c.Check(utr3.Location(), check.Equals, t)

				c.Check(t.CDSstart, check.Equals, d.CDSstart)
				c.Check(t.CDSend, check.Equals, d.CDSend)
				c.Check(t.UTR5start(), check.Equals, d.UTR5start)
				c.Check(t.UTR5end(), check.Equals, d.UTR5end)
				c.Check(t.UTR3start(), check.Equals, d.UTR3start)
				c.Check(t.UTR3end(), check.Equals, d.UTR3end)
			}
		}
	}
}

// Tests for Exon and Intron
type featureOrienter interface {
	feat.Orienter
	feat.Feature
}

var exonIntronTests = []struct {
	Test        string
	Feat        featureOrienter
	Start, End  int
	Len         int
	Transcript  feat.Feature
	Orientation feat.Orientation
}{
	{
		Test:        "Exon on transcript",
		Feat:        Exon{Offset: 200, Length: 200},
		Start:       200,
		End:         400,
		Len:         200,
		Orientation: feat.Forward,
	},
	{
		Test:        "Intron on transcript",
		Feat:        Intron{Offset: 300, Length: 500},
		Start:       300,
		End:         800,
		Len:         500,
		Orientation: feat.Forward,
	},
}

func (s *S) TestExonIntron(c *check.C) {
	for _, d := range exonIntronTests {
		e := d.Feat

		c.Check(e.Start(), check.Equals, d.Start)
		c.Check(e.End(), check.Equals, d.End)
		c.Check(e.Len(), check.Equals, d.Len)
		c.Check(e.Location(), check.DeepEquals, d.Transcript)
		c.Check(e.Orientation(), check.Equals, d.Orientation)
	}
}

// Tests for Exons
var exonsTests = []struct {
	Test                        string
	InputExons                  []Exon
	Location                    feat.Feature
	Start, End, Len, SplicedLen int
	AddErr                      string
	MadeIntrons                 Introns
}{
	{
		Test: "Exons not in order",
		InputExons: []Exon{
			{Offset: 300, Length: 100},
			{Offset: 0, Length: 100},
		},
		Start:      0,
		End:        400,
		Len:        2,
		SplicedLen: 200,
		MadeIntrons: Introns{
			Intron{Offset: 100, Length: 200},
		},
	},
	{
		Test: "Exons overlap",
		InputExons: []Exon{
			{Offset: 0, Length: 100},
			{Offset: 50, Length: 100},
		},
		AddErr: "exons overlap",
	},
	{
		Test: "Exons on different transcripts",
		InputExons: []Exon{
			{Transcript: &codingTranscriptA},
			{Transcript: &codingTranscriptB},
		},
		AddErr: "exons location differ",
	},
}

func (s *S) TestExons(c *check.C) {
	for _, d := range exonsTests {
		var e Exons
		ie := d.InputExons

		// Test SetExons
		if e, err := e.Add(ie...); err != nil {
			c.Assert(err, check.ErrorMatches, d.AddErr)
		} else {
			c.Check(e.Location(), check.DeepEquals, d.Location)
			c.Check(e.Start(), check.Equals, d.Start)
			c.Check(e.End(), check.Equals, d.End)
			c.Check(e.Len(), check.Equals, d.Len)
			c.Check(e.SplicedLen(), check.Equals, d.SplicedLen)
			c.Check(e.Introns(), check.DeepEquals, d.MadeIntrons)
		}
	}
}