comparison filter_vcf.py @ 11:cd59be4a7fe3 draft default tip

Uploaded
author ulfschaefer
date Mon, 21 Dec 2015 11:12:19 -0500
parents 834a312c0114
children
comparison
equal deleted inserted replaced
10:c2f8e7580133 11:cd59be4a7fe3
4 Created on 6 Oct 2015 4 Created on 6 Oct 2015
5 5
6 @author: alex 6 @author: alex
7 ''' 7 '''
8 import argparse 8 import argparse
9 import logging
10 import yaml
9 11
10 from phe.variant import VariantSet 12 from phe.variant import VariantSet
11 13
12 14
13 def get_args(): 15 def get_args():
14 16
15 args = argparse.ArgumentParser() 17 args = argparse.ArgumentParser()
16 18
17 args.add_argument("--vcf", "-v", required=True, help="VCF file to (re)filter.") 19 args.add_argument("--vcf", "-v", required=True, help="VCF file to (re)filter.")
18 args.add_argument("--filters", "-f", required=True, help="Filter(s) to apply as key:threshold pairs, separated by comma.") 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
19 args.add_argument("--output", "-o", required=True, help="Location for filtered VCF to be written.") 26 args.add_argument("--output", "-o", required=True, help="Location for filtered VCF to be written.")
27
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.")
20 31
21 return args.parse_args() 32 return args.parse_args()
22 33
34 def load_config(config_path):
35 with open(config_path) as fp:
36 config = yaml.load(fp)
37
38 return config.get("filters", {})
23 39
24 def main(): 40 def main():
25 args = get_args() 41 args = get_args()
26 42
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
27 var_set = VariantSet(args.vcf, filters=args.filters) 53 var_set = VariantSet(args.vcf, filters=args.filters)
28 54
29 var_set.filter_variants() 55 if args.filters:
56 var_set.filter_variants()
30 57
31 var_set.serialise(args.output) 58 var_set.write_variants(args.output, only_good=args.only_good)
59
60
32 61
33 if __name__ == '__main__': 62 if __name__ == '__main__':
34 exit(main()) 63 exit(main())