+ // set Ref flag when writing new variants to encoder
+ encodeRef bool
+
+ onAddTileVariant func(libref tileLibRef, hash [blake2b.Size256]byte, seq []byte) error
+ onAddGenome func(CompactGenome) error
+ onAddRefseq func(CompactSequence) error
+
+ mtx sync.RWMutex
+ vlock []sync.Locker
+}
+
+func (tilelib *tileLibrary) loadTagSet(newtagset [][]byte) error {
+ // Loading a tagset means either passing it through to the
+ // output (if it's the first one we've seen), or just ensuring
+ // it doesn't disagree with what we already have.
+ if len(newtagset) == 0 {
+ return nil
+ }
+ tilelib.mtx.Lock()
+ defer tilelib.mtx.Unlock()
+ if tilelib.taglib == nil || tilelib.taglib.Len() == 0 {
+ tilelib.taglib = &tagLibrary{}
+ err := tilelib.taglib.setTags(newtagset)
+ if err != nil {
+ return err
+ }
+ if tilelib.encoder != nil {
+ err = tilelib.encoder.Encode(LibraryEntry{
+ TagSet: newtagset,
+ })
+ if err != nil {
+ return err
+ }
+ }
+ } else if tilelib.taglib.Len() != len(newtagset) {
+ return fmt.Errorf("cannot merge libraries with differing tagsets")
+ } else {
+ current := tilelib.taglib.Tags()
+ for i := range newtagset {
+ if !bytes.Equal(newtagset[i], current[i]) {
+ return fmt.Errorf("cannot merge libraries with differing tagsets")
+ }
+ }
+ }
+ return nil
+}
+
+func (tilelib *tileLibrary) loadTileVariants(tvs []TileVariant, variantmap map[tileLibRef]tileVariantID) error {
+ for _, tv := range tvs {
+ // Assign a new variant ID (unique across all inputs)
+ // for each input variant.
+ variantmap[tileLibRef{Tag: tv.Tag, Variant: tv.Variant}] = tilelib.getRef(tv.Tag, tv.Sequence, tv.Ref).Variant
+ }
+ return nil
+}
+
+func (tilelib *tileLibrary) loadCompactGenomes(cgs []CompactGenome, variantmap map[tileLibRef]tileVariantID) error {
+ log.Debugf("loadCompactGenomes: %d", len(cgs))
+ var wg sync.WaitGroup
+ errs := make(chan error, 1)
+ for _, cg := range cgs {
+ wg.Add(1)
+ cg := cg
+ go func() {
+ defer wg.Done()
+ for i, variant := range cg.Variants {
+ if len(errs) > 0 {
+ return
+ }
+ if variant == 0 {
+ continue
+ }
+ tag := tagID(i / 2)
+ newvariant, ok := variantmap[tileLibRef{Tag: tag, Variant: variant}]
+ if !ok {
+ err := fmt.Errorf("oops: genome %q has variant %d for tag %d, but that variant was not in its library", cg.Name, variant, tag)
+ select {
+ case errs <- err:
+ default:
+ }
+ return
+ }
+ // log.Tracef("loadCompactGenomes: cg %s tag %d variant %d => %d", cg.Name, tag, variant, newvariant)
+ cg.Variants[i] = newvariant
+ }
+ if tilelib.onAddGenome != nil {
+ err := tilelib.onAddGenome(cg)
+ if err != nil {
+ select {
+ case errs <- err:
+ default:
+ }
+ return
+ }
+ }
+ if tilelib.encoder != nil {
+ err := tilelib.encoder.Encode(LibraryEntry{
+ CompactGenomes: []CompactGenome{cg},
+ })
+ if err != nil {
+ select {
+ case errs <- err:
+ default:
+ }
+ return
+ }
+ }
+ if tilelib.compactGenomes != nil {
+ tilelib.mtx.Lock()
+ defer tilelib.mtx.Unlock()
+ tilelib.compactGenomes[cg.Name] = cg.Variants
+ }
+ }()
+ }
+ wg.Wait()
+ go close(errs)
+ return <-errs
+}
+
+func (tilelib *tileLibrary) loadCompactSequences(cseqs []CompactSequence, variantmap map[tileLibRef]tileVariantID) error {
+ log.Infof("loadCompactSequences: %d todo", len(cseqs))
+ for _, cseq := range cseqs {
+ log.Infof("loadCompactSequences: checking %s", cseq.Name)
+ for _, tseq := range cseq.TileSequences {
+ for i, libref := range tseq {
+ if libref.Variant == 0 {
+ // No variant (e.g., import
+ // dropped tiles with
+ // no-calls) = no translation.
+ continue
+ }
+ v, ok := variantmap[libref]
+ if !ok {
+ return fmt.Errorf("oops: CompactSequence %q has variant %d for tag %d, but that variant was not in its library", cseq.Name, libref.Variant, libref.Tag)
+ }
+ tseq[i].Variant = v
+ }
+ }
+ if tilelib.encoder != nil {
+ if err := tilelib.encoder.Encode(LibraryEntry{
+ CompactSequences: []CompactSequence{cseq},
+ }); err != nil {
+ return err
+ }
+ }
+ if tilelib.onAddRefseq != nil {
+ err := tilelib.onAddRefseq(cseq)
+ if err != nil {
+ return err
+ }
+ }
+ log.Infof("loadCompactSequences: checking %s done", cseq.Name)
+ }
+ tilelib.mtx.Lock()
+ defer tilelib.mtx.Unlock()
+ if tilelib.refseqs == nil {
+ tilelib.refseqs = map[string]map[string][]tileLibRef{}
+ }
+ for _, cseq := range cseqs {
+ tilelib.refseqs[cseq.Name] = cseq.TileSequences
+ }
+ log.Info("loadCompactSequences: done")
+ return nil
+}
+
+func allFiles(path string, re *regexp.Regexp) ([]string, error) {
+ var files []string
+ f, err := open(path)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+ fis, err := f.Readdir(-1)
+ if err != nil {
+ return []string{path}, nil
+ }
+ for _, fi := range fis {
+ if fi.Name() == "." || fi.Name() == ".." {
+ continue
+ } else if child := path + "/" + fi.Name(); fi.IsDir() {
+ add, err := allFiles(child, re)
+ if err != nil {
+ return nil, err
+ }
+ files = append(files, add...)
+ } else if re == nil || re.MatchString(child) {
+ files = append(files, child)
+ }
+ }
+ sort.Strings(files)
+ return files, nil
+}
+
+var matchGobFile = regexp.MustCompile(`\.gob(\.gz)?$`)