+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).Variant
+ }
+ return nil
+}
+
+func (tilelib *tileLibrary) loadCompactGenomes(cgs []CompactGenome, variantmap map[tileLibRef]tileVariantID, onLoadGenome func(CompactGenome)) 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 onLoadGenome != nil {
+ onLoadGenome(cg)
+ }
+ 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.Debugf("loadCompactSequences: %d", len(cseqs))
+ for _, cseq := range cseqs {
+ 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
+ }
+ }
+ }
+ 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
+ }
+ return nil
+}
+
+// Load library data from rdr. Tile variants might be renumbered in
+// the process; in that case, genomes variants will be renumbered to
+// match.
+//
+// If onLoadGenome is non-nil, call it on each CompactGenome entry.
+func (tilelib *tileLibrary) LoadGob(ctx context.Context, rdr io.Reader, gz bool, onLoadGenome func(CompactGenome)) error {
+ cgs := []CompactGenome{}
+ cseqs := []CompactSequence{}
+ variantmap := map[tileLibRef]tileVariantID{}
+ err := DecodeLibrary(rdr, gz, func(ent *LibraryEntry) error {
+ if ctx.Err() != nil {
+ return ctx.Err()
+ }
+ if err := tilelib.loadTagSet(ent.TagSet); err != nil {
+ return err
+ }
+ if err := tilelib.loadTileVariants(ent.TileVariants, variantmap); err != nil {
+ return err
+ }
+ cgs = append(cgs, ent.CompactGenomes...)
+ cseqs = append(cseqs, ent.CompactSequences...)
+ return nil
+ })
+ if err != nil {
+ return err
+ }
+ if ctx.Err() != nil {
+ return ctx.Err()
+ }
+ err = tilelib.loadCompactGenomes(cgs, variantmap, onLoadGenome)
+ if err != nil {
+ return err
+ }
+ err = tilelib.loadCompactSequences(cseqs, variantmap)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+type importStats struct {
+ InputFile string
+ InputLabel string
+ InputLength int
+ InputCoverage int
+ PathLength int
+ DroppedOutOfOrderTiles int
+}
+
+func (tilelib *tileLibrary) TileFasta(filelabel string, rdr io.Reader, matchChromosome *regexp.Regexp) (tileSeq, []importStats, error) {