1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 "git.arvados.org/arvados.git/sdk/go/arvados"
22 log "github.com/sirupsen/logrus"
32 func (f *filter) Flags(flags *flag.FlagSet) {
33 flags.IntVar(&f.MaxVariants, "max-variants", -1, "drop tiles with more than `N` variants")
34 flags.Float64Var(&f.MinCoverage, "min-coverage", 0, "drop tiles with coverage less than `P` across all haplotypes (0 < P ≤ 1)")
35 flags.IntVar(&f.MaxTag, "max-tag", -1, "drop tiles with tag ID > `N`")
36 flags.StringVar(&f.MatchGenome, "match-genome", "", "keep genomes whose names contain `regexp`, drop the rest")
39 func (f *filter) Args() []string {
41 fmt.Sprintf("-max-variants=%d", f.MaxVariants),
42 fmt.Sprintf("-min-coverage=%f", f.MinCoverage),
43 fmt.Sprintf("-max-tag=%d", f.MaxTag),
44 fmt.Sprintf("-match-genome=%s", f.MatchGenome),
48 func (f *filter) Apply(tilelib *tileLibrary) {
49 // Zero out variants at tile positions that have more than
50 // f.MaxVariants tile variants.
51 if f.MaxVariants >= 0 {
52 for tag, variants := range tilelib.variant {
53 if f.MaxTag >= 0 && tag >= f.MaxTag {
56 if len(variants) <= f.MaxVariants {
59 for _, cg := range tilelib.compactGenomes {
68 // Zero out variants at tile positions that have less than
70 mincov := int(2*f.MinCoverage*float64(len(tilelib.compactGenomes)) + 1)
72 for tag := 0; tag < len(tilelib.variant) && (tag < f.MaxTag || f.MaxTag < 0); tag++ {
74 for _, cg := range tilelib.compactGenomes {
75 if len(cg) < tag*2+2 {
88 for _, cg := range tilelib.compactGenomes {
96 // Truncate genomes and tile data to f.MaxTag (TODO: truncate
99 if len(tilelib.variant) > f.MaxTag {
100 tilelib.variant = tilelib.variant[:f.MaxTag]
102 for name, cg := range tilelib.compactGenomes {
103 if len(cg) > 2*f.MaxTag {
104 tilelib.compactGenomes[name] = cg[:2*f.MaxTag]
109 re, err := regexp.Compile(f.MatchGenome)
111 log.Errorf("invalid regexp %q does not match anything, dropping all genomes", f.MatchGenome)
113 for name := range tilelib.compactGenomes {
114 if !re.MatchString(name) {
115 delete(tilelib.compactGenomes, name)
120 type filtercmd struct {
125 func (cmd *filtercmd) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
129 fmt.Fprintf(stderr, "%s\n", err)
132 flags := flag.NewFlagSet("", flag.ContinueOnError)
133 flags.SetOutput(stderr)
134 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
135 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
136 projectUUID := flags.String("project", "", "project `UUID` for output data")
137 priority := flags.Int("priority", 500, "container request priority")
138 inputFilename := flags.String("i", "-", "input `file`")
139 outputFilename := flags.String("o", "-", "output `file`")
140 cmd.filter.Flags(flags)
141 err = flags.Parse(args)
142 if err == flag.ErrHelp {
145 } else if err != nil {
152 log.Println(http.ListenAndServe(*pprof, nil))
157 if *outputFilename != "-" {
158 err = errors.New("cannot specify output file in container mode: not implemented")
161 runner := arvadosContainerRunner{
162 Name: "lightning filter",
163 Client: arvados.NewClientFromEnv(),
164 ProjectUUID: *projectUUID,
169 err = runner.TranslatePaths(inputFilename)
173 runner.Args = []string{"filter", "-local=true",
174 "-i", *inputFilename,
175 "-o", "/mnt/output/library.gob",
176 "-max-variants", fmt.Sprintf("%d", cmd.MaxVariants),
177 "-min-coverage", fmt.Sprintf("%f", cmd.MinCoverage),
178 "-max-tag", fmt.Sprintf("%d", cmd.MaxTag),
181 output, err = runner.Run()
185 fmt.Fprintln(stdout, output+"/library.gob")
189 var infile io.ReadCloser
190 if *inputFilename == "-" {
191 infile = ioutil.NopCloser(stdin)
193 infile, err = os.Open(*inputFilename)
200 cgs, err := ReadCompactGenomes(infile, strings.HasSuffix(*inputFilename, ".gz"))
208 log.Printf("reading done, %d genomes", len(cgs))
210 log.Print("filtering")
212 for _, cg := range cgs {
213 if ntags < len(cg.Variants)/2 {
214 ntags = len(cg.Variants) / 2
216 if cmd.MaxVariants < 0 {
219 for idx, variant := range cg.Variants {
220 if variant > tileVariantID(cmd.MaxVariants) {
221 for _, cg := range cgs {
222 if len(cg.Variants) > idx {
223 cg.Variants[idx & ^1] = 0
224 cg.Variants[idx|1] = 0
231 if cmd.MaxTag >= 0 && ntags > cmd.MaxTag {
233 for i, cg := range cgs {
234 if len(cg.Variants) > cmd.MaxTag*2 {
235 cgs[i].Variants = cg.Variants[:cmd.MaxTag*2]
240 if cmd.MinCoverage > 0 {
241 mincov := int(cmd.MinCoverage * float64(len(cgs)*2))
242 cov := make([]int, ntags)
243 for _, cg := range cgs {
244 for idx, variant := range cg.Variants {
250 for tag, c := range cov {
252 for _, cg := range cgs {
253 if len(cg.Variants) > tag*2 {
254 cg.Variants[tag*2] = 0
255 cg.Variants[tag*2+1] = 0
262 log.Print("filtering done")
264 var outfile io.WriteCloser
265 if *outputFilename == "-" {
266 outfile = nopCloser{cmd.output}
268 outfile, err = os.OpenFile(*outputFilename, os.O_CREATE|os.O_WRONLY, 0777)
272 defer outfile.Close()
274 w := bufio.NewWriter(outfile)
275 enc := gob.NewEncoder(w)
277 err = enc.Encode(LibraryEntry{
283 log.Print("writing done")
288 err = outfile.Close()