16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 log "github.com/sirupsen/logrus"
24 func (cmd *stats) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
28 fmt.Fprintf(stderr, "%s\n", err)
31 flags := flag.NewFlagSet("", flag.ContinueOnError)
32 flags.SetOutput(stderr)
33 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
34 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
35 projectUUID := flags.String("project", "", "project `UUID` for output data")
36 priority := flags.Int("priority", 500, "container request priority")
37 inputFilename := flags.String("i", "-", "input `file`")
38 outputFilename := flags.String("o", "-", "output `file`")
39 flags.BoolVar(&cmd.debugUnplaced, "debug-unplaced", false, "output full list of unplaced tags")
40 err = flags.Parse(args)
41 if err == flag.ErrHelp {
44 } else if err != nil {
50 log.Println(http.ListenAndServe(*pprof, nil))
55 if *outputFilename != "-" {
56 err = errors.New("cannot specify output file in container mode: not implemented")
59 runner := arvadosContainerRunner{
60 Name: "lightning stats",
61 Client: arvados.NewClientFromEnv(),
62 ProjectUUID: *projectUUID,
67 err = runner.TranslatePaths(inputFilename)
71 runner.Args = []string{"stats", "-local=true", fmt.Sprintf("-debug-unplaced=%v", cmd.debugUnplaced), "-i", *inputFilename, "-o", "/mnt/output/stats.json"}
73 output, err = runner.Run()
77 fmt.Fprintln(stdout, output+"/stats.json")
81 var input io.ReadCloser
82 if *inputFilename == "-" {
83 input = ioutil.NopCloser(stdin)
85 input, err = os.Open(*inputFilename)
92 var output io.WriteCloser
93 if *outputFilename == "-" {
94 output = nopCloser{stdout}
96 output, err = os.OpenFile(*outputFilename, os.O_CREATE|os.O_WRONLY, 0777)
103 bufw := bufio.NewWriter(output)
104 err = cmd.doStats(input, bufw)
119 func (cmd *stats) doStats(input io.Reader, output io.Writer) error {
124 TagsPlacedNTimes []int // a[x]==y means there were y tags that placed x times
127 NCVariantsBySize []int
128 UnplacedTags []string `json:",omitempty"`
132 var tagPlacements []int
133 tileVariantCalls := map[tileLibRef]int{}
134 dec := gob.NewDecoder(bufio.NewReaderSize(input, 1<<26))
137 err := dec.Decode(&ent)
140 } else if err != nil {
141 return fmt.Errorf("gob decode: %w", err)
143 ret.Genomes += len(ent.CompactGenomes)
144 ret.TileVariants += len(ent.TileVariants)
145 if len(ent.TagSet) > 0 {
147 return errors.New("invalid input: contains multiple tagsets")
149 ret.Tags = len(ent.TagSet)
152 for _, tv := range ent.TileVariants {
153 if need := 1 + len(tv.Sequence) - len(ret.VariantsBySize); need > 0 {
154 ret.VariantsBySize = append(ret.VariantsBySize, make([]int, need)...)
155 ret.NCVariantsBySize = append(ret.NCVariantsBySize, make([]int, need)...)
160 for _, b := range tv.Sequence {
161 if b == 'a' || b == 'c' || b == 'g' || b == 't' {
169 ret.NCVariantsBySize[len(tv.Sequence)]++
171 ret.VariantsBySize[len(tv.Sequence)]++
174 tileVariantCalls[tileLibRef{Tag: tv.Tag, Variant: tv.Variant}] = calls
176 for _, g := range ent.CompactGenomes {
177 if need := (len(g.Variants)+1)/2 - len(tagPlacements); need > 0 {
178 tagPlacements = append(tagPlacements, make([]int, need)...)
180 calledBases := int64(0)
181 for idx, v := range g.Variants {
183 tagPlacements[idx/2]++
184 calledBases += int64(tileVariantCalls[tileLibRef{Tag: tagID(idx / 2), Variant: v}])
187 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)