Sort rows by label shown in csv, not full file path.
[lightning.git] / pipeline_test.go
1 package main
2
3 import (
4         "bytes"
5         "fmt"
6         "io"
7         "io/ioutil"
8         "os"
9         "sort"
10         "strings"
11         "sync"
12
13         "gopkg.in/check.v1"
14 )
15
16 type pipelineSuite struct{}
17
18 var _ = check.Suite(&pipelineSuite{})
19
20 func (s *pipelineSuite) TestImport(c *check.C) {
21         for _, infile := range []string{
22                 "testdata/pipeline1/",
23                 "testdata/ref.fasta",
24         } {
25                 c.Logf("TestImport: %s", infile)
26                 var wg sync.WaitGroup
27
28                 statsin, importout := io.Pipe()
29                 wg.Add(1)
30                 go func() {
31                         defer wg.Done()
32                         code := (&importer{}).RunCommand("lightning import", []string{"-local=true", "-skip-ooo=true", "-output-tiles", "-tag-library", "testdata/tags", infile}, bytes.NewReader(nil), importout, os.Stderr)
33                         c.Check(code, check.Equals, 0)
34                         importout.Close()
35                 }()
36                 statsout := &bytes.Buffer{}
37                 wg.Add(1)
38                 go func() {
39                         defer wg.Done()
40                         code := (&statscmd{}).RunCommand("lightning stats", []string{"-local"}, statsin, statsout, os.Stderr)
41                         c.Check(code, check.Equals, 0)
42                 }()
43                 wg.Wait()
44                 c.Logf("%s", statsout.String())
45         }
46 }
47
48 func (s *pipelineSuite) TestImportMerge(c *check.C) {
49         libfile := make([]string, 2)
50         tmpdir := c.MkDir()
51
52         var wg sync.WaitGroup
53         for i, infile := range []string{
54                 "testdata/ref.fasta",
55                 "testdata/pipeline1/",
56         } {
57                 i, infile := i, infile
58                 c.Logf("TestImportMerge: %s", infile)
59                 libfile[i] = fmt.Sprintf("%s/%d.gob", tmpdir, i)
60                 wg.Add(1)
61                 go func() {
62                         defer wg.Done()
63                         args := []string{"-local=true", "-o=" + libfile[i], "-skip-ooo=true", "-output-tiles", "-tag-library", "testdata/tags"}
64                         if i == 0 {
65                                 // ref only
66                                 args = append(args, "-save-incomplete-tiles")
67                         }
68                         args = append(args, infile)
69                         code := (&importer{}).RunCommand("lightning import", args, bytes.NewReader(nil), &bytes.Buffer{}, os.Stderr)
70                         c.Check(code, check.Equals, 0)
71                 }()
72         }
73         wg.Wait()
74
75         merged := &bytes.Buffer{}
76         code := (&merger{}).RunCommand("lightning merge", []string{"-local", libfile[0], libfile[1]}, bytes.NewReader(nil), merged, os.Stderr)
77         c.Check(code, check.Equals, 0)
78         c.Logf("len(merged) %d", merged.Len())
79
80         statsout := &bytes.Buffer{}
81         code = (&statscmd{}).RunCommand("lightning stats", []string{"-local"}, bytes.NewReader(merged.Bytes()), statsout, os.Stderr)
82         c.Check(code, check.Equals, 0)
83         c.Check(statsout.Len() > 0, check.Equals, true)
84         c.Logf("%s", statsout.String())
85
86         c.Check(ioutil.WriteFile(tmpdir+"/merged.gob", merged.Bytes(), 0666), check.IsNil)
87
88         hgvsout := &bytes.Buffer{}
89         code = (&exporter{}).RunCommand("lightning export", []string{"-local", "-ref", "testdata/ref.fasta", "-output-format", "hgvs", "-i", tmpdir + "/merged.gob"}, bytes.NewReader(nil), hgvsout, os.Stderr)
90         c.Check(code, check.Equals, 0)
91         c.Check(hgvsout.Len() > 0, check.Equals, true)
92         c.Logf("%s", hgvsout.String())
93         c.Check(hgvsout.String(), check.Equals, `chr1:g.1_3delinsGGC    .
94 chr1:g.[41_42delinsAA];[41=]    .
95 chr1:g.[161=];[161A>T]  .
96 chr1:g.[178=];[178A>T]  .
97 chr1:g.222_224del       .
98 chr1:g.[302=];[302_305delinsAAAA]       .
99 .       chr2:g.[1=];[1_3delinsAAA]
100 .       chr2:g.125_127delinsAAA
101 chr2:g.[241_254del];[241=]      .
102 chr2:g.[258_269delinsAA];[258=] .
103 chr2:g.[315C>A];[315=]  .
104 chr2:g.[470_472del];[470=]      .
105 chr2:g.[471=];[471_472delinsAA] .
106 `)
107
108         vcfout := &bytes.Buffer{}
109         code = (&exporter{}).RunCommand("lightning export", []string{"-local", "-ref", "testdata/ref.fasta", "-output-format", "vcf", "-i", tmpdir + "/merged.gob", "-output-bed", tmpdir + "/export.bed"}, bytes.NewReader(nil), vcfout, os.Stderr)
110         c.Check(code, check.Equals, 0)
111         c.Check(vcfout.Len() > 0, check.Equals, true)
112         c.Logf("%s", vcfout.String())
113         c.Check(vcfout.String(), check.Equals, `chr1    1       NNN     GGC     1/1     0/0
114 chr1    41      TT      AA      1/0     0/0
115 chr1    161     A       T       0/1     0/0
116 chr1    178     A       T       0/1     0/0
117 chr1    221     TCCA    T       1/1     0/0
118 chr1    302     TTTT    AAAA    0/1     0/0
119 chr2    1       TTT     AAA     0/0     0/1
120 chr2    125     CTT     AAA     0/0     1/1
121 chr2    240     ATTTTTCTTGCTCTC A       1/0     0/0
122 chr2    258     CCTTGTATTTTT    AA      1/0     0/0
123 chr2    315     C       A       1/0     0/0
124 chr2    469     GTGG    G       1/0     0/0
125 chr2    471     GG      AA      0/1     0/0
126 `)
127         bedout, err := ioutil.ReadFile(tmpdir + "/export.bed")
128         c.Check(err, check.IsNil)
129         c.Logf("%s", string(bedout))
130         c.Check(string(bedout), check.Equals, `chr1 0 248 0 1000 . 0 224
131 chr1 224 372 1 1000 . 248 348
132 chr1 348 496 2 1000 . 372 472
133 chr1 472 572 3 1000 . 496 572
134 chr2 0 248 4 1000 . 0 224
135 chr2 224 372 5 750 . 248 348
136 chr2 348 496 6 1000 . 372 472
137 chr2 472 572 7 1000 . 496 572
138 `)
139
140         annotateout := &bytes.Buffer{}
141         code = (&annotatecmd{}).RunCommand("lightning annotate", []string{"-local", "-variant-hash=true", "-i", tmpdir + "/merged.gob"}, bytes.NewReader(nil), annotateout, os.Stderr)
142         c.Check(code, check.Equals, 0)
143         c.Check(annotateout.Len() > 0, check.Equals, true)
144         sorted := sortLines(annotateout.String())
145         c.Logf("%s", sorted)
146         c.Check(sorted, check.Equals, sortLines(`0,8d4fe9a63921b,chr1:g.161A>T
147 0,8d4fe9a63921b,chr1:g.178A>T
148 0,8d4fe9a63921b,chr1:g.1_3delinsGGC
149 0,8d4fe9a63921b,chr1:g.222_224del
150 0,ba4263ca4199c,chr1:g.1_3delinsGGC
151 0,ba4263ca4199c,chr1:g.222_224del
152 0,ba4263ca4199c,chr1:g.41_42delinsAA
153 1,139890345dbb8,chr1:g.302_305delinsAAAA
154 4,cbfca15d241d3,chr2:g.125_127delinsAAA
155 4,cbfca15d241d3,chr2:g.1_3delinsAAA
156 4,f5fafe9450b02,chr2:g.241_245delinsAAAAA
157 4,f5fafe9450b02,chr2:g.291C>A
158 4,fe9a71a42adb4,chr2:g.125_127delinsAAA
159 6,e36dce85efbef,chr2:g.471_472delinsAA
160 6,f81388b184f4a,chr2:g.470_472del
161 `))
162 }
163
164 func sortLines(txt string) string {
165         lines := strings.Split(strings.TrimRightFunc(txt, func(c rune) bool { return c == '\n' }), "\n")
166         sort.Strings(lines)
167         return strings.Join(lines, "\n") + "\n"
168 }