Fix -include-variant-1
[lightning.git] / slicenumpy.go
index 601cb40eacc768df1eb6c27009096f725fb601ed..08cb2c0114d5b130443434663268ade4c3b51207 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 {
@@ -120,6 +122,7 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
                        "-chi2-case-control-file=" + cmd.chi2CaseControlFile,
                        "-chi2-case-control-column=" + cmd.chi2CaseControlColumn,
                        "-chi2-p-value=" + fmt.Sprintf("%f", cmd.chi2PValue),
+                       "-include-variant-1=" + fmt.Sprintf("%v", cmd.includeVariant1),
                }
                runner.Args = append(runner.Args, cmd.filter.Args()...)
                var output string
@@ -247,6 +250,9 @@ func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, s
        for seqname, cseq := range refseq {
                pos := 0
                for _, libref := range cseq {
+                       if cmd.filter.MaxTag >= 0 && libref.Tag > tagID(cmd.filter.MaxTag) {
+                               continue
+                       }
                        tiledata := reftiledata[libref]
                        if len(tiledata) == 0 {
                                err = fmt.Errorf("missing tiledata for tag %d variant %d in %s in ref", libref.Tag, libref.Variant, seqname)
@@ -340,9 +346,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))
        }
 
@@ -477,16 +485,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]
@@ -501,6 +505,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))
@@ -619,12 +629,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
@@ -634,7 +645,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 || (cmd.filter.MaxTag >= 0 && tag > tagID(cmd.filter.MaxTag)) {
                                                        continue
                                                }
                                                if variants, ok := seq[tag]; ok && len(variants) > int(v) && len(variants[v].Sequence) > 0 {
@@ -917,16 +928,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
@@ -1181,13 +1195,13 @@ type onehotXref struct {
 
 const onehotXrefSize = unsafe.Sizeof(onehotXref{})
 
-// Build onehot matrix (m[variant*2+isHet][genome] == 0 or 1) for all
+// Build onehot matrix (m[tileVariantIndex][genome] == 0 or 1) for all
 // variants of a single tile/tag#.
 //
 // Return nil if no tile variant passes Χ² filter.
 func (cmd *sliceNumpy) tv2homhet(cgs map[string]CompactGenome, maxv tileVariantID, remap []tileVariantID, tag, chunkstarttag tagID) ([][]int8, []onehotXref) {
-       if maxv < 2 {
-               // everyone has the most common variant
+       if maxv < 1 || (maxv < 2 && !cmd.includeVariant1) {
+               // everyone has the most common variant (of the variants we don't drop)
                return nil, nil
        }
        tagoffset := tag - chunkstarttag
@@ -1205,34 +1219,36 @@ func (cmd *sliceNumpy) tv2homhet(cgs map[string]CompactGenome, maxv tileVariantI
                obs[i] = make([]bool, len(cmd.cgnames))
        }
        for cgid, name := range cmd.cgnames {
-               cgvars := cgs[name].Variants
-               for v := tileVariantID(2); v <= maxv; v++ {
-                       if remap[cgvars[tagoffset*2]] == v && remap[cgvars[tagoffset*2+1]] == v {
+               cgvars := cgs[name].Variants[tagoffset*2:]
+               tv0, tv1 := remap[cgvars[0]], remap[cgvars[1]]
+               for v := tileVariantID(1); v <= maxv; v++ {
+                       if tv0 == v && tv1 == v {
                                obs[v*2][cgid] = true
-                       } else if remap[cgvars[tagoffset*2]] == v || remap[cgvars[tagoffset*2+1]] == v {
+                       } else if tv0 == v || tv1 == v {
                                obs[v*2+1][cgid] = true
                        }
                }
        }
        var onehot [][]int8
        var xref []onehotXref
-       for homcol := 4; homcol < len(obs); homcol += 2 {
-               p := [2]float64{
-                       pvalue(obs[homcol], cmd.chi2Cases),
-                       pvalue(obs[homcol+1], cmd.chi2Cases),
-               }
-               if cmd.chi2PValue < 1 && !(p[0] < cmd.chi2PValue || p[1] < cmd.chi2PValue) {
+       for col := 2; col < len(obs); col++ {
+               // col 0,1 correspond to tile variant 0, i.e.,
+               // no-call; col 2,3 correspond to the most common
+               // variant; so we (normally) start at col 4.
+               if col < 4 && !cmd.includeVariant1 {
                        continue
                }
-               for het := 0; het < 2; het++ {
-                       onehot = append(onehot, bool2int8(obs[homcol+het]))
-                       xref = append(xref, onehotXref{
-                               tag:     tag,
-                               variant: tileVariantID(homcol / 2),
-                               het:     het == 1,
-                               pvalue:  p[het],
-                       })
+               p := pvalue(obs[col], cmd.chi2Cases)
+               if cmd.chi2PValue < 1 && !(p < cmd.chi2PValue) {
+                       continue
                }
+               onehot = append(onehot, bool2int8(obs[col]))
+               xref = append(xref, onehotXref{
+                       tag:     tag,
+                       variant: tileVariantID(col >> 1),
+                       het:     col&1 == 1,
+                       pvalue:  p,
+               })
        }
        return onehot, xref
 }