+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
+}
+
+func (tilelib *tileLibrary) dump(out io.Writer) {
+ printTV := func(tag int, variant tileVariantID) {
+ if variant < 1 {
+ fmt.Fprintf(out, " -")
+ } else if tag >= len(tilelib.variant) {
+ fmt.Fprintf(out, " (!tag=%d)", tag)
+ } else if int(variant) > len(tilelib.variant[tag]) {
+ fmt.Fprintf(out, " (tag=%d,!variant=%d)", tag, variant)
+ } else {
+ fmt.Fprintf(out, " %x", tilelib.variant[tag][variant-1][:8])
+ }
+ }
+ for refname, refseqs := range tilelib.refseqs {
+ for seqname, seq := range refseqs {
+ fmt.Fprintf(out, "ref %s %s", refname, seqname)
+ for _, libref := range seq {
+ printTV(int(libref.Tag), libref.Variant)
+ }
+ fmt.Fprintf(out, "\n")
+ }
+ }
+ for name, cg := range tilelib.compactGenomes {
+ fmt.Fprintf(out, "cg %s", name)
+ for tag, variant := range cg {
+ printTV(tag/2, variant)
+ }
+ fmt.Fprintf(out, "\n")
+ }
+}
+
+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) {