1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
23 "git.arvados.org/arvados.git/sdk/go/arvados"
24 log "github.com/sirupsen/logrus"
27 type anno2vcf struct {
30 func (cmd *anno2vcf) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
34 fmt.Fprintf(stderr, "%s\n", err)
37 flags := flag.NewFlagSet("", flag.ContinueOnError)
38 flags.SetOutput(stderr)
39 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
40 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
41 projectUUID := flags.String("project", "", "project `UUID` for output data")
42 priority := flags.Int("priority", 500, "container request priority")
43 inputDir := flags.String("input-dir", "./in", "input `directory`")
44 outputDir := flags.String("output-dir", "./out", "output `directory`")
45 err = flags.Parse(args)
46 if err == flag.ErrHelp {
49 } else if err != nil {
55 log.Println(http.ListenAndServe(*pprof, nil))
60 runner := arvadosContainerRunner{
61 Name: "lightning anno2vcf",
62 Client: arvados.NewClientFromEnv(),
63 ProjectUUID: *projectUUID,
70 err = runner.TranslatePaths(inputDir)
74 runner.Args = []string{"anno2vcf", "-local=true",
76 "-input-dir", *inputDir,
77 "-output-dir", "/mnt/output",
80 output, err = runner.Run()
84 fmt.Fprintln(stdout, output)
88 d, err := open(*inputDir)
94 fis, err := d.Readdir(-1)
111 throttle := throttle{Max: runtime.GOMAXPROCS(0)}
112 log.Print("reading input files")
113 for _, fi := range fis {
114 if !strings.HasSuffix(fi.Name(), "annotations.csv") {
117 filename := *inputDir + "/" + fi.Name()
120 defer throttle.Release()
121 log.Printf("reading %s", filename)
122 buf, err := ioutil.ReadFile(filename)
124 throttle.Report(fmt.Errorf("%s: %s", filename, err))
127 lines := bytes.Split(buf, []byte{'\n'})
128 calls := make([]*call, 0, len(lines))
129 for lineIdx, line := range lines {
133 if lineIdx & ^0xfff == 0 && throttle.Err() != nil {
136 fields := bytes.Split(line, []byte{','})
137 if len(fields) != 8 {
138 throttle.Report(fmt.Errorf("%s line %d: wrong number of fields (%d != %d): %q", fi.Name(), lineIdx+1, len(fields), 8, line))
141 tile, _ := strconv.ParseInt(string(fields[0]), 10, 64)
142 variant, _ := strconv.ParseInt(string(fields[2]), 10, 64)
143 position, _ := strconv.ParseInt(string(fields[5]), 10, 64)
144 calls = append(calls, &call{
146 variant: int(variant),
147 sequence: append([]byte(nil), fields[4]...),
148 position: int(position),
149 deletion: append([]byte(nil), fields[6]...),
150 insertion: append([]byte(nil), fields[7]...),
154 allcalls = append(allcalls, calls...)
159 if throttle.Err() != nil {
160 log.Print(throttle.Err())
164 sort.Slice(allcalls, func(i, j int) bool {
165 ii, jj := allcalls[i], allcalls[j]
166 if cmp := bytes.Compare(ii.sequence, jj.sequence); cmp != 0 {
169 if cmp := ii.position - jj.position; cmp != 0 {
172 if cmp := len(ii.deletion) - len(jj.deletion); cmp != 0 {
175 if cmp := bytes.Compare(ii.insertion, jj.insertion); cmp != 0 {
178 if cmp := ii.tile - jj.tile; cmp != 0 {
181 return ii.variant < jj.variant
184 vcfFilename := *outputDir + "/annotations.vcf"
185 log.Printf("writing %s", vcfFilename)
186 f, err := os.Create(vcfFilename)
191 bufw := bufio.NewWriterSize(f, 1<<20)
192 _, err = fmt.Fprintf(bufw, `##fileformat=VCFv4.0
193 ##INFO=<ID=TV,Number=.,Type=String,Description="tile-variant">
194 #CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO
199 placeholder := []byte{'.'}
200 for i := 0; i < len(allcalls); {
203 info := fmt.Sprintf("TV=,%d-%d,", call.tile, call.variant)
204 for i < len(allcalls) &&
205 bytes.Equal(call.sequence, allcalls[i].sequence) &&
206 call.position == allcalls[i].position &&
207 len(call.deletion) == len(allcalls[i].deletion) &&
208 bytes.Equal(call.insertion, allcalls[i].insertion) {
211 info += fmt.Sprintf("%d-%d,", call.tile, call.variant)
213 deletion := call.deletion
214 if len(deletion) == 0 {
215 deletion = placeholder
217 insertion := call.insertion
218 if len(insertion) == 0 {
219 insertion = placeholder
221 _, err = fmt.Fprintf(bufw, "%s\t%d\t.\t%s\t%s\t.\t.\t%s\n", call.sequence, call.position, deletion, insertion, info)