Merge branch '19868-pca-in-ml' into main
[lightning.git] / cmd.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         "fmt"
9         "io"
10         "io/ioutil"
11         "os"
12         "os/exec"
13         "runtime/debug"
14         "strings"
15
16         "git.arvados.org/arvados.git/lib/cmd"
17         "github.com/mattn/go-isatty"
18         "github.com/sirupsen/logrus"
19 )
20
21 var (
22         handler = cmd.Multi(map[string]cmd.Handler{
23                 "version":   cmd.Version,
24                 "-version":  cmd.Version,
25                 "--version": cmd.Version,
26
27                 "ref2genome":         &ref2genome{},
28                 "vcf2fasta":          &vcf2fasta{},
29                 "import":             &importer{},
30                 "annotate":           &annotatecmd{},
31                 "export":             &exporter{},
32                 "export-numpy":       &exportNumpy{},
33                 "flake":              &flakecmd{},
34                 "slice":              &slicecmd{},
35                 "slice-numpy":        &sliceNumpy{},
36                 "tiling-stats":       &tilingStats{},
37                 "anno2vcf":           &anno2vcf{},
38                 "numpy-comvar":       &numpyComVar{},
39                 "filter":             &filtercmd{},
40                 "build-docker-image": &buildDockerImage{},
41                 "plot":               &pythonPlot{},
42                 "pca-plot":           &pythonPlot{},
43                 "manhattan-plot":     &manhattanPlot{},
44                 "diff-fasta":         &diffFasta{},
45                 "stats":              &statscmd{},
46                 "merge":              &merger{},
47                 "dump":               &dump{},
48                 "dumpgob":            &dumpGob{},
49                 "choose-samples":     &chooseSamples{},
50         })
51 )
52
53 func init() {
54         if os.Getenv("GOGC") == "" {
55                 debug.SetGCPercent(30)
56         }
57 }
58
59 func Main() {
60         if !isatty.IsTerminal(os.Stderr.Fd()) {
61                 logrus.StandardLogger().Formatter = &logrus.TextFormatter{DisableTimestamp: true}
62         }
63         if len(os.Args) >= 2 && !strings.HasSuffix(os.Args[1], "version") {
64                 // print version (then run subcommand)
65                 cmd.Version.RunCommand("lightning", nil, nil, os.Stderr, os.Stderr)
66         }
67         os.Exit(handler.RunCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
68 }
69
70 type buildDockerImage struct{}
71
72 func (cmd *buildDockerImage) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
73         tmpdir, err := ioutil.TempDir("", "")
74         if err != nil {
75                 fmt.Fprint(stderr, err)
76                 return 1
77         }
78         defer os.RemoveAll(tmpdir)
79         err = ioutil.WriteFile(tmpdir+"/Dockerfile", []byte(`FROM debian:bullseye
80 RUN DEBIAN_FRONTEND=noninteractive \
81   apt-get update && \
82   apt-get dist-upgrade -y && \
83   apt-get install -y --no-install-recommends bcftools bedtools samtools python2 python3-sklearn python3-matplotlib python3-pip ca-certificates && \
84   apt-get clean && \
85   pip3 install qmplot
86 `), 0644)
87         if err != nil {
88                 fmt.Fprint(stderr, err)
89                 return 1
90         }
91         docker := exec.Command("docker", "build", "--tag=lightning-runtime", tmpdir)
92         docker.Stdout = stdout
93         docker.Stderr = stderr
94         err = docker.Run()
95         if err != nil {
96                 return 1
97         }
98         fmt.Fprintf(stderr, "built and tagged new docker image, lightning-runtime\n")
99         return 0
100 }