1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "github.com/kshedden/gonpy"
16 type exportNumpySuite struct{}
18 var _ = check.Suite(&exportNumpySuite{})
20 func (s *exportNumpySuite) TestFastaToNumpy(c *check.C) {
23 err := ioutil.WriteFile(tmpdir+"/chr1-12-100.bed", []byte("chr1\t12\t100\ttest.1\n"), 0644)
24 c.Check(err, check.IsNil)
26 var buffer bytes.Buffer
27 exited := (&importer{}).RunCommand("import", []string{"-local=true", "-o", tmpdir + "/library.gob.gz", "-tag-library", "testdata/tags", "-output-tiles", "-save-incomplete-tiles", "testdata/a.1.fasta", "testdata/tinyref.fasta"}, &bytes.Buffer{}, os.Stderr, os.Stderr)
28 c.Assert(exited, check.Equals, 0)
29 exited = (&exportNumpy{}).RunCommand("export-numpy", []string{"-local=true", "-input-dir", tmpdir, "-output-dir", tmpdir, "-output-annotations", tmpdir + "/annotations.csv", "-regions", tmpdir + "/chr1-12-100.bed"}, &buffer, os.Stderr, os.Stderr)
30 c.Check(exited, check.Equals, 0)
31 f, err := os.Open(tmpdir + "/matrix.npy")
32 c.Assert(err, check.IsNil)
34 npy, err := gonpy.NewReader(f)
35 c.Assert(err, check.IsNil)
36 variants, err := npy.GetInt16()
37 c.Assert(err, check.IsNil)
38 c.Check(variants, check.HasLen, 6)
39 for i := 0; i < 4 && i < len(variants); i += 2 {
41 c.Check(variants[i+1], check.Equals, int16(2), check.Commentf("i=%d, v=%v", i, variants))
43 c.Check(variants[i], check.Equals, int16(2), check.Commentf("i=%d, v=%v", i, variants))
46 for i := 4; i < 6 && i < len(variants); i += 2 {
47 c.Check(variants[i], check.Equals, int16(1), check.Commentf("i=%d, v=%v", i, variants))
49 annotations, err := ioutil.ReadFile(tmpdir + "/annotations.csv")
50 c.Check(err, check.IsNil)
51 c.Logf("%s", string(annotations))
52 c.Check(string(annotations), check.Matches, `(?ms)(.*\n)?1,1,2,chr1:g.84_85insACTGCGATCTGA\n.*`)
53 c.Check(string(annotations), check.Matches, `(?ms)(.*\n)?1,1,1,chr1:g.87_96delinsGCATCTGCA\n.*`)
56 func sortUints(variants []int16) {
57 for i := 0; i < len(variants); i += 2 {
58 if variants[i] > variants[i+1] {
59 for j := 0; j < len(variants); j++ {
60 variants[j], variants[j+1] = variants[j+1], variants[j]
67 func (s *exportNumpySuite) TestOnehot(c *check.C) {
68 for _, trial := range []struct {
74 {2, []int16{1, 1, 1, 1}, 2, []int16{1, 1, 1, 1}},
75 {2, []int16{1, 1, 1, 2}, 3, []int16{1, 1, 0, 1, 0, 1}},
77 // 2nd column => 3 one-hot columns
78 // 4th column => 0 one-hot columns
90 out, _, outcols := recodeOnehot(trial.in, trial.incols)
91 c.Check(out, check.DeepEquals, trial.out)
92 c.Check(outcols, check.Equals, trial.outcols)