1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "github.com/kshedden/gonpy"
16 type sliceSuite struct{}
18 var _ = check.Suite(&sliceSuite{})
20 func (s *sliceSuite) TestImportAndSlice(c *check.C) {
22 err := os.Mkdir(tmpdir+"/lib1", 0777)
23 c.Assert(err, check.IsNil)
24 err = os.Mkdir(tmpdir+"/lib2", 0777)
25 c.Assert(err, check.IsNil)
26 err = os.Mkdir(tmpdir+"/lib3", 0777)
27 c.Assert(err, check.IsNil)
28 cwd, err := os.Getwd()
29 c.Assert(err, check.IsNil)
30 err = os.Symlink(cwd+"/testdata/pipeline1", tmpdir+"/pipeline1")
31 c.Assert(err, check.IsNil)
32 err = os.Symlink(cwd+"/testdata/pipeline1", tmpdir+"/pipeline1dup")
33 c.Assert(err, check.IsNil)
35 err = ioutil.WriteFile(tmpdir+"/chr1-12-100.bed", []byte("chr1\t12\t100\ttest.1\n"), 0644)
36 c.Check(err, check.IsNil)
38 c.Log("=== import testdata/ref ===")
39 exited := (&importer{}).RunCommand("import", []string{
41 "-tag-library", "testdata/tags",
43 "-save-incomplete-tiles",
44 "-o", tmpdir + "/lib1/library1.gob",
46 }, nil, os.Stderr, os.Stderr)
47 c.Assert(exited, check.Equals, 0)
49 c.Log("=== import testdata/pipeline1 ===")
50 exited = (&importer{}).RunCommand("import", []string{
52 "-tag-library", "testdata/tags",
54 "-o", tmpdir + "/lib2/library2.gob",
55 tmpdir + "/pipeline1",
56 }, nil, os.Stderr, os.Stderr)
57 c.Assert(exited, check.Equals, 0)
59 c.Log("=== import pipeline1dup ===")
60 exited = (&importer{}).RunCommand("import", []string{
62 "-tag-library", "testdata/tags",
64 "-o", tmpdir + "/lib3/library3.gob",
65 tmpdir + "/pipeline1dup",
66 }, nil, os.Stderr, os.Stderr)
67 c.Assert(exited, check.Equals, 0)
71 c.Log("=== slice ===")
72 exited = (&slicecmd{}).RunCommand("slice", []string{
74 "-output-dir=" + slicedir,
79 }, nil, os.Stderr, os.Stderr)
80 c.Check(exited, check.Equals, 0)
81 out, _ := exec.Command("find", slicedir, "-ls").CombinedOutput()
84 c.Log("=== slice-numpy ===")
87 exited := (&sliceNumpy{}).RunCommand("slice-numpy", []string{
89 "-input-dir=" + slicedir,
90 "-output-dir=" + npydir,
91 }, nil, os.Stderr, os.Stderr)
92 c.Check(exited, check.Equals, 0)
93 out, _ := exec.Command("find", npydir, "-ls").CombinedOutput()
96 f, err := os.Open(npydir + "/matrix.0000.npy")
97 c.Assert(err, check.IsNil)
99 npy, err := gonpy.NewReader(f)
100 c.Assert(err, check.IsNil)
101 c.Check(npy.Shape, check.DeepEquals, []int{4, 4})
102 variants, err := npy.GetInt16()
103 c.Check(variants, check.DeepEquals, []int16{2, 1, 1, 2, -1, -1, 1, 1, 2, 1, 1, 2, -1, -1, 1, 1})
105 annotations, err := ioutil.ReadFile(npydir + "/matrix.0000.annotations.csv")
106 c.Assert(err, check.IsNil)
107 c.Logf("%s", annotations)
108 for _, s := range []string{
111 "chr1:g.1_3delinsGGC",
114 c.Check(string(annotations), check.Matches, "(?ms).*"+s+".*")
117 annotations, err = ioutil.ReadFile(npydir + "/matrix.0002.annotations.csv")
118 c.Assert(err, check.IsNil)
119 c.Logf("%s", annotations)
120 for _, s := range []string{
121 ",2,chr2:g.1_3delinsAAA",
122 ",2,chr2:g.125_127delinsAAA",
123 ",4,chr2:g.125_127delinsAAA",
125 c.Check(string(annotations), check.Matches, "(?ms).*"+s+".*")
129 c.Log("=== slice-numpy + regions ===")
132 exited := (&sliceNumpy{}).RunCommand("slice-numpy", []string{
134 "-regions=" + tmpdir + "/chr1-12-100.bed",
135 "-input-dir=" + slicedir,
136 "-output-dir=" + npydir,
137 "-chunked-hgvs-matrix=true",
138 }, nil, os.Stderr, os.Stderr)
139 c.Check(exited, check.Equals, 0)
140 out, _ := exec.Command("find", npydir, "-ls").CombinedOutput()
143 f, err := os.Open(npydir + "/matrix.0000.npy")
144 c.Assert(err, check.IsNil)
146 npy, err := gonpy.NewReader(f)
147 c.Assert(err, check.IsNil)
148 c.Check(npy.Shape, check.DeepEquals, []int{4, 2})
149 variants, err := npy.GetInt16()
150 c.Check(variants, check.DeepEquals, []int16{2, 1, -1, -1, 2, 1, -1, -1})
152 annotations, err := ioutil.ReadFile(npydir + "/matrix.0000.annotations.csv")
153 c.Assert(err, check.IsNil)
154 c.Logf("%s", annotations)
155 for _, s := range []string{
158 "chr1:g.1_3delinsGGC",
161 c.Check(string(annotations), check.Matches, "(?ms).*"+s+".*")
164 for _, fnm := range []string{
165 npydir + "/matrix.0001.annotations.csv",
166 npydir + "/matrix.0002.annotations.csv",
168 annotations, err := ioutil.ReadFile(fnm)
169 c.Assert(err, check.IsNil)
170 c.Check(string(annotations), check.Equals, "", check.Commentf(fnm))
174 err = ioutil.WriteFile(tmpdir+"/chr1and2-100-200.bed", []byte("chr1\t100\t200\ttest.1\nchr2\t100\t200\ttest.2\n"), 0644)
175 c.Check(err, check.IsNil)
177 c.Log("=== slice-numpy + regions + merge ===")
180 exited := (&sliceNumpy{}).RunCommand("slice-numpy", []string{
182 "-regions=" + tmpdir + "/chr1and2-100-200.bed",
183 "-input-dir=" + slicedir,
184 "-output-dir=" + npydir,
185 "-merge-output=true",
186 "-single-hgvs-matrix=true",
187 }, nil, os.Stderr, os.Stderr)
188 c.Check(exited, check.Equals, 0)
189 out, _ := exec.Command("find", npydir, "-ls").CombinedOutput()
192 f, err := os.Open(npydir + "/matrix.npy")
193 c.Assert(err, check.IsNil)
195 npy, err := gonpy.NewReader(f)
196 c.Assert(err, check.IsNil)
197 c.Check(npy.Shape, check.DeepEquals, []int{4, 4})
198 variants, err := npy.GetInt16()
199 if c.Check(err, check.IsNil) {
200 c.Check(variants, check.DeepEquals, []int16{2, 1, 3, 1, -1, -1, 4, 2, 2, 1, 3, 1, -1, -1, 4, 2})
203 annotations, err := ioutil.ReadFile(npydir + "/matrix.annotations.csv")
204 c.Assert(err, check.IsNil)
205 c.Logf("%s", annotations)
206 for _, s := range []string{
207 "0,0,1,chr1:g.161A>T",
208 "0,0,1,chr1:g.178A>T",
209 "4,1,2,chr2:g.125_127delinsAAA",
211 c.Check(string(annotations), check.Matches, "(?ms).*"+s+".*")
215 c.Log("=== slice-numpy + chunked hgvs matrix ===")
217 err = ioutil.WriteFile(tmpdir+"/casecontrol.tsv", []byte(`SampleID CC
220 pipeline1dup/input1 1
221 pipeline1dup/input2 0
223 c.Assert(err, check.IsNil)
225 exited := (&sliceNumpy{}).RunCommand("slice-numpy", []string{
227 "-chunked-hgvs-matrix=true",
228 "-chi2-case-control-file=" + tmpdir + "/casecontrol.tsv",
229 "-chi2-case-control-column=CC",
231 "-min-coverage=0.75",
232 "-input-dir=" + slicedir,
233 "-output-dir=" + npydir,
234 }, nil, os.Stderr, os.Stderr)
235 c.Check(exited, check.Equals, 0)
236 out, _ := exec.Command("find", npydir, "-ls").CombinedOutput()
239 annotations, err := ioutil.ReadFile(npydir + "/hgvs.chr2.annotations.csv")
240 c.Assert(err, check.IsNil)
241 c.Check(string(annotations), check.Equals, `0,chr2:g.470_472del
247 c.Log("=== slice-numpy + onehotChunked ===")
249 err = ioutil.WriteFile(tmpdir+"/casecontrol.tsv", []byte(`SampleID CC
252 pipeline1dup/input1 1
253 pipeline1dup/input2 0
255 c.Assert(err, check.IsNil)
257 exited := (&sliceNumpy{}).RunCommand("slice-numpy", []string{
259 "-chunked-onehot=true",
260 "-chi2-case-control-file=" + tmpdir + "/casecontrol.tsv",
261 "-chi2-case-control-column=CC",
263 "-min-coverage=0.75",
264 "-input-dir=" + slicedir,
265 "-output-dir=" + npydir,
266 }, nil, os.Stderr, os.Stderr)
267 c.Check(exited, check.Equals, 0)
268 out, _ := exec.Command("find", npydir, "-ls").CombinedOutput()
271 f, err := os.Open(npydir + "/onehot.0002.npy")
272 c.Assert(err, check.IsNil)
274 npy, err := gonpy.NewReader(f)
275 c.Assert(err, check.IsNil)
276 c.Check(npy.Shape, check.DeepEquals, []int{4, 6})
277 onehot, err := npy.GetInt8()
278 if c.Check(err, check.IsNil) {
279 for r := 0; r < npy.Shape[0]; r++ {
280 c.Logf("%v", onehot[r*npy.Shape[1]:(r+1)*npy.Shape[1]])
282 c.Check(onehot, check.DeepEquals, []int8{
283 0, 0, 0, 1, 0, 0, // input1
284 0, 1, 0, 0, 0, 1, // input2
285 0, 0, 0, 1, 0, 0, // dup/input1
286 0, 1, 0, 0, 0, 1, // dup/input2
291 c.Log("=== slice-numpy + onehotSingle ===")
293 err = ioutil.WriteFile(tmpdir+"/casecontrol.tsv", []byte(`SampleID CC
296 pipeline1dup/input1 1
297 pipeline1dup/input2 0
299 c.Assert(err, check.IsNil)
301 exited := (&sliceNumpy{}).RunCommand("slice-numpy", []string{
303 "-single-onehot=true",
304 "-chi2-case-control-file=" + tmpdir + "/casecontrol.tsv",
305 "-chi2-case-control-column=CC",
307 "-min-coverage=0.75",
308 "-input-dir=" + slicedir,
309 "-output-dir=" + npydir,
310 }, nil, os.Stderr, os.Stderr)
311 c.Check(exited, check.Equals, 0)
312 out, _ := exec.Command("find", npydir, "-ls").CombinedOutput()
315 f, err := os.Open(npydir + "/onehot.npy")
316 c.Assert(err, check.IsNil)
318 npy, err := gonpy.NewReader(f)
319 c.Assert(err, check.IsNil)
320 c.Check(npy.Shape, check.DeepEquals, []int{4, 16})
321 onehot, err := npy.GetInt8()
322 if c.Check(err, check.IsNil) {
323 for r := 0; r < npy.Shape[0]; r++ {
324 c.Logf("%v", onehot[r*npy.Shape[1]:(r+1)*npy.Shape[1]])
326 c.Check(onehot, check.DeepEquals, []int8{
327 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, // input1
328 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, // input2
329 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, // dup/input1
330 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, // dup/input2