Test numpy annotations.
[lightning.git] / exportnumpy_test.go
1 package lightning
2
3 import (
4         "bytes"
5         "io/ioutil"
6         "os"
7
8         "github.com/kshedden/gonpy"
9         "gopkg.in/check.v1"
10 )
11
12 type exportSuite struct{}
13
14 var _ = check.Suite(&exportSuite{})
15
16 func (s *exportSuite) TestFastaToNumpy(c *check.C) {
17         tmpdir := c.MkDir()
18
19         var buffer bytes.Buffer
20         exited := (&importer{}).RunCommand("import", []string{"-local=true", "-tag-library", "testdata/tags", "-output-tiles", "-save-incomplete-tiles", "testdata/a.1.fasta", "testdata/tinyref.fasta"}, &bytes.Buffer{}, &buffer, os.Stderr)
21         c.Assert(exited, check.Equals, 0)
22         var output bytes.Buffer
23         exited = (&exportNumpy{}).RunCommand("export-numpy", []string{"-local=true", "-output-annotations", tmpdir + "/annotations.csv"}, &buffer, &output, os.Stderr)
24         c.Check(exited, check.Equals, 0)
25         npy, err := gonpy.NewReader(&output)
26         c.Assert(err, check.IsNil)
27         variants, err := npy.GetInt16()
28         c.Assert(err, check.IsNil)
29         for i := 0; i < 4; i += 2 {
30                 if variants[i] == 1 {
31                         c.Check(variants[i+1], check.Equals, int16(2), check.Commentf("i=%d, v=%v", i, variants))
32                 } else {
33                         c.Check(variants[i], check.Equals, int16(2), check.Commentf("i=%d, v=%v", i, variants))
34                 }
35         }
36         for i := 4; i < 9; i += 2 {
37                 c.Check(variants[i], check.Equals, int16(1), check.Commentf("i=%d, v=%v", i, variants))
38         }
39         annotations, err := ioutil.ReadFile(tmpdir + "/annotations.csv")
40         c.Check(err, check.IsNil)
41         c.Check(string(annotations), check.Matches, `(?ms).*1,2,chr1:g.84_85insACTGCGATCTGA\n.*`)
42         c.Check(string(annotations), check.Matches, `(?ms).*1,1,chr1:g.87_96delinsGCATCTGCA\n.*`)
43 }
44
45 func sortUints(variants []int16) {
46         for i := 0; i < len(variants); i += 2 {
47                 if variants[i] > variants[i+1] {
48                         for j := 0; j < len(variants); j++ {
49                                 variants[j], variants[j+1] = variants[j+1], variants[j]
50                         }
51                         return
52                 }
53         }
54 }
55
56 func (s *exportSuite) TestOnehot(c *check.C) {
57         for _, trial := range []struct {
58                 incols  int
59                 in      []int16
60                 outcols int
61                 out     []int16
62         }{
63                 {2, []int16{1, 1, 1, 1}, 2, []int16{1, 1, 1, 1}},
64                 {2, []int16{1, 1, 1, 2}, 3, []int16{1, 1, 0, 1, 0, 1}},
65                 {
66                         // 2nd column => 3 one-hot columns
67                         // 4th column => 0 one-hot columns
68                         4, []int16{
69                                 1, 1, 0, 0,
70                                 1, 2, 1, 0,
71                                 1, 3, 0, 0,
72                         }, 5, []int16{
73                                 1, 1, 0, 0, 0,
74                                 1, 0, 1, 0, 1,
75                                 1, 0, 0, 1, 0,
76                         },
77                 },
78         } {
79                 out, _, outcols := recodeOnehot(trial.in, trial.incols)
80                 c.Check(out, check.DeepEquals, trial.out)
81                 c.Check(outcols, check.Equals, trial.outcols)
82         }
83 }