forked from grailbio/bio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
200 lines (179 loc) · 5.51 KB
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package cmd
// This file defines fusionWriter and fusionReader. Type fusionWriter dumps
// GeneDB and Fragment into a recordio file, and fusionReader reads them back.
// The recordio file can be used to bypass the 1st phase of the af4 and run only
// the filtering phase.
import (
"bufio"
"bytes"
"context"
"encoding/gob"
"io"
"log"
"github.com/Schaudge/grailbase/file"
"github.com/Schaudge/grailbase/recordio"
"github.com/Schaudge/grailbase/recordio/recordiozstd"
"github.com/Schaudge/grailbio/fusion"
)
// fusionWriter is for writing GeneDB and Fragments to a recordio file.
// The recordio file can be used to bypass the 1st phase of the af4 and run only
// the filtering phase.
type fusionWriter struct {
out file.File
w recordio.Writer
geneDB *fusion.GeneDB
opts fusion.Opts
}
const (
// <fileVersionHeader, fileVersion> is stored in a recordio header.B
fileVersionHeader = "af4version"
fileVersion = "AF4_V1"
)
// fusionFileHeader is stored in the trailer section of the recordio file.
type fusionFileHeader struct {
// Opts is the list of options used to generate the candidates.
Opts fusion.Opts
// Gene is the list of genes registered in geneDB. Kmers are dropped, since they aren't used in the[q 2nd phase.
Gene []fusion.GeneInfo
}
func encodeGOB(gw *gob.Encoder, v interface{}) {
if err := gw.Encode(v); err != nil {
panic(err)
}
}
func decodeGOB(gr *gob.Decoder, v interface{}) {
if err := gr.Decode(v); err != nil {
panic(err)
}
}
func newFusionWriter(ctx context.Context, outPath string, geneDB *fusion.GeneDB, opts fusion.Opts) *fusionWriter {
recordiozstd.Init()
out, err := file.Create(ctx, outPath)
if err != nil {
log.Panicf("rio open %v: %v", outPath, err)
}
w := recordio.NewWriter(out.Writer(ctx), recordio.WriterOpts{
Transformers: []string{recordiozstd.Name},
})
w.AddHeader(fileVersionHeader, fileVersion)
w.AddHeader(recordio.KeyTrailer, true)
return &fusionWriter{out: out, w: w, geneDB: geneDB, opts: opts}
}
// Write adds a fusion candidate. Any error will crash the process.
func (w *fusionWriter) Write(c fusion.Candidate) {
b := bytes.NewBuffer(nil)
gw := gob.NewEncoder(b)
encodeGOB(gw, c)
w.w.Append(b.Bytes())
}
// Close closes the writer. It must be called exactly once, after writing all
// the candidates.
func (w *fusionWriter) Close(ctx context.Context) {
b := bytes.NewBuffer(nil)
gw := gob.NewEncoder(b)
h := fusionFileHeader{
Opts: w.opts,
}
minGeneID, limitGeneID := w.geneDB.GeneIDRange()
for gid := minGeneID; gid < limitGeneID; gid++ {
h.Gene = append(h.Gene, *w.geneDB.GeneInfo(gid))
}
encodeGOB(gw, h)
w.w.SetTrailer(b.Bytes())
err := w.w.Finish()
if err != nil {
log.Panic("close", err)
}
if err := w.out.Close(ctx); err != nil {
log.Panic("close", err)
}
}
// fusionReader is for reading GeneDB and Fragments from a recordio file created
// by fusionWriter. The recordio file can be used to bypass the 1st phase of
// the af4 and run only the filtering phase.
type fusionReader struct {
in file.File
r recordio.Scanner
geneDB *fusion.GeneDB
opts fusion.Opts
c fusion.Candidate // last candidate read by Scan.
}
func newFusionReader(ctx context.Context, inPath string) *fusionReader {
in, err := file.Open(ctx, inPath)
if err != nil {
log.Panicf("open %s: %v", inPath, err)
}
recordiozstd.Init()
r := recordio.NewScanner(in.Reader(ctx), recordio.ScannerOpts{})
versionFound := false
for _, kv := range r.Header() {
if kv.Key == fileVersionHeader {
if kv.Value.(string) != fileVersion {
log.Panicf("TODO: fusion file version mismatch, got %v, expect %v",
kv.Value.(string), fileVersion)
}
versionFound = true
break
}
}
if !versionFound {
log.Panic(fileVersionHeader + " not found")
}
gr := gob.NewDecoder(bytes.NewReader(r.Trailer()))
h := fusionFileHeader{}
decodeGOB(gr, &h)
geneDB := fusion.NewGeneDB(h.Opts)
geneDB.PrepopulateGeneInfo(h.Gene)
return &fusionReader{in: in, r: r, geneDB: geneDB, opts: h.Opts}
}
// Opts returns the fusion options written in the recordio file. This method can be called
// any time.
func (r *fusionReader) Opts() fusion.Opts { return r.opts }
// GeneDB returns the geneDB written in the recordio file. The DB doesn't
// contain kmer information. This method can be called any time.
func (r *fusionReader) GeneDB() *fusion.GeneDB { return r.geneDB }
// Scan reads the next fusion candidate.
//
// REQUIRES: Close hasn't been called.
func (r *fusionReader) Scan() bool {
if !r.r.Scan() {
return false
}
b := bytes.NewReader(r.r.Get().([]byte))
gr := gob.NewDecoder(b)
r.c = fusion.Candidate{}
decodeGOB(gr, &r.c)
return true
}
// Get yields the current candidate.
//
// REQUIRES: Last Scan call returned true.
func (r *fusionReader) Get() fusion.Candidate { return r.c }
// Close closes the reader. It must be called exactly once. Any error will
// crash the writer.
func (r *fusionReader) Close(ctx context.Context) {
if err := r.r.Err(); err != nil {
log.Panic(err)
}
if err := r.in.Close(ctx); err != nil {
log.Panic(err)
}
}
// Open a buffered file writer. The 2nd arg (func) must be called to flush &
// close the file.
func createFile(ctx context.Context, path string) (io.Writer, func()) {
out, err := file.Create(ctx, path)
if err != nil {
log.Panicf("create %s: %v", path, err)
}
log.Printf("Creating %v", path)
buf := bufio.NewWriterSize(out.Writer(ctx), 1<<20)
return buf, func() {
if err := buf.Flush(); err != nil {
log.Panicf("close %s: %v", path, err)
}
if err := out.Close(ctx); err != nil {
log.Panicf("close %s: %v", path, err)
}
}
}