0
|
1 #!/usr/bin/env python
|
|
2 '''Simple VCF parser using custom filters.
|
|
3
|
|
4 Created on 6 Oct 2015
|
|
5
|
|
6 @author: alex
|
|
7 '''
|
|
8 import argparse
|
11
|
9 import logging
|
|
10 import yaml
|
0
|
11
|
|
12 from phe.variant import VariantSet
|
|
13
|
|
14
|
|
15 def get_args():
|
|
16
|
|
17 args = argparse.ArgumentParser()
|
|
18
|
|
19 args.add_argument("--vcf", "-v", required=True, help="VCF file to (re)filter.")
|
11
|
20
|
|
21 group = args.add_mutually_exclusive_group()
|
|
22
|
|
23 group.add_argument("--filters", "-f", help="Filter(s) to apply as key:threshold pairs, separated by comma.")
|
|
24 group.add_argument("--config", "-c", help="Config with filters in YAML format. E.g.filters:-key:value")
|
|
25
|
0
|
26 args.add_argument("--output", "-o", required=True, help="Location for filtered VCF to be written.")
|
|
27
|
11
|
28 args.add_argument("--only-good", action="store_true", default=False, help="Write only variants that PASS all filters (default all variants are written).")
|
|
29
|
|
30 args.add_argument("--debug", action="store_true", default=False, help="Make output more verbose.")
|
|
31
|
0
|
32 return args.parse_args()
|
|
33
|
11
|
34 def load_config(config_path):
|
|
35 with open(config_path) as fp:
|
|
36 config = yaml.load(fp)
|
|
37
|
|
38 return config.get("filters", {})
|
0
|
39
|
|
40 def main():
|
|
41 args = get_args()
|
|
42
|
11
|
43 log_level = logging.DEBUG if args.debug else logging.INFO
|
|
44 logging.basicConfig(format="[%(asctime)s] %(levelname)s: %(message)s",
|
|
45 level=log_level)
|
|
46
|
|
47 if args.config is not None:
|
|
48 args.filters = load_config(args.config)
|
|
49 elif args.filters is None:
|
|
50 logging.error("Either --config or --filters needs to be specified.")
|
|
51 return 1
|
|
52
|
0
|
53 var_set = VariantSet(args.vcf, filters=args.filters)
|
|
54
|
11
|
55 if args.filters:
|
|
56 var_set.filter_variants()
|
0
|
57
|
11
|
58 var_set.write_variants(args.output, only_good=args.only_good)
|
|
59
|
|
60
|
0
|
61
|
|
62 if __name__ == '__main__':
|
|
63 exit(main())
|