Track which tile variants each hgvs.Variant appeared in.
[lightning.git] / gob.go
1 package lightning
2
3 import (
4         "bufio"
5         "encoding/gob"
6         "io"
7         "io/ioutil"
8         _ "net/http/pprof"
9
10         "github.com/klauspost/pgzip"
11         "golang.org/x/crypto/blake2b"
12 )
13
14 type CompactGenome struct {
15         Name     string
16         Variants []tileVariantID
17 }
18
19 type CompactSequence struct {
20         Name          string
21         TileSequences map[string][]tileLibRef
22 }
23
24 type TileVariant struct {
25         Tag      tagID
26         Variant  tileVariantID
27         Blake2b  [blake2b.Size256]byte
28         Sequence []byte
29 }
30
31 type LibraryEntry struct {
32         TagSet           [][]byte
33         CompactGenomes   []CompactGenome
34         CompactSequences []CompactSequence
35         TileVariants     []TileVariant
36 }
37
38 func ReadCompactGenomes(rdr io.Reader, gz bool) ([]CompactGenome, error) {
39         var ret []CompactGenome
40         err := DecodeLibrary(rdr, gz, func(ent *LibraryEntry) error {
41                 ret = append(ret, ent.CompactGenomes...)
42                 return nil
43         })
44         return ret, err
45 }
46
47 func DecodeLibrary(rdr io.Reader, gz bool, cb func(*LibraryEntry) error) error {
48         zrdr := ioutil.NopCloser(rdr)
49         var err error
50         if gz {
51                 zrdr, err = pgzip.NewReader(bufio.NewReaderSize(rdr, 1<<20))
52                 if err != nil {
53                         return err
54                 }
55         }
56         dec := gob.NewDecoder(zrdr)
57         for err == nil {
58                 var ent LibraryEntry
59                 err = dec.Decode(&ent)
60                 if err == nil {
61                         err = cb(&ent)
62                 }
63         }
64         if err != io.EOF {
65                 return err
66         }
67         return zrdr.Close()
68 }