Fix sparse one-hot coordinates for chunk n>0.
[lightning.git] / slicenumpy.go
index 4a2f6654d227f526ded40731f9611cda42a94909..a7c6e24c5716f02ddff39264ac8df4b4505bcd00 100644 (file)
@@ -41,6 +41,7 @@ type sliceNumpy struct {
        chi2PValue            float64
        minCoverage           int
        cgnames               []string
+       includeVariant1       bool
 }
 
 func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
@@ -70,6 +71,7 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
        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)")
        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)")
        flags.Float64Var(&cmd.chi2PValue, "chi2-p-value", 1, "do Χ² test and omit columns with p-value above this threshold")
+       flags.BoolVar(&cmd.includeVariant1, "include-variant-1", false, "include most common variant when building one-hot matrix")
        cmd.filter.Flags(flags)
        err = flags.Parse(args)
        if err == flag.ErrHelp {
@@ -247,7 +249,7 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
        for seqname, cseq := range refseq {
                pos := 0
                for _, libref := range cseq {
-                       if libref.Tag > tagID(cmd.filter.MaxTag) {
+                       if cmd.filter.MaxTag >= 0 && libref.Tag > tagID(cmd.filter.MaxTag) {
                                continue
                        }
                        tiledata := reftiledata[libref]
@@ -343,9 +345,11 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
                toMerge = make([][]int16, len(infiles))
        }
        var onehotIndirect [][2][]uint32 // [chunkIndex][axis][index]
+       var onehotChunkSize []uint32
        var onehotXrefs [][]onehotXref
        if *onehotSingle {
                onehotIndirect = make([][2][]uint32, len(infiles))
+               onehotChunkSize = make([]uint32, len(infiles))
                onehotXrefs = make([][]onehotXref, len(infiles))
        }
 
@@ -480,16 +484,12 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
                        annow := bufio.NewWriterSize(annof, 1<<20)
                        outcol := 0
                        for tag := tagstart; tag < tagend; tag++ {
-                               rt, ok := reftile[tag]
-                               if !ok {
-                                       if mask == nil {
-                                               outcol++
-                                       }
-                                       // Excluded by specified
-                                       // regions, or reference does
-                                       // not use any variant of this
-                                       // tile. (TODO: log this?
-                                       // mention it in annotations?)
+                               rt := reftile[tag]
+                               if rt == nil && mask != nil {
+                                       // Excluded by specified regions
+                                       continue
+                               }
+                               if cmd.filter.MaxTag >= 0 && tag > tagID(cmd.filter.MaxTag) {
                                        continue
                                }
                                remap := variantRemap[tag-tagstart]
@@ -504,6 +504,12 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
                                        onehotChunk = append(onehotChunk, onehot...)
                                        onehotXref = append(onehotXref, xrefs...)
                                }
+                               if rt == nil {
+                                       // Reference does not use any
+                                       // variant of this tile
+                                       outcol++
+                                       continue
+                               }
                                fmt.Fprintf(annow, "%d,%d,%d,=,%s,%d,,,\n", tag, outcol, rt.variant, rt.seqname, rt.pos)
                                variants := seq[tag]
                                reftilestr := strings.ToUpper(string(rt.tiledata))
@@ -622,12 +628,13 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
                        }
                        if *onehotSingle {
                                onehotIndirect[infileIdx] = onehotChunk2Indirect(onehotChunk)
+                               onehotChunkSize[infileIdx] = uint32(len(onehotChunk))
                                onehotXrefs[infileIdx] = onehotXref
                                n := len(onehotIndirect[infileIdx][0])
-                               log.Infof("%04d: keeping onehot coordinates in memory (n=%d, mem=%d)", infileIdx, n, n*8)
+                               log.Infof("%04d: keeping onehot coordinates in memory (n=%d, mem=%d)", infileIdx, n, n*8*2)
                        }
                        if !(*onehotSingle || *onehotChunked) || *mergeOutput || *hgvsSingle {
-                               log.Infof("%04d: preparing numpy", infileIdx)
+                               log.Infof("%04d: preparing numpy (rows=%d, cols=%d)", infileIdx, len(cmd.cgnames), 2*outcol)
                                throttleNumpyMem.Acquire()
                                rows := len(cmd.cgnames)
                                cols := 2 * outcol
@@ -637,7 +644,7 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
                                        outcol := 0
                                        for col, v := range cgs[name].Variants {
                                                tag := tagstart + tagID(col/2)
-                                               if mask != nil && reftile[tag] == nil {
+                                               if mask != nil && reftile[tag] == nil || tag > tagID(cmd.filter.MaxTag) {
                                                        continue
                                                }
                                                if variants, ok := seq[tag]; ok && len(variants) > int(v) && len(variants[v].Sequence) > 0 {
@@ -920,16 +927,19 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
                }
                onehot := make([]uint32, nzCount*2) // [r,r,r,...,c,c,c,...]
                var xrefs []onehotXref
+               chunkOffset := uint32(0)
                outcol := 0
                for i, part := range onehotIndirect {
                        for i := range part[1] {
-                               part[1][i] += uint32(outcol)
+                               part[1][i] += chunkOffset
                        }
                        copy(onehot[outcol:], part[0])
                        copy(onehot[outcol+nzCount:], part[1])
-                       outcol += len(part[0])
                        xrefs = append(xrefs, onehotXrefs[i]...)
 
+                       outcol += len(part[0])
+                       chunkOffset += onehotChunkSize[i]
+
                        part[0] = nil
                        part[1] = nil
                        onehotXrefs[i] = nil
@@ -1219,7 +1229,13 @@ func (cmd *sliceNumpy) tv2homhet(cgs map[string]CompactGenome, maxv tileVariantI
        }
        var onehot [][]int8
        var xref []onehotXref
-       for homcol := 4; homcol < len(obs); homcol += 2 {
+       for homcol := 2; homcol < len(obs); homcol += 2 {
+               // homcol 0,1 correspond to tile variant 0, i.e.,
+               // no-call; homcol 2,3 correspond to the most common
+               // variant; so we (normally) start at homcol 4.
+               if homcol < 4 && !cmd.includeVariant1 {
+                       continue
+               }
                for het := 0; het < 2; het++ {
                        p := pvalue(obs[homcol+het], cmd.chi2Cases)
                        if cmd.chi2PValue < 1 && !(p < cmd.chi2PValue) {