12 type tileVariantID int32 // 1-based
14 type tileLibRef struct {
19 type tileSeq map[string][]tileLibRef
21 type tileLibrary struct {
23 variant [][][md5.Size]byte
25 // seq map[[md5.Size]byte][]byte
30 func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader) (tileSeq, error) {
36 todo := make(chan jobT)
37 scanner := bufio.NewScanner(rdr)
43 buf := scanner.Bytes()
44 if len(buf) == 0 || buf[0] == '>' {
45 todo <- jobT{seqlabel, fasta}
46 seqlabel, fasta = string(buf[1:]), nil
47 log.Printf("%s %s reading fasta", filelabel, seqlabel)
49 fasta = append(fasta, bytes.ToLower(buf)...)
52 todo <- jobT{seqlabel, fasta}
54 for job := range todo {
55 if len(job.fasta) == 0 {
58 log.Printf("%s %s tiling", filelabel, job.label)
60 tilestart := -1 // position in fasta of tile that ends here
61 tiletagid := tagID(-1) // tag id starting tile that ends here
62 tilelib.taglib.FindAll(job.fasta, func(id tagID, pos int) {
64 path = append(path, tilelib.getRef(tiletagid, job.fasta[tilestart:pos]))
70 path = append(path, tilelib.getRef(tiletagid, job.fasta[tilestart:]))
73 log.Printf("%s %s tiled with path len %d", filelabel, job.label, len(path))
75 return ret, scanner.Err()
78 // Return a tileLibRef for a tile with the given tag and sequence,
79 // adding the sequence to the library if needed.
80 func (tilelib *tileLibrary) getRef(tag tagID, seq []byte) tileLibRef {
82 defer tilelib.mtx.Unlock()
83 // if tilelib.seq == nil {
84 // tilelib.seq = map[[md5.Size]byte][]byte{}
86 if len(tilelib.variant) <= int(tag) {
87 tilelib.variant = append(tilelib.variant, make([][][md5.Size]byte, int(tag)-len(tilelib.variant)+1)...)
89 seqhash := md5.Sum(seq)
90 for i, varhash := range tilelib.variant[tag] {
91 if varhash == seqhash {
92 return tileLibRef{tag: tag, variant: tileVariantID(i + 1)}
95 tilelib.variant[tag] = append(tilelib.variant[tag], seqhash)
96 // tilelib.seq[seqhash] = append([]byte(nil), seq...)
97 return tileLibRef{tag: tag, variant: tileVariantID(len(tilelib.variant[tag]))}