Compact on the fly to reduce memory use.
[lightning.git] / tilelib.go
1 package main
2
3 import (
4         "bufio"
5         "bytes"
6         "io"
7         "log"
8         "strings"
9         "sync"
10
11         "golang.org/x/crypto/blake2b"
12 )
13
14 type tileVariantID uint16 // 1-based
15
16 type tileLibRef struct {
17         tag     tagID
18         variant tileVariantID
19 }
20
21 type tileSeq map[string][]tileLibRef
22
23 func (tseq tileSeq) Variants() []tileVariantID {
24         maxtag := 0
25         for _, refs := range tseq {
26                 for _, ref := range refs {
27                         if maxtag < int(ref.tag) {
28                                 maxtag = int(ref.tag)
29                         }
30                 }
31         }
32         vars := make([]tileVariantID, maxtag+1)
33         for _, refs := range tseq {
34                 for _, ref := range refs {
35                         vars[int(ref.tag)] = ref.variant
36                 }
37         }
38         return vars
39 }
40
41 type tileLibrary struct {
42         taglib  *tagLibrary
43         variant [][][blake2b.Size256]byte
44         // count [][]int
45         // seq map[[blake2b.Size]byte][]byte
46         variants int
47
48         mtx sync.Mutex
49 }
50
51 func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader) (tileSeq, error) {
52         ret := tileSeq{}
53         type jobT struct {
54                 label string
55                 fasta []byte
56         }
57         todo := make(chan jobT)
58         scanner := bufio.NewScanner(rdr)
59         go func() {
60                 defer close(todo)
61                 var fasta []byte
62                 var seqlabel string
63                 for scanner.Scan() {
64                         buf := scanner.Bytes()
65                         if len(buf) == 0 || buf[0] == '>' {
66                                 todo <- jobT{seqlabel, fasta}
67                                 seqlabel, fasta = string(buf[1:]), nil
68                                 log.Printf("%s %s reading fasta", filelabel, seqlabel)
69                         } else {
70                                 fasta = append(fasta, bytes.ToLower(buf)...)
71                         }
72                 }
73                 todo <- jobT{seqlabel, fasta}
74         }()
75         path := make([]tileLibRef, 2000000)
76         for job := range todo {
77                 if len(job.fasta) == 0 || strings.Contains(job.label, "_") {
78                         continue
79                 }
80                 log.Printf("%s %s tiling", filelabel, job.label)
81                 path = path[:0]
82                 tilestart := -1        // position in fasta of tile that ends here
83                 tiletagid := tagID(-1) // tag id starting tile that ends here
84                 tilelib.taglib.FindAll(job.fasta, func(id tagID, pos, taglen int) {
85                         if tilestart >= 0 {
86                                 path = append(path, tilelib.getRef(tiletagid, job.fasta[tilestart:pos+taglen]))
87                         }
88                         tilestart = pos
89                         tiletagid = id
90                 })
91                 if tiletagid >= 0 {
92                         path = append(path, tilelib.getRef(tiletagid, job.fasta[tilestart:]))
93                 }
94                 pathcopy := make([]tileLibRef, len(path))
95                 copy(pathcopy, path)
96                 ret[job.label] = pathcopy
97                 log.Printf("%s %s tiled with path len %d", filelabel, job.label, len(path))
98         }
99         return ret, scanner.Err()
100 }
101
102 func (tilelib *tileLibrary) Len() int {
103         tilelib.mtx.Lock()
104         defer tilelib.mtx.Unlock()
105         return tilelib.variants
106 }
107
108 // Return a tileLibRef for a tile with the given tag and sequence,
109 // adding the sequence to the library if needed.
110 func (tilelib *tileLibrary) getRef(tag tagID, seq []byte) tileLibRef {
111         for _, b := range seq {
112                 if b != 'a' && b != 'c' && b != 'g' && b != 't' {
113                         // return "tile not found" if seq has any
114                         // no-calls
115                         return tileLibRef{tag: tag}
116                 }
117         }
118         tilelib.mtx.Lock()
119         defer tilelib.mtx.Unlock()
120         // if tilelib.seq == nil {
121         //      tilelib.seq = map[[blake2b.Size]byte][]byte{}
122         // }
123         if tilelib.variant == nil {
124                 tilelib.variant = make([][][blake2b.Size256]byte, tilelib.taglib.Len())
125         }
126         seqhash := blake2b.Sum256(seq)
127         for i, varhash := range tilelib.variant[tag] {
128                 if varhash == seqhash {
129                         return tileLibRef{tag: tag, variant: tileVariantID(i + 1)}
130                 }
131         }
132         tilelib.variants++
133         tilelib.variant[tag] = append(tilelib.variant[tag], seqhash)
134         // tilelib.seq[seqhash] = append([]byte(nil), seq...)
135         return tileLibRef{tag: tag, variant: tileVariantID(len(tilelib.variant[tag]))}
136 }