1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
28 "git.arvados.org/arvados.git/sdk/go/arvados"
29 "github.com/arvados/lightning/hgvs"
30 "github.com/kshedden/gonpy"
31 log "github.com/sirupsen/logrus"
32 "golang.org/x/crypto/blake2b"
35 type sliceNumpy struct {
38 chi2CaseControlColumn string
39 chi2CaseControlFile string
46 func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
50 fmt.Fprintf(stderr, "%s\n", err)
53 flags := flag.NewFlagSet("", flag.ContinueOnError)
54 flags.SetOutput(stderr)
55 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
56 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
57 projectUUID := flags.String("project", "", "project `UUID` for output data")
58 priority := flags.Int("priority", 500, "container request priority")
59 inputDir := flags.String("input-dir", "./in", "input `directory`")
60 outputDir := flags.String("output-dir", "./out", "output `directory`")
61 ref := flags.String("ref", "", "reference name (if blank, choose last one that appears in input)")
62 regionsFilename := flags.String("regions", "", "only output columns/annotations that intersect regions in specified bed `file`")
63 expandRegions := flags.Int("expand-regions", 0, "expand specified regions by `N` base pairs on each side`")
64 mergeOutput := flags.Bool("merge-output", false, "merge output into one matrix.npy and one matrix.annotations.csv")
65 hgvsSingle := flags.Bool("single-hgvs-matrix", false, "also generate hgvs-based matrix")
66 hgvsChunked := flags.Bool("chunked-hgvs-matrix", false, "also generate hgvs-based matrix per chromosome")
67 onehotSingle := flags.Bool("single-onehot", false, "generate one-hot tile-based matrix")
68 onehotChunked := flags.Bool("chunked-onehot", false, "generate one-hot tile-based matrix per input chunk")
69 flags.IntVar(&cmd.threads, "threads", 16, "number of memory-hungry assembly threads")
70 flags.StringVar(&cmd.chi2CaseControlFile, "chi2-case-control-file", "", "tsv file or directory indicating cases and controls for Χ² test (if directory, all .tsv files will be read)")
71 flags.StringVar(&cmd.chi2CaseControlColumn, "chi2-case-control-column", "", "name of case/control column in case-control files for Χ² test (value must be 0 for control, 1 for case)")
72 flags.Float64Var(&cmd.chi2PValue, "chi2-p-value", 1, "do Χ² test and omit columns with p-value above this threshold")
73 cmd.filter.Flags(flags)
74 err = flags.Parse(args)
75 if err == flag.ErrHelp {
78 } else if err != nil {
84 log.Println(http.ListenAndServe(*pprof, nil))
88 if cmd.chi2PValue != 1 && (cmd.chi2CaseControlFile == "" || cmd.chi2CaseControlColumn == "") {
89 log.Errorf("cannot use provided -chi2-p-value=%f because -chi2-case-control-file= or -chi2-case-control-column= value is empty", cmd.chi2PValue)
94 runner := arvadosContainerRunner{
95 Name: "lightning slice-numpy",
96 Client: arvados.NewClientFromEnv(),
97 ProjectUUID: *projectUUID,
104 err = runner.TranslatePaths(inputDir, regionsFilename, &cmd.chi2CaseControlFile)
108 runner.Args = []string{"slice-numpy", "-local=true",
110 "-input-dir=" + *inputDir,
111 "-output-dir=/mnt/output",
112 "-threads=" + fmt.Sprintf("%d", cmd.threads),
113 "-regions=" + *regionsFilename,
114 "-expand-regions=" + fmt.Sprintf("%d", *expandRegions),
115 "-merge-output=" + fmt.Sprintf("%v", *mergeOutput),
116 "-single-hgvs-matrix=" + fmt.Sprintf("%v", *hgvsSingle),
117 "-chunked-hgvs-matrix=" + fmt.Sprintf("%v", *hgvsChunked),
118 "-single-onehot=" + fmt.Sprintf("%v", *onehotSingle),
119 "-chunked-onehot=" + fmt.Sprintf("%v", *onehotChunked),
120 "-chi2-case-control-file=" + cmd.chi2CaseControlFile,
121 "-chi2-case-control-column=" + cmd.chi2CaseControlColumn,
122 "-chi2-p-value=" + fmt.Sprintf("%f", cmd.chi2PValue),
124 runner.Args = append(runner.Args, cmd.filter.Args()...)
126 output, err = runner.Run()
130 fmt.Fprintln(stdout, output)
134 infiles, err := allFiles(*inputDir, matchGobFile)
138 if len(infiles) == 0 {
139 err = fmt.Errorf("no input files found in %s", *inputDir)
142 sort.Strings(infiles)
144 var refseq map[string][]tileLibRef
145 var reftiledata = make(map[tileLibRef][]byte, 11000000)
146 in0, err := open(infiles[0])
151 matchGenome, err := regexp.Compile(cmd.filter.MatchGenome)
153 err = fmt.Errorf("-match-genome: invalid regexp: %q", cmd.filter.MatchGenome)
159 DecodeLibrary(in0, strings.HasSuffix(infiles[0], ".gz"), func(ent *LibraryEntry) error {
160 if len(ent.TagSet) > 0 {
163 for _, cseq := range ent.CompactSequences {
164 if cseq.Name == *ref || *ref == "" {
165 refseq = cseq.TileSequences
168 for _, cg := range ent.CompactGenomes {
169 if matchGenome.MatchString(cg.Name) {
170 cmd.cgnames = append(cmd.cgnames, cg.Name)
173 for _, tv := range ent.TileVariants {
175 reftiledata[tileLibRef{tv.Tag, tv.Variant}] = tv.Sequence
185 err = fmt.Errorf("%s: reference sequence not found", infiles[0])
188 if len(tagset) == 0 {
189 err = fmt.Errorf("tagset not found")
193 taglib := &tagLibrary{}
194 err = taglib.setTags(tagset)
198 taglen := taglib.TagLen()
200 if len(cmd.cgnames) == 0 {
201 err = fmt.Errorf("no genomes found matching regexp %q", cmd.filter.MatchGenome)
204 sort.Strings(cmd.cgnames)
205 err = cmd.useCaseControlFiles()
209 cmd.minCoverage = int(math.Ceil(cmd.filter.MinCoverage * float64(len(cmd.cgnames))))
212 labelsFilename := *outputDir + "/samples.csv"
213 log.Infof("writing labels to %s", labelsFilename)
215 f, err = os.Create(labelsFilename)
220 for i, name := range cmd.cgnames {
222 if cmd.chi2Cases != nil && cmd.chi2Cases[i] {
225 _, err = fmt.Fprintf(f, "%d,%q,%d\n", i, trimFilenameForLabel(name), cc)
227 err = fmt.Errorf("write %s: %w", labelsFilename, err)
233 err = fmt.Errorf("close %s: %w", labelsFilename, err)
238 log.Info("indexing reference tiles")
239 type reftileinfo struct {
240 variant tileVariantID
241 seqname string // chr1
242 pos int // distance from start of chromosome to starttag
243 tiledata []byte // acgtggcaa...
245 isdup := map[tagID]bool{}
246 reftile := map[tagID]*reftileinfo{}
247 for seqname, cseq := range refseq {
249 for _, libref := range cseq {
250 tiledata := reftiledata[libref]
251 if len(tiledata) == 0 {
252 err = fmt.Errorf("missing tiledata for tag %d variant %d in %s in ref", libref.Tag, libref.Variant, seqname)
255 foundthistag := false
256 taglib.FindAll(tiledata[:len(tiledata)-1], func(tagid tagID, offset, _ int) {
257 if !foundthistag && tagid == libref.Tag {
261 if dupref, ok := reftile[tagid]; ok {
262 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique, also found inside %+v from %s @ %d", tileLibRef{Tag: tagid, Variant: dupref.variant}, dupref.seqname, dupref.pos, libref, seqname, pos+offset+1)
263 delete(reftile, tagid)
265 log.Printf("found tag %d at offset %d inside tile variant %+v on %s @ %d", tagid, offset, libref, seqname, pos+offset+1)
269 if isdup[libref.Tag] {
270 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique", libref, seqname, pos)
271 } else if reftile[libref.Tag] != nil {
272 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique", tileLibRef{Tag: libref.Tag, Variant: reftile[libref.Tag].variant}, reftile[libref.Tag].seqname, reftile[libref.Tag].pos)
273 delete(reftile, libref.Tag)
274 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique", libref, seqname, pos)
275 isdup[libref.Tag] = true
277 reftile[libref.Tag] = &reftileinfo{
279 variant: libref.Variant,
284 pos += len(tiledata) - taglen
286 log.Printf("... %s done, len %d", seqname, pos+taglen)
290 if *regionsFilename != "" {
291 log.Printf("loading regions from %s", *regionsFilename)
292 mask, err = makeMask(*regionsFilename, *expandRegions)
296 log.Printf("before applying mask, len(reftile) == %d", len(reftile))
297 log.Printf("deleting reftile entries for regions outside %d intervals", mask.Len())
298 for tag, rt := range reftile {
299 if !mask.Check(strings.TrimPrefix(rt.seqname, "chr"), rt.pos, rt.pos+len(rt.tiledata)) {
303 log.Printf("after applying mask, len(reftile) == %d", len(reftile))
306 type hgvsColSet map[hgvs.Variant][2][]int8
307 encodeHGVS := throttle{Max: len(refseq)}
308 encodeHGVSTodo := map[string]chan hgvsColSet{}
309 tmpHGVSCols := map[string]*os.File{}
311 for seqname := range refseq {
313 f, err = os.Create(*outputDir + "/tmp." + seqname + ".gob")
317 defer os.Remove(f.Name())
318 bufw := bufio.NewWriterSize(f, 1<<24)
319 enc := gob.NewEncoder(bufw)
320 tmpHGVSCols[seqname] = f
321 todo := make(chan hgvsColSet, 128)
322 encodeHGVSTodo[seqname] = todo
323 encodeHGVS.Go(func() error {
324 for colset := range todo {
325 err := enc.Encode(colset)
327 encodeHGVS.Report(err)
338 var toMerge [][]int16
339 if *mergeOutput || *hgvsSingle {
340 toMerge = make([][]int16, len(infiles))
342 var onehotIndirect [][2][]uint32 // [chunkIndex][axis][index]
343 var onehotXrefs [][]onehotXref
345 onehotIndirect = make([][2][]uint32, len(infiles))
346 onehotXrefs = make([][]onehotXref, len(infiles))
349 throttleMem := throttle{Max: cmd.threads} // TODO: estimate using mem and data size
350 throttleNumpyMem := throttle{Max: cmd.threads/2 + 1}
351 log.Info("generating annotations and numpy matrix for each slice")
353 for infileIdx, infile := range infiles {
354 infileIdx, infile := infileIdx, infile
355 throttleMem.Go(func() error {
356 seq := make(map[tagID][]TileVariant, 50000)
357 cgs := make(map[string]CompactGenome, len(cmd.cgnames))
358 f, err := open(infile)
363 log.Infof("%04d: reading %s", infileIdx, infile)
364 err = DecodeLibrary(f, strings.HasSuffix(infile, ".gz"), func(ent *LibraryEntry) error {
365 for _, tv := range ent.TileVariants {
369 if mask != nil && reftile[tv.Tag] == nil {
375 variants := seq[tv.Tag]
376 if len(variants) == 0 {
377 variants = make([]TileVariant, 100)
379 for len(variants) <= int(tv.Variant) {
380 variants = append(variants, TileVariant{})
382 variants[int(tv.Variant)] = tv
383 seq[tv.Tag] = variants
385 for _, cg := range ent.CompactGenomes {
386 if !matchGenome.MatchString(cg.Name) {
389 // pad to full slice size
390 // to avoid out-of-bounds
392 if sliceSize := 2 * int(cg.EndTag-cg.StartTag); len(cg.Variants) < sliceSize {
393 cg.Variants = append(cg.Variants, make([]tileVariantID, sliceSize-len(cg.Variants))...)
402 tagstart := cgs[cmd.cgnames[0]].StartTag
403 tagend := cgs[cmd.cgnames[0]].EndTag
407 log.Infof("%04d: renumber/dedup variants for tags %d-%d", infileIdx, tagstart, tagend)
408 variantRemap := make([][]tileVariantID, tagend-tagstart)
409 throttleCPU := throttle{Max: runtime.GOMAXPROCS(0)}
410 for tag, variants := range seq {
411 tag, variants := tag, variants
412 throttleCPU.Acquire()
414 defer throttleCPU.Release()
415 count := make(map[[blake2b.Size256]byte]int, len(variants))
419 count[blake2b.Sum256(rt.tiledata)] = 0
422 for _, cg := range cgs {
423 idx := int(tag-tagstart) * 2
424 for allele := 0; allele < 2; allele++ {
425 v := cg.Variants[idx+allele]
426 if v > 0 && len(variants[v].Sequence) > 0 {
427 count[variants[v].Blake2b]++
431 // hash[i] will be the hash of
432 // the variant(s) that should
433 // be at rank i (0-based).
434 hash := make([][blake2b.Size256]byte, 0, len(count))
435 for b := range count {
436 hash = append(hash, b)
438 sort.Slice(hash, func(i, j int) bool {
439 bi, bj := &hash[i], &hash[j]
440 if ci, cj := count[*bi], count[*bj]; ci != cj {
443 return bytes.Compare((*bi)[:], (*bj)[:]) < 0
446 // rank[b] will be the 1-based
447 // new variant number for
448 // variants whose hash is b.
449 rank := make(map[[blake2b.Size256]byte]tileVariantID, len(hash))
450 for i, h := range hash {
451 rank[h] = tileVariantID(i + 1)
453 // remap[v] will be the new
454 // variant number for original
456 remap := make([]tileVariantID, len(variants))
457 for i, tv := range variants {
458 remap[i] = rank[tv.Blake2b]
460 variantRemap[tag-tagstart] = remap
462 rt.variant = rank[blake2b.Sum256(rt.tiledata)]
468 var onehotChunk [][]int8
469 var onehotXref []onehotXref
471 annotationsFilename := fmt.Sprintf("%s/matrix.%04d.annotations.csv", *outputDir, infileIdx)
472 log.Infof("%04d: writing %s", infileIdx, annotationsFilename)
473 annof, err := os.Create(annotationsFilename)
477 annow := bufio.NewWriterSize(annof, 1<<20)
479 for tag := tagstart; tag < tagend; tag++ {
480 rt, ok := reftile[tag]
485 // Excluded by specified
486 // regions, or reference does
487 // not use any variant of this
488 // tile. (TODO: log this?
489 // mention it in annotations?)
492 remap := variantRemap[tag-tagstart]
493 maxv := tileVariantID(0)
494 for _, v := range remap {
499 if *onehotChunked || *onehotSingle {
500 onehot, xrefs := cmd.tv2homhet(cgs, maxv, remap, tag, tagstart)
501 onehotChunk = append(onehotChunk, onehot...)
502 onehotXref = append(onehotXref, xrefs...)
504 fmt.Fprintf(annow, "%d,%d,%d,=,%s,%d,,,\n", tag, outcol, rt.variant, rt.seqname, rt.pos)
506 reftilestr := strings.ToUpper(string(rt.tiledata))
508 done := make([]bool, maxv+1)
509 variantDiffs := make([][]hgvs.Variant, maxv+1)
510 for v, tv := range variants {
512 if v == rt.variant || done[v] {
517 if len(tv.Sequence) < taglen || !bytes.HasSuffix(rt.tiledata, tv.Sequence[len(tv.Sequence)-taglen:]) {
518 fmt.Fprintf(annow, "%d,%d,%d,,%s,%d,,,\n", tag, outcol, v, rt.seqname, rt.pos)
521 if lendiff := len(rt.tiledata) - len(tv.Sequence); lendiff < -1000 || lendiff > 1000 {
522 fmt.Fprintf(annow, "%d,%d,%d,,%s,%d,,,\n", tag, outcol, v, rt.seqname, rt.pos)
525 diffs, _ := hgvs.Diff(reftilestr, strings.ToUpper(string(tv.Sequence)), 0)
526 for i := range diffs {
527 diffs[i].Position += rt.pos
529 for _, diff := range diffs {
530 fmt.Fprintf(annow, "%d,%d,%d,%s:g.%s,%s,%d,%s,%s,%s\n", tag, outcol, v, rt.seqname, diff.String(), rt.seqname, diff.Position, diff.Ref, diff.New, diff.Left)
533 variantDiffs[v] = diffs
537 // We can now determine, for each HGVS
538 // variant (diff) in this reftile
539 // region, whether a given genome
540 // phase/allele (1) has the variant, (0) has
541 // =ref or a different variant in that
542 // position, or (-1) is lacking
543 // coverage / couldn't be diffed.
544 hgvsCol := hgvsColSet{}
545 for _, diffs := range variantDiffs {
546 for _, diff := range diffs {
547 if _, ok := hgvsCol[diff]; ok {
550 hgvsCol[diff] = [2][]int8{
551 make([]int8, len(cmd.cgnames)),
552 make([]int8, len(cmd.cgnames)),
556 for row, name := range cmd.cgnames {
557 variants := cgs[name].Variants[(tag-tagstart)*2:]
558 for ph := 0; ph < 2; ph++ {
560 if int(v) >= len(remap) {
566 // hgvsCol[*][ph][row] is already 0
567 } else if len(variantDiffs[v]) == 0 {
568 // lacking coverage / couldn't be diffed
569 for _, col := range hgvsCol {
573 for _, diff := range variantDiffs[v] {
574 hgvsCol[diff][ph][row] = 1
579 for diff, colpair := range hgvsCol {
580 allele2homhet(colpair)
581 if !cmd.filterHGVScolpair(colpair) {
582 delete(hgvsCol, diff)
585 if len(hgvsCol) > 0 {
586 encodeHGVSTodo[rt.seqname] <- hgvsCol
601 // transpose onehotChunk[col][row] to numpy[row*ncols+col]
602 rows := len(cmd.cgnames)
603 cols := len(onehotChunk)
604 log.Infof("%04d: preparing onehot numpy (rows=%d, cols=%d, mem=%d)", infileIdx, len(cmd.cgnames), len(onehotChunk), len(cmd.cgnames)*len(onehotChunk))
605 throttleNumpyMem.Acquire()
606 out := onehotcols2int8(onehotChunk)
607 fnm := fmt.Sprintf("%s/onehot.%04d.npy", *outputDir, infileIdx)
608 err = writeNumpyInt8(fnm, out, rows, cols)
612 fnm = fmt.Sprintf("%s/onehot-columns.%04d.npy", *outputDir, infileIdx)
613 err = writeNumpyInt32(fnm, onehotXref2int32(onehotXref), 4, len(onehotXref))
618 throttleNumpyMem.Release()
621 onehotIndirect[infileIdx] = onehotChunk2Indirect(onehotChunk)
622 onehotXrefs[infileIdx] = onehotXref
623 n := len(onehotIndirect[infileIdx][0])
624 log.Infof("%04d: keeping onehot coordinates in memory (n=%d, mem=%d)", infileIdx, n, n*8)
626 if !(*onehotSingle || *onehotChunked) || *mergeOutput || *hgvsSingle {
627 log.Infof("%04d: preparing numpy", infileIdx)
628 throttleNumpyMem.Acquire()
629 rows := len(cmd.cgnames)
631 out := make([]int16, rows*cols)
632 for row, name := range cmd.cgnames {
633 out := out[row*cols:]
635 for col, v := range cgs[name].Variants {
636 tag := tagstart + tagID(col/2)
637 if mask != nil && reftile[tag] == nil {
640 if variants, ok := seq[tag]; ok && len(variants) > int(v) && len(variants[v].Sequence) > 0 {
641 out[outcol] = int16(variantRemap[tag-tagstart][v])
651 throttleNumpyMem.Release()
652 if *mergeOutput || *hgvsSingle {
653 log.Infof("%04d: matrix fragment %d rows x %d cols", infileIdx, rows, cols)
654 toMerge[infileIdx] = out
656 if !*mergeOutput && !*onehotChunked && !*onehotSingle {
657 fnm := fmt.Sprintf("%s/matrix.%04d.npy", *outputDir, infileIdx)
658 err = writeNumpyInt16(fnm, out, rows, cols)
665 log.Infof("%s: done (%d/%d)", infile, int(atomic.AddInt64(&done, 1)), len(infiles))
669 if err = throttleMem.Wait(); err != nil {
674 log.Info("flushing hgvsCols temp files")
675 for seqname := range refseq {
676 close(encodeHGVSTodo[seqname])
678 err = encodeHGVS.Wait()
682 for seqname := range refseq {
683 log.Infof("%s: reading hgvsCols from temp file", seqname)
684 f := tmpHGVSCols[seqname]
685 _, err = f.Seek(0, io.SeekStart)
689 var hgvsCols hgvsColSet
690 dec := gob.NewDecoder(bufio.NewReaderSize(f, 1<<24))
692 err = dec.Decode(&hgvsCols)
697 log.Infof("%s: sorting %d hgvs variants", seqname, len(hgvsCols))
698 variants := make([]hgvs.Variant, 0, len(hgvsCols))
699 for v := range hgvsCols {
700 variants = append(variants, v)
702 sort.Slice(variants, func(i, j int) bool {
703 vi, vj := &variants[i], &variants[j]
704 if vi.Position != vj.Position {
705 return vi.Position < vj.Position
706 } else if vi.Ref != vj.Ref {
707 return vi.Ref < vj.Ref
709 return vi.New < vj.New
712 rows := len(cmd.cgnames)
713 cols := len(variants) * 2
714 log.Infof("%s: building hgvs matrix (rows=%d, cols=%d, mem=%d)", seqname, rows, cols, rows*cols)
715 out := make([]int8, rows*cols)
716 for varIdx, variant := range variants {
717 hgvsCols := hgvsCols[variant]
718 for row := range cmd.cgnames {
719 for ph := 0; ph < 2; ph++ {
720 out[row*cols+varIdx+ph] = hgvsCols[ph][row]
724 err = writeNumpyInt8(fmt.Sprintf("%s/hgvs.%s.npy", *outputDir, seqname), out, rows, cols)
730 fnm := fmt.Sprintf("%s/hgvs.%s.annotations.csv", *outputDir, seqname)
731 log.Infof("%s: writing hgvs column labels to %s", seqname, fnm)
732 var hgvsLabels bytes.Buffer
733 for varIdx, variant := range variants {
734 fmt.Fprintf(&hgvsLabels, "%d,%s:g.%s\n", varIdx, seqname, variant.String())
736 err = ioutil.WriteFile(fnm, hgvsLabels.Bytes(), 0666)
743 if *mergeOutput || *hgvsSingle {
744 var annow *bufio.Writer
747 annoFilename := fmt.Sprintf("%s/matrix.annotations.csv", *outputDir)
748 annof, err = os.Create(annoFilename)
752 annow = bufio.NewWriterSize(annof, 1<<20)
755 rows := len(cmd.cgnames)
757 for _, chunk := range toMerge {
758 cols += len(chunk) / rows
760 log.Infof("merging output matrix (rows=%d, cols=%d, mem=%d) and annotations", rows, cols, rows*cols*2)
763 out = make([]int16, rows*cols)
765 hgvsCols := map[string][2][]int16{} // hgvs -> [[g0,g1,g2,...], [g0,g1,g2,...]] (slice of genomes for each phase)
767 for outIdx, chunk := range toMerge {
768 chunkcols := len(chunk) / rows
770 for row := 0; row < rows; row++ {
771 copy(out[row*cols+startcol:], chunk[row*chunkcols:(row+1)*chunkcols])
774 toMerge[outIdx] = nil
776 annotationsFilename := fmt.Sprintf("%s/matrix.%04d.annotations.csv", *outputDir, outIdx)
777 log.Infof("reading %s", annotationsFilename)
778 buf, err := os.ReadFile(annotationsFilename)
783 err = os.Remove(annotationsFilename)
788 for _, line := range bytes.Split(buf, []byte{'\n'}) {
792 fields := bytes.SplitN(line, []byte{','}, 9)
793 tag, _ := strconv.Atoi(string(fields[0]))
794 incol, _ := strconv.Atoi(string(fields[1]))
795 tileVariant, _ := strconv.Atoi(string(fields[2]))
796 hgvsID := string(fields[3])
797 seqname := string(fields[4])
798 pos, _ := strconv.Atoi(string(fields[5]))
801 // Null entry for un-diffable
806 // Null entry for ref tile
809 if mask != nil && !mask.Check(strings.TrimPrefix(seqname, "chr"), pos, pos+len(refseq)) {
810 // The tile intersects one of
811 // the selected regions, but
812 // this particular HGVS
816 hgvsColPair := hgvsCols[hgvsID]
817 if hgvsColPair[0] == nil {
818 // values in new columns start
819 // out as -1 ("no data yet")
820 // or 0 ("=ref") here, may
821 // change to 1 ("hgvs variant
822 // present") below, either on
823 // this line or a future line.
824 hgvsColPair = [2][]int16{make([]int16, len(cmd.cgnames)), make([]int16, len(cmd.cgnames))}
825 rt, ok := reftile[tagID(tag)]
827 err = fmt.Errorf("bug: seeing annotations for tag %d, but it has no reftile entry", tag)
830 for ph := 0; ph < 2; ph++ {
831 for row := 0; row < rows; row++ {
832 v := chunk[row*chunkcols+incol*2+ph]
833 if tileVariantID(v) == rt.variant {
834 hgvsColPair[ph][row] = 0
836 hgvsColPair[ph][row] = -1
840 hgvsCols[hgvsID] = hgvsColPair
842 hgvsref := hgvs.Variant{
847 fmt.Fprintf(annow, "%d,%d,%d,%s:g.%s,%s,%d,%s,%s,%s\n", tag, incol+startcol/2, rt.variant, seqname, hgvsref.String(), seqname, pos, refseq, refseq, fields[8])
851 fmt.Fprintf(annow, "%d,%d,%d,%s,%s,%d,%s,%s,%s\n", tag, incol+startcol/2, tileVariant, hgvsID, seqname, pos, refseq, fields[7], fields[8])
853 for ph := 0; ph < 2; ph++ {
854 for row := 0; row < rows; row++ {
855 v := chunk[row*chunkcols+incol*2+ph]
856 if int(v) == tileVariant {
857 hgvsColPair[ph][row] = 1
863 startcol += chunkcols
874 err = writeNumpyInt16(fmt.Sprintf("%s/matrix.npy", *outputDir), out, rows, cols)
882 cols = len(hgvsCols) * 2
883 log.Printf("building hgvs-based matrix: %d rows x %d cols", rows, cols)
884 out = make([]int16, rows*cols)
885 hgvsIDs := make([]string, 0, cols/2)
886 for hgvsID := range hgvsCols {
887 hgvsIDs = append(hgvsIDs, hgvsID)
889 sort.Strings(hgvsIDs)
890 var hgvsLabels bytes.Buffer
891 for idx, hgvsID := range hgvsIDs {
892 fmt.Fprintf(&hgvsLabels, "%d,%s\n", idx, hgvsID)
893 for ph := 0; ph < 2; ph++ {
894 hgvscol := hgvsCols[hgvsID][ph]
895 for row, val := range hgvscol {
896 out[row*cols+idx*2+ph] = val
900 err = writeNumpyInt16(fmt.Sprintf("%s/hgvs.npy", *outputDir), out, rows, cols)
905 fnm := fmt.Sprintf("%s/hgvs.annotations.csv", *outputDir)
906 log.Printf("writing hgvs labels: %s", fnm)
907 err = ioutil.WriteFile(fnm, hgvsLabels.Bytes(), 0777)
915 for _, part := range onehotIndirect {
916 nzCount += len(part[0])
918 onehot := make([]uint32, nzCount*2) // [r,r,r,...,c,c,c,...]
919 var xrefs []onehotXref
921 for i, part := range onehotIndirect {
922 for i := range part[1] {
923 part[1][i] += uint32(outcol)
925 copy(onehot[outcol:], part[0])
926 copy(onehot[outcol+nzCount:], part[1])
927 outcol += len(part[0])
928 xrefs = append(xrefs, onehotXrefs[i]...)
935 fnm := fmt.Sprintf("%s/onehot.npy", *outputDir)
936 err = writeNumpyUint32(fnm, onehot, 2, nzCount)
940 fnm = fmt.Sprintf("%s/onehot-columns.npy", *outputDir)
941 err = writeNumpyInt32(fnm, onehotXref2int32(xrefs), 4, len(xrefs))
949 // Read case/control files, remove non-case/control entries from
950 // cmd.cgnames, and build cmd.chi2Cases.
951 func (cmd *sliceNumpy) useCaseControlFiles() error {
952 if cmd.chi2CaseControlFile == "" {
955 infiles, err := allFiles(cmd.chi2CaseControlFile, nil)
959 // index in cmd.cgnames => case(true) / control(false)
961 for _, infile := range infiles {
962 f, err := open(infile)
966 buf, err := io.ReadAll(f)
972 for _, tsv := range bytes.Split(buf, []byte{'\n'}) {
976 split := strings.Split(string(tsv), "\t")
979 for col, name := range split {
980 if name == cmd.chi2CaseControlColumn {
986 return fmt.Errorf("%s: no column named %q in header row %q", infile, cmd.chi2CaseControlColumn, tsv)
990 if len(split) <= ccCol {
995 for i, name := range cmd.cgnames {
996 if strings.Contains(name, pattern) {
998 log.Warnf("pattern %q in %s matches multiple genome IDs (%qs, %q)", pattern, infile, cmd.cgnames[found], name)
1004 log.Warnf("pattern %q in %s does not match any genome IDs", pattern, infile)
1007 if split[ccCol] == "0" {
1010 if split[ccCol] == "1" {
1015 allnames := cmd.cgnames
1019 for i, name := range allnames {
1020 if cc, ok := cc[i]; ok {
1021 cmd.cgnames = append(cmd.cgnames, name)
1022 cmd.chi2Cases = append(cmd.chi2Cases, cc)
1028 log.Printf("%d cases, %d controls, %d neither (dropped)", ncases, len(cmd.cgnames)-ncases, len(allnames)-len(cmd.cgnames))
1032 func (cmd *sliceNumpy) filterHGVScolpair(colpair [2][]int8) bool {
1033 if cmd.chi2PValue >= 1 {
1036 col0 := make([]bool, 0, len(cmd.chi2Cases))
1037 col1 := make([]bool, 0, len(cmd.chi2Cases))
1038 cases := make([]bool, 0, len(cmd.chi2Cases))
1039 for i, c := range cmd.chi2Cases {
1040 if colpair[0][i] < 0 {
1043 col0 = append(col0, colpair[0][i] != 0)
1044 col1 = append(col1, colpair[1][i] != 0)
1045 cases = append(cases, c)
1047 return len(cases) >= cmd.minCoverage &&
1048 (pvalue(col0, cases) <= cmd.chi2PValue || pvalue(col1, cases) <= cmd.chi2PValue)
1051 func writeNumpyUint32(fnm string, out []uint32, rows, cols int) error {
1052 output, err := os.Create(fnm)
1056 defer output.Close()
1057 bufw := bufio.NewWriterSize(output, 1<<26)
1058 npw, err := gonpy.NewWriter(nopCloser{bufw})
1062 log.WithFields(log.Fields{
1066 "bytes": rows * cols * 4,
1067 }).Infof("writing numpy: %s", fnm)
1068 npw.Shape = []int{rows, cols}
1069 npw.WriteUint32(out)
1074 return output.Close()
1077 func writeNumpyInt32(fnm string, out []int32, rows, cols int) error {
1078 output, err := os.Create(fnm)
1082 defer output.Close()
1083 bufw := bufio.NewWriterSize(output, 1<<26)
1084 npw, err := gonpy.NewWriter(nopCloser{bufw})
1088 log.WithFields(log.Fields{
1092 "bytes": rows * cols * 4,
1093 }).Infof("writing numpy: %s", fnm)
1094 npw.Shape = []int{rows, cols}
1100 return output.Close()
1103 func writeNumpyInt16(fnm string, out []int16, rows, cols int) error {
1104 output, err := os.Create(fnm)
1108 defer output.Close()
1109 bufw := bufio.NewWriterSize(output, 1<<26)
1110 npw, err := gonpy.NewWriter(nopCloser{bufw})
1114 log.WithFields(log.Fields{
1118 "bytes": rows * cols * 2,
1119 }).Infof("writing numpy: %s", fnm)
1120 npw.Shape = []int{rows, cols}
1126 return output.Close()
1129 func writeNumpyInt8(fnm string, out []int8, rows, cols int) error {
1130 output, err := os.Create(fnm)
1134 defer output.Close()
1135 bufw := bufio.NewWriterSize(output, 1<<26)
1136 npw, err := gonpy.NewWriter(nopCloser{bufw})
1140 log.WithFields(log.Fields{
1144 "bytes": rows * cols,
1145 }).Infof("writing numpy: %s", fnm)
1146 npw.Shape = []int{rows, cols}
1152 return output.Close()
1155 func allele2homhet(colpair [2][]int8) {
1156 a, b := colpair[0], colpair[1]
1157 for i, av := range a {
1159 if av < 0 || bv < 0 {
1162 } else if av > 0 && bv > 0 {
1165 } else if av > 0 || bv > 0 {
1169 // ref (or a different variant in same position)
1170 // (this is a no-op) a[i], b[i] = 0, 0
1175 type onehotXref struct {
1177 variant tileVariantID
1182 const onehotXrefSize = unsafe.Sizeof(onehotXref{})
1184 // Build onehot matrix (m[variant*2+isHet][genome] == 0 or 1) for all
1185 // variants of a single tile/tag#.
1187 // Return nil if no tile variant passes Χ² filter.
1188 func (cmd *sliceNumpy) tv2homhet(cgs map[string]CompactGenome, maxv tileVariantID, remap []tileVariantID, tag, chunkstarttag tagID) ([][]int8, []onehotXref) {
1190 // everyone has the most common variant
1193 tagoffset := tag - chunkstarttag
1195 for _, cg := range cgs {
1196 if cg.Variants[tagoffset*2] > 0 && cg.Variants[tagoffset*2+1] > 0 {
1200 if coverage < cmd.minCoverage {
1203 obs := make([][]bool, (maxv+1)*2) // 2 slices (hom + het) for each variant#
1204 for i := range obs {
1205 obs[i] = make([]bool, len(cmd.cgnames))
1207 for cgid, name := range cmd.cgnames {
1208 cgvars := cgs[name].Variants
1209 for v := tileVariantID(2); v <= maxv; v++ {
1210 if remap[cgvars[tagoffset*2]] == v && remap[cgvars[tagoffset*2+1]] == v {
1211 obs[v*2][cgid] = true
1212 } else if remap[cgvars[tagoffset*2]] == v || remap[cgvars[tagoffset*2+1]] == v {
1213 obs[v*2+1][cgid] = true
1218 var xref []onehotXref
1219 for homcol := 4; homcol < len(obs); homcol += 2 {
1221 pvalue(obs[homcol], cmd.chi2Cases),
1222 pvalue(obs[homcol+1], cmd.chi2Cases),
1224 if cmd.chi2PValue < 1 && !(p[0] < cmd.chi2PValue || p[1] < cmd.chi2PValue) {
1227 for het := 0; het < 2; het++ {
1228 onehot = append(onehot, bool2int8(obs[homcol+het]))
1229 xref = append(xref, onehotXref{
1231 variant: tileVariantID(homcol / 2),
1240 func bool2int8(in []bool) []int8 {
1241 out := make([]int8, len(in))
1242 for i, v := range in {
1250 // convert a []onehotXref with length N to a numpy-style []int32
1251 // matrix with N columns, one row per field of onehotXref struct.
1253 // Hom/het row contains hom=0, het=1.
1255 // P-value row contains 1000000x actual p-value.
1256 func onehotXref2int32(xrefs []onehotXref) []int32 {
1258 xdata := make([]int32, 4*xcols)
1259 for i, xref := range xrefs {
1260 xdata[i] = int32(xref.tag)
1261 xdata[xcols+i] = int32(xref.variant)
1263 xdata[xcols*2+i] = 1
1265 xdata[xcols*3+i] = int32(xref.pvalue * 1000000)
1270 // transpose onehot data from in[col][row] to numpy-style
1271 // out[row*cols+col].
1272 func onehotcols2int8(in [][]int8) []int8 {
1278 out := make([]int8, rows*cols)
1279 for row := 0; row < rows; row++ {
1280 outrow := out[row*cols:]
1281 for col, incol := range in {
1282 outrow[col] = incol[row]
1288 // Return [2][]uint32{rowIndices, colIndices} indicating which
1289 // elements of matrixT[c][r] have non-zero values.
1290 func onehotChunk2Indirect(matrixT [][]int8) [2][]uint32 {
1292 for c, col := range matrixT {
1293 for r, val := range col {
1295 nz[0] = append(nz[0], uint32(r))
1296 nz[1] = append(nz[1], uint32(c))