1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
24 "git.arvados.org/arvados.git/sdk/go/arvados"
25 log "github.com/sirupsen/logrus"
26 "golang.org/x/crypto/blake2b"
32 selectedTags map[tagID]bool
35 func (cmd *dump) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
36 err := cmd.run(prog, args, stdin, stdout, stderr)
43 func (cmd *dump) run(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
44 flags := flag.NewFlagSet("", flag.ContinueOnError)
45 flags.SetOutput(stderr)
46 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
47 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
48 projectUUID := flags.String("project", "", "project `UUID` for output data")
49 priority := flags.Int("priority", 500, "container request priority")
50 inputDir := flags.String("input-dir", "./in", "input `directory`")
51 outputDir := flags.String("output-dir", "./out", "output `directory`")
52 ref := flags.String("ref", "", "reference name (if blank, choose last one that appears in input)")
53 regionsFilename := flags.String("regions", "", "only output columns/annotations that intersect regions in specified bed `file`")
54 expandRegions := flags.Int("expand-regions", 0, "expand specified regions by `N` base pairs on each side`")
55 selectedTags := flags.String("tags", "", "tag numbers to dump")
56 cmd.filter.Flags(flags)
57 err := flags.Parse(args)
58 if err == flag.ErrHelp {
60 } else if err != nil {
66 log.Println(http.ListenAndServe(*pprof, nil))
71 runner := arvadosContainerRunner{
72 Name: "lightning slice-numpy",
73 Client: arvados.NewClientFromEnv(),
74 ProjectUUID: *projectUUID,
81 err = runner.TranslatePaths(inputDir, regionsFilename)
85 runner.Args = []string{"dump", "-local=true",
87 "-input-dir=" + *inputDir,
88 "-output-dir=/mnt/output",
89 "-regions=" + *regionsFilename,
90 "-expand-regions=" + fmt.Sprintf("%d", *expandRegions),
91 "-tags=" + *selectedTags,
93 runner.Args = append(runner.Args, cmd.filter.Args()...)
94 output, err := runner.Run()
98 fmt.Fprintln(stdout, output)
102 if *selectedTags != "" {
103 cmd.selectedTags = map[tagID]bool{}
104 for _, tagstr := range strings.Split(*selectedTags, ",") {
105 tag, err := strconv.ParseInt(tagstr, 10, 64)
109 cmd.selectedTags[tagID(tag)] = true
113 infiles, err := allFiles(*inputDir, matchGobFile)
117 if len(infiles) == 0 {
118 return fmt.Errorf("no input files found in %s", *inputDir)
120 sort.Strings(infiles)
122 var refseq map[string][]tileLibRef
123 var reftiledata = make(map[tileLibRef][]byte, 11000000)
124 in0, err := open(infiles[0])
129 matchGenome, err := regexp.Compile(cmd.filter.MatchGenome)
131 err = fmt.Errorf("-match-genome: invalid regexp: %q", cmd.filter.MatchGenome)
137 DecodeLibrary(in0, strings.HasSuffix(infiles[0], ".gz"), func(ent *LibraryEntry) error {
138 if len(ent.TagSet) > 0 {
139 taglen = len(ent.TagSet[0])
141 for _, cseq := range ent.CompactSequences {
142 if cseq.Name == *ref || *ref == "" {
143 refseq = cseq.TileSequences
146 for _, cg := range ent.CompactGenomes {
147 if matchGenome.MatchString(cg.Name) {
148 cmd.cgnames = append(cmd.cgnames, cg.Name)
151 for _, tv := range ent.TileVariants {
153 reftiledata[tileLibRef{tv.Tag, tv.Variant}] = tv.Sequence
163 return fmt.Errorf("%s: reference sequence not found", infiles[0])
166 return fmt.Errorf("tagset not found")
168 if len(cmd.cgnames) == 0 {
169 return fmt.Errorf("no genomes found matching regexp %q", cmd.filter.MatchGenome)
171 sort.Strings(cmd.cgnames)
173 log.Info("indexing reference tiles")
174 type reftileinfo struct {
175 variant tileVariantID
176 seqname string // chr1
177 pos int // distance from start of chromosome to starttag
178 tiledata []byte // acgtggcaa...
180 isdup := map[tagID]bool{}
181 reftile := map[tagID]*reftileinfo{}
182 for seqname, cseq := range refseq {
184 for _, libref := range cseq {
185 tiledata := reftiledata[libref]
186 if len(tiledata) == 0 {
187 return fmt.Errorf("missing tiledata for tag %d variant %d in %s in ref", libref.Tag, libref.Variant, seqname)
189 if isdup[libref.Tag] {
190 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique", libref, seqname, pos)
191 } else if reftile[libref.Tag] != nil {
192 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique", tileLibRef{Tag: libref.Tag, Variant: reftile[libref.Tag].variant}, reftile[libref.Tag].seqname, reftile[libref.Tag].pos)
193 delete(reftile, libref.Tag)
194 log.Printf("dropping reference tile %+v from %s @ %d, tag not unique", libref, seqname, pos)
195 isdup[libref.Tag] = true
197 reftile[libref.Tag] = &reftileinfo{
199 variant: libref.Variant,
204 pos += len(tiledata) - taglen
206 log.Printf("... %s done, len %d", seqname, pos+taglen)
210 if *regionsFilename != "" {
211 log.Printf("loading regions from %s", *regionsFilename)
212 mask, err = makeMask(*regionsFilename, *expandRegions)
216 log.Printf("before applying mask, len(reftile) == %d", len(reftile))
217 log.Printf("deleting reftile entries for regions outside %d intervals", mask.Len())
218 for tag, rt := range reftile {
219 if !mask.Check(strings.TrimPrefix(rt.seqname, "chr"), rt.pos, rt.pos+len(rt.tiledata)) {
223 log.Printf("after applying mask, len(reftile) == %d", len(reftile))
226 if cmd.selectedTags != nil {
227 log.Printf("deleting reftile entries other than %d selected tags", len(cmd.selectedTags))
228 for tag := range reftile {
229 if !cmd.selectedTags[tag] {
233 log.Printf("after applying selected tags, len(reftile) == %d", len(reftile))
236 dumpVariantsName := fmt.Sprintf("%s/variants.csv", *outputDir)
237 log.Infof("writing %s", dumpVariantsName)
238 dumpVariantsF, err := os.Create(dumpVariantsName)
242 dumpVariantsW := bufio.NewWriterSize(dumpVariantsF, 1<<20)
245 throttleMem := throttle{Max: runtime.GOMAXPROCS(0)}
246 log.Infof("reading %d slices with max concurrency %d", len(infiles), throttleMem.Max)
248 for infileIdx, infile := range infiles {
249 infileIdx, infile := infileIdx, infile
250 throttleMem.Go(func() error {
251 seq := make(map[tagID][]TileVariant, 50000)
252 cgs := make(map[string]CompactGenome, len(cmd.cgnames))
253 f, err := open(infile)
258 log.Infof("%04d: reading %s", infileIdx, infile)
259 err = DecodeLibrary(f, strings.HasSuffix(infile, ".gz"), func(ent *LibraryEntry) error {
260 for _, tv := range ent.TileVariants {
264 if mask != nil && reftile[tv.Tag] == nil {
270 variants := seq[tv.Tag]
271 if len(variants) == 0 {
272 variants = make([]TileVariant, 100)
274 for len(variants) <= int(tv.Variant) {
275 variants = append(variants, TileVariant{})
277 variants[int(tv.Variant)] = tv
278 seq[tv.Tag] = variants
280 for _, cg := range ent.CompactGenomes {
281 if !matchGenome.MatchString(cg.Name) {
284 // pad to full slice size
285 // to avoid out-of-bounds
287 if sliceSize := 2 * int(cg.EndTag-cg.StartTag); len(cg.Variants) < sliceSize {
288 cg.Variants = append(cg.Variants, make([]tileVariantID, sliceSize-len(cg.Variants))...)
297 tagstart := cgs[cmd.cgnames[0]].StartTag
298 tagend := cgs[cmd.cgnames[0]].EndTag
302 log.Infof("%04d: renumber/dedup variants for tags %d-%d", infileIdx, tagstart, tagend)
303 variantRemap := make([][]tileVariantID, tagend-tagstart)
304 throttleCPU := throttle{Max: runtime.GOMAXPROCS(0)}
305 for tag, variants := range seq {
306 tag, variants := tag, variants
307 throttleCPU.Go(func() error {
308 count := make(map[[blake2b.Size256]byte]int, len(variants))
311 var rthash [blake2b.Size256]byte
313 rthash = blake2b.Sum256(rt.tiledata)
317 for _, cg := range cgs {
318 idx := int(tag-tagstart) * 2
319 for allele := 0; allele < 2; allele++ {
320 v := cg.Variants[idx+allele]
321 if v > 0 && len(variants[v].Sequence) > 0 {
322 count[variants[v].Blake2b]++
326 // hash[i] will be the hash of
327 // the variant(s) that should
328 // be at rank i (0-based).
329 hash := make([][blake2b.Size256]byte, 0, len(count))
330 for b := range count {
331 hash = append(hash, b)
333 sort.Slice(hash, func(i, j int) bool {
334 bi, bj := &hash[i], &hash[j]
335 if ci, cj := count[*bi], count[*bj]; ci != cj {
338 return bytes.Compare((*bi)[:], (*bj)[:]) < 0
341 // rank[b] will be the 1-based
342 // new variant number for
343 // variants whose hash is b.
344 rank := make(map[[blake2b.Size256]byte]tileVariantID, len(hash))
345 for i, h := range hash {
346 rank[h] = tileVariantID(i + 1)
348 // remap[v] will be the new
349 // variant number for original
351 remap := make([]tileVariantID, len(variants))
352 for i, tv := range variants {
353 remap[i] = rank[tv.Blake2b]
355 variantRemap[tag-tagstart] = remap
357 rt.variant = rank[rthash]
364 for tag := tagstart; tag < tagend; tag++ {
365 rt, ok := reftile[tag]
367 // Excluded by specified
368 // regions, or reference does
369 // not use any variant of this
370 // tile. (TODO: log this?
371 // mention it in annotations?)
374 remap := variantRemap[tag-tagstart]
375 maxv := tileVariantID(0)
376 for _, v := range remap {
384 fmt.Fprintf(dumpVariantsW, "%d,%d,1,%s,%d,%s\n", tag, rt.variant, rt.seqname, rt.pos+1, bytes.ToUpper(rt.tiledata))
387 done := make([]bool, maxv+1)
388 for v, tv := range variants {
390 if v == 0 || v == rt.variant || done[v] {
396 fmt.Fprintf(dumpVariantsW, "%d,%d,0,%s,%d,%s\n", tag, v, rt.seqname, rt.pos+1, bytes.ToUpper(tv.Sequence))
400 log.Infof("%s: done (%d/%d)", infile, int(atomic.AddInt64(&done, 1)), len(infiles))
404 if err = throttleMem.Wait(); err != nil {
407 err = dumpVariantsW.Flush()
411 err = dumpVariantsF.Close()