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 "github.com/klauspost/pgzip"
25 log "github.com/sirupsen/logrus"
28 type slicecmd struct{}
30 func (cmd *slicecmd) 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 outputDir := flags.String("output-dir", "./out", "output `directory`")
44 tagsPerFile := flags.Int("tags-per-file", 50000, "tags per file (nfiles will be ~10M÷x)")
45 err = flags.Parse(args)
46 if err == flag.ErrHelp {
49 } else if err != nil {
52 inputDirs := flags.Args()
53 if len(inputDirs) == 0 {
54 err = errors.New("no input dirs specified")
60 log.Println(http.ListenAndServe(*pprof, nil))
65 runner := arvadosContainerRunner{
66 Name: "lightning slice",
67 Client: arvados.NewClientFromEnv(),
68 ProjectUUID: *projectUUID,
75 for i := range inputDirs {
76 err = runner.TranslatePaths(&inputDirs[i])
81 runner.Args = append([]string{"slice", "-local=true",
83 "-output-dir", "/mnt/output",
86 output, err = runner.Run()
90 fmt.Fprintln(stdout, output)
94 err = Slice(*tagsPerFile, *outputDir, inputDirs)
101 // Read tags+tiles+genomes from srcdir, write to dstdir with (up to)
102 // the specified number of tags per file.
103 func Slice(tagsPerFile int, dstdir string, srcdirs []string) error {
105 for _, srcdir := range srcdirs {
106 files, err := allFiles(srcdir, matchGobFile)
110 infiles = append(infiles, files...)
112 // dirNamespace[dir] is an int in [0,len(dirNamespace)), used below to
113 // namespace variant numbers from different dirs.
114 dirNamespace := map[string]tileVariantID{}
115 for _, path := range infiles {
116 dir, _ := filepath.Split(path)
117 if _, ok := dirNamespace[dir]; !ok {
118 dirNamespace[dir] = tileVariantID(len(dirNamespace))
121 namespaces := tileVariantID(len(dirNamespace))
127 bufws []*bufio.Writer
131 countTileVariants int64
133 countReferences int64
136 throttle := throttle{Max: runtime.GOMAXPROCS(0)}
137 for _, infile := range infiles {
139 throttle.Go(func() error {
140 f, err := open(infile)
145 dir, _ := filepath.Split(infile)
146 namespace := dirNamespace[dir]
147 log.Printf("reading %s (namespace %d)", infile, namespace)
148 return DecodeLibrary(f, strings.HasSuffix(infile, ".gz"), func(ent *LibraryEntry) error {
149 if err := throttle.Err(); err != nil {
152 if len(ent.TagSet) > 0 {
153 tagsetOnce.Do(func() {
156 fs, bufws, gzws, encs, err = openOutFiles(dstdir, len(ent.TagSet), tagsPerFile)
161 for _, enc := range encs {
162 err = enc.Encode(LibraryEntry{TagSet: tagset})
170 if err := throttle.Err(); err != nil {
173 atomic.AddInt64(&countTileVariants, int64(len(ent.TileVariants)))
174 for _, tv := range ent.TileVariants {
175 tv.Variant = tv.Variant*namespaces + namespace
178 fileno = int(tv.Tag) / tagsPerFile
180 err := encs[fileno].Encode(LibraryEntry{
181 TileVariants: []TileVariant{tv},
187 // Here, each output file gets a
188 // CompactGenome entry for each
189 // genome, even if there are no
190 // variants in the relevant range.
191 // Easier for downstream code.
192 atomic.AddInt64(&countGenomes, int64(len(ent.CompactGenomes)))
193 for _, cg := range ent.CompactGenomes {
194 for i, v := range cg.Variants {
196 cg.Variants[i] = v*namespaces + namespace
199 for i, enc := range encs {
200 start := i * tagsPerFile
201 end := start + tagsPerFile
202 if max := len(cg.Variants)/2 + int(cg.StartTag); end > max {
205 if start < int(cg.StartTag) {
206 start = int(cg.StartTag)
208 var variants []tileVariantID
210 variants = cg.Variants[(start-int(cg.StartTag))*2 : (end-int(cg.StartTag))*2]
212 err := enc.Encode(LibraryEntry{CompactGenomes: []CompactGenome{{
215 StartTag: tagID(start),
216 EndTag: tagID(start + tagsPerFile),
223 // Write all ref seqs to the first
224 // slice. Easier for downstream code.
225 atomic.AddInt64(&countReferences, int64(len(ent.CompactSequences)))
226 if len(ent.CompactSequences) > 0 {
227 for _, cs := range ent.CompactSequences {
228 for _, tseq := range cs.TileSequences {
229 for i, libref := range tseq {
230 tseq[i].Variant = libref.Variant*namespaces + namespace
234 err := encs[0].Encode(LibraryEntry{CompactSequences: ent.CompactSequences})
244 if throttle.Err() != nil {
245 closeOutFiles(fs, bufws, gzws, encs)
246 return throttle.Err()
248 defer log.Printf("Total %d tile variants, %d genomes, %d reference sequences", countTileVariants, countGenomes, countReferences)
249 return closeOutFiles(fs, bufws, gzws, encs)
252 func openOutFiles(dstdir string, tags, tagsPerFile int) (fs []*os.File, bufws []*bufio.Writer, gzws []*pgzip.Writer, encs []*gob.Encoder, err error) {
253 nfiles := (tags + tagsPerFile - 1) / tagsPerFile
254 fs = make([]*os.File, nfiles)
255 bufws = make([]*bufio.Writer, nfiles)
256 gzws = make([]*pgzip.Writer, nfiles)
257 encs = make([]*gob.Encoder, nfiles)
258 for i := 0; i*tagsPerFile < tags; i++ {
259 fs[i], err = os.Create(dstdir + fmt.Sprintf("/library%04d.gob.gz", i))
263 bufws[i] = bufio.NewWriterSize(fs[i], 1<<26)
264 gzws[i] = pgzip.NewWriter(bufws[i])
265 encs[i] = gob.NewEncoder(gzws[i])
270 func closeOutFiles(fs []*os.File, bufws []*bufio.Writer, gzws []*pgzip.Writer, encs []*gob.Encoder) error {
272 for _, gzw := range gzws {
275 if err != nil && firstErr == nil {
280 for _, bufw := range bufws {
283 if err != nil && firstErr == nil {
288 for _, f := range fs {
291 if err != nil && firstErr == nil {