Export selected regions.
[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         err := ioutil.WriteFile(tmpdir+"/chr1-12-100.bed", []byte("chr1\t12\t100\ttest.1\n"), 0644)
20         c.Check(err, check.IsNil)
21
22         var buffer bytes.Buffer
23         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)
24         c.Assert(exited, check.Equals, 0)
25         var output bytes.Buffer
26         exited = (&exportNumpy{}).RunCommand("export-numpy", []string{"-local=true", "-output-annotations", tmpdir + "/annotations.csv", "-regions", tmpdir + "/chr1-12-100.bed"}, &buffer, &output, os.Stderr)
27         c.Check(exited, check.Equals, 0)
28         npy, err := gonpy.NewReader(&output)
29         c.Assert(err, check.IsNil)
30         variants, err := npy.GetInt16()
31         c.Assert(err, check.IsNil)
32         c.Check(variants, check.HasLen, 6)
33         for i := 0; i < 4 && i < len(variants); i += 2 {
34                 if variants[i] == 1 {
35                         c.Check(variants[i+1], check.Equals, int16(2), check.Commentf("i=%d, v=%v", i, variants))
36                 } else {
37                         c.Check(variants[i], check.Equals, int16(2), check.Commentf("i=%d, v=%v", i, variants))
38                 }
39         }
40         for i := 4; i < 6 && i < len(variants); i += 2 {
41                 c.Check(variants[i], check.Equals, int16(1), check.Commentf("i=%d, v=%v", i, variants))
42         }
43         annotations, err := ioutil.ReadFile(tmpdir + "/annotations.csv")
44         c.Check(err, check.IsNil)
45         c.Logf("%s", string(annotations))
46         c.Check(string(annotations), check.Matches, `(?ms)(.*\n)?1,1,2,chr1:g.84_85insACTGCGATCTGA\n.*`)
47         c.Check(string(annotations), check.Matches, `(?ms)(.*\n)?1,1,1,chr1:g.87_96delinsGCATCTGCA\n.*`)
48 }
49
50 func sortUints(variants []int16) {
51         for i := 0; i < len(variants); i += 2 {
52                 if variants[i] > variants[i+1] {
53                         for j := 0; j < len(variants); j++ {
54                                 variants[j], variants[j+1] = variants[j+1], variants[j]
55                         }
56                         return
57                 }
58         }
59 }
60
61 func (s *exportSuite) TestOnehot(c *check.C) {
62         for _, trial := range []struct {
63                 incols  int
64                 in      []int16
65                 outcols int
66                 out     []int16
67         }{
68                 {2, []int16{1, 1, 1, 1}, 2, []int16{1, 1, 1, 1}},
69                 {2, []int16{1, 1, 1, 2}, 3, []int16{1, 1, 0, 1, 0, 1}},
70                 {
71                         // 2nd column => 3 one-hot columns
72                         // 4th column => 0 one-hot columns
73                         4, []int16{
74                                 1, 1, 0, 0,
75                                 1, 2, 1, 0,
76                                 1, 3, 0, 0,
77                         }, 5, []int16{
78                                 1, 1, 0, 0, 0,
79                                 1, 0, 1, 0, 1,
80                                 1, 0, 0, 1, 0,
81                         },
82                 },
83         } {
84                 out, _, outcols := recodeOnehot(trial.in, trial.incols)
85                 c.Check(out, check.DeepEquals, trial.out)
86                 c.Check(outcols, check.Equals, trial.outcols)
87         }
88 }