10 log "github.com/sirupsen/logrus"
11 "golang.org/x/crypto/blake2b"
14 type tileVariantID uint16 // 1-based
16 type tileLibRef struct {
21 type tileSeq map[string][]tileLibRef
23 func (tseq tileSeq) Variants() []tileVariantID {
25 for _, refs := range tseq {
26 for _, ref := range refs {
27 if maxtag < int(ref.tag) {
32 vars := make([]tileVariantID, maxtag+1)
33 for _, refs := range tseq {
34 for _, ref := range refs {
35 vars[int(ref.tag)] = ref.variant
41 type tileLibrary struct {
44 variant [][][blake2b.Size256]byte
46 // seq map[[blake2b.Size]byte][]byte
52 func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader) (tileSeq, error) {
58 todo := make(chan jobT)
59 scanner := bufio.NewScanner(rdr)
65 buf := scanner.Bytes()
66 if len(buf) == 0 || buf[0] == '>' {
67 todo <- jobT{seqlabel, fasta}
68 seqlabel, fasta = string(buf[1:]), nil
69 log.Debugf("%s %s reading fasta", filelabel, seqlabel)
71 fasta = append(fasta, bytes.ToLower(buf)...)
74 todo <- jobT{seqlabel, fasta}
76 type foundtag struct {
81 found := make([]foundtag, 2000000)
82 path := make([]tileLibRef, 2000000)
85 for job := range todo {
86 if len(job.fasta) == 0 {
88 } else if strings.Contains(job.label, "_") {
92 log.Debugf("%s %s tiling", filelabel, job.label)
95 tilelib.taglib.FindAll(job.fasta, func(tagid tagID, pos, taglen int) {
96 found = append(found, foundtag{pos: pos, tagid: tagid, taglen: taglen})
99 last := foundtag{tagid: -1}
100 for i, f := range found {
101 log.Tracef("%s %s found[%d] == %#v", filelabel, job.label, i, f)
103 if f.tagid < last.tagid+1 {
104 // e.g., last=B, this=A
105 log.Debugf("%s %s skipped out-of-order tag %d (found at %d) because it follows tag %d (found at %d)", filelabel, job.label, f.tagid, f.pos, last.tagid, last.pos)
108 if f.tagid > last.tagid+1 && i+1 < len(found) && found[i+1].tagid <= f.tagid {
109 // e.g., last=A, this=C, next=B
110 log.Debugf("%s %s skipped out-of-order tag %d (found at %d) because it appears between tag %d (found at %d) and %d (found at %d)", filelabel, job.label, f.tagid, f.pos, last.tagid, last.pos, found[i+1].tagid, found[i+1].pos)
115 path = append(path, tilelib.getRef(last.tagid, job.fasta[last.pos:f.pos+f.taglen]))
120 path = append(path, tilelib.getRef(last.tagid, job.fasta[last.pos:]))
123 pathcopy := make([]tileLibRef, len(path))
125 ret[job.label] = pathcopy
126 log.Debugf("%s %s tiled with path len %d", filelabel, job.label, len(path))
127 totalPathLen += len(path)
129 log.Printf("%s tiled with total path len %d in %d sequences (skipped %d sequences with '_' in name)", filelabel, totalPathLen, len(ret), skippedSequences)
130 return ret, scanner.Err()
133 func (tilelib *tileLibrary) Len() int {
135 defer tilelib.mtx.Unlock()
136 return tilelib.variants
139 // Return a tileLibRef for a tile with the given tag and sequence,
140 // adding the sequence to the library if needed.
141 func (tilelib *tileLibrary) getRef(tag tagID, seq []byte) tileLibRef {
142 for _, b := range seq {
143 if b != 'a' && b != 'c' && b != 'g' && b != 't' {
144 // return "tile not found" if seq has any
146 return tileLibRef{tag: tag}
150 defer tilelib.mtx.Unlock()
151 // if tilelib.seq == nil {
152 // tilelib.seq = map[[blake2b.Size]byte][]byte{}
154 if tilelib.variant == nil {
155 tilelib.variant = make([][][blake2b.Size256]byte, tilelib.taglib.Len())
157 seqhash := blake2b.Sum256(seq)
158 for i, varhash := range tilelib.variant[tag] {
159 if varhash == seqhash {
160 return tileLibRef{tag: tag, variant: tileVariantID(i + 1)}
164 tilelib.variant[tag] = append(tilelib.variant[tag], seqhash)
165 // tilelib.seq[seqhash] = append([]byte(nil), seq...)
166 return tileLibRef{tag: tag, variant: tileVariantID(len(tilelib.variant[tag]))}