16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 log "github.com/sirupsen/logrus"
26 func (f *filter) Flags(flags *flag.FlagSet) {
27 flags.IntVar(&f.MaxVariants, "max-variants", -1, "drop tiles with more than `N` variants")
28 flags.Float64Var(&f.MinCoverage, "min-coverage", 0, "drop tiles with coverage less than `P` across all haplotypes (0 < P ≤ 1)")
29 flags.IntVar(&f.MaxTag, "max-tag", -1, "drop tiles with tag ID > `N`")
32 func (f *filter) Args() []string {
34 fmt.Sprintf("-max-variants=%d", f.MaxVariants),
35 fmt.Sprintf("-min-coverage=%f", f.MinCoverage),
36 fmt.Sprintf("-max-tag=%d", f.MaxTag),
40 func (f *filter) Apply(tilelib *tileLibrary) {
41 // Zero out variants at tile positions that have more than
42 // f.MaxVariants tile variants.
43 if f.MaxVariants >= 0 {
44 for tag, variants := range tilelib.variant {
45 if f.MaxTag >= 0 && tag >= f.MaxTag {
48 if len(variants) <= f.MaxVariants {
51 for _, cg := range tilelib.compactGenomes {
60 // Zero out variants at tile positions that have less than
62 mincov := int(2*f.MinCoverage*float64(len(tilelib.compactGenomes)) + 1)
64 for tag := 0; tag < len(tilelib.variant) && tag < f.MaxTag; tag++ {
66 for _, cg := range tilelib.compactGenomes {
77 for _, cg := range tilelib.compactGenomes {
83 // Truncate genomes and tile data to f.MaxTag (TODO: truncate
86 if len(tilelib.variant) > f.MaxTag {
87 tilelib.variant = tilelib.variant[:f.MaxTag]
89 for name, cg := range tilelib.compactGenomes {
90 if len(cg) > 2*f.MaxTag {
91 tilelib.compactGenomes[name] = cg[:2*f.MaxTag]
97 type filtercmd struct {
102 func (cmd *filtercmd) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
106 fmt.Fprintf(stderr, "%s\n", err)
109 flags := flag.NewFlagSet("", flag.ContinueOnError)
110 flags.SetOutput(stderr)
111 pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
112 runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
113 projectUUID := flags.String("project", "", "project `UUID` for output data")
114 priority := flags.Int("priority", 500, "container request priority")
115 inputFilename := flags.String("i", "-", "input `file`")
116 outputFilename := flags.String("o", "-", "output `file`")
117 cmd.filter.Flags(flags)
118 err = flags.Parse(args)
119 if err == flag.ErrHelp {
122 } else if err != nil {
129 log.Println(http.ListenAndServe(*pprof, nil))
134 if *outputFilename != "-" {
135 err = errors.New("cannot specify output file in container mode: not implemented")
138 runner := arvadosContainerRunner{
139 Name: "lightning filter",
140 Client: arvados.NewClientFromEnv(),
141 ProjectUUID: *projectUUID,
146 err = runner.TranslatePaths(inputFilename)
150 runner.Args = []string{"filter", "-local=true",
151 "-i", *inputFilename,
152 "-o", "/mnt/output/library.gob",
153 "-max-variants", fmt.Sprintf("%d", cmd.MaxVariants),
154 "-min-coverage", fmt.Sprintf("%f", cmd.MinCoverage),
155 "-max-tag", fmt.Sprintf("%d", cmd.MaxTag),
158 output, err = runner.Run()
162 fmt.Fprintln(stdout, output+"/library.gob")
166 var infile io.ReadCloser
167 if *inputFilename == "-" {
168 infile = ioutil.NopCloser(stdin)
170 infile, err = os.Open(*inputFilename)
177 cgs, err := ReadCompactGenomes(infile, strings.HasSuffix(*inputFilename, ".gz"))
185 log.Printf("reading done, %d genomes", len(cgs))
187 log.Print("filtering")
189 for _, cg := range cgs {
190 if ntags < len(cg.Variants)/2 {
191 ntags = len(cg.Variants) / 2
193 if cmd.MaxVariants < 0 {
196 for idx, variant := range cg.Variants {
197 if variant > tileVariantID(cmd.MaxVariants) {
198 for _, cg := range cgs {
199 if len(cg.Variants) > idx {
200 cg.Variants[idx & ^1] = 0
201 cg.Variants[idx|1] = 0
208 if cmd.MaxTag >= 0 && ntags > cmd.MaxTag {
210 for i, cg := range cgs {
211 if len(cg.Variants) > cmd.MaxTag*2 {
212 cgs[i].Variants = cg.Variants[:cmd.MaxTag*2]
217 if cmd.MinCoverage > 0 {
218 mincov := int(cmd.MinCoverage * float64(len(cgs)*2))
219 cov := make([]int, ntags)
220 for _, cg := range cgs {
221 for idx, variant := range cg.Variants {
227 for tag, c := range cov {
229 for _, cg := range cgs {
230 if len(cg.Variants) > tag*2 {
231 cg.Variants[tag*2] = 0
232 cg.Variants[tag*2+1] = 0
239 log.Print("filtering done")
241 var outfile io.WriteCloser
242 if *outputFilename == "-" {
243 outfile = nopCloser{cmd.output}
245 outfile, err = os.OpenFile(*outputFilename, os.O_CREATE|os.O_WRONLY, 0777)
249 defer outfile.Close()
251 w := bufio.NewWriter(outfile)
252 enc := gob.NewEncoder(w)
254 err = enc.Encode(LibraryEntry{
260 log.Print("writing done")
265 err = outfile.Close()