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)
109 last := foundtag{tagid: -1}
111 keep := longestIncreasingSubsequence(len(found), func(i int) int { return int(found[i].tagid) })
112 for i, x := range keep {
115 skipped = len(found) - len(keep)
116 found = found[:len(keep)]
118 for i, f := range found {
119 log.Tracef("%s %s found[%d] == %#v", filelabel, job.label, i, f)
121 path = append(path, tilelib.getRef(last.tagid, job.fasta[last.pos:f.pos+f.taglen]))
126 path = append(path, tilelib.getRef(last.tagid, job.fasta[last.pos:]))
129 pathcopy := make([]tileLibRef, len(path))
131 ret[job.label] = pathcopy
132 log.Debugf("%s %s tiled with path len %d, skipped %d", filelabel, job.label, len(path), skipped)
133 totalPathLen += len(path)
135 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)
136 return ret, scanner.Err()
139 func (tilelib *tileLibrary) Len() int {
141 defer tilelib.mtx.Unlock()
142 return tilelib.variants
145 // Return a tileLibRef for a tile with the given tag and sequence,
146 // adding the sequence to the library if needed.
147 func (tilelib *tileLibrary) getRef(tag tagID, seq []byte) tileLibRef {
148 for _, b := range seq {
149 if b != 'a' && b != 'c' && b != 'g' && b != 't' {
150 // return "tile not found" if seq has any
152 return tileLibRef{tag: tag}
156 defer tilelib.mtx.Unlock()
157 // if tilelib.seq == nil {
158 // tilelib.seq = map[[blake2b.Size]byte][]byte{}
160 if tilelib.variant == nil {
161 tilelib.variant = make([][][blake2b.Size256]byte, tilelib.taglib.Len())
163 seqhash := blake2b.Sum256(seq)
164 for i, varhash := range tilelib.variant[tag] {
165 if varhash == seqhash {
166 return tileLibRef{tag: tag, variant: tileVariantID(i + 1)}
170 tilelib.variant[tag] = append(tilelib.variant[tag], seqhash)
171 // tilelib.seq[seqhash] = append([]byte(nil), seq...)
172 return tileLibRef{tag: tag, variant: tileVariantID(len(tilelib.variant[tag]))}