Fix Χ² calculation.
[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                 "anno2vcf":           &anno2vcf{},
37                 "numpy-comvar":       &numpyComVar{},
38                 "filter":             &filtercmd{},
39                 "build-docker-image": &buildDockerImage{},
40                 "pca-go":             &goPCA{},
41                 "pca-py":             &pythonPCA{},
42                 "plot":               &pythonPlot{},
43                 "diff-fasta":         &diffFasta{},
44                 "stats":              &statscmd{},
45                 "merge":              &merger{},
46                 "dumpgob":            &dumpGob{},
47         })
48 )
49
50 func init() {
51         if os.Getenv("GOGC") == "" {
52                 debug.SetGCPercent(30)
53         }
54 }
55
56 func Main() {
57         if !isatty.IsTerminal(os.Stderr.Fd()) {
58                 logrus.StandardLogger().Formatter = &logrus.TextFormatter{DisableTimestamp: true}
59         }
60         if len(os.Args) >= 2 && !strings.HasSuffix(os.Args[1], "version") {
61                 cmd.Version.RunCommand("lightning", nil, nil, os.Stderr, os.Stderr)
62         }
63         os.Exit(handler.RunCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
64 }
65
66 type buildDockerImage struct{}
67
68 func (cmd *buildDockerImage) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
69         tmpdir, err := ioutil.TempDir("", "")
70         if err != nil {
71                 fmt.Fprint(stderr, err)
72                 return 1
73         }
74         defer os.RemoveAll(tmpdir)
75         err = ioutil.WriteFile(tmpdir+"/Dockerfile", []byte(`FROM debian:bullseye
76 RUN DEBIAN_FRONTEND=noninteractive \
77   apt-get update && \
78   apt-get dist-upgrade -y && \
79   apt-get install -y --no-install-recommends bcftools bedtools samtools python2 python3-sklearn python3-matplotlib ca-certificates && \
80   apt-get clean
81 `), 0644)
82         if err != nil {
83                 fmt.Fprint(stderr, err)
84                 return 1
85         }
86         docker := exec.Command("docker", "build", "--tag=lightning-runtime", tmpdir)
87         docker.Stdout = stdout
88         docker.Stderr = stderr
89         err = docker.Run()
90         if err != nil {
91                 return 1
92         }
93         fmt.Fprintf(stderr, "built and tagged new docker image, lightning-runtime\n")
94         return 0
95 }