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 {
43 variant [][][blake2b.Size256]byte
45 // seq map[[blake2b.Size]byte][]byte
51 func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader) (tileSeq, error) {
57 todo := make(chan jobT)
58 scanner := bufio.NewScanner(rdr)
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.Debugf("%s %s reading fasta", filelabel, seqlabel)
70 fasta = append(fasta, bytes.ToLower(buf)...)
73 todo <- jobT{seqlabel, fasta}
75 path := make([]tileLibRef, 2000000)
78 for job := range todo {
79 if len(job.fasta) == 0 {
81 } else if strings.Contains(job.label, "_") {
85 log.Debugf("%s %s tiling", filelabel, job.label)
87 tilestart := -1 // position in fasta of tile that ends here
88 tiletagid := tagID(-1) // tag id starting tile that ends here
89 tilelib.taglib.FindAll(job.fasta, func(id tagID, pos, taglen int) {
91 path = append(path, tilelib.getRef(tiletagid, job.fasta[tilestart:pos+taglen]))
97 path = append(path, tilelib.getRef(tiletagid, job.fasta[tilestart:]))
99 pathcopy := make([]tileLibRef, len(path))
101 ret[job.label] = pathcopy
102 log.Debugf("%s %s tiled with path len %d", filelabel, job.label, len(path))
103 totalPathLen += len(path)
105 log.Printf("%s tiled with total path len %d in %d sequences (skipped %d sequences with '_' in name)", filelabel, totalPathLen, len(ret), skippedSequences)
106 return ret, scanner.Err()
109 func (tilelib *tileLibrary) Len() int {
111 defer tilelib.mtx.Unlock()
112 return tilelib.variants
115 // Return a tileLibRef for a tile with the given tag and sequence,
116 // adding the sequence to the library if needed.
117 func (tilelib *tileLibrary) getRef(tag tagID, seq []byte) tileLibRef {
118 for _, b := range seq {
119 if b != 'a' && b != 'c' && b != 'g' && b != 't' {
120 // return "tile not found" if seq has any
122 return tileLibRef{tag: tag}
126 defer tilelib.mtx.Unlock()
127 // if tilelib.seq == nil {
128 // tilelib.seq = map[[blake2b.Size]byte][]byte{}
130 if tilelib.variant == nil {
131 tilelib.variant = make([][][blake2b.Size256]byte, tilelib.taglib.Len())
133 seqhash := blake2b.Sum256(seq)
134 for i, varhash := range tilelib.variant[tag] {
135 if varhash == seqhash {
136 return tileLibRef{tag: tag, variant: tileVariantID(i + 1)}
140 tilelib.variant[tag] = append(tilelib.variant[tag], seqhash)
141 // tilelib.seq[seqhash] = append([]byte(nil), seq...)
142 return tileLibRef{tag: tag, variant: tileVariantID(len(tilelib.variant[tag]))}