1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
26 "git.arvados.org/arvados.git/sdk/go/arvados"
27 "github.com/arvados/lightning/hgvs"
28 "github.com/klauspost/pgzip"
29 "github.com/kshedden/gonpy"
30 "github.com/sirupsen/logrus"
31 log "github.com/sirupsen/logrus"
34 type tvVariant struct {
36 librefs map[tileLibRef]bool
39 type outputFormat interface {
42 Head(out io.Writer, cgs []CompactGenome, cases []bool, p float64) error
43 Print(out io.Writer, seqname string, varslice []tvVariant) error
44 Finish(outdir string, out io.Writer, seqname string) error
48 var outputFormats = map[string]func() outputFormat{
49 "hgvs-numpy": func() outputFormat {
50 return &formatHGVSNumpy{alleles: map[string][][]int8{}}
52 "hgvs-onehot": func() outputFormat { return formatHGVSOneHot{} },
53 "hgvs": func() outputFormat { return formatHGVS{} },
54 "pvcf": func() outputFormat { return formatPVCF{} },
55 "vcf": func() outputFormat { return formatVCF{} },
58 type exporter struct {
59 outputFormat outputFormat
68 func (cmd *exporter) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
72 fmt.Fprintf(stderr, "%s\n", err)
75 flags := flag.NewFlagSet("", flag.ContinueOnError)
76 flags.SetOutput(stderr)
77 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
78 pprofdir := flags.String("pprof-dir", "", "write Go profile data to `directory` periodically")
79 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
80 projectUUID := flags.String("project", "", "project `UUID` for output data")
81 priority := flags.Int("priority", 500, "container request priority")
82 refname := flags.String("ref", "", "reference genome `name`")
83 inputDir := flags.String("input-dir", ".", "input `directory`")
84 cases := flags.String("cases", "", "file indicating which genomes are positive cases (for computing p-values)")
85 flags.Float64Var(&cmd.maxPValue, "p-value", 1, "do chi square test and omit columns with p-value above this threshold")
86 outputDir := flags.String("output-dir", ".", "output `directory`")
87 outputFormatStr := flags.String("output-format", "hgvs", "output `format`: hgvs, pvcf, or vcf")
88 outputBed := flags.String("output-bed", "", "also output bed `file`")
89 flags.BoolVar(&cmd.outputPerChrom, "output-per-chromosome", true, "output one file per chromosome")
90 flags.BoolVar(&cmd.compress, "z", false, "write gzip-compressed output files")
91 labelsFilename := flags.String("output-labels", "", "also output genome labels csv `file`")
92 flags.IntVar(&cmd.maxTileSize, "max-tile-size", 50000, "don't try to make annotations for tiles bigger than given `size`")
93 cmd.filter.Flags(flags)
94 err = flags.Parse(args)
95 if err == flag.ErrHelp {
98 } else if err != nil {
102 err = fmt.Errorf("extra unparsed command line arguments: %q", flag.Args())
106 if f, ok := outputFormats[*outputFormatStr]; !ok {
107 err = fmt.Errorf("invalid output format %q", *outputFormatStr)
110 cmd.outputFormat = f()
115 log.Println(http.ListenAndServe(*pprof, nil))
119 go writeProfilesPeriodically(*pprofdir)
123 if *outputDir != "." {
124 err = errors.New("cannot specify output directory in container mode: not implemented")
127 runner := arvadosContainerRunner{
128 Name: "lightning export",
129 Client: arvados.NewClientFromEnv(),
130 ProjectUUID: *projectUUID,
136 err = runner.TranslatePaths(inputDir, cases)
140 if *outputBed != "" {
141 if strings.Contains(*outputBed, "/") {
142 err = fmt.Errorf("cannot use -output-bed filename %q containing '/' char", *outputBed)
145 *outputBed = "/mnt/output/" + *outputBed
147 runner.Args = []string{"export", "-local=true",
149 "-pprof-dir", "/mnt/output",
152 "-p-value", fmt.Sprintf("%f", cmd.maxPValue),
153 "-output-format", *outputFormatStr,
154 "-output-bed", *outputBed,
155 "-output-labels", "/mnt/output/labels.csv",
156 "-output-per-chromosome=" + fmt.Sprintf("%v", cmd.outputPerChrom),
157 "-max-tile-size", fmt.Sprintf("%d", cmd.maxTileSize),
158 "-input-dir", *inputDir,
159 "-output-dir", "/mnt/output",
160 "-z=" + fmt.Sprintf("%v", cmd.compress),
162 runner.Args = append(runner.Args, cmd.filter.Args()...)
164 output, err = runner.Run()
168 fmt.Fprintln(stdout, output)
172 var cgs []CompactGenome
173 tilelib := &tileLibrary{
175 retainTileSequences: true,
176 compactGenomes: map[string][]tileVariantID{},
178 err = tilelib.LoadDir(context.Background(), *inputDir)
183 refseq, ok := tilelib.refseqs[*refname]
185 err = fmt.Errorf("reference name %q not found in input; have %v", *refname, func() (names []string) {
186 for name := range tilelib.refseqs {
187 names = append(names, name)
194 log.Infof("filtering: %+v", cmd.filter)
195 cmd.filter.Apply(tilelib)
197 names := cgnames(tilelib)
198 for _, name := range names {
199 cgs = append(cgs, CompactGenome{Name: name, Variants: tilelib.compactGenomes[name]})
201 if *labelsFilename != "" {
202 log.Infof("writing labels to %s", *labelsFilename)
204 f, err = os.OpenFile(*labelsFilename, os.O_CREATE|os.O_WRONLY, 0777)
209 for i, name := range names {
210 _, err = fmt.Fprintf(f, "%d,%q,%q\n", i, trimFilenameForLabel(name), cmd.outputFormat.Filename())
212 err = fmt.Errorf("write %s: %w", *labelsFilename, err)
218 err = fmt.Errorf("close %s: %w", *labelsFilename, err)
223 cmd.cases = make([]bool, len(names))
225 log.Infof("reading cases file: %s", *cases)
227 f, err = open(*cases)
233 buf, err = io.ReadAll(f)
237 for _, pattern := range bytes.Split(buf, []byte("\n")) {
238 if len(pattern) == 0 {
241 pattern := string(pattern)
243 for i, name := range names {
244 if !strings.Contains(name, pattern) {
247 err = fmt.Errorf("pattern %q in cases file matches multiple genome IDs: %q, %q", pattern, names[idx], name)
254 log.Warnf("pattern %q in cases file does not match any genome IDs", pattern)
257 cmd.cases[idx] = true
263 var bedbufw *bufio.Writer
264 if *outputBed != "" {
265 bedfile, err = os.OpenFile(*outputBed, os.O_CREATE|os.O_WRONLY, 0666)
269 defer bedfile.Close()
270 bedbufw = bufio.NewWriterSize(bedfile, 16*1024*1024)
274 err = cmd.export(*outputDir, bedout, tilelib, refseq, cgs)
279 err = bedbufw.Flush()
283 err = bedfile.Close()
291 func (cmd *exporter) export(outdir string, bedout io.Writer, tilelib *tileLibrary, refseq map[string][]tileLibRef, cgs []CompactGenome) error {
292 var seqnames []string
293 var missing []tileLibRef
294 for seqname, librefs := range refseq {
295 seqnames = append(seqnames, seqname)
296 for _, libref := range librefs {
297 if libref.Variant != 0 && tilelib.TileVariantSequence(libref) == nil {
298 missing = append(missing, libref)
302 sort.Strings(seqnames)
304 if len(missing) > 0 {
305 if limit := 100; len(missing) > limit {
306 log.Warnf("first %d missing tiles: %v", limit, missing[:limit])
308 log.Warnf("missing tiles: %v", missing)
310 return fmt.Errorf("%d needed tiles are missing from library", len(missing))
313 outw := make([]io.WriteCloser, len(seqnames))
314 bedw := make([]io.WriteCloser, len(seqnames))
316 var merges sync.WaitGroup
317 merge := func(dst io.Writer, src []io.WriteCloser, label string) {
319 for i, seqname := range seqnames {
326 log.Infof("writing %s %s", seqname, label)
327 scanner := bufio.NewScanner(pr)
330 dst.Write(scanner.Bytes())
331 dst.Write([]byte{'\n'})
334 log.Infof("writing %s %s done", seqname, label)
338 if cmd.outputPerChrom {
339 for i, seqname := range seqnames {
340 fnm := filepath.Join(outdir, strings.Replace(cmd.outputFormat.Filename(), ".", "."+seqname+".", 1))
344 f, err := os.OpenFile(fnm, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
349 log.Infof("writing %q", f.Name())
352 z := pgzip.NewWriter(f)
356 err = cmd.outputFormat.Head(outw[i], cgs, cmd.cases, cmd.maxPValue)
362 fnm := filepath.Join(outdir, cmd.outputFormat.Filename())
366 f, err := os.OpenFile(fnm, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
371 log.Infof("writing %q", fnm)
372 var out io.Writer = f
374 z := pgzip.NewWriter(out)
378 cmd.outputFormat.Head(out, cgs, cmd.cases, cmd.maxPValue)
379 merge(out, outw, "output")
382 merge(bedout, bedw, "bed")
385 throttle := throttle{Max: runtime.NumCPU()}
386 if max := cmd.outputFormat.MaxGoroutines(); max > 0 {
389 log.Infof("assembling %d sequences in %d goroutines", len(seqnames), throttle.Max)
390 for seqidx, seqname := range seqnames {
391 seqidx, seqname := seqidx, seqname
396 defer throttle.Release()
400 outwb := bufio.NewWriterSize(outw, 8*1024*1024)
401 eachVariant(bedw, tilelib.taglib.keylen, seqname, refseq[seqname], tilelib, cgs, cmd.outputFormat.PadLeft(), cmd.maxTileSize, func(varslice []tvVariant) {
402 err := cmd.outputFormat.Print(outwb, seqname, varslice)
405 err := cmd.outputFormat.Finish(outdir, outwb, seqname)
416 return throttle.Err()
419 // Align genome tiles to reference tiles, call callback func on each
420 // variant, and (if bedw is not nil) write tile coverage to bedw.
421 func eachVariant(bedw io.Writer, taglen int, seqname string, reftiles []tileLibRef, tilelib *tileLibrary, cgs []CompactGenome, padLeft bool, maxTileSize int, callback func(varslice []tvVariant)) {
423 progressbar := time.NewTicker(time.Minute)
424 defer progressbar.Stop()
425 var outmtx sync.Mutex
428 variantAt := map[int][]tvVariant{} // variantAt[chromOffset][genomeIndex*2+phase]
429 for refstep, libref := range reftiles {
431 case <-progressbar.C:
434 fin := t0.Add(time.Duration(float64(time.Now().Sub(t0)) * float64(len(reftiles)) / float64(refstep)))
435 eta = fmt.Sprintf("%v (%v)", fin.Format(time.RFC3339), fin.Sub(time.Now()))
439 log.Printf("exportSeq: %s: refstep %d of %d, %.0f/s, ETA %v", seqname, refstep, len(reftiles), float64(refstep)/time.Now().Sub(t0).Seconds(), eta)
442 diffs := map[tileLibRef][]hgvs.Variant{}
443 refseq := tilelib.TileVariantSequence(libref)
444 tagcoverage := 0 // number of times the start tag was found in genomes -- max is len(cgs)*2
445 for cgidx, cg := range cgs {
446 for phase := 0; phase < 2; phase++ {
447 var variant tileVariantID
448 if i := int(libref.Tag)*2 + phase; len(cg.Variants) > i {
449 variant = cg.Variants[i]
454 if variant == libref.Variant || variant == 0 {
457 glibref := tileLibRef{Tag: libref.Tag, Variant: variant}
458 vars, ok := diffs[glibref]
460 genomeseq := tilelib.TileVariantSequence(glibref)
461 if len(genomeseq) == 0 {
462 // Hash is known but sequence
463 // is not, e.g., retainNoCalls
464 // was false during import
467 if len(genomeseq) > maxTileSize {
470 refSequence := refseq
471 // If needed, extend the
472 // reference sequence up to
473 // the tag at the end of the
474 // genomeseq sequence.
475 refstepend := refstep + 1
476 for refstepend < len(reftiles) && len(refSequence) >= taglen && !bytes.EqualFold(refSequence[len(refSequence)-taglen:], genomeseq[len(genomeseq)-taglen:]) && len(refSequence) <= maxTileSize {
477 if &refSequence[0] == &refseq[0] {
478 refSequence = append([]byte(nil), refSequence...)
480 refSequence = append(refSequence, tilelib.TileVariantSequence(reftiles[refstepend])...)
483 // (TODO: handle no-calls)
484 if len(refSequence) <= maxTileSize {
485 refstr := strings.ToUpper(string(refSequence))
486 genomestr := strings.ToUpper(string(genomeseq))
487 vars, _ = hgvs.Diff(refstr, genomestr, time.Second)
489 diffs[glibref] = vars
491 for _, v := range vars {
496 varslice := variantAt[v.Position]
498 varslice = make([]tvVariant, len(cgs)*2)
499 variantAt[v.Position] = varslice
501 varslice[cgidx*2+phase].Variant = v
502 if varslice[cgidx*2+phase].librefs == nil {
503 varslice[cgidx*2+phase].librefs = map[tileLibRef]bool{glibref: true}
505 varslice[cgidx*2+phase].librefs[glibref] = true
510 refpos += len(refseq) - taglen
512 // Flush entries from variantAt that are behind
513 // refpos. Flush all entries if this is the last
514 // reftile of the path/chromosome.
515 flushpos := make([]int, 0, len(variantAt))
516 lastrefstep := refstep == len(reftiles)-1
517 for pos := range variantAt {
518 if lastrefstep || pos <= refpos {
519 flushpos = append(flushpos, pos)
522 sort.Slice(flushpos, func(i, j int) bool { return flushpos[i] < flushpos[j] })
523 flushvariants := make([][]tvVariant, len(flushpos))
524 for i, pos := range flushpos {
525 varslice := variantAt[pos]
526 delete(variantAt, pos)
527 // Check for uninitialized (zero-value)
528 // elements in varslice
529 for i := range varslice {
530 if varslice[i].Position != 0 {
531 // Not a zero-value element
534 // Set the position so
535 // varslice[*].Position are all equal
536 varslice[i].Position = pos
537 // This could be either =ref or a
538 // missing/low-quality tile. Figure
540 vidx := int(libref.Tag)*2 + i%2
541 if vidx >= len(cgs[i/2].Variants) {
543 varslice[i].New = "-"
546 v := cgs[i/2].Variants[vidx]
547 if v < 1 || len(tilelib.TileVariantSequence(tileLibRef{Tag: libref.Tag, Variant: v})) == 0 {
548 // Missing/low-quality tile.
549 varslice[i].New = "-" // fasta "gap of indeterminate length"
552 flushvariants[i] = varslice
556 defer outmtx.Unlock()
557 for _, varslice := range flushvariants {
561 if bedw != nil && len(refseq) > 0 {
562 tilestart := refpos - len(refseq) + taglen
567 thickstart := tilestart + taglen
573 // coverage score, 0 to 1000
576 score = 1000 * tagcoverage / len(cgs) / 2
579 fmt.Fprintf(bedw, "%s %d %d %d %d . %d %d\n",
580 seqname, tilestart, tileend,
583 thickstart, thickend)
588 func bucketVarsliceByRef(varslice []tvVariant) map[string]map[string]int {
589 byref := map[string]map[string]int{}
590 for _, v := range varslice {
591 if v.Ref == "" && v.New == "" {
601 alts = map[string]int{}
609 type formatVCF struct{}
611 func (formatVCF) MaxGoroutines() int { return 0 }
612 func (formatVCF) Filename() string { return "out.vcf" }
613 func (formatVCF) PadLeft() bool { return true }
614 func (formatVCF) Finish(string, io.Writer, string) error { return nil }
615 func (formatVCF) Head(out io.Writer, cgs []CompactGenome, cases []bool, p float64) error {
616 _, err := fmt.Fprint(out, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n")
619 func (formatVCF) Print(out io.Writer, seqname string, varslice []tvVariant) error {
620 for ref, alts := range bucketVarsliceByRef(varslice) {
621 altslice := make([]string, 0, len(alts))
622 for alt := range alts {
623 altslice = append(altslice, alt)
625 sort.Strings(altslice)
628 for i, a := range altslice {
632 info += strconv.Itoa(alts[a])
634 _, err := fmt.Fprintf(out, "%s\t%d\t.\t%s\t%s\t.\t.\t%s\n", seqname, varslice[0].Position, ref, strings.Join(altslice, ","), info)
642 type formatPVCF struct{}
644 func (formatPVCF) MaxGoroutines() int { return 0 }
645 func (formatPVCF) Filename() string { return "out.vcf" }
646 func (formatPVCF) PadLeft() bool { return true }
647 func (formatPVCF) Finish(string, io.Writer, string) error { return nil }
648 func (formatPVCF) Head(out io.Writer, cgs []CompactGenome, cases []bool, p float64) error {
649 fmt.Fprintln(out, `##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">`)
650 fmt.Fprintf(out, "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT")
651 for _, cg := range cgs {
652 fmt.Fprintf(out, "\t%s", cg.Name)
654 _, err := fmt.Fprintf(out, "\n")
658 func (formatPVCF) Print(out io.Writer, seqname string, varslice []tvVariant) error {
659 for ref, alts := range bucketVarsliceByRef(varslice) {
660 altslice := make([]string, 0, len(alts))
661 for alt := range alts {
662 altslice = append(altslice, alt)
664 sort.Strings(altslice)
665 for i, a := range altslice {
668 _, err := fmt.Fprintf(out, "%s\t%d\t.\t%s\t%s\t.\t.\t.\tGT", seqname, varslice[0].Position, ref, strings.Join(altslice, ","))
672 for i := 0; i < len(varslice); i += 2 {
673 v1, v2 := varslice[i], varslice[i+1]
674 a1, a2 := alts[v1.New], alts[v2.New]
676 // variant on allele 0 belongs on a
677 // different output line -- same
678 // chr,pos but different "ref" length
684 _, err := fmt.Fprintf(out, "\t%d/%d", a1, a2)
689 _, err = out.Write([]byte{'\n'})
697 type formatHGVS struct{}
699 func (formatHGVS) MaxGoroutines() int { return 0 }
700 func (formatHGVS) Filename() string { return "out.tsv" }
701 func (formatHGVS) PadLeft() bool { return false }
702 func (formatHGVS) Head(out io.Writer, cgs []CompactGenome, cases []bool, p float64) error { return nil }
703 func (formatHGVS) Finish(string, io.Writer, string) error { return nil }
704 func (formatHGVS) Print(out io.Writer, seqname string, varslice []tvVariant) error {
705 for i := 0; i < len(varslice)/2; i++ {
707 out.Write([]byte{'\t'})
709 var1, var2 := varslice[i*2], varslice[i*2+1]
710 if var1.New == "-" || var2.New == "-" {
711 _, err := out.Write([]byte{'N'})
717 if var1.Variant == var2.Variant {
718 if var1.Ref == var1.New {
719 _, err := out.Write([]byte{'.'})
724 _, err := fmt.Fprintf(out, "%s:g.%s", seqname, var1.String())
730 _, err := fmt.Fprintf(out, "%s:g.[%s];[%s]", seqname, var1.String(), var2.String())
736 _, err := out.Write([]byte{'\n'})
740 type formatHGVSOneHot struct{}
742 func (formatHGVSOneHot) MaxGoroutines() int { return 0 }
743 func (formatHGVSOneHot) Filename() string { return "out.tsv" }
744 func (formatHGVSOneHot) PadLeft() bool { return false }
745 func (formatHGVSOneHot) Head(out io.Writer, cgs []CompactGenome, cases []bool, p float64) error {
748 func (formatHGVSOneHot) Finish(string, io.Writer, string) error { return nil }
749 func (formatHGVSOneHot) Print(out io.Writer, seqname string, varslice []tvVariant) error {
750 vars := map[hgvs.Variant]bool{}
751 for _, v := range varslice {
753 vars[v.Variant] = true
757 // sort variants to ensure output is deterministic
758 sorted := make([]hgvs.Variant, 0, len(vars))
759 for v := range vars {
760 sorted = append(sorted, v)
762 sort.Slice(sorted, func(a, b int) bool { return hgvs.Less(sorted[a], sorted[b]) })
764 for _, v := range sorted {
768 fmt.Fprintf(out, "%s.%s", seqname, v.String())
769 for i := 0; i < len(varslice); i += 2 {
770 if varslice[i].Variant == v || varslice[i+1].Variant == v {
771 out.Write([]byte("\t1"))
773 out.Write([]byte("\t0"))
776 _, err := out.Write([]byte{'\n'})
784 type formatHGVSNumpy struct {
787 alleles map[string][][]int8 // alleles[seqname][variantidx][genomeidx*2+phase]
792 func (*formatHGVSNumpy) MaxGoroutines() int { return 4 }
793 func (*formatHGVSNumpy) Filename() string { return "annotations.csv" }
794 func (*formatHGVSNumpy) PadLeft() bool { return false }
795 func (f *formatHGVSNumpy) Head(out io.Writer, cgs []CompactGenome, cases []bool, p float64) error {
800 func (f *formatHGVSNumpy) Print(outw io.Writer, seqname string, varslice []tvVariant) error {
801 // sort variants to ensure output is deterministic
802 sorted := make([]hgvs.Variant, 0, len(varslice))
803 for _, v := range varslice {
804 sorted = append(sorted, v.Variant)
806 sort.Slice(sorted, func(a, b int) bool { return hgvs.Less(sorted[a], sorted[b]) })
809 seqalleles := f.alleles[seqname]
812 chi2x := make([]bool, 0, len(varslice))
813 chi2y := make([]bool, 0, len(varslice))
815 // append a row to seqalleles for each unique non-ref variant
817 var previous hgvs.Variant
818 for _, v := range sorted {
819 if previous == v || v.Ref == v.New || v.New == "-" {
823 chi2x, chi2y := chi2x, chi2y
824 newrow := make([]int8, len(varslice))
825 for i, allele := range varslice {
826 if allele.Variant == v {
828 chi2x = append(chi2x, true)
829 chi2y = append(chi2y, f.cases[i/2])
830 } else if allele.Variant.New == "-" {
833 chi2x = append(chi2x, false)
834 chi2y = append(chi2y, f.cases[i/2])
837 if f.maxPValue < 1 && pvalue(chi2x, chi2y) > f.maxPValue {
840 seqalleles = append(seqalleles, newrow)
841 _, err := fmt.Fprintf(outw, "%d,%q\n", len(seqalleles)-1, seqname+"."+v.String())
848 f.alleles[seqname] = seqalleles
852 func (f *formatHGVSNumpy) Finish(outdir string, _ io.Writer, seqname string) error {
853 // Write seqname's data to a .npy matrix with one row per
854 // genome and 2 columns per variant.
856 seqalleles := f.alleles[seqname]
857 delete(f.alleles, seqname)
859 if len(seqalleles) == 0 {
862 out := make([]int8, len(seqalleles)*len(seqalleles[0]))
863 rows := len(seqalleles[0]) / 2
864 cols := len(seqalleles) * 2
865 // copy seqalleles[varidx][genome*2+phase] to
866 // out[genome*nvars*2 + varidx*2 + phase]
867 for varidx, alleles := range seqalleles {
868 for g := 0; g < len(alleles)/2; g++ {
869 aa, ab := alleles[g*2], alleles[g*2+1]
870 if aa < 0 || ab < 0 {
872 out[g*cols+varidx*2] = -1
873 out[g*cols+varidx*2+1] = -1
874 } else if aa > 0 && ab > 0 {
876 out[g*cols+varidx*2] = 1
877 } else if aa > 0 || ab > 0 {
879 out[g*cols+varidx*2+1] = 1
883 outf, err := os.OpenFile(outdir+"/matrix."+seqname+".npy", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0777)
888 bufw := bufio.NewWriter(outf)
889 npw, err := gonpy.NewWriter(nopCloser{bufw})
893 log.WithFields(logrus.Fields{
897 }).Info("writing numpy")
898 npw.Shape = []int{rows, cols}
899 f.writelock.Lock() // serialize because WriteInt8 uses lots of memory