variant [][][blake2b.Size256]byte
refseqs map[string]map[string][]tileLibRef
compactGenomes map[string][]tileVariantID
- // count [][]int
- seq map[[blake2b.Size256]byte][]byte
- variants int64
+ seq2 map[[2]byte]map[[blake2b.Size256]byte][]byte
+ seq2lock map[[2]byte]sync.Locker
+ variants int64
// if non-nil, write out any tile variants added while tiling
encoder *gob.Encoder
}
return
}
- log.Tracef("loadCompactGenomes: cg %s tag %d variant %d => %d", cg.Name, tag, variant, newvariant)
+ // log.Tracef("loadCompactGenomes: cg %s tag %d variant %d => %d", cg.Name, tag, variant, newvariant)
cg.Variants[i] = newvariant
}
if onLoadGenome != nil {
for _, fi := range fis {
if fi.Name() == "." || fi.Name() == ".." {
continue
- } else if fi.IsDir() {
- err = walk(path + "/" + fi.Name())
+ } else if child := path + "/" + fi.Name(); fi.IsDir() {
+ err = walk(child)
if err != nil {
return err
}
- } else if strings.HasSuffix(path, ".gob") || strings.HasSuffix(path, ".gob.gz") {
- files = append(files, path)
+ } else if strings.HasSuffix(child, ".gob") || strings.HasSuffix(child, ".gob.gz") {
+ files = append(files, child)
}
}
return nil
}
+ log.Infof("LoadDir: walk dir %s", path)
err := walk(path)
if err != nil {
return err
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var mtx sync.Mutex
- cgs := []CompactGenome{}
- cseqs := []CompactSequence{}
- variantmap := map[tileLibRef]tileVariantID{}
+ allcgs := make([][]CompactGenome, len(files))
+ allcseqs := make([][]CompactSequence, len(files))
+ allvariantmap := map[tileLibRef]tileVariantID{}
errs := make(chan error, len(files))
- for _, path := range files {
- path := path
+ log.Infof("LoadDir: read %d files", len(files))
+ for fileno, path := range files {
+ fileno, path := fileno, path
go func() {
f, err := open(path)
if err != nil {
return
}
defer f.Close()
- errs <- DecodeLibrary(f, strings.HasSuffix(path, ".gz"), func(ent *LibraryEntry) error {
+ defer log.Infof("LoadDir: finished reading %s", path)
+
+ var variantmap = map[tileLibRef]tileVariantID{}
+ var cgs []CompactGenome
+ var cseqs []CompactSequence
+ err = DecodeLibrary(f, strings.HasSuffix(path, ".gz"), func(ent *LibraryEntry) error {
if ctx.Err() != nil {
return ctx.Err()
}
- mtx.Lock()
- defer mtx.Unlock()
- if err := tilelib.loadTagSet(ent.TagSet); err != nil {
- return err
+ if len(ent.TagSet) > 0 {
+ mtx.Lock()
+ if tilelib.taglib == nil || tilelib.taglib.Len() != len(ent.TagSet) {
+ // load first set of tags, or
+ // report mismatch if 2 sets
+ // have different #tags.
+ if err := tilelib.loadTagSet(ent.TagSet); err != nil {
+ mtx.Unlock()
+ return err
+ }
+ }
+ mtx.Unlock()
}
- if err := tilelib.loadTileVariants(ent.TileVariants, variantmap); err != nil {
- return err
+ for _, tv := range ent.TileVariants {
+ variantmap[tileLibRef{Tag: tv.Tag, Variant: tv.Variant}] = tilelib.getRef(tv.Tag, tv.Sequence).Variant
}
cgs = append(cgs, ent.CompactGenomes...)
cseqs = append(cseqs, ent.CompactSequences...)
return nil
})
+ allcgs[fileno] = cgs
+ allcseqs[fileno] = cseqs
+ mtx.Lock()
+ defer mtx.Unlock()
+ for k, v := range variantmap {
+ allvariantmap[k] = v
+ }
+ errs <- err
}()
}
for range files {
return err
}
}
- err = tilelib.loadCompactGenomes(cgs, variantmap, onLoadGenome)
+
+ log.Info("LoadDir: loadCompactGenomes")
+ var flatcgs []CompactGenome
+ for _, cgs := range allcgs {
+ flatcgs = append(flatcgs, cgs...)
+ }
+ err = tilelib.loadCompactGenomes(flatcgs, allvariantmap, onLoadGenome)
if err != nil {
return err
}
- err = tilelib.loadCompactSequences(cseqs, variantmap)
+
+ log.Info("LoadDir: loadCompactSequences")
+ var flatcseqs []CompactSequence
+ for _, cseqs := range allcseqs {
+ flatcseqs = append(flatcseqs, cseqs...)
+ }
+ err = tilelib.loadCompactSequences(flatcseqs, allvariantmap)
if err != nil {
return err
}
+
+ log.Info("LoadDir done")
return nil
}
for i := range encoders {
encoders[i] = gob.NewEncoder(zws[i])
}
+
+ cgnames := make([]string, 0, len(tilelib.compactGenomes))
+ for name := range tilelib.compactGenomes {
+ cgnames = append(cgnames, name)
+ }
+ sort.Strings(cgnames)
+
+ log.Infof("WriteDir: writing %d files", nfiles)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errs := make(chan error, nfiles)
return
}
if start == 0 {
- // For now, just write all the genomes and refs
- // to the first file
- for name, cg := range tilelib.compactGenomes {
- err := encoders[start].Encode(LibraryEntry{CompactGenomes: []CompactGenome{{
- Name: name,
- Variants: cg,
- }}})
- if err != nil {
- errs <- err
- return
- }
- }
+ // For now, just write all the refs to
+ // the first file
for name, tseqs := range tilelib.refseqs {
err := encoders[start].Encode(LibraryEntry{CompactSequences: []CompactSequence{{
Name: name,
}
}
}
+ for i := start; i < len(cgnames); i += nfiles {
+ err := encoders[start].Encode(LibraryEntry{CompactGenomes: []CompactGenome{{
+ Name: cgnames[i],
+ Variants: tilelib.compactGenomes[cgnames[i]],
+ }}})
+ if err != nil {
+ errs <- err
+ return
+ }
+ }
tvs := []TileVariant{}
for tag := start; tag < len(tilelib.variant) && ctx.Err() == nil; tag += nfiles {
tvs = tvs[:0]
Tag: tagID(tag),
Variant: tileVariantID(idx + 1),
Blake2b: hash,
- Sequence: tilelib.seq[hash],
+ Sequence: tilelib.hashSequence(hash),
})
}
err := encoders[start].Encode(LibraryEntry{TileVariants: tvs})
return err
}
}
+ log.Info("WriteDir: flushing")
for i := range zws {
err := zws[i].Close()
if err != nil {
return err
}
}
+ log.Info("WriteDir: done")
return nil
}
vlock.Unlock()
if tilelib.retainTileSequences && !dropSeq {
- tilelib.mtx.Lock()
- if tilelib.seq == nil {
- tilelib.seq = map[[blake2b.Size256]byte][]byte{}
+ seqCopy := append([]byte(nil), seq...)
+ if tilelib.seq2 == nil {
+ tilelib.mtx.Lock()
+ if tilelib.seq2 == nil {
+ tilelib.seq2lock = map[[2]byte]sync.Locker{}
+ m := map[[2]byte]map[[blake2b.Size256]byte][]byte{}
+ var k [2]byte
+ for i := 0; i < 256; i++ {
+ k[0] = byte(i)
+ for j := 0; j < 256; j++ {
+ k[1] = byte(j)
+ m[k] = map[[blake2b.Size256]byte][]byte{}
+ tilelib.seq2lock[k] = &sync.Mutex{}
+ }
+ }
+ tilelib.seq2 = m
+ }
+ tilelib.mtx.Unlock()
}
- tilelib.seq[seqhash] = append([]byte(nil), seq...)
- tilelib.mtx.Unlock()
+ var k [2]byte
+ copy(k[:], seqhash[:])
+ locker := tilelib.seq2lock[k]
+ locker.Lock()
+ tilelib.seq2[k][seqhash] = seqCopy
+ locker.Unlock()
}
if tilelib.encoder != nil {
return tileLibRef{Tag: tag, Variant: variant}
}
+func (tilelib *tileLibrary) hashSequence(hash [blake2b.Size256]byte) []byte {
+ var partition [2]byte
+ copy(partition[:], hash[:])
+ return tilelib.seq2[partition][hash]
+}
+
func (tilelib *tileLibrary) TileVariantSequence(libref tileLibRef) []byte {
if libref.Variant == 0 || len(tilelib.variant) <= int(libref.Tag) || len(tilelib.variant[libref.Tag]) < int(libref.Variant) {
return nil
}
- return tilelib.seq[tilelib.variant[libref.Tag][libref.Variant-1]]
+ return tilelib.hashSequence(tilelib.variant[libref.Tag][libref.Variant-1])
}
// Tidy deletes unreferenced tile variants and renumbers variants so