1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
30 "git.arvados.org/arvados.git/sdk/go/arvados"
31 "github.com/arvados/lightning/hgvs"
32 "github.com/james-bowman/nlp"
33 "github.com/kshedden/gonpy"
34 "github.com/sirupsen/logrus"
35 log "github.com/sirupsen/logrus"
36 "golang.org/x/crypto/blake2b"
37 "gonum.org/v1/gonum/mat"
40 const annotationMaxTileSpan = 100
42 type sliceNumpy struct {
54 trainingSet []int // samples index => training set index, or -1 if not in training set
56 pvalue func(onehot []bool) float64
60 func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
61 err := cmd.run(prog, args, stdin, stdout, stderr)
63 fmt.Fprintf(stderr, "%s\n", err)
69 func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
70 flags := flag.NewFlagSet("", flag.ContinueOnError)
71 flags.SetOutput(stderr)
72 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
73 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
74 arvadosRAM := flags.Int("arvados-ram", 750000000000, "amount of memory to request for arvados container (`bytes`)")
75 arvadosVCPUs := flags.Int("arvados-vcpus", 96, "number of VCPUs to request for arvados container")
76 projectUUID := flags.String("project", "", "project `UUID` for output data")
77 priority := flags.Int("priority", 500, "container request priority")
78 inputDir := flags.String("input-dir", "./in", "input `directory`")
79 outputDir := flags.String("output-dir", "./out", "output `directory`")
80 ref := flags.String("ref", "", "reference name (if blank, choose last one that appears in input)")
81 regionsFilename := flags.String("regions", "", "only output columns/annotations that intersect regions in specified bed `file`")
82 expandRegions := flags.Int("expand-regions", 0, "expand specified regions by `N` base pairs on each side`")
83 mergeOutput := flags.Bool("merge-output", false, "merge output into one matrix.npy and one matrix.annotations.csv")
84 hgvsSingle := flags.Bool("single-hgvs-matrix", false, "also generate hgvs-based matrix")
85 hgvsChunked := flags.Bool("chunked-hgvs-matrix", false, "also generate hgvs-based matrix per chromosome")
86 onehotSingle := flags.Bool("single-onehot", false, "generate one-hot tile-based matrix")
87 onehotChunked := flags.Bool("chunked-onehot", false, "generate one-hot tile-based matrix per input chunk")
88 samplesFilename := flags.String("samples", "", "`samples.csv` file with training/validation and case/control groups (see 'lightning choose-samples')")
89 caseControlOnly := flags.Bool("case-control-only", false, "drop samples that are not in case/control groups")
90 onlyPCA := flags.Bool("pca", false, "run principal component analysis, write components to pca.npy and samples.csv")
91 flags.IntVar(&cmd.pcaComponents, "pca-components", 4, "number of PCA components to compute / use in logistic regression")
92 maxPCATiles := flags.Int("max-pca-tiles", 0, "maximum tiles to use as PCA input (filter, then drop every 2nd colum pair until below max)")
93 debugTag := flags.Int("debug-tag", -1, "log debugging details about specified tag")
94 flags.IntVar(&cmd.threads, "threads", 16, "number of memory-hungry assembly threads, and number of VCPUs to request for arvados container")
95 flags.Float64Var(&cmd.chi2PValue, "chi2-p-value", 1, "do Χ² test (or logistic regression if -samples file has PCA components) and omit columns with p-value above this threshold")
96 flags.BoolVar(&cmd.includeVariant1, "include-variant-1", false, "include most common variant when building one-hot matrix")
97 cmd.filter.Flags(flags)
98 err := flags.Parse(args)
99 if err == flag.ErrHelp {
101 } else if err != nil {
103 } else if flags.NArg() > 0 {
104 return fmt.Errorf("errant command line arguments after parsed flags: %v", flags.Args())
109 log.Println(http.ListenAndServe(*pprof, nil))
113 if cmd.chi2PValue != 1 && *samplesFilename == "" {
114 return fmt.Errorf("cannot use provided -chi2-p-value=%f because -samples= value is empty", cmd.chi2PValue)
117 cmd.debugTag = tagID(*debugTag)
120 runner := arvadosContainerRunner{
121 Name: "lightning slice-numpy",
122 Client: arvados.NewClientFromEnv(),
123 ProjectUUID: *projectUUID,
124 RAM: int64(*arvadosRAM),
125 VCPUs: *arvadosVCPUs,
130 err = runner.TranslatePaths(inputDir, regionsFilename, samplesFilename)
134 runner.Args = []string{"slice-numpy", "-local=true",
136 "-input-dir=" + *inputDir,
137 "-output-dir=/mnt/output",
138 "-threads=" + fmt.Sprintf("%d", cmd.threads),
139 "-regions=" + *regionsFilename,
140 "-expand-regions=" + fmt.Sprintf("%d", *expandRegions),
141 "-merge-output=" + fmt.Sprintf("%v", *mergeOutput),
142 "-single-hgvs-matrix=" + fmt.Sprintf("%v", *hgvsSingle),
143 "-chunked-hgvs-matrix=" + fmt.Sprintf("%v", *hgvsChunked),
144 "-single-onehot=" + fmt.Sprintf("%v", *onehotSingle),
145 "-chunked-onehot=" + fmt.Sprintf("%v", *onehotChunked),
146 "-samples=" + *samplesFilename,
147 "-case-control-only=" + fmt.Sprintf("%v", *caseControlOnly),
148 "-pca=" + fmt.Sprintf("%v", *onlyPCA),
149 "-pca-components=" + fmt.Sprintf("%d", cmd.pcaComponents),
150 "-max-pca-tiles=" + fmt.Sprintf("%d", *maxPCATiles),
151 "-chi2-p-value=" + fmt.Sprintf("%f", cmd.chi2PValue),
152 "-include-variant-1=" + fmt.Sprintf("%v", cmd.includeVariant1),
153 "-debug-tag=" + fmt.Sprintf("%d", cmd.debugTag),
155 runner.Args = append(runner.Args, cmd.filter.Args()...)
157 output, err = runner.Run()
161 fmt.Fprintln(stdout, output)
165 infiles, err := allFiles(*inputDir, matchGobFile)
169 if len(infiles) == 0 {
170 err = fmt.Errorf("no input files found in %s", *inputDir)
173 sort.Strings(infiles)
175 var refseq map[string][]tileLibRef
176 var reftiledata = make(map[tileLibRef][]byte, 11000000)
177 in0, err := open(infiles[0])
182 matchGenome, err := regexp.Compile(cmd.filter.MatchGenome)
184 err = fmt.Errorf("-match-genome: invalid regexp: %q", cmd.filter.MatchGenome)
188 if *samplesFilename != "" {
189 cmd.samples, err = loadSampleInfo(*samplesFilename)
193 if len(cmd.samples[0].pcaComponents) > 0 {
194 cmd.pvalue = glmPvalueFunc(cmd.samples, cmd.pcaComponents)
195 // Unfortunately, statsmodel/glm lib logs
196 // stuff to os.Stdout when it panics on an
197 // unsolvable problem. We recover() from the
198 // panic in glm.go, but we also need to
199 // commandeer os.Stdout to avoid producing
200 // large quantities of logs.
201 stdoutWas := os.Stdout
202 defer func() { os.Stdout = stdoutWas }()
203 os.Stdout, err = os.Open(os.DevNull)
208 } else if *caseControlOnly {
209 return fmt.Errorf("-case-control-only does not make sense without -samples")
214 err = DecodeLibrary(in0, strings.HasSuffix(infiles[0], ".gz"), func(ent *LibraryEntry) error {
215 if len(ent.TagSet) > 0 {
218 for _, cseq := range ent.CompactSequences {
219 if cseq.Name == *ref || *ref == "" {
220 refseq = cseq.TileSequences
223 for _, cg := range ent.CompactGenomes {
224 if matchGenome.MatchString(cg.Name) {
225 cmd.cgnames = append(cmd.cgnames, cg.Name)
228 for _, tv := range ent.TileVariants {
230 reftiledata[tileLibRef{tv.Tag, tv.Variant}] = tv.Sequence
240 err = fmt.Errorf("%s: reference sequence not found", infiles[0])
243 if len(tagset) == 0 {
244 err = fmt.Errorf("tagset not found")
248 taglib := &tagLibrary{}
249 err = taglib.setTags(tagset)
253 taglen := taglib.TagLen()
254 sort.Strings(cmd.cgnames)
256 if len(cmd.cgnames) == 0 {
257 return fmt.Errorf("fatal: 0 matching samples in library, nothing to do")
259 cmd.trainingSet = make([]int, len(cmd.cgnames))
260 if *samplesFilename == "" {
261 cmd.trainingSetSize = len(cmd.cgnames)
262 for i, name := range cmd.cgnames {
263 cmd.samples = append(cmd.samples, sampleInfo{
264 id: trimFilenameForLabel(name),
267 cmd.trainingSet[i] = i
269 } else if len(cmd.cgnames) != len(cmd.samples) {
270 return fmt.Errorf("mismatched sample list: %d samples in library, %d in %s", len(cmd.cgnames), len(cmd.samples), *samplesFilename)
272 for i, name := range cmd.cgnames {
273 if s := trimFilenameForLabel(name); s != cmd.samples[i].id {
274 return fmt.Errorf("mismatched sample list: sample %d is %q in library, %q in %s", i, s, cmd.samples[i].id, *samplesFilename)
277 if *caseControlOnly {
278 for i := 0; i < len(cmd.samples); i++ {
279 if !cmd.samples[i].isTraining && !cmd.samples[i].isValidation {
280 if i+1 < len(cmd.samples) {
281 copy(cmd.samples[i:], cmd.samples[i+1:])
282 copy(cmd.cgnames[i:], cmd.cgnames[i+1:])
284 cmd.samples = cmd.samples[:len(cmd.samples)-1]
285 cmd.cgnames = cmd.cgnames[:len(cmd.cgnames)-1]
291 cmd.trainingSetSize = 0
292 for i := range cmd.cgnames {
293 if cmd.samples[i].isTraining {
294 cmd.trainingSet[i] = cmd.trainingSetSize
295 cmd.trainingSetSize++
296 cmd.chi2Cases = append(cmd.chi2Cases, cmd.samples[i].isCase)
298 cmd.trainingSet[i] = -1
301 if cmd.pvalue == nil {
302 cmd.pvalue = func(onehot []bool) float64 {
303 return pvalue(onehot, cmd.chi2Cases)
307 if cmd.filter.MinCoverage == 1 {
308 // In the generic formula below, floating point
309 // arithmetic can effectively push the coverage
310 // threshold above 1.0, which is impossible/useless.
311 // 1.0 needs to mean exactly 100% coverage.
312 cmd.minCoverage = len(cmd.cgnames)
314 cmd.minCoverage = int(math.Ceil(cmd.filter.MinCoverage * float64(len(cmd.cgnames))))
318 samplesOutFilename := *outputDir + "/samples.csv"
319 log.Infof("writing sample metadata to %s", samplesOutFilename)
321 f, err = os.Create(samplesOutFilename)
326 for i, si := range cmd.samples {
330 } else if si.isControl {
338 _, err = fmt.Fprintf(f, "%d,%s,%s,%s\n", i, si.id, cc, tv)
340 err = fmt.Errorf("write %s: %w", samplesOutFilename, err)
346 err = fmt.Errorf("close %s: %w", samplesOutFilename, err)
352 log.Info("indexing reference tiles")
353 type reftileinfo struct {
354 variant tileVariantID
355 seqname string // chr1
356 pos int // distance from start of chromosome to starttag
357 tiledata []byte // acgtggcaa...
358 excluded bool // true if excluded by regions file
359 nexttag tagID // tagID of following tile (-1 for last tag of chromosome)
361 isdup := map[tagID]bool{}
362 reftile := map[tagID]*reftileinfo{}
363 for seqname, cseq := range refseq {
365 lastreftag := tagID(-1)
366 for _, libref := range cseq {
367 if cmd.filter.MaxTag >= 0 && libref.Tag > tagID(cmd.filter.MaxTag) {
370 tiledata := reftiledata[libref]
371 if len(tiledata) == 0 {
372 err = fmt.Errorf("missing tiledata for tag %d variant %d in %s in ref", libref.Tag, libref.Variant, seqname)
375 foundthistag := false
376 taglib.FindAll(tiledata[:len(tiledata)-1], func(tagid tagID, offset, _ int) {
377 if !foundthistag && tagid == libref.Tag {
381 if dupref, ok := reftile[tagid]; ok {
382 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)
383 delete(reftile, tagid)
385 log.Printf("found tag %d at offset %d inside tile variant %+v on %s @ %d", tagid, offset, libref, seqname, pos+offset+1)
389 if isdup[libref.Tag] {
390 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique", libref, seqname, pos)
391 } else if reftile[libref.Tag] != nil {
392 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)
393 delete(reftile, libref.Tag)
394 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique", libref, seqname, pos)
395 isdup[libref.Tag] = true
397 reftile[libref.Tag] = &reftileinfo{
399 variant: libref.Variant,
405 reftile[lastreftag].nexttag = libref.Tag
407 lastreftag = libref.Tag
409 pos += len(tiledata) - taglen
411 log.Printf("... %s done, len %d", seqname, pos+taglen)
415 if *regionsFilename != "" {
416 log.Printf("loading regions from %s", *regionsFilename)
417 mask, err = makeMask(*regionsFilename, *expandRegions)
421 log.Printf("before applying mask, len(reftile) == %d", len(reftile))
422 log.Printf("deleting reftile entries for regions outside %d intervals", mask.Len())
423 for _, rt := range reftile {
424 if !mask.Check(strings.TrimPrefix(rt.seqname, "chr"), rt.pos, rt.pos+len(rt.tiledata)) {
428 log.Printf("after applying mask, len(reftile) == %d", len(reftile))
431 type hgvsColSet map[hgvs.Variant][2][]int8
432 encodeHGVS := throttle{Max: len(refseq)}
433 encodeHGVSTodo := map[string]chan hgvsColSet{}
434 tmpHGVSCols := map[string]*os.File{}
436 for seqname := range refseq {
438 f, err = os.Create(*outputDir + "/tmp." + seqname + ".gob")
442 defer os.Remove(f.Name())
443 bufw := bufio.NewWriterSize(f, 1<<24)
444 enc := gob.NewEncoder(bufw)
445 tmpHGVSCols[seqname] = f
446 todo := make(chan hgvsColSet, 128)
447 encodeHGVSTodo[seqname] = todo
448 encodeHGVS.Go(func() error {
449 for colset := range todo {
450 err := enc.Encode(colset)
452 encodeHGVS.Report(err)
463 var toMerge [][]int16
464 if *mergeOutput || *hgvsSingle {
465 toMerge = make([][]int16, len(infiles))
467 var onehotIndirect [][2][]uint32 // [chunkIndex][axis][index]
468 var onehotChunkSize []uint32
469 var onehotXrefs [][]onehotXref
470 if *onehotSingle || *onlyPCA {
471 onehotIndirect = make([][2][]uint32, len(infiles))
472 onehotChunkSize = make([]uint32, len(infiles))
473 onehotXrefs = make([][]onehotXref, len(infiles))
475 chunkStartTag := make([]tagID, len(infiles))
477 throttleMem := throttle{Max: cmd.threads} // TODO: estimate using mem and data size
478 throttleNumpyMem := throttle{Max: cmd.threads/2 + 1}
479 log.Info("generating annotations and numpy matrix for each slice")
480 var errSkip = errors.New("skip infile")
482 for infileIdx, infile := range infiles {
483 infileIdx, infile := infileIdx, infile
484 throttleMem.Go(func() error {
485 seq := make(map[tagID][]TileVariant, 50000)
486 cgs := make(map[string]CompactGenome, len(cmd.cgnames))
487 f, err := open(infile)
492 log.Infof("%04d: reading %s", infileIdx, infile)
493 err = DecodeLibrary(f, strings.HasSuffix(infile, ".gz"), func(ent *LibraryEntry) error {
494 for _, tv := range ent.TileVariants {
499 // corresponding ref tile, if
500 // mask is in play (we can't
501 // determine coordinates for
503 if mask != nil && reftile[tv.Tag] == nil {
507 // corresponding ref tile is
508 // outside target regions --
509 // unless it's a potential
511 if mask != nil && reftile[tv.Tag].excluded &&
512 (int(tv.Tag+1) >= len(tagset) ||
513 (bytes.HasSuffix(tv.Sequence, tagset[tv.Tag+1]) && reftile[tv.Tag+1] != nil && !reftile[tv.Tag+1].excluded)) {
516 if tv.Tag == cmd.debugTag {
517 log.Printf("infile %d %s tag %d variant %d hash %x", infileIdx, infile, tv.Tag, tv.Variant, tv.Blake2b[:3])
519 variants := seq[tv.Tag]
520 if len(variants) == 0 {
521 variants = make([]TileVariant, 100)
523 for len(variants) <= int(tv.Variant) {
524 variants = append(variants, TileVariant{})
526 variants[int(tv.Variant)] = tv
527 seq[tv.Tag] = variants
529 for _, cg := range ent.CompactGenomes {
530 if cmd.filter.MaxTag >= 0 && cg.StartTag > tagID(cmd.filter.MaxTag) {
533 if !matchGenome.MatchString(cg.Name) {
536 // pad to full slice size
537 // to avoid out-of-bounds
539 if sliceSize := 2 * int(cg.EndTag-cg.StartTag); len(cg.Variants) < sliceSize {
540 cg.Variants = append(cg.Variants, make([]tileVariantID, sliceSize-len(cg.Variants))...)
548 } else if err != nil {
549 return fmt.Errorf("%04d: DecodeLibrary(%s): err", infileIdx, infile)
551 tagstart := cgs[cmd.cgnames[0]].StartTag
552 tagend := cgs[cmd.cgnames[0]].EndTag
553 chunkStartTag[infileIdx] = tagstart
557 log.Infof("%04d: renumber/dedup variants for tags %d-%d", infileIdx, tagstart, tagend)
558 variantRemap := make([][]tileVariantID, tagend-tagstart)
559 throttleCPU := throttle{Max: runtime.GOMAXPROCS(0)}
560 for tag, variants := range seq {
561 tag, variants := tag, variants
562 throttleCPU.Go(func() error {
564 count := make(map[[blake2b.Size256]byte]int, len(variants))
568 count[blake2b.Sum256(rt.tiledata)] = 0
571 for cgname, cg := range cgs {
572 idx := int(tag-tagstart) * 2
573 for allele := 0; allele < 2; allele++ {
574 v := cg.Variants[idx+allele]
575 if v > 0 && len(variants[v].Sequence) > 0 {
576 count[variants[v].Blake2b]++
579 if v > 0 && tag == cmd.debugTag {
580 log.Printf("tag %d cg %s allele %d tv %d hash %x count is now %d", tag, cgname, allele, v, variants[v].Blake2b[:3], count[variants[v].Blake2b])
584 if alleleCoverage < cmd.minCoverage*2 {
585 idx := int(tag-tagstart) * 2
586 for _, cg := range cgs {
588 cg.Variants[idx+1] = 0
590 if tag == cmd.debugTag {
591 log.Printf("tag %d alleleCoverage %d < min %d, sample data wiped", tag, alleleCoverage, cmd.minCoverage*2)
596 // hash[i] will be the hash of
597 // the variant(s) that should
598 // be at rank i (0-based).
599 hash := make([][blake2b.Size256]byte, 0, len(count))
600 for b := range count {
601 hash = append(hash, b)
603 sort.Slice(hash, func(i, j int) bool {
604 bi, bj := &hash[i], &hash[j]
605 if ci, cj := count[*bi], count[*bj]; ci != cj {
608 return bytes.Compare((*bi)[:], (*bj)[:]) < 0
611 // rank[b] will be the 1-based
612 // new variant number for
613 // variants whose hash is b.
614 rank := make(map[[blake2b.Size256]byte]tileVariantID, len(hash))
615 for i, h := range hash {
616 rank[h] = tileVariantID(i + 1)
618 if tag == cmd.debugTag {
619 for h, r := range rank {
620 log.Printf("tag %d rank(%x) = %v", tag, h[:3], r)
623 // remap[v] will be the new
624 // variant number for original
626 remap := make([]tileVariantID, len(variants))
627 for i, tv := range variants {
628 remap[i] = rank[tv.Blake2b]
630 if tag == cmd.debugTag {
631 for in, out := range remap {
633 log.Printf("tag %d remap %d => %d", tag, in, out)
637 variantRemap[tag-tagstart] = remap
639 refrank := rank[blake2b.Sum256(rt.tiledata)]
640 if tag == cmd.debugTag {
641 log.Printf("tag %d reftile variant %d => %d", tag, rt.variant, refrank)
650 var onehotChunk [][]int8
651 var onehotXref []onehotXref
653 var annotationsFilename string
655 annotationsFilename = "/dev/null"
657 annotationsFilename = fmt.Sprintf("%s/matrix.%04d.annotations.csv", *outputDir, infileIdx)
658 log.Infof("%04d: writing %s", infileIdx, annotationsFilename)
660 annof, err := os.Create(annotationsFilename)
664 annow := bufio.NewWriterSize(annof, 1<<20)
666 for tag := tagstart; tag < tagend; tag++ {
668 if rt == nil && mask != nil {
669 // With no ref tile, we don't
670 // have coordinates to say
671 // this is in the desired
672 // regions -- so it's not.
673 // TODO: handle ref spanning
677 if rt != nil && rt.excluded {
678 // TODO: don't skip yet --
679 // first check for spanning
680 // tile variants that
681 // intersect non-excluded ref
685 if cmd.filter.MaxTag >= 0 && tag > tagID(cmd.filter.MaxTag) {
688 remap := variantRemap[tag-tagstart]
690 // was not assigned above,
691 // because minCoverage
695 maxv := tileVariantID(0)
696 for _, v := range remap {
701 if *onehotChunked || *onehotSingle || *onlyPCA {
702 onehot, xrefs := cmd.tv2homhet(cgs, maxv, remap, tag, tagstart, seq)
703 if tag == cmd.debugTag {
704 log.WithFields(logrus.Fields{
707 }).Info("tv2homhet()")
709 onehotChunk = append(onehotChunk, onehot...)
710 onehotXref = append(onehotXref, xrefs...)
717 // Reference does not use any
718 // variant of this tile
720 // TODO: diff against the
721 // relevant portion of the
722 // ref's spanning tile
726 fmt.Fprintf(annow, "%d,%d,%d,=,%s,%d,,,\n", tag, outcol, rt.variant, rt.seqname, rt.pos)
728 reftilestr := strings.ToUpper(string(rt.tiledata))
730 done := make([]bool, maxv+1)
731 variantDiffs := make([][]hgvs.Variant, maxv+1)
732 for v, tv := range variants {
734 if v == 0 || v == rt.variant || done[v] {
739 if len(tv.Sequence) < taglen {
742 // if reftilestr doesn't end
743 // in the same tag as tv,
744 // extend reftilestr with
745 // following ref tiles until
746 // it does (up to an arbitrary
747 // sanity-check limit)
748 reftilestr := reftilestr
749 endtagstr := strings.ToUpper(string(tv.Sequence[len(tv.Sequence)-taglen:]))
750 for i, rt := 0, rt; i < annotationMaxTileSpan && !strings.HasSuffix(reftilestr, endtagstr) && rt.nexttag >= 0; i++ {
751 rt = reftile[rt.nexttag]
755 reftilestr += strings.ToUpper(string(rt.tiledata[taglen:]))
757 if mask != nil && !mask.Check(strings.TrimPrefix(rt.seqname, "chr"), rt.pos, rt.pos+len(reftilestr)) {
760 if !strings.HasSuffix(reftilestr, endtagstr) {
761 fmt.Fprintf(annow, "%d,%d,%d,,%s,%d,,,\n", tag, outcol, v, rt.seqname, rt.pos)
764 if lendiff := len(reftilestr) - len(tv.Sequence); lendiff < -1000 || lendiff > 1000 {
765 fmt.Fprintf(annow, "%d,%d,%d,,%s,%d,,,\n", tag, outcol, v, rt.seqname, rt.pos)
768 diffs, _ := hgvs.Diff(reftilestr, strings.ToUpper(string(tv.Sequence)), 0)
769 for i := range diffs {
770 diffs[i].Position += rt.pos
772 for _, diff := range diffs {
773 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)
776 variantDiffs[v] = diffs
780 // We can now determine, for each HGVS
781 // variant (diff) in this reftile
782 // region, whether a given genome
783 // phase/allele (1) has the variant, (0) has
784 // =ref or a different variant in that
785 // position, or (-1) is lacking
786 // coverage / couldn't be diffed.
787 hgvsCol := hgvsColSet{}
788 for _, diffs := range variantDiffs {
789 for _, diff := range diffs {
790 if _, ok := hgvsCol[diff]; ok {
793 hgvsCol[diff] = [2][]int8{
794 make([]int8, len(cmd.cgnames)),
795 make([]int8, len(cmd.cgnames)),
799 for row, name := range cmd.cgnames {
800 variants := cgs[name].Variants[(tag-tagstart)*2:]
801 for ph := 0; ph < 2; ph++ {
803 if int(v) >= len(remap) {
809 // hgvsCol[*][ph][row] is already 0
810 } else if len(variantDiffs[v]) == 0 {
811 // lacking coverage / couldn't be diffed
812 for _, col := range hgvsCol {
816 for _, diff := range variantDiffs[v] {
817 hgvsCol[diff][ph][row] = 1
822 for diff, colpair := range hgvsCol {
823 allele2homhet(colpair)
824 if !cmd.filterHGVScolpair(colpair) {
825 delete(hgvsCol, diff)
828 if len(hgvsCol) > 0 {
829 encodeHGVSTodo[rt.seqname] <- hgvsCol
844 // transpose onehotChunk[col][row] to numpy[row*ncols+col]
845 rows := len(cmd.cgnames)
846 cols := len(onehotChunk)
847 log.Infof("%04d: preparing onehot numpy (rows=%d, cols=%d, mem=%d)", infileIdx, rows, cols, rows*cols)
848 throttleNumpyMem.Acquire()
849 out := onehotcols2int8(onehotChunk)
850 fnm := fmt.Sprintf("%s/onehot.%04d.npy", *outputDir, infileIdx)
851 err = writeNumpyInt8(fnm, out, rows, cols)
855 fnm = fmt.Sprintf("%s/onehot-columns.%04d.npy", *outputDir, infileIdx)
856 err = writeNumpyInt32(fnm, onehotXref2int32(onehotXref), 4, len(onehotXref))
861 throttleNumpyMem.Release()
863 if *onehotSingle || *onlyPCA {
864 onehotIndirect[infileIdx] = onehotChunk2Indirect(onehotChunk)
865 onehotChunkSize[infileIdx] = uint32(len(onehotChunk))
866 onehotXrefs[infileIdx] = onehotXref
867 n := len(onehotIndirect[infileIdx][0])
868 log.Infof("%04d: keeping onehot coordinates in memory (n=%d, mem=%d)", infileIdx, n, n*8*2)
870 if !(*onehotSingle || *onehotChunked || *onlyPCA) || *mergeOutput || *hgvsSingle {
871 log.Infof("%04d: preparing numpy (rows=%d, cols=%d)", infileIdx, len(cmd.cgnames), 2*outcol)
872 throttleNumpyMem.Acquire()
873 rows := len(cmd.cgnames)
875 out := make([]int16, rows*cols)
876 for row, name := range cmd.cgnames {
878 for col, v := range cgs[name].Variants {
879 tag := tagstart + tagID(col/2)
880 if cmd.filter.MaxTag >= 0 && tag > tagID(cmd.filter.MaxTag) {
883 if rt := reftile[tag]; rt == nil || rt.excluded {
887 out[outidx] = 0 // tag not found / spanning tile
888 } else if variants, ok := seq[tag]; ok && int(v) < len(variants) && len(variants[v].Sequence) > 0 {
889 out[outidx] = int16(variantRemap[tag-tagstart][v])
891 out[outidx] = -1 // low quality tile variant
893 if tag == cmd.debugTag {
894 log.Printf("tag %d row %d col %d outidx %d v %d out %d", tag, row, col, outidx, v, out[outidx])
902 throttleNumpyMem.Release()
903 if *mergeOutput || *hgvsSingle {
904 log.Infof("%04d: matrix fragment %d rows x %d cols", infileIdx, rows, cols)
905 toMerge[infileIdx] = out
907 if !*mergeOutput && !*onehotChunked && !*onehotSingle {
908 fnm := fmt.Sprintf("%s/matrix.%04d.npy", *outputDir, infileIdx)
909 err = writeNumpyInt16(fnm, out, rows, cols)
916 log.Infof("%s: done (%d/%d)", infile, int(atomic.AddInt64(&done, 1)), len(infiles))
920 if err = throttleMem.Wait(); err != nil {
925 log.Info("flushing hgvsCols temp files")
926 for seqname := range refseq {
927 close(encodeHGVSTodo[seqname])
929 err = encodeHGVS.Wait()
933 for seqname := range refseq {
934 log.Infof("%s: reading hgvsCols from temp file", seqname)
935 f := tmpHGVSCols[seqname]
936 _, err = f.Seek(0, io.SeekStart)
940 var hgvsCols hgvsColSet
941 dec := gob.NewDecoder(bufio.NewReaderSize(f, 1<<24))
943 err = dec.Decode(&hgvsCols)
948 log.Infof("%s: sorting %d hgvs variants", seqname, len(hgvsCols))
949 variants := make([]hgvs.Variant, 0, len(hgvsCols))
950 for v := range hgvsCols {
951 variants = append(variants, v)
953 sort.Slice(variants, func(i, j int) bool {
954 vi, vj := &variants[i], &variants[j]
955 if vi.Position != vj.Position {
956 return vi.Position < vj.Position
957 } else if vi.Ref != vj.Ref {
958 return vi.Ref < vj.Ref
960 return vi.New < vj.New
963 rows := len(cmd.cgnames)
964 cols := len(variants) * 2
965 log.Infof("%s: building hgvs matrix (rows=%d, cols=%d, mem=%d)", seqname, rows, cols, rows*cols)
966 out := make([]int8, rows*cols)
967 for varIdx, variant := range variants {
968 hgvsCols := hgvsCols[variant]
969 for row := range cmd.cgnames {
970 for ph := 0; ph < 2; ph++ {
971 out[row*cols+varIdx+ph] = hgvsCols[ph][row]
975 err = writeNumpyInt8(fmt.Sprintf("%s/hgvs.%s.npy", *outputDir, seqname), out, rows, cols)
981 fnm := fmt.Sprintf("%s/hgvs.%s.annotations.csv", *outputDir, seqname)
982 log.Infof("%s: writing hgvs column labels to %s", seqname, fnm)
983 var hgvsLabels bytes.Buffer
984 for varIdx, variant := range variants {
985 fmt.Fprintf(&hgvsLabels, "%d,%s:g.%s\n", varIdx, seqname, variant.String())
987 err = ioutil.WriteFile(fnm, hgvsLabels.Bytes(), 0666)
994 if *mergeOutput || *hgvsSingle {
995 var annow *bufio.Writer
998 annoFilename := fmt.Sprintf("%s/matrix.annotations.csv", *outputDir)
999 annof, err = os.Create(annoFilename)
1003 annow = bufio.NewWriterSize(annof, 1<<20)
1006 rows := len(cmd.cgnames)
1008 for _, chunk := range toMerge {
1009 cols += len(chunk) / rows
1011 log.Infof("merging output matrix (rows=%d, cols=%d, mem=%d) and annotations", rows, cols, rows*cols*2)
1014 out = make([]int16, rows*cols)
1016 hgvsCols := map[string][2][]int16{} // hgvs -> [[g0,g1,g2,...], [g0,g1,g2,...]] (slice of genomes for each phase)
1018 for outIdx, chunk := range toMerge {
1019 chunkcols := len(chunk) / rows
1021 for row := 0; row < rows; row++ {
1022 copy(out[row*cols+startcol:], chunk[row*chunkcols:(row+1)*chunkcols])
1025 toMerge[outIdx] = nil
1027 annotationsFilename := fmt.Sprintf("%s/matrix.%04d.annotations.csv", *outputDir, outIdx)
1028 log.Infof("reading %s", annotationsFilename)
1029 buf, err := os.ReadFile(annotationsFilename)
1034 err = os.Remove(annotationsFilename)
1039 for _, line := range bytes.Split(buf, []byte{'\n'}) {
1043 fields := bytes.SplitN(line, []byte{','}, 9)
1044 tag, _ := strconv.Atoi(string(fields[0]))
1045 incol, _ := strconv.Atoi(string(fields[1]))
1046 tileVariant, _ := strconv.Atoi(string(fields[2]))
1047 hgvsID := string(fields[3])
1048 seqname := string(fields[4])
1049 pos, _ := strconv.Atoi(string(fields[5]))
1052 // Null entry for un-diffable
1057 // Null entry for ref tile
1060 if mask != nil && !mask.Check(strings.TrimPrefix(seqname, "chr"), pos, pos+len(refseq)) {
1061 // The tile intersects one of
1062 // the selected regions, but
1063 // this particular HGVS
1064 // variant does not.
1067 hgvsColPair := hgvsCols[hgvsID]
1068 if hgvsColPair[0] == nil {
1069 // values in new columns start
1070 // out as -1 ("no data yet")
1071 // or 0 ("=ref") here, may
1072 // change to 1 ("hgvs variant
1073 // present") below, either on
1074 // this line or a future line.
1075 hgvsColPair = [2][]int16{make([]int16, len(cmd.cgnames)), make([]int16, len(cmd.cgnames))}
1076 rt, ok := reftile[tagID(tag)]
1078 err = fmt.Errorf("bug: seeing annotations for tag %d, but it has no reftile entry", tag)
1081 for ph := 0; ph < 2; ph++ {
1082 for row := 0; row < rows; row++ {
1083 v := chunk[row*chunkcols+incol*2+ph]
1084 if tileVariantID(v) == rt.variant {
1085 hgvsColPair[ph][row] = 0
1087 hgvsColPair[ph][row] = -1
1091 hgvsCols[hgvsID] = hgvsColPair
1093 hgvsref := hgvs.Variant{
1095 Ref: string(refseq),
1096 New: string(refseq),
1098 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])
1102 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])
1104 for ph := 0; ph < 2; ph++ {
1105 for row := 0; row < rows; row++ {
1106 v := chunk[row*chunkcols+incol*2+ph]
1107 if int(v) == tileVariant {
1108 hgvsColPair[ph][row] = 1
1114 startcol += chunkcols
1125 err = writeNumpyInt16(fmt.Sprintf("%s/matrix.npy", *outputDir), out, rows, cols)
1133 cols = len(hgvsCols) * 2
1134 log.Printf("building hgvs-based matrix: %d rows x %d cols", rows, cols)
1135 out = make([]int16, rows*cols)
1136 hgvsIDs := make([]string, 0, cols/2)
1137 for hgvsID := range hgvsCols {
1138 hgvsIDs = append(hgvsIDs, hgvsID)
1140 sort.Strings(hgvsIDs)
1141 var hgvsLabels bytes.Buffer
1142 for idx, hgvsID := range hgvsIDs {
1143 fmt.Fprintf(&hgvsLabels, "%d,%s\n", idx, hgvsID)
1144 for ph := 0; ph < 2; ph++ {
1145 hgvscol := hgvsCols[hgvsID][ph]
1146 for row, val := range hgvscol {
1147 out[row*cols+idx*2+ph] = val
1151 err = writeNumpyInt16(fmt.Sprintf("%s/hgvs.npy", *outputDir), out, rows, cols)
1156 fnm := fmt.Sprintf("%s/hgvs.annotations.csv", *outputDir)
1157 log.Printf("writing hgvs labels: %s", fnm)
1158 err = ioutil.WriteFile(fnm, hgvsLabels.Bytes(), 0777)
1164 if *onehotSingle || *onlyPCA {
1166 for _, part := range onehotIndirect {
1167 nzCount += len(part[0])
1169 onehot := make([]uint32, nzCount*2) // [r,r,r,...,c,c,c,...]
1170 var xrefs []onehotXref
1171 chunkOffset := uint32(0)
1173 for i, part := range onehotIndirect {
1174 for i := range part[1] {
1175 part[1][i] += chunkOffset
1177 copy(onehot[outcol:], part[0])
1178 copy(onehot[outcol+nzCount:], part[1])
1179 xrefs = append(xrefs, onehotXrefs[i]...)
1181 outcol += len(part[0])
1182 chunkOffset += onehotChunkSize[i]
1186 onehotXrefs[i] = nil
1187 debug.FreeOSMemory()
1190 fnm := fmt.Sprintf("%s/onehot.npy", *outputDir)
1191 err = writeNumpyUint32(fnm, onehot, 2, nzCount)
1195 fnm = fmt.Sprintf("%s/onehot-columns.npy", *outputDir)
1196 err = writeNumpyInt32(fnm, onehotXref2int32(xrefs), 5, len(xrefs))
1200 fnm = fmt.Sprintf("%s/stats.json", *outputDir)
1201 j, err := json.Marshal(map[string]interface{}{
1202 "pvalueCallCount": cmd.pvalueCallCount,
1207 err = os.WriteFile(fnm, j, 0777)
1214 for _, c := range onehot[nzCount:] {
1220 return fmt.Errorf("cannot do PCA: one-hot matrix is empty")
1222 log.Printf("have %d one-hot cols", cols)
1224 for *maxPCATiles > 0 && cols > *maxPCATiles*2 {
1225 cols = (cols + 1) / 2
1229 // we work with pairs of columns
1232 log.Printf("creating full matrix (%d rows) and training matrix (%d rows) with %d cols, stride %d", len(cmd.cgnames), cmd.trainingSetSize, cols, stride)
1233 mtxFull := mat.NewDense(len(cmd.cgnames), cols, nil)
1234 mtxTrain := mat.NewDense(cmd.trainingSetSize, cols, nil)
1235 for i, c := range onehot[nzCount:] {
1236 if int(c/2)%stride == 0 {
1237 outcol := int(c/2)/stride*2 + int(c)%2
1238 mtxFull.Set(int(onehot[i]), outcol, 1)
1239 if trainRow := cmd.trainingSet[int(onehot[i])]; trainRow >= 0 {
1240 mtxTrain.Set(trainRow, outcol, 1)
1244 log.Print("fitting")
1245 transformer := nlp.NewPCA(cmd.pcaComponents)
1246 transformer.Fit(mtxTrain.T())
1247 log.Printf("transforming")
1248 pca, err := transformer.Transform(mtxFull.T())
1253 outrows, outcols := pca.Dims()
1254 log.Printf("copying result to numpy output array: %d rows, %d cols", outrows, outcols)
1255 out := make([]float64, outrows*outcols)
1256 for i := 0; i < outrows; i++ {
1257 for j := 0; j < outcols; j++ {
1258 out[i*outcols+j] = pca.At(i, j)
1261 fnm := fmt.Sprintf("%s/pca.npy", *outputDir)
1262 log.Printf("writing numpy: %s", fnm)
1263 output, err := os.OpenFile(fnm, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0777)
1267 npw, err := gonpy.NewWriter(nopCloser{output})
1269 return fmt.Errorf("gonpy.NewWriter: %w", err)
1271 npw.Shape = []int{outrows, outcols}
1272 err = npw.WriteFloat64(out)
1274 return fmt.Errorf("WriteFloat64: %w", err)
1276 err = output.Close()
1282 samplesOutFilename := *outputDir + "/samples.csv"
1283 log.Infof("writing sample metadata to %s", samplesOutFilename)
1285 f, err = os.Create(samplesOutFilename)
1290 for i, si := range cmd.samples {
1294 } else if si.isControl {
1303 for c := 0; c < outcols; c++ {
1304 pcavals += fmt.Sprintf(",%f", pca.At(i, c))
1306 _, err = fmt.Fprintf(f, "%d,%s,%s,%s%s\n", i, si.id, cc, tv, pcavals)
1308 err = fmt.Errorf("write %s: %w", samplesOutFilename, err)
1314 err = fmt.Errorf("close %s: %w", samplesOutFilename, err)
1320 if !*mergeOutput && !*onehotChunked && !*onehotSingle && !*onlyPCA {
1321 tagoffsetFilename := *outputDir + "/chunk-tag-offset.csv"
1322 log.Infof("writing tag offsets to %s", tagoffsetFilename)
1324 f, err = os.Create(tagoffsetFilename)
1329 for idx, offset := range chunkStartTag {
1330 _, err = fmt.Fprintf(f, "%q,%d\n", fmt.Sprintf("matrix.%04d.npy", idx), offset)
1332 err = fmt.Errorf("write %s: %w", tagoffsetFilename, err)
1338 err = fmt.Errorf("close %s: %w", tagoffsetFilename, err)
1346 type sampleInfo struct {
1352 pcaComponents []float64
1355 // Read samples.csv file with case/control and training/validation
1357 func loadSampleInfo(samplesFilename string) ([]sampleInfo, error) {
1359 f, err := open(samplesFilename)
1363 buf, err := io.ReadAll(f)
1369 for _, csv := range bytes.Split(buf, []byte{'\n'}) {
1374 split := strings.Split(string(csv), ",")
1376 return nil, fmt.Errorf("%d fields < 4 in %s line %d: %q", len(split), samplesFilename, lineNum, csv)
1378 if split[0] == "Index" && split[1] == "SampleID" && split[2] == "CaseControl" && split[3] == "TrainingValidation" {
1381 idx, err := strconv.Atoi(split[0])
1384 return nil, fmt.Errorf("header does not look right: %q", csv)
1386 return nil, fmt.Errorf("%s line %d: index: %s", samplesFilename, lineNum, err)
1389 return nil, fmt.Errorf("%s line %d: index %d out of order", samplesFilename, lineNum, idx)
1391 var pcaComponents []float64
1393 for _, s := range split[4:] {
1394 f, err := strconv.ParseFloat(s, 64)
1396 return nil, fmt.Errorf("%s line %d: cannot parse float %q: %s", samplesFilename, lineNum, s, err)
1398 pcaComponents = append(pcaComponents, f)
1401 si = append(si, sampleInfo{
1403 isCase: split[2] == "1",
1404 isControl: split[2] == "0",
1405 isTraining: split[3] == "1",
1406 isValidation: split[3] == "0",
1407 pcaComponents: pcaComponents,
1413 func (cmd *sliceNumpy) filterHGVScolpair(colpair [2][]int8) bool {
1414 if cmd.chi2PValue >= 1 {
1417 col0 := make([]bool, 0, len(cmd.chi2Cases))
1418 col1 := make([]bool, 0, len(cmd.chi2Cases))
1419 cases := make([]bool, 0, len(cmd.chi2Cases))
1420 for i, c := range cmd.chi2Cases {
1421 if colpair[0][i] < 0 {
1424 col0 = append(col0, colpair[0][i] != 0)
1425 col1 = append(col1, colpair[1][i] != 0)
1426 cases = append(cases, c)
1428 return len(cases) >= cmd.minCoverage &&
1429 (pvalue(col0, cases) <= cmd.chi2PValue || pvalue(col1, cases) <= cmd.chi2PValue)
1432 func writeNumpyUint32(fnm string, out []uint32, rows, cols int) error {
1433 output, err := os.Create(fnm)
1437 defer output.Close()
1438 bufw := bufio.NewWriterSize(output, 1<<26)
1439 npw, err := gonpy.NewWriter(nopCloser{bufw})
1443 log.WithFields(log.Fields{
1447 "bytes": rows * cols * 4,
1448 }).Infof("writing numpy: %s", fnm)
1449 npw.Shape = []int{rows, cols}
1450 npw.WriteUint32(out)
1455 return output.Close()
1458 func writeNumpyInt32(fnm string, out []int32, rows, cols int) error {
1459 output, err := os.Create(fnm)
1463 defer output.Close()
1464 bufw := bufio.NewWriterSize(output, 1<<26)
1465 npw, err := gonpy.NewWriter(nopCloser{bufw})
1469 log.WithFields(log.Fields{
1473 "bytes": rows * cols * 4,
1474 }).Infof("writing numpy: %s", fnm)
1475 npw.Shape = []int{rows, cols}
1481 return output.Close()
1484 func writeNumpyInt16(fnm string, out []int16, rows, cols int) error {
1485 output, err := os.Create(fnm)
1489 defer output.Close()
1490 bufw := bufio.NewWriterSize(output, 1<<26)
1491 npw, err := gonpy.NewWriter(nopCloser{bufw})
1495 log.WithFields(log.Fields{
1499 "bytes": rows * cols * 2,
1500 }).Infof("writing numpy: %s", fnm)
1501 npw.Shape = []int{rows, cols}
1507 return output.Close()
1510 func writeNumpyInt8(fnm string, out []int8, rows, cols int) error {
1511 output, err := os.Create(fnm)
1515 defer output.Close()
1516 bufw := bufio.NewWriterSize(output, 1<<26)
1517 npw, err := gonpy.NewWriter(nopCloser{bufw})
1521 log.WithFields(log.Fields{
1525 "bytes": rows * cols,
1526 }).Infof("writing numpy: %s", fnm)
1527 npw.Shape = []int{rows, cols}
1533 return output.Close()
1536 func allele2homhet(colpair [2][]int8) {
1537 a, b := colpair[0], colpair[1]
1538 for i, av := range a {
1540 if av < 0 || bv < 0 {
1543 } else if av > 0 && bv > 0 {
1546 } else if av > 0 || bv > 0 {
1550 // ref (or a different variant in same position)
1551 // (this is a no-op) a[i], b[i] = 0, 0
1556 type onehotXref struct {
1558 variant tileVariantID
1563 const onehotXrefSize = unsafe.Sizeof(onehotXref{})
1565 // Build onehot matrix (m[tileVariantIndex][genome] == 0 or 1) for all
1566 // variants of a single tile/tag#.
1568 // Return nil if no tile variant passes Χ² filter.
1569 func (cmd *sliceNumpy) tv2homhet(cgs map[string]CompactGenome, maxv tileVariantID, remap []tileVariantID, tag, chunkstarttag tagID, seq map[tagID][]TileVariant) ([][]int8, []onehotXref) {
1570 if tag == cmd.debugTag {
1571 tv := make([]tileVariantID, len(cmd.cgnames)*2)
1572 for i, name := range cmd.cgnames {
1573 copy(tv[i*2:(i+1)*2], cgs[name].Variants[(tag-chunkstarttag)*2:])
1575 log.WithFields(logrus.Fields{
1576 "cgs[i].Variants[tag*2+j]": tv,
1580 "chunkstarttag": chunkstarttag,
1581 }).Info("tv2homhet()")
1583 if maxv < 1 || (maxv < 2 && !cmd.includeVariant1) {
1584 // everyone has the most common variant (of the variants we don't drop)
1587 tagoffset := tag - chunkstarttag
1589 for _, cg := range cgs {
1591 for _, v := range cg.Variants[tagoffset*2 : tagoffset*2+2] {
1592 if v > 0 && int(v) < len(seq[tag]) && len(seq[tag][v].Sequence) > 0 {
1600 if coverage < cmd.minCoverage {
1603 // "observed" array for p-value calculation (training set
1605 obs := make([][]bool, (maxv+1)*2) // 2 slices (hom + het) for each variant#
1606 // one-hot output (all samples)
1607 outcols := make([][]int8, (maxv+1)*2)
1608 for i := range obs {
1609 obs[i] = make([]bool, cmd.trainingSetSize)
1610 outcols[i] = make([]int8, len(cmd.cgnames))
1612 for cgid, name := range cmd.cgnames {
1613 tsid := cmd.trainingSet[cgid]
1614 cgvars := cgs[name].Variants[tagoffset*2:]
1615 tv0, tv1 := remap[cgvars[0]], remap[cgvars[1]]
1616 for v := tileVariantID(1); v <= maxv; v++ {
1617 if tv0 == v && tv1 == v {
1619 obs[v*2][tsid] = true
1621 outcols[v*2][cgid] = 1
1622 } else if tv0 == v || tv1 == v {
1624 obs[v*2+1][tsid] = true
1626 outcols[v*2+1][cgid] = 1
1631 var xref []onehotXref
1632 for col := 2; col < len(obs); col++ {
1633 // col 0,1 correspond to tile variant 0, i.e.,
1634 // no-call; col 2,3 correspond to the most common
1635 // variant; so we (normally) start at col 4.
1636 if col < 4 && !cmd.includeVariant1 {
1639 atomic.AddInt64(&cmd.pvalueCallCount, 1)
1640 p := cmd.pvalue(obs[col])
1641 if cmd.chi2PValue < 1 && !(p < cmd.chi2PValue) {
1644 onehot = append(onehot, outcols[col])
1645 xref = append(xref, onehotXref{
1647 variant: tileVariantID(col >> 1),
1655 // convert a []onehotXref with length N to a numpy-style []int32
1656 // matrix with N columns, one row per field of onehotXref struct.
1658 // Hom/het row contains hom=0, het=1.
1660 // P-value row contains 1000000x actual p-value.
1661 func onehotXref2int32(xrefs []onehotXref) []int32 {
1663 xdata := make([]int32, 5*xcols)
1664 for i, xref := range xrefs {
1665 xdata[i] = int32(xref.tag)
1666 xdata[xcols+i] = int32(xref.variant)
1668 xdata[xcols*2+i] = 1
1670 xdata[xcols*3+i] = int32(xref.pvalue * 1000000)
1671 xdata[xcols*4+i] = int32(-math.Log10(xref.pvalue) * 1000000)
1676 // transpose onehot data from in[col][row] to numpy-style
1677 // out[row*cols+col].
1678 func onehotcols2int8(in [][]int8) []int8 {
1684 out := make([]int8, rows*cols)
1685 for row := 0; row < rows; row++ {
1686 outrow := out[row*cols:]
1687 for col, incol := range in {
1688 outrow[col] = incol[row]
1694 // Return [2][]uint32{rowIndices, colIndices} indicating which
1695 // elements of matrixT[c][r] have non-zero values.
1696 func onehotChunk2Indirect(matrixT [][]int8) [2][]uint32 {
1698 for c, col := range matrixT {
1699 for r, val := range col {
1701 nz[0] = append(nz[0], uint32(r))
1702 nz[1] = append(nz[1], uint32(c))