11 type tileVariantID int32 // 1-based
13 type tileLibRef struct {
18 type tileSeq map[string][]tileLibRef
20 type tileLibrary struct {
22 variant [][][md5.Size]byte
24 seq map[[md5.Size]byte][]byte
29 func (tilelib *tileLibrary) TileFasta(rdr io.Reader) (tileSeq, error) {
31 flush := func(label string, fasta []byte) {
36 tilestart := -1 // position in fasta of tile that ends here
37 tiletagid := tagID(-1) // tag id starting tile that ends here
38 tilelib.taglib.FindAll(fasta, func(id tagID, pos int) {
40 path = append(path, tilelib.getRef(tiletagid, fasta[tilestart:pos]))
41 // log.Printf("%q: tile %d is variant %d of tile %d", label, len(path), path[len(path)-1], id)
47 path = append(path, tilelib.getRef(tiletagid, fasta[tilestart:]))
48 // log.Printf("%q: tile %d is variant %d of tile %d", label, len(path), path[len(path)-1], tiletagid)
54 scanner := bufio.NewScanner(rdr)
56 buf := scanner.Bytes()
57 if len(buf) == 0 || buf[0] == '>' {
60 label = string(buf[1:])
62 fasta = append(fasta, bytes.ToLower(buf)...)
65 if err := scanner.Err(); err != nil {
72 // Return a tileLibRef for a tile with the given tag and sequence,
73 // adding the sequence to the library if needed.
74 func (tilelib *tileLibrary) getRef(tag tagID, seq []byte) tileLibRef {
76 defer tilelib.mtx.Unlock()
77 if tilelib.seq == nil {
78 tilelib.seq = map[[md5.Size]byte][]byte{}
80 for len(tilelib.variant) <= int(tag) {
81 tilelib.variant = append(tilelib.variant, nil)
83 seqhash := md5.Sum(seq)
84 for i, varhash := range tilelib.variant[tag] {
85 if varhash == seqhash {
86 return tileLibRef{tag: tag, variant: tileVariantID(i + 1)}
89 tilelib.variant[tag] = append(tilelib.variant[tag], seqhash)
90 tilelib.seq[seqhash] = append([]byte(nil), seq...)
91 return tileLibRef{tag: tag, variant: tileVariantID(len(tilelib.variant[tag]))}