1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.arvados.org/arvados.git/sdk/go/arvados"
21 log "github.com/sirupsen/logrus"
24 type statscmd struct {
28 func (cmd *statscmd) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
32 fmt.Fprintf(stderr, "%s\n", err)
35 flags := flag.NewFlagSet("", flag.ContinueOnError)
36 flags.SetOutput(stderr)
37 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
38 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
39 projectUUID := flags.String("project", "", "project `UUID` for output data")
40 priority := flags.Int("priority", 500, "container request priority")
41 inputFilename := flags.String("i", "-", "input `file`")
42 outputFilename := flags.String("o", "-", "output `file`")
43 flags.BoolVar(&cmd.debugUnplaced, "debug-unplaced", false, "output full list of unplaced tags")
44 err = flags.Parse(args)
45 if err == flag.ErrHelp {
48 } else if err != nil {
54 log.Println(http.ListenAndServe(*pprof, nil))
59 if *outputFilename != "-" {
60 err = errors.New("cannot specify output file in container mode: not implemented")
63 runner := arvadosContainerRunner{
64 Name: "lightning stats",
65 Client: arvados.NewClientFromEnv(),
66 ProjectUUID: *projectUUID,
71 err = runner.TranslatePaths(inputFilename)
75 runner.Args = []string{"stats", "-local=true", fmt.Sprintf("-debug-unplaced=%v", cmd.debugUnplaced), "-i", *inputFilename, "-o", "/mnt/output/stats.json"}
77 output, err = runner.Run()
81 fmt.Fprintln(stdout, output+"/stats.json")
85 var input io.ReadCloser
86 if *inputFilename == "-" {
87 input = ioutil.NopCloser(stdin)
89 input, err = os.Open(*inputFilename)
96 var output io.WriteCloser
97 if *outputFilename == "-" {
98 output = nopCloser{stdout}
100 output, err = os.OpenFile(*outputFilename, os.O_CREATE|os.O_WRONLY, 0777)
107 bufw := bufio.NewWriter(output)
108 err = cmd.doStats(input, strings.HasSuffix(*inputFilename, ".gz"), bufw)
123 func (cmd *statscmd) doStats(input io.Reader, gz bool, output io.Writer) error {
128 TagsPlacedNTimes []int // a[x]==y means there were y tags that placed x times
131 NCVariantsBySize []int
132 UnplacedTags []string `json:",omitempty"`
136 var tagPlacements []int
137 tileVariantCalls := map[tileLibRef]int{}
138 err := DecodeLibrary(input, gz, func(ent *LibraryEntry) error {
139 ret.Genomes += len(ent.CompactGenomes)
140 ret.TileVariants += len(ent.TileVariants)
141 if len(ent.TagSet) > 0 {
143 return errors.New("invalid input: contains multiple tagsets")
145 ret.Tags = len(ent.TagSet)
148 for _, tv := range ent.TileVariants {
149 if need := 1 + len(tv.Sequence) - len(ret.VariantsBySize); need > 0 {
150 ret.VariantsBySize = append(ret.VariantsBySize, make([]int, need)...)
151 ret.NCVariantsBySize = append(ret.NCVariantsBySize, make([]int, need)...)
156 for _, b := range tv.Sequence {
157 if b == 'a' || b == 'c' || b == 'g' || b == 't' {
165 ret.NCVariantsBySize[len(tv.Sequence)]++
167 ret.VariantsBySize[len(tv.Sequence)]++
170 tileVariantCalls[tileLibRef{Tag: tv.Tag, Variant: tv.Variant}] = calls
172 for _, g := range ent.CompactGenomes {
173 if need := (len(g.Variants)+1)/2 - len(tagPlacements); need > 0 {
174 tagPlacements = append(tagPlacements, make([]int, need)...)
176 calledBases := int64(0)
177 for idx, v := range g.Variants {
179 tagPlacements[idx/2]++
180 calledBases += int64(tileVariantCalls[tileLibRef{Tag: tagID(idx / 2), Variant: v}])
183 ret.CalledBases = append(ret.CalledBases, calledBases)
190 for id, p := range tagPlacements {
191 for len(ret.TagsPlacedNTimes) <= p {
192 ret.TagsPlacedNTimes = append(ret.TagsPlacedNTimes, 0)
194 ret.TagsPlacedNTimes[p]++
195 if cmd.debugUnplaced && p == 0 {
196 ret.UnplacedTags = append(ret.UnplacedTags, fmt.Sprintf("%d %s", id, tagSet[id]))
200 return json.NewEncoder(output).Encode(ret)