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)
100 sort.Slice(fis, func(i, j int) bool { return fis[i].Name() < fis[j].Name() })
109 allcalls := map[string][]*call{}
111 thr := 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()
118 thr.Go(func() error {
119 log.Printf("reading %s", filename)
120 buf, err := ioutil.ReadFile(filename)
122 return fmt.Errorf("%s: %s", filename, err)
124 lines := bytes.Split(buf, []byte{'\n'})
125 calls := map[string][]*call{}
126 for lineIdx, line := range lines {
130 if lineIdx & ^0xfff == 0 && thr.Err() != nil {
133 fields := bytes.Split(line, []byte{','})
134 if len(fields) != 8 {
135 return fmt.Errorf("%s line %d: wrong number of fields (%d != %d): %q", fi.Name(), lineIdx+1, len(fields), 8, line)
137 tile, _ := strconv.ParseInt(string(fields[0]), 10, 64)
138 variant, _ := strconv.ParseInt(string(fields[2]), 10, 64)
139 position, _ := strconv.ParseInt(string(fields[5]), 10, 64)
140 seq := string(fields[4])
141 if calls[seq] == nil {
142 calls[seq] = make([]*call, 0, len(lines)/50)
144 calls[seq] = append(calls[seq], &call{
146 variant: int(variant),
147 position: int(position),
148 deletion: append([]byte(nil), fields[6]...),
149 insertion: append([]byte(nil), fields[7]...),
153 for seq, seqcalls := range calls {
154 allcalls[seq] = append(allcalls[seq], seqcalls...)
164 thr = throttle{Max: len(allcalls)}
165 for seq, seqcalls := range allcalls {
166 seq, seqcalls := seq, seqcalls
167 thr.Go(func() error {
168 log.Printf("%s: sorting", seq)
169 sort.Slice(seqcalls, func(i, j int) bool {
170 ii, jj := seqcalls[i], seqcalls[j]
171 if cmp := ii.position - jj.position; cmp != 0 {
174 if cmp := len(ii.deletion) - len(jj.deletion); cmp != 0 {
177 if cmp := bytes.Compare(ii.insertion, jj.insertion); cmp != 0 {
180 if cmp := ii.tile - jj.tile; cmp != 0 {
183 return ii.variant < jj.variant
186 vcfFilename := fmt.Sprintf("%s/annotations.%s.vcf", *outputDir, seq)
187 log.Printf("%s: writing %s", seq, vcfFilename)
189 f, err := os.Create(vcfFilename)
194 bufw := bufio.NewWriterSize(f, 1<<20)
195 _, err = fmt.Fprintf(bufw, `##fileformat=VCFv4.0
196 ##INFO=<ID=TV,Number=.,Type=String,Description="tile-variant">
197 #CHROM POS ID REF ALT QUAL FILTER INFO
202 placeholder := []byte{'.'}
203 for i := 0; i < len(seqcalls); {
206 info := fmt.Sprintf("TV=,%d-%d,", call.tile, call.variant)
207 for i < len(seqcalls) &&
208 call.position == seqcalls[i].position &&
209 len(call.deletion) == len(seqcalls[i].deletion) &&
210 bytes.Equal(call.insertion, seqcalls[i].insertion) {
213 info += fmt.Sprintf("%d-%d,", call.tile, call.variant)
215 deletion := call.deletion
216 if len(deletion) == 0 {
217 deletion = placeholder
219 insertion := call.insertion
220 if len(insertion) == 0 {
221 insertion = placeholder
223 _, err = fmt.Fprintf(bufw, "%s\t%d\t.\t%s\t%s\t.\t.\t%s\n", seq, call.position, deletion, insertion, info)
236 log.Printf("%s: done", seq)