19524: Generalize plot colors a little.
[lightning.git] / plot.go
1 // Copyright (C) The Lightning Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package lightning
6
7 import (
8         _ "embed"
9         "flag"
10         "fmt"
11         "io"
12         _ "net/http/pprof"
13
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15 )
16
17 type pythonPlot struct{}
18
19 //go:embed plot.py
20 var plotscript string
21
22 func (cmd *pythonPlot) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
23         var err error
24         defer func() {
25                 if err != nil {
26                         fmt.Fprintf(stderr, "%s\n", err)
27                 }
28         }()
29         flags := flag.NewFlagSet("", flag.ContinueOnError)
30         flags.SetOutput(stderr)
31         projectUUID := flags.String("project", "", "project `UUID` for output data")
32         inputFilename := flags.String("i", "-", "input `file`")
33         sampleListFilename := flags.String("samples", "", "use second column of `samples.csv` as complete list of sample IDs")
34         phenotypeFilename := flags.String("phenotype", "", "use `phenotype.csv` as id->phenotype mapping (column 0 is sample id)")
35         phenotypeColumn := flags.Int("phenotype-column", 1, "0-based column `index` of phenotype in phenotype.csv file")
36         priority := flags.Int("priority", 500, "container request priority")
37         err = flags.Parse(args)
38         if err == flag.ErrHelp {
39                 err = nil
40                 return 0
41         } else if err != nil {
42                 return 2
43         }
44
45         runner := arvadosContainerRunner{
46                 Name:        "lightning plot",
47                 Client:      arvados.NewClientFromEnv(),
48                 ProjectUUID: *projectUUID,
49                 RAM:         4 << 30,
50                 VCPUs:       1,
51                 Priority:    *priority,
52                 Mounts: map[string]map[string]interface{}{
53                         "/plot.py": map[string]interface{}{
54                                 "kind":    "text",
55                                 "content": plotscript,
56                         },
57                 },
58         }
59         err = runner.TranslatePaths(inputFilename, sampleListFilename, phenotypeFilename)
60         if err != nil {
61                 return 1
62         }
63         runner.Prog = "python3"
64         runner.Args = []string{"/plot.py", *inputFilename, *sampleListFilename, *phenotypeFilename, fmt.Sprintf("%d", *phenotypeColumn), "/mnt/output/plot.png"}
65         var output string
66         output, err = runner.Run()
67         if err != nil {
68                 return 1
69         }
70         fmt.Fprintln(stdout, output+"/plot.png")
71         return 0
72 }