11 log "github.com/sirupsen/logrus"
12 "golang.org/x/crypto/blake2b"
15 type tileVariantID uint16 // 1-based
17 type tileLibRef struct {
22 type tileSeq map[string][]tileLibRef
24 func (tseq tileSeq) Variants() ([]tileVariantID, int, int) {
26 for _, refs := range tseq {
27 for _, ref := range refs {
28 if maxtag < int(ref.tag) {
33 vars := make([]tileVariantID, maxtag+1)
35 for _, refs := range tseq {
36 for _, ref := range refs {
37 if vars[int(ref.tag)] != 0 {
42 vars[int(ref.tag)] = ref.variant
45 return vars, kept, dropped
48 type tileLibrary struct {
52 variant [][][blake2b.Size256]byte
54 // seq map[[blake2b.Size]byte][]byte
56 // if non-nil, write out any tile variants added while tiling
62 func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader) (tileSeq, error) {
68 todo := make(chan jobT)
69 scanner := bufio.NewScanner(rdr)
75 buf := scanner.Bytes()
76 if len(buf) > 0 && buf[0] == '>' {
77 todo <- jobT{seqlabel, fasta}
78 seqlabel, fasta = string(buf[1:]), nil
79 log.Debugf("%s %s reading fasta", filelabel, seqlabel)
81 fasta = append(fasta, bytes.ToLower(buf)...)
84 todo <- jobT{seqlabel, fasta}
86 type foundtag struct {
91 found := make([]foundtag, 2000000)
92 path := make([]tileLibRef, 2000000)
96 for job := range todo {
97 if len(job.fasta) == 0 {
99 } else if strings.Contains(job.label, "_") {
103 log.Debugf("%s %s tiling", filelabel, job.label)
106 tilelib.taglib.FindAll(job.fasta, func(tagid tagID, pos, taglen int) {
107 found = append(found, foundtag{pos: pos, tagid: tagid, taglen: taglen})
109 totalFoundTags += len(found)
113 last := foundtag{tagid: -1}
115 keep := longestIncreasingSubsequence(len(found), func(i int) int { return int(found[i].tagid) })
116 for i, x := range keep {
119 skipped = len(found) - len(keep)
120 found = found[:len(keep)]
122 for i, f := range found {
123 log.Tracef("%s %s found[%d] == %#v", filelabel, job.label, i, f)
125 path = append(path, tilelib.getRef(last.tagid, job.fasta[last.pos:f.pos+f.taglen]))
130 path = append(path, tilelib.getRef(last.tagid, job.fasta[last.pos:]))
133 pathcopy := make([]tileLibRef, len(path))
135 ret[job.label] = pathcopy
136 log.Debugf("%s %s tiled with path len %d, skipped %d", filelabel, job.label, len(path), skipped)
137 totalPathLen += len(path)
139 log.Printf("%s tiled with total path len %d in %d sequences (skipped %d sequences with '_' in name, skipped %d out-of-order tags)", filelabel, totalPathLen, len(ret), skippedSequences, totalFoundTags-totalPathLen)
140 return ret, scanner.Err()
143 func (tilelib *tileLibrary) Len() int {
145 defer tilelib.mtx.Unlock()
146 return tilelib.variants
149 // Return a tileLibRef for a tile with the given tag and sequence,
150 // adding the sequence to the library if needed.
151 func (tilelib *tileLibrary) getRef(tag tagID, seq []byte) tileLibRef {
152 if !tilelib.includeNoCalls {
153 for _, b := range seq {
154 if b != 'a' && b != 'c' && b != 'g' && b != 't' {
155 // return "tile not found" if seq has any
157 return tileLibRef{tag: tag}
162 // if tilelib.seq == nil {
163 // tilelib.seq = map[[blake2b.Size]byte][]byte{}
165 if tilelib.variant == nil && tilelib.taglib != nil {
166 tilelib.variant = make([][][blake2b.Size256]byte, tilelib.taglib.Len())
168 if int(tag) >= len(tilelib.variant) {
169 // If we haven't seen the tag library yet (as in a
170 // merge), tilelib.taglib.Len() is zero. We can still
171 // behave correctly, we just need to expand the
172 // tilelib.variant slice as needed.
173 if int(tag) >= cap(tilelib.variant) {
174 // Allocate 2x capacity.
175 newslice := make([][][blake2b.Size256]byte, int(tag)+1, (int(tag)+1)*2)
176 copy(newslice, tilelib.variant)
177 tilelib.variant = newslice[:int(tag)+1]
179 // Use previously allocated capacity, avoiding
181 tilelib.variant = tilelib.variant[:int(tag)+1]
184 seqhash := blake2b.Sum256(seq)
185 for i, varhash := range tilelib.variant[tag] {
186 if varhash == seqhash {
188 return tileLibRef{tag: tag, variant: tileVariantID(i + 1)}
192 tilelib.variant[tag] = append(tilelib.variant[tag], seqhash)
193 // tilelib.seq[seqhash] = append([]byte(nil), seq...)
194 variant := tileVariantID(len(tilelib.variant[tag]))
197 if tilelib.encoder != nil {
198 tilelib.encoder.Encode(LibraryEntry{
199 TileVariants: []TileVariant{{
207 return tileLibRef{tag: tag, variant: variant}