17 "git.arvados.org/arvados.git/sdk/go/arvados"
18 "github.com/kshedden/gonpy"
19 log "github.com/sirupsen/logrus"
22 type exportNumpy struct {
26 func (cmd *exportNumpy) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
30 fmt.Fprintf(stderr, "%s\n", err)
33 flags := flag.NewFlagSet("", flag.ContinueOnError)
34 flags.SetOutput(stderr)
35 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
36 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
37 projectUUID := flags.String("project", "", "project `UUID` for output data")
38 priority := flags.Int("priority", 500, "container request priority")
39 inputFilename := flags.String("i", "-", "input `file`")
40 outputFilename := flags.String("o", "-", "output `file`")
41 annotationsFilename := flags.String("output-annotations", "", "output `file` for tile variant annotations tsv")
42 librefsFilename := flags.String("output-onehot2tilevar", "", "when using -one-hot, create tsv `file` mapping column# to tag# and variant#")
43 onehot := flags.Bool("one-hot", false, "recode tile variants as one-hot")
44 cmd.filter.Flags(flags)
45 err = flags.Parse(args)
46 if err == flag.ErrHelp {
49 } else if err != nil {
55 log.Println(http.ListenAndServe(*pprof, nil))
60 if *outputFilename != "-" {
61 err = errors.New("cannot specify output file in container mode: not implemented")
64 runner := arvadosContainerRunner{
65 Name: "lightning export-numpy",
66 Client: arvados.NewClientFromEnv(),
67 ProjectUUID: *projectUUID,
72 err = runner.TranslatePaths(inputFilename)
76 runner.Args = []string{"export-numpy", "-local=true",
77 fmt.Sprintf("-one-hot=%v", *onehot),
79 "-o", "/mnt/output/matrix.npy",
80 "-output-annotations", "/mnt/output/annotations.tsv",
81 "-output-onehot2tilevar", "/mnt/output/onehot2tilevar.tsv",
82 "-max-variants", fmt.Sprintf("%d", cmd.filter.MaxVariants),
83 "-min-coverage", fmt.Sprintf("%f", cmd.filter.MinCoverage),
84 "-max-tag", fmt.Sprintf("%d", cmd.filter.MaxTag),
87 output, err = runner.Run()
91 fmt.Fprintln(stdout, output+"/matrix.npy")
95 var input io.ReadCloser
96 if *inputFilename == "-" {
97 input = ioutil.NopCloser(stdin)
99 input, err = os.Open(*inputFilename)
105 tilelib := &tileLibrary{
107 retainTileSequences: true,
108 compactGenomes: map[string][]tileVariantID{},
110 err = tilelib.LoadGob(context.Background(), input, strings.HasSuffix(*inputFilename, ".gz"), nil)
119 log.Info("filtering")
120 cmd.filter.Apply(tilelib)
124 if *annotationsFilename != "" {
125 log.Infof("writing annotations")
126 var annow io.WriteCloser
127 annow, err = os.OpenFile(*annotationsFilename, os.O_CREATE|os.O_WRONLY, 0666)
132 err = (&annotatecmd{maxTileSize: 5000}).exportTileDiffs(annow, tilelib)
142 log.Info("building numpy array")
143 out, rows, cols := cgs2array(tilelib)
145 log.Info("writing numpy file")
146 var output io.WriteCloser
147 if *outputFilename == "-" {
148 output = nopCloser{stdout}
150 output, err = os.OpenFile(*outputFilename, os.O_CREATE|os.O_WRONLY, 0777)
156 bufw := bufio.NewWriter(output)
157 npw, err := gonpy.NewWriter(nopCloser{bufw})
162 log.Info("recoding to onehot")
163 recoded, librefs, recodedcols := recodeOnehot(out, cols)
164 out, cols = recoded, recodedcols
165 if *librefsFilename != "" {
166 log.Infof("writing onehot column mapping")
167 err = cmd.writeLibRefs(*librefsFilename, tilelib, librefs)
173 log.Info("writing numpy")
174 npw.Shape = []int{rows, cols}
187 func (*exportNumpy) writeLibRefs(fnm string, tilelib *tileLibrary, librefs []tileLibRef) error {
188 f, err := os.OpenFile(fnm, os.O_CREATE|os.O_WRONLY, 0666)
193 for i, libref := range librefs {
194 _, err = fmt.Fprintf(f, "%d\t%d\t%d\n", i, libref.Tag, libref.Variant)
202 func cgs2array(tilelib *tileLibrary) (data []int16, rows, cols int) {
204 for name := range tilelib.compactGenomes {
205 cgnames = append(cgnames, name)
207 sort.Strings(cgnames)
209 rows = len(tilelib.compactGenomes)
210 for _, cg := range tilelib.compactGenomes {
216 // flag low-quality tile variants so we can change to -1 below
217 lowqual := make([]map[tileVariantID]bool, cols/2)
218 for tag, variants := range tilelib.variant {
220 for varidx, hash := range variants {
221 if len(tilelib.seq[hash]) == 0 {
223 lq = map[tileVariantID]bool{}
226 lq[tileVariantID(varidx+1)] = true
231 data = make([]int16, rows*cols)
232 for row, name := range cgnames {
233 for i, v := range tilelib.compactGenomes[name] {
234 if v > 0 && lowqual[i/2][v] {
235 data[row*cols+i] = -1
237 data[row*cols+i] = int16(v)
245 func recodeOnehot(in []int16, incols int) (out []int16, librefs []tileLibRef, outcols int) {
246 rows := len(in) / incols
247 maxvalue := make([]int16, incols)
248 for row := 0; row < rows; row++ {
249 for col := 0; col < incols; col++ {
250 if v := in[row*incols+col]; maxvalue[col] < v {
255 outcol := make([]int, incols)
257 for incol, maxv := range maxvalue {
258 outcol[incol] = outcols
262 for v := 1; v <= int(maxv); v++ {
263 librefs = append(librefs, tileLibRef{Tag: tagID(incol), Variant: tileVariantID(v)})
267 log.Printf("recodeOnehot: dropped %d input cols with zero maxvalue", dropped)
269 out = make([]int16, rows*outcols)
270 for inidx, row := 0, 0; row < rows; row++ {
271 outrow := out[row*outcols:]
272 for col := 0; col < incols; col++ {
273 if v := in[inidx]; v > 0 {
274 outrow[outcol[col]+int(v)-1] = 1
282 type nopCloser struct {
286 func (nopCloser) Close() error { return nil }