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 ref2genome struct {
31 func (cmd *ref2genome) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
35 fmt.Fprintf(stderr, "%s\n", err)
38 flags := flag.NewFlagSet("", flag.ContinueOnError)
39 flags.SetOutput(stderr)
40 flags.StringVar(&cmd.refFile, "ref", "", "reference fasta `file`")
41 flags.StringVar(&cmd.projectUUID, "project", "", "project `UUID` for containers and output data")
42 flags.StringVar(&cmd.outputFilename, "o", "", "output filename")
43 flags.BoolVar(&cmd.runLocal, "local", false, "run on local host (default: run in an arvados container)")
44 priority := flags.Int("priority", 500, "container request priority")
45 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
46 err = flags.Parse(args)
47 if err == flag.ErrHelp {
50 } else if err != nil {
52 } else if cmd.refFile == "" {
53 err = errors.New("reference data (-ref) not specified")
55 } else if flags.NArg() > 0 {
56 err = fmt.Errorf("errant command line arguments after parsed flags: %v", flags.Args())
62 log.Println(http.ListenAndServe(*pprof, nil))
67 if cmd.outputFilename != "" {
68 err = errors.New("cannot specify output filename in non-local mode")
71 runner := arvadosContainerRunner{
72 Name: "lightning ref2genome",
73 Client: arvados.NewClientFromEnv(),
74 ProjectUUID: cmd.projectUUID,
79 err = runner.TranslatePaths(&cmd.refFile)
83 runner.Args = []string{"ref2genome", "-local=true", "-ref", cmd.refFile, "-o", "/mnt/output/ref.genome"}
85 output, err = runner.Run()
89 fmt.Fprintln(stdout, output+"/ref.genome")
93 var out io.WriteCloser
94 if cmd.outputFilename == "" {
95 out = nopCloser{stdout}
97 out, err = os.OpenFile(cmd.outputFilename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
102 f, err := os.Open(cmd.refFile)
108 if strings.HasSuffix(cmd.refFile, ".gz") {
109 in, err = gzip.NewReader(f)
116 label, seqlen := "", 0
117 scanner := bufio.NewScanner(in)
119 buf := scanner.Bytes()
120 if len(buf) > 0 && buf[0] == '>' {
122 fmt.Fprintf(out, "%s\t%d\n", label, seqlen)
124 label = strings.TrimSpace(string(buf[1:]))
125 label = strings.SplitN(label, " ", 2)[0]
128 seqlen += len(bytes.TrimSpace(buf))
132 fmt.Fprintf(out, "%s\t%d\n", label, seqlen)
134 if err = scanner.Err(); err != nil {
137 if err = out.Close(); err != nil {