1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.arvados.org/arvados.git/sdk/go/arvados"
23 log "github.com/sirupsen/logrus"
26 type anno2vcf struct {
29 func (cmd *anno2vcf) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
33 fmt.Fprintf(stderr, "%s\n", err)
36 flags := flag.NewFlagSet("", flag.ContinueOnError)
37 flags.SetOutput(stderr)
38 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
39 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
40 projectUUID := flags.String("project", "", "project `UUID` for output data")
41 priority := flags.Int("priority", 500, "container request priority")
42 inputDir := flags.String("input-dir", "./in", "input `directory`")
43 outputDir := flags.String("output-dir", "./out", "output `directory`")
44 err = flags.Parse(args)
45 if err == flag.ErrHelp {
48 } else if err != nil {
54 log.Println(http.ListenAndServe(*pprof, nil))
59 runner := arvadosContainerRunner{
60 Name: "lightning anno2vcf",
61 Client: arvados.NewClientFromEnv(),
62 ProjectUUID: *projectUUID,
69 err = runner.TranslatePaths(inputDir)
73 runner.Args = []string{"anno2vcf", "-local=true",
75 "-input-dir", *inputDir,
76 "-output-dir", "/mnt/output",
79 output, err = runner.Run()
83 fmt.Fprintln(stdout, output)
87 d, err := open(*inputDir)
93 fis, err := d.Readdir(-1)
99 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 f, err := open(filename)
125 buf, err := io.ReadAll(f)
127 return fmt.Errorf("%s: %s", filename, err)
130 lines := bytes.Split(buf, []byte{'\n'})
131 calls := map[string][]*call{}
132 for lineIdx, line := range lines {
136 if lineIdx&0xff == 0 && thr.Err() != nil {
139 fields := bytes.Split(line, []byte{','})
141 return fmt.Errorf("%s line %d: wrong number of fields (%d < %d): %q", fi.Name(), lineIdx+1, len(fields), 8, line)
145 // "=" reference or ""
146 // non-diffable tile variant
149 tile, _ := strconv.ParseInt(string(fields[0]), 10, 64)
150 variant, _ := strconv.ParseInt(string(fields[2]), 10, 64)
151 position, _ := strconv.ParseInt(string(fields[5]), 10, 64)
152 seq := string(fields[4])
153 if calls[seq] == nil {
154 calls[seq] = make([]*call, 0, len(lines)/50)
158 if (len(del) == 0 || len(ins) == 0) && len(fields) >= 9 {
159 // "123,,AA,T" means 123insAA
160 // preceded by T. We record it
161 // here as "122 T TAA" to
162 // avoid writing an empty
163 // "ref" field in our
164 // VCF. Similarly, we record
165 // deletions as "122 TAA T"
166 // rather than "123 AA .".
167 del = append(append(make([]byte, 0, len(fields[8])+len(del)), fields[8]...), del...)
168 ins = append(append(make([]byte, 0, len(fields[8])+len(ins)), fields[8]...), ins...)
169 position -= int64(len(fields[8]))
171 del = append([]byte(nil), del...)
172 ins = append([]byte(nil), ins...)
174 calls[seq] = append(calls[seq], &call{
176 variant: int(variant),
177 position: int(position),
184 for seq, seqcalls := range calls {
185 allcalls[seq] = append(allcalls[seq], seqcalls...)
195 thr = throttle{Max: len(allcalls)}
196 for seq, seqcalls := range allcalls {
197 seq, seqcalls := seq, seqcalls
198 thr.Go(func() error {
199 log.Printf("%s: sorting", seq)
200 sort.Slice(seqcalls, func(i, j int) bool {
201 ii, jj := seqcalls[i], seqcalls[j]
202 if cmp := ii.position - jj.position; cmp != 0 {
205 if cmp := len(ii.deletion) - len(jj.deletion); cmp != 0 {
208 if cmp := bytes.Compare(ii.insertion, jj.insertion); cmp != 0 {
211 if cmp := ii.tile - jj.tile; cmp != 0 {
214 return ii.variant < jj.variant
217 vcfFilename := fmt.Sprintf("%s/annotations.%s.vcf", *outputDir, seq)
218 log.Printf("%s: writing %s", seq, vcfFilename)
220 f, err := os.Create(vcfFilename)
225 bufw := bufio.NewWriterSize(f, 1<<20)
226 _, err = fmt.Fprintf(bufw, `##fileformat=VCFv4.0
227 ##INFO=<ID=TV,Number=.,Type=String,Description="tile-variant">
228 #CHROM POS ID REF ALT QUAL FILTER INFO
233 placeholder := []byte{'.'}
234 for i := 0; i < len(seqcalls); {
237 info := fmt.Sprintf("TV=,%d-%d,", call.tile, call.variant)
238 for i < len(seqcalls) &&
239 call.position == seqcalls[i].position &&
240 len(call.deletion) == len(seqcalls[i].deletion) &&
241 bytes.Equal(call.insertion, seqcalls[i].insertion) {
244 info += fmt.Sprintf("%d-%d,", call.tile, call.variant)
246 deletion := call.deletion
247 if len(deletion) == 0 {
248 deletion = placeholder
250 insertion := call.insertion
251 if len(insertion) == 0 {
252 insertion = placeholder
254 _, err = fmt.Fprintf(bufw, "%s\t%d\t%s\t%s\t%s\t.\t.\t%s\n", seq, call.position, call.hgvsID, deletion, insertion, info)
267 log.Printf("%s: done", seq)