X-Git-Url: https://git.arvados.org/lightning.git/blobdiff_plain/39ff1c4d88a5d0273af65c354d68fa07dfe664a4..0bdd658af496fbc2ca2f9e55a5f4ee6c18d06b66:/slicenumpy.go diff --git a/slicenumpy.go b/slicenumpy.go index 8a14bfdab0..c3d02a99bc 100644 --- a/slicenumpy.go +++ b/slicenumpy.go @@ -8,6 +8,7 @@ import ( "bufio" "bytes" "encoding/gob" + "encoding/json" "errors" "flag" "fmt" @@ -43,6 +44,8 @@ type sliceNumpy struct { threads int chi2Cases []bool chi2PValue float64 + glmMinFrequency float64 + pcaComponents int minCoverage int includeVariant1 bool debugTag tagID @@ -51,6 +54,8 @@ type sliceNumpy struct { samples []sampleInfo trainingSet []int // samples index => training set index, or -1 if not in training set trainingSetSize int + pvalue func(onehot []bool) float64 + pvalueCallCount int64 } func (cmd *sliceNumpy) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int { @@ -71,6 +76,7 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, arvadosVCPUs := flags.Int("arvados-vcpus", 96, "number of VCPUs to request for arvados container") projectUUID := flags.String("project", "", "project `UUID` for output data") priority := flags.Int("priority", 500, "container request priority") + preemptible := flags.Bool("preemptible", true, "request preemptible instance") inputDir := flags.String("input-dir", "./in", "input `directory`") outputDir := flags.String("output-dir", "./out", "output `directory`") ref := flags.String("ref", "", "reference name (if blank, choose last one that appears in input)") @@ -82,12 +88,14 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, onehotSingle := flags.Bool("single-onehot", false, "generate one-hot tile-based matrix") onehotChunked := flags.Bool("chunked-onehot", false, "generate one-hot tile-based matrix per input chunk") samplesFilename := flags.String("samples", "", "`samples.csv` file with training/validation and case/control groups (see 'lightning choose-samples')") - onlyPCA := flags.Bool("pca", false, "generate pca matrix") - pcaComponents := flags.Int("pca-components", 4, "number of PCA components") + caseControlOnly := flags.Bool("case-control-only", false, "drop samples that are not in case/control groups") + onlyPCA := flags.Bool("pca", false, "run principal component analysis, write components to pca.npy and samples.csv") + flags.IntVar(&cmd.pcaComponents, "pca-components", 4, "number of PCA components to compute / use in logistic regression") maxPCATiles := flags.Int("max-pca-tiles", 0, "maximum tiles to use as PCA input (filter, then drop every 2nd colum pair until below max)") debugTag := flags.Int("debug-tag", -1, "log debugging details about specified tag") flags.IntVar(&cmd.threads, "threads", 16, "number of memory-hungry assembly threads, and number of VCPUs to request for arvados container") - flags.Float64Var(&cmd.chi2PValue, "chi2-p-value", 1, "do Χ² test and omit columns with p-value above this threshold") + 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") + flags.Float64Var(&cmd.glmMinFrequency, "glm-min-frequency", 0.01, "skip GLM calculation on tile variants below this frequency in the training set") 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) @@ -95,6 +103,8 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, return nil } else if err != nil { return err + } else if flags.NArg() > 0 { + return fmt.Errorf("errant command line arguments after parsed flags: %v", flags.Args()) } if *pprof != "" { @@ -119,6 +129,7 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, Priority: *priority, KeepCache: 2, APIAccess: true, + Preemptible: *preemptible, } err = runner.TranslatePaths(inputDir, regionsFilename, samplesFilename) if err != nil { @@ -137,10 +148,12 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, "-single-onehot=" + fmt.Sprintf("%v", *onehotSingle), "-chunked-onehot=" + fmt.Sprintf("%v", *onehotChunked), "-samples=" + *samplesFilename, + "-case-control-only=" + fmt.Sprintf("%v", *caseControlOnly), "-pca=" + fmt.Sprintf("%v", *onlyPCA), - "-pca-components=" + fmt.Sprintf("%d", *pcaComponents), + "-pca-components=" + fmt.Sprintf("%d", cmd.pcaComponents), "-max-pca-tiles=" + fmt.Sprintf("%d", *maxPCATiles), "-chi2-p-value=" + fmt.Sprintf("%f", cmd.chi2PValue), + "-glm-min-frequency=" + fmt.Sprintf("%f", cmd.glmMinFrequency), "-include-variant-1=" + fmt.Sprintf("%v", cmd.includeVariant1), "-debug-tag=" + fmt.Sprintf("%d", cmd.debugTag), } @@ -178,10 +191,12 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, } if *samplesFilename != "" { - cmd.samples, err = cmd.loadSampleInfo(*samplesFilename) + cmd.samples, err = loadSampleInfo(*samplesFilename) if err != nil { return err } + } else if *caseControlOnly { + return fmt.Errorf("-case-control-only does not make sense without -samples") } cmd.cgnames = nil @@ -244,18 +259,40 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, } else if len(cmd.cgnames) != len(cmd.samples) { return fmt.Errorf("mismatched sample list: %d samples in library, %d in %s", len(cmd.cgnames), len(cmd.samples), *samplesFilename) } else { - cmd.trainingSetSize = 0 for i, name := range cmd.cgnames { if s := trimFilenameForLabel(name); s != cmd.samples[i].id { return fmt.Errorf("mismatched sample list: sample %d is %q in library, %q in %s", i, s, cmd.samples[i].id, *samplesFilename) } + } + if *caseControlOnly { + for i := 0; i < len(cmd.samples); i++ { + if !cmd.samples[i].isTraining && !cmd.samples[i].isValidation { + if i+1 < len(cmd.samples) { + copy(cmd.samples[i:], cmd.samples[i+1:]) + copy(cmd.cgnames[i:], cmd.cgnames[i+1:]) + } + cmd.samples = cmd.samples[:len(cmd.samples)-1] + cmd.cgnames = cmd.cgnames[:len(cmd.cgnames)-1] + i-- + } + } + } + cmd.chi2Cases = nil + cmd.trainingSetSize = 0 + for i := range cmd.cgnames { if cmd.samples[i].isTraining { cmd.trainingSet[i] = cmd.trainingSetSize cmd.trainingSetSize++ + cmd.chi2Cases = append(cmd.chi2Cases, cmd.samples[i].isCase) } else { cmd.trainingSet[i] = -1 } } + if cmd.pvalue == nil { + cmd.pvalue = func(onehot []bool) float64 { + return pvalue(onehot, cmd.chi2Cases) + } + } } if cmd.filter.MinCoverage == 1 { // In the generic formula below, floating point @@ -267,6 +304,33 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, cmd.minCoverage = int(math.Ceil(cmd.filter.MinCoverage * float64(len(cmd.cgnames)))) } + if len(cmd.samples[0].pcaComponents) > 0 { + cmd.pvalue = glmPvalueFunc(cmd.samples, cmd.pcaComponents, cmd.glmMinFrequency) + // Unfortunately, statsmodel/glm lib logs stuff to + // os.Stdout when it panics on an unsolvable + // problem. We recover() from the panic in glm.go, but + // we also need to commandeer os.Stdout to avoid + // producing large quantities of logs. + stdoutWas := os.Stdout + defer func() { os.Stdout = stdoutWas }() + os.Stdout, err = os.Open(os.DevNull) + if err != nil { + return err + } + } + + // cgnamemap[name]==true for samples that we are including in + // output + cgnamemap := map[string]bool{} + for _, name := range cmd.cgnames { + cgnamemap[name] = true + } + + err = writeSampleInfo(cmd.samples, *outputDir) + if err != nil { + return err + } + log.Info("indexing reference tiles") type reftileinfo struct { variant tileVariantID @@ -448,7 +512,7 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, if cmd.filter.MaxTag >= 0 && cg.StartTag > tagID(cmd.filter.MaxTag) { return errSkip } - if !matchGenome.MatchString(cg.Name) { + if !cgnamemap[cg.Name] { continue } // pad to full slice size @@ -604,6 +668,12 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, break } remap := variantRemap[tag-tagstart] + if remap == nil { + // was not assigned above, + // because minCoverage + outcol++ + continue + } maxv := tileVariantID(0) for _, v := range remap { if maxv < v { @@ -1109,6 +1179,17 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, if err != nil { return err } + fnm = fmt.Sprintf("%s/stats.json", *outputDir) + j, err := json.Marshal(map[string]interface{}{ + "pvalueCallCount": cmd.pvalueCallCount, + }) + if err != nil { + return err + } + err = os.WriteFile(fnm, j, 0777) + if err != nil { + return err + } } if *onlyPCA { cols := 0 @@ -1126,6 +1207,10 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, cols = (cols + 1) / 2 stride = stride * 2 } + if cols%2 == 1 { + // we work with pairs of columns + cols++ + } log.Printf("creating full matrix (%d rows) and training matrix (%d rows) with %d cols, stride %d", len(cmd.cgnames), cmd.trainingSetSize, cols, stride) mtxFull := mat.NewDense(len(cmd.cgnames), cols, nil) mtxTrain := mat.NewDense(cmd.trainingSetSize, cols, nil) @@ -1139,7 +1224,7 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, } } log.Print("fitting") - transformer := nlp.NewPCA(*pcaComponents) + transformer := nlp.NewPCA(cmd.pcaComponents) transformer.Fit(mtxTrain.T()) log.Printf("transforming") pca, err := transformer.Transform(mtxFull.T()) @@ -1176,42 +1261,19 @@ func (cmd *sliceNumpy) run(prog string, args []string, stdin io.Reader, stdout, } log.Print("done") - samplesOutFilename := *outputDir + "/samples.csv" - log.Infof("writing sample metadata to %s", samplesOutFilename) - var f *os.File - f, err = os.Create(samplesOutFilename) - if err != nil { - return err - } - defer f.Close() - for i, si := range cmd.samples { - var cc, tv string - if si.isCase { - cc = "1" - } else if si.isControl { - cc = "0" - } - if si.isTraining { - tv = "1" - } else { - tv = "0" - } - var pcavals string + log.Print("copying pca components to sampleInfo") + for i := range cmd.samples { + cmd.samples[i].pcaComponents = make([]float64, outcols) for c := 0; c < outcols; c++ { - pcavals += fmt.Sprintf(",%f", pca.At(i, c)) - } - _, err = fmt.Fprintf(f, "%d,%s,%s,%s%s\n", i, si.id, cc, tv, pcavals) - if err != nil { - err = fmt.Errorf("write %s: %w", samplesOutFilename, err) - return err + cmd.samples[i].pcaComponents[i] = pca.At(i, c) } } - err = f.Close() + log.Print("done") + + err = writeSampleInfo(cmd.samples, *outputDir) if err != nil { - err = fmt.Errorf("close %s: %w", samplesOutFilename, err) return err } - log.Print("done") } } if !*mergeOutput && !*onehotChunked && !*onehotSingle && !*onlyPCA { @@ -1251,7 +1313,7 @@ type sampleInfo struct { // Read samples.csv file with case/control and training/validation // flags. -func (cmd *sliceNumpy) loadSampleInfo(samplesFilename string) ([]sampleInfo, error) { +func loadSampleInfo(samplesFilename string) ([]sampleInfo, error) { var si []sampleInfo f, err := open(samplesFilename) if err != nil { @@ -1269,8 +1331,8 @@ func (cmd *sliceNumpy) loadSampleInfo(samplesFilename string) ([]sampleInfo, err continue } split := strings.Split(string(csv), ",") - if len(split) != 4 { - return nil, fmt.Errorf("%d fields != 4 in %s line %d: %q", len(split), samplesFilename, lineNum, csv) + if len(split) < 4 { + return nil, fmt.Errorf("%d fields < 4 in %s line %d: %q", len(split), samplesFilename, lineNum, csv) } if split[0] == "Index" && split[1] == "SampleID" && split[2] == "CaseControl" && split[3] == "TrainingValidation" { continue @@ -1285,17 +1347,75 @@ func (cmd *sliceNumpy) loadSampleInfo(samplesFilename string) ([]sampleInfo, err if idx != len(si) { return nil, fmt.Errorf("%s line %d: index %d out of order", samplesFilename, lineNum, idx) } + var pcaComponents []float64 + if len(split) > 4 { + for _, s := range split[4:] { + f, err := strconv.ParseFloat(s, 64) + if err != nil { + return nil, fmt.Errorf("%s line %d: cannot parse float %q: %s", samplesFilename, lineNum, s, err) + } + pcaComponents = append(pcaComponents, f) + } + } si = append(si, sampleInfo{ - id: split[1], - isCase: split[2] == "1", - isControl: split[2] == "0", - isTraining: split[3] == "1", - isValidation: split[3] == "0", + id: split[1], + isCase: split[2] == "1", + isControl: split[2] == "0", + isTraining: split[3] == "1", + isValidation: split[3] == "0" && len(split[2]) > 0, // fix errant 0s in input + pcaComponents: pcaComponents, }) } return si, nil } +func writeSampleInfo(samples []sampleInfo, outputDir string) error { + fnm := outputDir + "/samples.csv" + log.Infof("writing sample metadata to %s", fnm) + f, err := os.Create(fnm) + if err != nil { + return err + } + defer f.Close() + pcaLabels := "" + if len(samples) > 0 { + for i := range samples[0].pcaComponents { + pcaLabels += fmt.Sprintf(",PCA%d", i) + } + } + _, err = fmt.Fprintf(f, "Index,SampleID,CaseControl,TrainingValidation%s\n", pcaLabels) + if err != nil { + return err + } + for i, si := range samples { + var cc, tv string + if si.isCase { + cc = "1" + } else if si.isControl { + cc = "0" + } + if si.isTraining { + tv = "1" + } else if si.isValidation { + tv = "0" + } + var pcavals string + for _, pcaval := range si.pcaComponents { + pcavals += fmt.Sprintf(",%f", pcaval) + } + _, err = fmt.Fprintf(f, "%d,%s,%s,%s%s\n", i, si.id, cc, tv, pcavals) + if err != nil { + return fmt.Errorf("write %s: %w", fnm, err) + } + } + err = f.Close() + if err != nil { + return fmt.Errorf("close %s: %w", fnm, err) + } + log.Print("done") + return nil +} + func (cmd *sliceNumpy) filterHGVScolpair(colpair [2][]int8) bool { if cmd.chi2PValue >= 1 { return true @@ -1486,22 +1606,30 @@ func (cmd *sliceNumpy) tv2homhet(cgs map[string]CompactGenome, maxv tileVariantI if coverage < cmd.minCoverage { return nil, nil } + // "observed" array for p-value calculation (training set + // only) obs := make([][]bool, (maxv+1)*2) // 2 slices (hom + het) for each variant# + // one-hot output (all samples) + outcols := make([][]int8, (maxv+1)*2) for i := range obs { obs[i] = make([]bool, cmd.trainingSetSize) + outcols[i] = make([]int8, len(cmd.cgnames)) } for cgid, name := range cmd.cgnames { tsid := cmd.trainingSet[cgid] - if tsid < 0 { - continue - } 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][tsid] = true + if tsid >= 0 { + obs[v*2][tsid] = true + } + outcols[v*2][cgid] = 1 } else if tv0 == v || tv1 == v { - obs[v*2+1][tsid] = true + if tsid >= 0 { + obs[v*2+1][tsid] = true + } + outcols[v*2+1][cgid] = 1 } } } @@ -1514,11 +1642,12 @@ func (cmd *sliceNumpy) tv2homhet(cgs map[string]CompactGenome, maxv tileVariantI if col < 4 && !cmd.includeVariant1 { continue } - p := pvalue(obs[col], cmd.chi2Cases) + atomic.AddInt64(&cmd.pvalueCallCount, 1) + p := cmd.pvalue(obs[col]) if cmd.chi2PValue < 1 && !(p < cmd.chi2PValue) { continue } - onehot = append(onehot, bool2int8(obs[col])) + onehot = append(onehot, outcols[col]) xref = append(xref, onehotXref{ tag: tag, variant: tileVariantID(col >> 1), @@ -1529,16 +1658,6 @@ func (cmd *sliceNumpy) tv2homhet(cgs map[string]CompactGenome, maxv tileVariantI return onehot, xref } -func bool2int8(in []bool) []int8 { - out := make([]int8, len(in)) - for i, v := range in { - if v { - out[i] = 1 - } - } - return out -} - // convert a []onehotXref with length N to a numpy-style []int32 // matrix with N columns, one row per field of onehotXref struct. //