1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/sdk/go/arvados"
20 log "github.com/sirupsen/logrus"
23 type tilingStats struct {
26 func (cmd *tilingStats) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
30 fmt.Fprintf(stderr, "%s\n", err)
33 flags := flag.NewFlagSet("", flag.ContinueOnError)
34 flags.SetOutput(stderr)
35 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
36 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
37 projectUUID := flags.String("project", "", "project `UUID` for output data")
38 priority := flags.Int("priority", 500, "container request priority")
39 inputDir := flags.String("input-dir", "./in", "input `directory`")
40 outputDir := flags.String("output-dir", "./out", "output `directory`")
41 err = flags.Parse(args)
42 if err == flag.ErrHelp {
45 } else if err != nil {
47 } else if flags.NArg() > 0 {
48 err = fmt.Errorf("errant command line arguments after parsed flags: %v", flags.Args())
54 log.Println(http.ListenAndServe(*pprof, nil))
59 runner := arvadosContainerRunner{
60 Name: "lightning tiling-stats",
61 Client: arvados.NewClientFromEnv(),
62 ProjectUUID: *projectUUID,
69 err = runner.TranslatePaths(inputDir)
73 runner.Args = []string{"tiling-stats", "-local=true",
75 "-input-dir=" + *inputDir,
76 "-output-dir=/mnt/output",
79 output, err = runner.Run()
83 fmt.Fprintln(stdout, output)
87 infiles, err := allFiles(*inputDir, matchGobFile)
91 if len(infiles) == 0 {
92 err = fmt.Errorf("no input files found in %s", *inputDir)
97 var refseqs []CompactSequence
98 var reftiledata = make(map[tileLibRef][]byte, 11000000)
99 in0, err := open(infiles[0])
105 err = DecodeLibrary(in0, strings.HasSuffix(infiles[0], ".gz"), func(ent *LibraryEntry) error {
106 if len(ent.TagSet) > 0 {
107 taglen = len(ent.TagSet[0])
109 refseqs = append(refseqs, ent.CompactSequences...)
110 for _, tv := range ent.TileVariants {
112 reftiledata[tileLibRef{tv.Tag, tv.Variant}] = tv.Sequence
121 if len(refseqs) == 0 {
122 err = fmt.Errorf("%s: reference sequence not found", infiles[0])
126 err = fmt.Errorf("%s: tagset not found", infiles[0])
130 for _, cseq := range refseqs {
131 _, basename := filepath.Split(cseq.Name)
132 bedname := fmt.Sprintf("%s/%s.bed", *outputDir, basename)
133 log.Infof("writing %s", bedname)
135 f, err = os.OpenFile(bedname, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0777)
140 bufw := bufio.NewWriterSize(f, 1<<24)
141 seqnames := make([]string, 0, len(cseq.TileSequences))
142 for seqname := range cseq.TileSequences {
143 seqnames = append(seqnames, seqname)
145 sort.Strings(seqnames)
146 // Mark duplicate tags (tags that place more than once
148 duptag := map[tagID]bool{}
149 for _, seqname := range seqnames {
150 for _, libref := range cseq.TileSequences[seqname] {
151 if _, seen := duptag[libref.Tag]; seen {
152 duptag[libref.Tag] = true
154 duptag[libref.Tag] = false
158 for _, seqname := range seqnames {
160 for _, libref := range cseq.TileSequences[seqname] {
161 if duptag[libref.Tag] {
164 tiledata := reftiledata[libref]
165 if len(tiledata) <= taglen {
166 err = fmt.Errorf("bogus input data: ref tile libref %v has len %d < taglen %d", libref, len(tiledata), taglen)
169 score := 1000 * countBases(tiledata) / len(tiledata)
170 _, err = fmt.Fprintf(bufw, "%s %d %d %d %d . %d %d\n",
172 pos, pos+len(tiledata),
175 pos+taglen, pos+len(tiledata)-taglen)
179 pos += len(tiledata) - taglen