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, int, int) {
25 for _, refs := range tseq {
26 for _, ref := range refs {
27 if maxtag < int(ref.tag) {
32 vars := make([]tileVariantID, maxtag+1)
34 for _, refs := range tseq {
35 for _, ref := range refs {
36 if vars[int(ref.tag)] != 0 {
41 vars[int(ref.tag)] = ref.variant
44 return vars, kept, dropped
47 type tileLibrary struct {
50 variant [][][blake2b.Size256]byte
52 // seq map[[blake2b.Size]byte][]byte
58 func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader) (tileSeq, error) {
64 todo := make(chan jobT)
65 scanner := bufio.NewScanner(rdr)
71 buf := scanner.Bytes()
72 if len(buf) == 0 || buf[0] == '>' {
73 todo <- jobT{seqlabel, fasta}
74 seqlabel, fasta = string(buf[1:]), nil
75 log.Debugf("%s %s reading fasta", filelabel, seqlabel)
77 fasta = append(fasta, bytes.ToLower(buf)...)
80 todo <- jobT{seqlabel, fasta}
82 type foundtag struct {
87 found := make([]foundtag, 2000000)
88 path := make([]tileLibRef, 2000000)
92 for job := range todo {
93 if len(job.fasta) == 0 {
95 } else if strings.Contains(job.label, "_") {
99 log.Debugf("%s %s tiling", filelabel, job.label)
102 tilelib.taglib.FindAll(job.fasta, func(tagid tagID, pos, taglen int) {
103 found = append(found, foundtag{pos: pos, tagid: tagid, taglen: taglen})
105 totalFoundTags += len(found)
108 last := foundtag{tagid: -1}
109 for i, f := range found {
110 log.Tracef("%s %s found[%d] == %#v", filelabel, job.label, i, f)
112 if f.tagid < last.tagid+1 {
113 log.Debugf("%s %s skipped out-of-order tag %d (found at %d) because it appears after tag %d (found at %d)", filelabel, job.label, f.tagid, f.pos, last.tagid, last.pos)
116 if f.tagid > last.tagid+1 && // accepting this tag would mean skipping some tags
117 i+1 < len(found) && // there is a "next" found tag after this one
118 found[i+1].tagid > last.tagid && // next found tag is usable (we haven't already passed it in accepted sequence)
119 found[i+1].tagid <= f.tagid { // next found tag is expected before this one (so we can't use both)
120 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)
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), len(found)-len(path))
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 for _, b := range seq {
153 if b != 'a' && b != 'c' && b != 'g' && b != 't' {
154 // return "tile not found" if seq has any
156 return tileLibRef{tag: tag}
160 defer tilelib.mtx.Unlock()
161 // if tilelib.seq == nil {
162 // tilelib.seq = map[[blake2b.Size]byte][]byte{}
164 if tilelib.variant == nil {
165 tilelib.variant = make([][][blake2b.Size256]byte, tilelib.taglib.Len())
167 seqhash := blake2b.Sum256(seq)
168 for i, varhash := range tilelib.variant[tag] {
169 if varhash == seqhash {
170 return tileLibRef{tag: tag, variant: tileVariantID(i + 1)}
174 tilelib.variant[tag] = append(tilelib.variant[tag], seqhash)
175 // tilelib.seq[seqhash] = append([]byte(nil), seq...)
176 return tileLibRef{tag: tag, variant: tileVariantID(len(tilelib.variant[tag]))}