21 "git.arvados.org/arvados.git/sdk/go/arvados"
22 "github.com/arvados/lightning/hgvs"
23 "github.com/kshedden/gonpy"
24 "github.com/sirupsen/logrus"
25 log "github.com/sirupsen/logrus"
28 type exportNumpy struct {
32 func (cmd *exportNumpy) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
36 fmt.Fprintf(stderr, "%s\n", err)
39 flags := flag.NewFlagSet("", flag.ContinueOnError)
40 flags.SetOutput(stderr)
41 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
42 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
43 projectUUID := flags.String("project", "", "project `UUID` for output data")
44 priority := flags.Int("priority", 500, "container request priority")
45 inputDir := flags.String("input-dir", "./in", "input `directory`")
46 outputDir := flags.String("output-dir", "./out", "output `directory`")
47 annotationsFilename := flags.String("output-annotations", "", "output `file` for tile variant annotations csv")
48 librefsFilename := flags.String("output-onehot2tilevar", "", "when using -one-hot, create csv `file` mapping column# to tag# and variant#")
49 labelsFilename := flags.String("output-labels", "", "output `file` for genome labels csv")
50 regionsFilename := flags.String("regions", "", "only output columns/annotations that intersect regions in specified bed `file`")
51 expandRegions := flags.Int("expand-regions", 0, "expand specified regions by `N` base pairs on each side`")
52 onehot := flags.Bool("one-hot", false, "recode tile variants as one-hot")
53 chunks := flags.Int("chunks", 1, "split output into `N` numpy files")
54 cmd.filter.Flags(flags)
55 err = flags.Parse(args)
56 if err == flag.ErrHelp {
59 } else if err != nil {
65 log.Println(http.ListenAndServe(*pprof, nil))
70 runner := arvadosContainerRunner{
71 Name: "lightning export-numpy",
72 Client: arvados.NewClientFromEnv(),
73 ProjectUUID: *projectUUID,
80 err = runner.TranslatePaths(inputDir, regionsFilename)
84 runner.Args = []string{"export-numpy", "-local=true",
86 fmt.Sprintf("-one-hot=%v", *onehot),
87 "-input-dir", *inputDir,
88 "-output-dir", "/mnt/output",
89 "-output-annotations", "/mnt/output/annotations.csv",
90 "-output-onehot2tilevar", "/mnt/output/onehot2tilevar.csv",
91 "-output-labels", "/mnt/output/labels.csv",
92 "-regions", *regionsFilename,
93 "-expand-regions", fmt.Sprintf("%d", *expandRegions),
94 "-max-variants", fmt.Sprintf("%d", cmd.filter.MaxVariants),
95 "-min-coverage", fmt.Sprintf("%f", cmd.filter.MinCoverage),
96 "-max-tag", fmt.Sprintf("%d", cmd.filter.MaxTag),
97 "-chunks", fmt.Sprintf("%d", *chunks),
100 output, err = runner.Run()
104 fmt.Fprintln(stdout, output+"/matrix.npy")
108 tilelib := &tileLibrary{
110 retainTileSequences: true,
111 compactGenomes: map[string][]tileVariantID{},
113 err = tilelib.LoadDir(context.Background(), *inputDir, nil)
118 log.Info("filtering")
119 cmd.filter.Apply(tilelib)
123 log.Info("building lowqual map")
124 lowqual := lowqual(tilelib)
125 names := cgnames(tilelib)
127 if *labelsFilename != "" {
128 log.Infof("writing labels to %s", *labelsFilename)
130 f, err = os.OpenFile(*labelsFilename, os.O_CREATE|os.O_WRONLY, 0777)
135 outBasename := "matrix.npy"
136 for i, name := range names {
137 _, err = fmt.Fprintf(f, "%d,%q,%q\n", i, trimFilenameForLabel(name), outBasename)
139 err = fmt.Errorf("write %s: %w", *labelsFilename, err)
145 err = fmt.Errorf("close %s: %w", *labelsFilename, err)
150 log.Info("determining which tiles intersect given regions")
151 dropTiles, err := chooseTiles(tilelib, *regionsFilename, *expandRegions)
156 annotation2tvs := map[string]map[hgvs.Variant][]tileLibRef{}
157 if *annotationsFilename != "" {
158 log.Info("writing annotations")
159 var annow io.WriteCloser
160 annow, err = os.OpenFile(*annotationsFilename, os.O_CREATE|os.O_WRONLY, 0666)
168 dropTiles: dropTiles,
169 reportAnnotation: func(tag tagID, _ int, variant tileVariantID, refname string, seqname string, pdi hgvs.Variant) {
172 if annotation2tvs[seqname] == nil {
173 annotation2tvs[seqname] = map[hgvs.Variant][]tileLibRef{}
175 annotation2tvs[seqname][pdi] = append(annotation2tvs[seqname][pdi], tileLibRef{Tag: tag, Variant: variant})
177 }).exportTileDiffs(annow, tilelib)
187 var lastErr atomic.Value
188 var wg sync.WaitGroup
189 for seqname, pdivars := range annotation2tvs {
190 seqname, pdivars := seqname, pdivars
194 log.Infof("choosing hgvs columns for seq %s", seqname)
195 var pdis []hgvs.Variant
196 for pdi, librefs := range pdivars {
197 // Include this HGVS column if it was
198 // seen in a variant of any
200 for _, libref := range librefs {
201 if int(libref.Tag) >= len(dropTiles) || !dropTiles[libref.Tag] {
202 pdis = append(pdis, pdi)
207 sort.Slice(pdis, func(i, j int) bool {
208 if cmp := pdis[i].Position - pdis[j].Position; cmp != 0 {
210 } else if pdis[i].Ref != pdis[j].Ref {
211 return pdis[i].Ref < pdis[j].Ref
213 return pdis[i].New < pdis[j].New
216 log.Infof("writing column labels for seq %s", seqname)
218 for _, pdi := range pdis {
219 fmt.Fprintf(&buf, "%s:g.%s\n", seqname, pdi.String())
221 err := ioutil.WriteFile(*outputDir+"/"+seqname+".columns.csv", buf.Bytes(), 0777)
227 log.Infof("building hgvs matrix for seq %s", seqname)
228 data := make([]int8, len(names)*len(pdis)*2)
229 for row, name := range names {
230 cg := tilelib.compactGenomes[name]
231 rowstart := row * len(pdis) * 2
232 for col, pdi := range pdis {
233 for _, libref := range pdivars[pdi] {
234 if len(cg) <= int(libref.Tag)*2+1 {
237 for phase := 0; phase < 2; phase++ {
238 if cg[int(libref.Tag)*2+phase] == libref.Variant {
239 data[rowstart+col*2+phase] = 1
245 log.Infof("writing hgvs numpy for seq %s", seqname)
246 f, err := os.OpenFile(*outputDir+"/"+seqname+".npy", os.O_CREATE|os.O_WRONLY, 0777)
252 npw, err := gonpy.NewWriter(f)
257 npw.Shape = []int{len(names), len(pdis) * 2}
259 // gonpy closes f and ignores errors, doh.
262 // lastErr.Store(err)
268 if e, ok := lastErr.Load().(error); ok {
273 chunksize := (len(tilelib.variant) + *chunks - 1) / *chunks
274 for chunk := 0; chunk < *chunks; chunk++ {
275 log.Infof("preparing chunk %d of %d", chunk+1, *chunks)
276 tagstart := chunk * chunksize
277 tagend := tagstart + chunksize
278 if tagend > len(tilelib.variant) {
279 tagend = len(tilelib.variant)
281 out, rows, cols := cgs2array(tilelib, names, lowqual, dropTiles, tagstart, tagend)
283 var npw *gonpy.NpyWriter
284 var output io.WriteCloser
285 fnm := *outputDir + "/matrix.npy"
287 fnm = fmt.Sprintf("%s/matrix.%d.npy", *outputDir, chunk)
289 output, err = os.OpenFile(fnm, os.O_CREATE|os.O_WRONLY, 0777)
294 bufw := bufio.NewWriter(output)
295 npw, err = gonpy.NewWriter(nopCloser{bufw})
300 log.Info("recoding to onehot")
301 recoded, librefs, recodedcols := recodeOnehot(out, cols)
302 out, cols = recoded, recodedcols
303 if *librefsFilename != "" {
304 log.Infof("writing onehot column mapping")
305 err = cmd.writeLibRefs(*librefsFilename, tilelib, librefs)
311 log.WithFields(logrus.Fields{
315 }).Info("writing numpy")
316 npw.Shape = []int{rows, cols}
330 func (*exportNumpy) writeLibRefs(fnm string, tilelib *tileLibrary, librefs []tileLibRef) error {
331 f, err := os.OpenFile(fnm, os.O_CREATE|os.O_WRONLY, 0666)
336 for i, libref := range librefs {
337 _, err = fmt.Fprintf(f, "%d,%d,%d\n", i, libref.Tag, libref.Variant)
345 func cgnames(tilelib *tileLibrary) (cgnames []string) {
346 for name := range tilelib.compactGenomes {
347 cgnames = append(cgnames, name)
349 sort.Slice(cgnames, func(i, j int) bool {
350 return trimFilenameForLabel(cgnames[i]) < trimFilenameForLabel(cgnames[j])
355 func lowqual(tilelib *tileLibrary) (lowqual []map[tileVariantID]bool) {
356 lowqual = make([]map[tileVariantID]bool, len(tilelib.variant))
357 for tag, variants := range tilelib.variant {
359 for varidx, hash := range variants {
360 if len(tilelib.seq[hash]) == 0 {
362 lq = map[tileVariantID]bool{}
365 lq[tileVariantID(varidx+1)] = true
372 func cgs2array(tilelib *tileLibrary, names []string, lowqual []map[tileVariantID]bool, dropTiles []bool, tagstart, tagend int) (data []int16, rows, cols int) {
373 rows = len(tilelib.compactGenomes)
374 for tag := tagstart; tag < tagend; tag++ {
375 if len(dropTiles) <= tag || !dropTiles[tag] {
379 data = make([]int16, rows*cols)
380 for row, name := range names {
381 cg := tilelib.compactGenomes[name]
383 for tag := tagstart; tag < tagend && tag*2+1 < len(cg); tag++ {
384 if len(dropTiles) > tag && dropTiles[tag] {
387 for phase := 0; phase < 2; phase++ {
389 if v > 0 && lowqual[tag][v] {
390 data[row*cols+outidx] = -1
392 data[row*cols+outidx] = int16(v)
401 func chooseTiles(tilelib *tileLibrary, regionsFilename string, expandRegions int) (drop []bool, err error) {
402 if regionsFilename == "" {
405 rfile, err := zopen(regionsFilename)
410 regions, err := ioutil.ReadAll(rfile)
415 log.Print("chooseTiles: building mask")
417 for _, line := range bytes.Split(regions, []byte{'\n'}) {
418 if bytes.HasPrefix(line, []byte{'#'}) {
421 fields := bytes.Split(line, []byte{'\t'})
425 refseqname := string(fields[0])
426 if strings.HasPrefix(refseqname, "chr") {
427 refseqname = refseqname[3:]
429 start, err1 := strconv.Atoi(string(fields[1]))
430 end, err2 := strconv.Atoi(string(fields[2]))
431 if err1 == nil && err2 == nil {
434 start, err1 = strconv.Atoi(string(fields[3]))
435 end, err2 = strconv.Atoi(string(fields[4]))
436 if err1 == nil && err2 == nil {
440 err = fmt.Errorf("cannot parse input line as BED or GFF/GTF: %q", line)
444 mask.Add(refseqname, start-expandRegions, end+expandRegions)
446 log.Print("chooseTiles: mask.Freeze")
449 tagset := tilelib.taglib.Tags()
450 if len(tagset) == 0 {
451 err = errors.New("cannot choose tiles by region in a library without tags")
454 taglen := len(tagset[0])
456 log.Print("chooseTiles: check ref tiles")
457 // Find position+size of each reference tile, and if it
458 // intersects any of the desired regions, set drop[tag]=false.
460 // (Note it doesn't quite work to do the more obvious thing --
461 // start with drop=false and change to true when ref tiles
462 // intersect target regions -- because that would give us
463 // drop=false for tiles that don't appear at all in the
466 // TODO: (optionally?) don't drop tags for which some tile
467 // variants are spanning tiles, i.e., where the reference tile
468 // does not intersect the desired regions, but a spanning tile
469 // from a genome does.
470 drop = make([]bool, len(tilelib.variant))
471 for i := range drop {
474 for refname, refseqs := range tilelib.refseqs {
475 for refseqname, reftiles := range refseqs {
476 if strings.HasPrefix(refseqname, "chr") {
477 refseqname = refseqname[3:]
480 for _, libref := range reftiles {
481 if libref.Variant < 1 {
482 err = fmt.Errorf("reference %q seq %q uses variant zero at tag %d", refname, refseqname, libref.Tag)
485 seq := tilelib.TileVariantSequence(libref)
486 if len(seq) < taglen {
487 err = fmt.Errorf("reference %q seq %q uses tile %d variant %d with sequence len %d < taglen %d", refname, refseqname, libref.Tag, libref.Variant, len(seq), taglen)
491 tileend = tilestart + len(seq) - taglen
492 if mask.Check(refseqname, tilestart, tileend) {
493 drop[libref.Tag] = false
499 log.Print("chooseTiles: done")
503 func recodeOnehot(in []int16, incols int) (out []int16, librefs []tileLibRef, outcols int) {
504 rows := len(in) / incols
505 maxvalue := make([]int16, incols)
506 for row := 0; row < rows; row++ {
507 for col := 0; col < incols; col++ {
508 if v := in[row*incols+col]; maxvalue[col] < v {
513 outcol := make([]int, incols)
515 for incol, maxv := range maxvalue {
516 outcol[incol] = outcols
520 for v := 1; v <= int(maxv); v++ {
521 librefs = append(librefs, tileLibRef{Tag: tagID(incol), Variant: tileVariantID(v)})
525 log.Printf("recodeOnehot: dropped %d input cols with zero maxvalue", dropped)
527 out = make([]int16, rows*outcols)
528 for inidx, row := 0, 0; row < rows; row++ {
529 outrow := out[row*outcols:]
530 for col := 0; col < incols; col++ {
531 if v := in[inidx]; v > 0 {
532 outrow[outcol[col]+int(v)-1] = 1
540 type nopCloser struct {
544 func (nopCloser) Close() error { return nil }
546 func trimFilenameForLabel(s string) string {
547 if i := strings.LastIndex(s, "/"); i >= 0 {
550 s = strings.TrimSuffix(s, ".gz")
551 s = strings.TrimSuffix(s, ".fa")
552 s = strings.TrimSuffix(s, ".fasta")
553 s = strings.TrimSuffix(s, ".1")
554 s = strings.TrimSuffix(s, ".2")
555 s = strings.TrimSuffix(s, ".gz")
556 s = strings.TrimSuffix(s, ".vcf")