25 "git.arvados.org/arvados.git/sdk/go/arvados"
26 "github.com/lucasb-eyer/go-colorful"
27 log "github.com/sirupsen/logrus"
29 "gonum.org/v1/plot/plotter"
30 "gonum.org/v1/plot/vg"
31 "gonum.org/v1/plot/vg/draw"
34 type importer struct {
47 func (cmd *importer) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
51 fmt.Fprintf(stderr, "%s\n", err)
54 flags := flag.NewFlagSet("", flag.ContinueOnError)
55 flags.SetOutput(stderr)
56 flags.StringVar(&cmd.tagLibraryFile, "tag-library", "", "tag library fasta `file`")
57 flags.StringVar(&cmd.refFile, "ref", "", "reference fasta `file`")
58 flags.StringVar(&cmd.outputFile, "o", "-", "output `file`")
59 flags.StringVar(&cmd.projectUUID, "project", "", "project `UUID` for output data")
60 flags.BoolVar(&cmd.runLocal, "local", false, "run on local host (default: run in an arvados container)")
61 flags.BoolVar(&cmd.skipOOO, "skip-ooo", false, "skip out-of-order tags")
62 flags.BoolVar(&cmd.outputTiles, "output-tiles", false, "include tile variant sequences in output file")
63 flags.BoolVar(&cmd.includeNoCalls, "include-no-calls", false, "treat tiles with no-calls as regular tiles")
64 flags.StringVar(&cmd.outputStats, "output-stats", "", "output stats to `file` (json)")
65 priority := flags.Int("priority", 500, "container request priority")
66 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
67 loglevel := flags.String("loglevel", "info", "logging threshold (trace, debug, info, warn, error, fatal, or panic)")
68 err = flags.Parse(args)
69 if err == flag.ErrHelp {
72 } else if err != nil {
74 } else if cmd.tagLibraryFile == "" {
75 fmt.Fprintln(os.Stderr, "cannot import without -tag-library argument")
77 } else if flags.NArg() == 0 {
84 log.Println(http.ListenAndServe(*pprof, nil))
88 lvl, err := log.ParseLevel(*loglevel)
95 runner := arvadosContainerRunner{
96 Name: "lightning import",
97 Client: arvados.NewClientFromEnv(),
98 ProjectUUID: cmd.projectUUID,
103 err = runner.TranslatePaths(&cmd.tagLibraryFile, &cmd.refFile, &cmd.outputFile)
107 inputs := flags.Args()
108 for i := range inputs {
109 err = runner.TranslatePaths(&inputs[i])
114 if cmd.outputFile == "-" {
115 cmd.outputFile = "/mnt/output/library.gob"
117 // Not yet implemented, but this should write
118 // the collection to an existing collection,
119 // possibly even an in-place update.
120 err = errors.New("cannot specify output file in container mode: not implemented")
123 runner.Args = append([]string{"import",
125 "-loglevel=" + *loglevel,
126 fmt.Sprintf("-skip-ooo=%v", cmd.skipOOO),
127 fmt.Sprintf("-output-tiles=%v", cmd.outputTiles),
128 fmt.Sprintf("-include-no-calls=%v", cmd.includeNoCalls),
129 "-output-stats", "/mnt/output/stats.json",
130 "-tag-library", cmd.tagLibraryFile,
132 "-o", cmd.outputFile,
135 output, err = runner.Run()
139 fmt.Fprintln(stdout, output+"/library.gob")
143 infiles, err := listInputFiles(flags.Args())
148 taglib, err := cmd.loadTagLibrary()
153 var output io.WriteCloser
154 if cmd.outputFile == "-" {
155 output = nopCloser{stdout}
157 output, err = os.OpenFile(cmd.outputFile, os.O_CREATE|os.O_WRONLY, 0777)
163 bufw := bufio.NewWriter(output)
164 cmd.encoder = gob.NewEncoder(bufw)
166 tilelib := &tileLibrary{taglib: taglib, includeNoCalls: cmd.includeNoCalls, skipOOO: cmd.skipOOO}
168 cmd.encoder.Encode(LibraryEntry{TagSet: taglib.Tags()})
169 tilelib.encoder = cmd.encoder
172 for range time.Tick(10 * time.Minute) {
173 log.Printf("tilelib.Len() == %d", tilelib.Len())
177 err = cmd.tileInputs(tilelib, infiles)
192 func (cmd *importer) tileFasta(tilelib *tileLibrary, infile string) (tileSeq, []importStats, error) {
193 var input io.ReadCloser
194 input, err := os.Open(infile)
199 if strings.HasSuffix(infile, ".gz") {
200 input, err = gzip.NewReader(input)
206 return tilelib.TileFasta(infile, input)
209 func (cmd *importer) loadTagLibrary() (*tagLibrary, error) {
210 log.Printf("tag library %s load starting", cmd.tagLibraryFile)
211 f, err := os.Open(cmd.tagLibraryFile)
216 var rdr io.ReadCloser = f
217 if strings.HasSuffix(cmd.tagLibraryFile, ".gz") {
218 rdr, err = gzip.NewReader(f)
220 return nil, fmt.Errorf("%s: gzip: %s", cmd.tagLibraryFile, err)
224 var taglib tagLibrary
225 err = taglib.Load(rdr)
229 if taglib.Len() < 1 {
230 return nil, fmt.Errorf("cannot tile: tag library is empty")
232 log.Printf("tag library %s load done", cmd.tagLibraryFile)
237 vcfFilenameRe = regexp.MustCompile(`\.vcf(\.gz)?$`)
238 fasta1FilenameRe = regexp.MustCompile(`\.1\.fa(sta)?(\.gz)?$`)
239 fasta2FilenameRe = regexp.MustCompile(`\.2\.fa(sta)?(\.gz)?$`)
240 fastaFilenameRe = regexp.MustCompile(`\.fa(sta)?(\.gz)?$`)
243 func listInputFiles(paths []string) (files []string, err error) {
244 for _, path := range paths {
245 if fi, err := os.Stat(path); err != nil {
246 return nil, fmt.Errorf("%s: stat failed: %s", path, err)
247 } else if !fi.IsDir() {
248 if !fasta2FilenameRe.MatchString(path) {
249 files = append(files, path)
253 d, err := os.Open(path)
255 return nil, fmt.Errorf("%s: open failed: %s", path, err)
258 names, err := d.Readdirnames(0)
260 return nil, fmt.Errorf("%s: readdir failed: %s", path, err)
263 for _, name := range names {
264 if vcfFilenameRe.MatchString(name) {
265 files = append(files, filepath.Join(path, name))
266 } else if fastaFilenameRe.MatchString(name) && !fasta2FilenameRe.MatchString(name) {
267 files = append(files, filepath.Join(path, name))
272 for _, file := range files {
273 if fastaFilenameRe.MatchString(file) {
275 } else if vcfFilenameRe.MatchString(file) {
276 if _, err := os.Stat(file + ".csi"); err == nil {
278 } else if _, err = os.Stat(file + ".tbi"); err == nil {
281 return nil, fmt.Errorf("%s: cannot read without .tbi or .csi index file", file)
284 return nil, fmt.Errorf("don't know how to handle filename %s", file)
290 func (cmd *importer) tileInputs(tilelib *tileLibrary, infiles []string) error {
291 starttime := time.Now()
292 errs := make(chan error, 1)
293 todo := make(chan func() error, len(infiles)*2)
294 allstats := make([][]importStats, len(infiles)*2)
295 var encodeJobs sync.WaitGroup
296 for idx, infile := range infiles {
297 idx, infile := idx, infile
298 var phases sync.WaitGroup
300 variants := make([][]tileVariantID, 2)
301 if fasta1FilenameRe.MatchString(infile) {
302 todo <- func() error {
304 log.Printf("%s starting", infile)
305 defer log.Printf("%s done", infile)
306 tseqs, stats, err := cmd.tileFasta(tilelib, infile)
307 allstats[idx*2] = stats
308 var kept, dropped int
309 variants[0], kept, dropped = tseqs.Variants()
310 log.Printf("%s found %d unique tags plus %d repeats", infile, kept, dropped)
313 infile2 := fasta1FilenameRe.ReplaceAllString(infile, `.2.fa$1$2`)
314 todo <- func() error {
316 log.Printf("%s starting", infile2)
317 defer log.Printf("%s done", infile2)
318 tseqs, stats, err := cmd.tileFasta(tilelib, infile2)
319 allstats[idx*2+1] = stats
320 var kept, dropped int
321 variants[1], kept, dropped = tseqs.Variants()
322 log.Printf("%s found %d unique tags plus %d repeats", infile2, kept, dropped)
326 } else if fastaFilenameRe.MatchString(infile) {
327 todo <- func() error {
330 log.Printf("%s starting", infile)
331 defer log.Printf("%s done", infile)
332 tseqs, stats, err := cmd.tileFasta(tilelib, infile)
333 allstats[idx*2] = stats
338 for _, tseq := range tseqs {
341 log.Printf("%s tiled %d seqs, total len %d", infile, len(tseqs), totlen)
342 return cmd.encoder.Encode(LibraryEntry{
343 CompactSequences: []CompactSequence{{Name: infile, TileSequences: tseqs}},
346 // Don't write out a CompactGenomes entry
348 } else if vcfFilenameRe.MatchString(infile) {
349 for phase := 0; phase < 2; phase++ {
351 todo <- func() error {
353 log.Printf("%s phase %d starting", infile, phase+1)
354 defer log.Printf("%s phase %d done", infile, phase+1)
355 tseqs, stats, err := cmd.tileGVCF(tilelib, infile, phase)
356 allstats[idx*2] = stats
357 var kept, dropped int
358 variants[phase], kept, dropped = tseqs.Variants()
359 log.Printf("%s phase %d found %d unique tags plus %d repeats", infile, phase+1, kept, dropped)
364 panic(fmt.Sprintf("bug: unhandled filename %q", infile))
368 defer encodeJobs.Done()
373 err := cmd.encoder.Encode(LibraryEntry{
374 CompactGenomes: []CompactGenome{{Name: infile, Variants: flatten(variants)}},
385 var tileJobs sync.WaitGroup
387 for i := 0; i < runtime.NumCPU()*9/8+1; i++ {
389 atomic.AddInt64(&running, 1)
391 defer tileJobs.Done()
392 defer atomic.AddInt64(&running, -1)
393 for fn := range todo {
404 remain := len(todo) + int(atomic.LoadInt64(&running)) - 1
405 if remain < cap(todo) {
406 ttl := time.Now().Sub(starttime) * time.Duration(remain) / time.Duration(cap(todo)-remain)
407 eta := time.Now().Add(ttl)
408 log.Printf("progress %d/%d, eta %v (%v)", cap(todo)-remain, cap(todo), eta, ttl)
422 if cmd.outputStats != "" {
423 f, err := os.OpenFile(cmd.outputStats, os.O_CREATE|os.O_WRONLY, 0666)
427 var flatstats []importStats
428 for _, stats := range allstats {
429 flatstats = append(flatstats, stats...)
431 err = json.NewEncoder(f).Encode(flatstats)
440 func (cmd *importer) tileGVCF(tilelib *tileLibrary, infile string, phase int) (tileseq tileSeq, stats []importStats, err error) {
441 if cmd.refFile == "" {
442 err = errors.New("cannot import vcf: reference data (-ref) not specified")
445 args := []string{"bcftools", "consensus", "--fasta-ref", cmd.refFile, "-H", fmt.Sprint(phase + 1), infile}
446 indexsuffix := ".tbi"
447 if _, err := os.Stat(infile + ".csi"); err == nil {
450 if out, err := exec.Command("docker", "image", "ls", "-q", "lightning-runtime").Output(); err == nil && len(out) > 0 {
451 args = append([]string{
452 "docker", "run", "--rm",
454 "--volume=" + infile + ":" + infile + ":ro",
455 "--volume=" + infile + indexsuffix + ":" + infile + indexsuffix + ":ro",
456 "--volume=" + cmd.refFile + ":" + cmd.refFile + ":ro",
460 consensus := exec.Command(args[0], args[1:]...)
461 consensus.Stderr = os.Stderr
462 stdout, err := consensus.StdoutPipe()
467 err = consensus.Start()
471 defer consensus.Wait()
472 tileseq, stats, err = tilelib.TileFasta(fmt.Sprintf("%s phase %d", infile, phase+1), stdout)
480 err = consensus.Wait()
482 err = fmt.Errorf("%s phase %d: bcftools: %s", infile, phase, err)
488 func flatten(variants [][]tileVariantID) []tileVariantID {
490 for _, v := range variants {
495 flat := make([]tileVariantID, ntags*2)
496 for i := 0; i < ntags; i++ {
497 for hap := 0; hap < 2; hap++ {
498 if i < len(variants[hap]) {
499 flat[i*2+hap] = variants[hap][i]
506 type importstatsplot struct{}
508 func (cmd *importstatsplot) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
509 err := cmd.Plot(stdin, stdout)
511 log.Errorf("%s", err)
517 func (cmd *importstatsplot) Plot(stdin io.Reader, stdout io.Writer) error {
518 var stats []importStats
519 err := json.NewDecoder(stdin).Decode(&stats)
528 p.Title.Text = "coverage preserved by import (excl X<0.65)"
529 p.X.Label.Text = "input base calls ÷ sequence length"
530 p.Y.Label.Text = "output base calls ÷ input base calls"
531 p.Add(plotter.NewGrid())
533 data := map[string]plotter.XYs{}
534 for _, stat := range stats {
535 data[stat.InputLabel] = append(data[stat.InputLabel], plotter.XY{
536 X: float64(stat.InputCoverage) / float64(stat.InputLength),
537 Y: float64(stat.TileCoverage) / float64(stat.InputCoverage),
542 for label := range data {
543 labels = append(labels, label)
546 palette, err := colorful.SoftPalette(len(labels))
551 for idx, label := range labels {
552 s, err := plotter.NewScatter(data[label])
556 s.GlyphStyle.Color = palette[idx]
557 s.GlyphStyle.Radius = vg.Millimeter / 2
558 s.GlyphStyle.Shape = draw.CrossGlyph{}
562 p.Legend.Add(label, s)
568 w, err := p.WriterTo(8*vg.Inch, 6*vg.Inch, "svg")
572 _, err = w.WriteTo(stdout)